summaryrefslogtreecommitdiff
path: root/upstream/tools/xmlGenerator/domainGeneratorConnector.cpp
blob: a83822f8db191aade7a4966c516be9e08a58aa2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
 * Copyright (c) 2015, Intel Corporation
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation and/or
 * other materials provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors
 * may be used to endorse or promote products derived from this software without
 * specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <ParameterMgrFullConnector.h>
#include <Tokenizer.h>
#include <Utility.h>

#include <iostream>
#include <sstream>
#include <memory>
#include <string>
#include <limits>
#include <numeric>
#include <algorithm>
#include <stdexcept>

using std::string;

class MyLogger final : public CParameterMgrFullConnector::ILogger
{
public:
    void info(const std::string &log) override { std::cerr << "Info: " << log << std::endl; }

    void warning(const std::string &log) override { std::cerr << "Warning: " << log << std::endl; }
};

class XmlGenerator
{
public:
    using Exception = std::runtime_error;

    XmlGenerator(const string &toplevelConfig, bool validate, bool verbose, string schemasDir)
        : mConnector(toplevelConfig), mCommandHandler(mConnector.createCommandHandler())
    {
        if (verbose) {
            mLogger.reset(new MyLogger);
            mConnector.setLogger(mLogger.get());
        }

        mConnector.setSchemaUri(schemasDir);
        mConnector.setValidateSchemasOnStart(validate);

        // Disable irrelevant failure conditions
        mConnector.setFailureOnMissingSubsystem(false);
        mConnector.setFailureOnFailedSettingsLoad(false);

        // Disable the remote interface because we don't need it and it might
        // get in the way (e.g. the port is already in use)
        mConnector.setForceNoRemoteInterface(true);
    }

    /** Reads each line of the input stream and takes an action accordingly
     *
     * Returns when the input stream reaches end of file
     *
     * The commands are the usual PF tunning commands and some additional specials.
     * Special commands:
     *  - `createSelectionCriterion inclusive|exclusive <name> <value> [value, ...]`
     *    Create a criterion with the given properties.
     *  - `start` start the Parameter Framework. All criteria must have been created.
     *
     * @param[in] input The input stream to read from
     *
     * @return the number of error that occurred
     */
    size_t parse(std::istream &input);

    /** Check for elements belonging to several domains
     *
     * Prints conflicting elements, if any, on the error output.
     *
     * @returns true if there are conflicting elements, false otherwise
     */
    bool conflictingElements();

    /** Prints the Parameter Framework's instance configuration
     *
     * @param[out] output The stream to which output the configuration
     */
    void exportDomains(std::ostream &output);

private:
    void addCriteria(std::vector<string> &tokens);
    void start();

    CParameterMgrFullConnector mConnector;
    std::unique_ptr<MyLogger> mLogger;
    std::unique_ptr<CommandHandlerInterface> mCommandHandler;
};

void XmlGenerator::addCriteria(std::vector<string> &tokens)
{
    if (tokens.size() < 3) {
        throw Exception("Not enough arguments to criterion creation request");
    }

    auto inclusiveness = tokens.front() == "inclusive";
    tokens.erase(begin(tokens));

    auto name = tokens.front();
    tokens.erase(begin(tokens));

    auto criterionType = mConnector.createSelectionCriterionType(inclusiveness);
    if (criterionType == nullptr) {
        throw Exception("Failed to create an " + string(inclusiveness ? "inclusive" : "exclusive") +
                        " criterion type");
    }

    int index = 0;
    for (const auto &literalValue : tokens) {
        // inclusive criteria are bitfields
        int numericalValue = inclusiveness ? 1 << index : index;
        string error;
        bool success = criterionType->addValuePair(numericalValue, literalValue, error);

        if (not success) {
            std::ostringstream message;
            message << "Valuepair (" << numericalValue << ", '" << literalValue
                    << "') rejected for " << name << ": " << error;
            throw Exception(message.str());
        }
        index++;
    }

    // We don't need to keep a reference to the criterion - no need to store
    // the returned pointer.
    if (mConnector.createSelectionCriterion(name, criterionType) == nullptr) {
        throw Exception("Failed to create criterion '" + name + "'");
    }
}

size_t XmlGenerator::parse(std::istream &input)
{
    string line;
    size_t errorNb = 0;
    while (not input.eof()) {
        std::getline(std::cin, line);

        auto tokens = Tokenizer(line, string(1, '\0'), false).split();
        if (tokens.empty()) {
            continue;
        }
        auto command = tokens.front();
        tokens.erase(begin(tokens)); // drop the command name

        if (command == "createSelectionCriterion") {
            addCriteria(tokens);
        } else if (command == "start") {
            start();
        } else {
            string output;
            if (not mCommandHandler->process(command, tokens, output)) {
                errorNb++;

                std::cerr << accumulate(begin(tokens), end(tokens),
                                        "Failed to executing command: `" + command + "'",
                                        [](string l, string r) { return l + " `" + r + "'"; })
                          << std::endl
                          << output << std::endl;
            }
        }
    }
    return errorNb;
}

bool XmlGenerator::conflictingElements()
{
    string conflicting;
    if (not mCommandHandler->process("listConflictingElements", {}, conflicting)) {
        // Should not happen
        throw Exception("Failed to list conflicting elements");
    }

    if (not conflicting.empty()) {
        std::cerr << "There are conflicting elements:" << std::endl << conflicting;
        return true;
    }

    return false;
}

void XmlGenerator::start()
{
    string error;
    if (not mConnector.start(error)) {
        throw Exception("Start failed: " + error);
    }

    error.clear();
    // Switch to tunning mode as the tunning commands
    // are the only commands possible with this connector.
    if (not mConnector.setTuningMode(true, error)) {
        throw Exception("Failed to turn tuning mode on: " + error);
    }
}

void XmlGenerator::exportDomains(std::ostream &output)
{
    string error;
    string domains;
    if (not mConnector.exportDomainsXml(domains, true, false, error)) {
        throw Exception("Export failed: " + error);
    } else {
        output << domains;
    }
}

static const char *usage =
    R"(Usage: domainGeneratorConnector <top-level config> <verbose> <validate> <path>

 <verbose>       'verbose': verbose, else: terse
 <validate>      'validate': validate, else: don't validate
 <path>          path to the schemas' directory

All arguments are mandatory. If no validation is required,
the path to the schemas can be an empty string.

Exit with the number of (recoverable or not error) that occured.

This program is not intended to be used standalone but rather called through
domainGenerator.py)";

/** On linux at least, a program can not exit with a value greater than 255.
 * @return min(code, 255);
 */
template <class T>
static inline int normalizeExitCode(T code)
{
    return int(std::min<T>(code, std::numeric_limits<uint8_t>::max()));
}

int main(int argc, char *argv[])
{
    using std::endl;

    if (argc <= 4) {
        std::cerr << usage << std::endl;
        return 1;
    }

    string toplevelConfig = argv[1];
    bool verbose = string(argv[2]) == "verbose";
    bool validate = string(argv[3]) == "validate";
    string schemasDir = argv[4];

    if (verbose) {
        std::cerr << "Domain generator config:" << endl
                  << "    toplevelConfig=" << toplevelConfig << endl
                  << "    verbose=" << verbose << endl
                  << "    validate=" << validate << endl
                  << "    schemasDir=" << schemasDir << endl;
    }

    try {
        XmlGenerator xmlGenerator(toplevelConfig, validate, verbose, schemasDir);
        auto errorNb = xmlGenerator.parse(std::cin);
        if (xmlGenerator.conflictingElements()) {
            errorNb++;
        }
        xmlGenerator.exportDomains(std::cout);

        return normalizeExitCode(errorNb);
    } catch (std::exception &e) {
        std::cerr << e.what() << std::endl;
        return 1;
    }
}