aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/main/java/org/onap/cli/fw/cmd/OnapCommand.java
blob: 74e488b41700f60bd7335a6bd8ee7dde7e2d4fb0 (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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
 * Copyright 2017 Huawei Technologies Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.onap.cli.fw.cmd;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.onap.cli.fw.conf.OnapCommandConstants;
import org.onap.cli.fw.error.OnapCommandException;
import org.onap.cli.fw.error.OnapCommandHelpFailed;
import org.onap.cli.fw.error.OnapCommandNotInitialized;
import org.onap.cli.fw.info.OnapCommandInfo;
import org.onap.cli.fw.input.OnapCommandParameter;
import org.onap.cli.fw.input.OnapCommandParameterType;
import org.onap.cli.fw.output.OnapCommandResult;
import org.onap.cli.fw.output.OnapCommandResultAttribute;
import org.onap.cli.fw.output.OnapCommandResultAttributeScope;
import org.onap.cli.fw.output.OnapCommandResultType;
import org.onap.cli.fw.schema.OnapCommandSchemaInfo;
import org.onap.cli.fw.schema.OnapCommandSchemaLoader;
import org.onap.cli.fw.schema.OnapCommandSchemaMerger;
import org.onap.cli.fw.store.OnapCommandArtifactStore;
import org.onap.cli.fw.store.OnapCommandArtifactStore.Artifact;
import org.onap.cli.fw.store.OnapCommandExecutionStore.ExecutionStoreContext;
import org.onap.cli.fw.utils.OnapCommandHelperUtils;
import org.onap.cli.fw.utils.OnapCommandUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;


/**
 * Oclip Command.
 *
 */
public abstract class OnapCommand {

    private static Logger log = LoggerFactory.getLogger(OnapCommand.class);
    private static Gson gson = new GsonBuilder().serializeNulls().create();

    private String cmdDescription;

    private String cmdName;

    private String cmdSchemaName;

    private OnapCommandInfo info = new OnapCommandInfo();

    private Set<OnapCommandParameter> cmdParameters = new HashSet<>();

    private OnapCommandResult cmdResult = new OnapCommandResult();

    private List<String> defaultSchemas = new ArrayList<>();

    protected boolean isInitialzied = false;

    private  boolean isRpc = false;

    private ExecutionStoreContext executionStoreContext;

    public boolean isRpc() {
        return isRpc;
    }

    public void setRpc(boolean isRpc) {
        this.isRpc = isRpc;
    }

    protected OnapCommand() {
        this.addDefaultSchemas(OnapCommandConstants.DEFAULT_PARAMETER_FILE_NAME);
    }

    public List<String> getSchemas() {
        List<String> schemas = new ArrayList<>();
        schemas.addAll(this.defaultSchemas);
        schemas.add(this.getSchemaName());
        return schemas;
    }

    public String getSchemaVersion() {
        return OnapCommandConstants.OPEN_CLI_SCHEMA_VERSION_VALUE_1_0;
    }

    public String getDescription() {
        return this.cmdDescription;
    }

    public void setDescription(String description) {
        this.cmdDescription = description;
    }

    public String getName() {
        return this.cmdName;
    }

    public void setName(String name) {
        this.cmdName = name;
    }

    public OnapCommandInfo getInfo() {
        return info;
    }

    public void setInfo(OnapCommandInfo info) {
        this.info = info;
    }

    public void setParameters(Set<OnapCommandParameter> parameters) {
        this.cmdParameters = parameters;
    }

    public Set<OnapCommandParameter> getParameters() {
        return this.cmdParameters;
    }

    public Map<String, OnapCommandParameter> getParametersMap() {
        return OnapCommandUtils.getInputMap(this.getParameters());
    }

    public OnapCommandResult getResult() {
        return this.cmdResult;
    }

    public void setResult(OnapCommandResult result) {
        this.cmdResult = result;
    }

    public String getSchemaName() {
        return cmdSchemaName;
    }

    protected void setSchemaName(String schemaName) {
        this.cmdSchemaName = schemaName;
    }

    protected void addDefaultSchemas(String schema) {
        this.defaultSchemas.add(schema);
    }

    public List<String> getDefaultSchema() {
        return this.defaultSchemas;
    }

    public List<String> getArgs() {
        List <String> args = new ArrayList<>();
        for (OnapCommandParameter param: this.getParameters()) {
            args.add(OnapCommandParameter.printLongOption(param.getName()));
            args.add(param.getValue().toString());
        }

        return args;
    }

    public String getArgsJson(boolean ignoreDefaults) {
        Map <String, String> args = new HashMap<>();

        for (OnapCommandParameter param: this.getParameters()) {
            if (ignoreDefaults && param.isDefaultParam())
                continue;

            args.put(param.getName(), param.getValue().toString());
        }

        try {
            return gson.toJson(args);
        } catch (Exception e) { // NOSONAR
            log.error("exception occured {}", e.getMessage());
            return "{}";
        }
    }

    /**
     * Initialize this command from command schema and assumes schema is already validated.
     *
     * @throws OnapCommandException
     *
     * @return List of error strings
     */
    public List<String> initializeSchema(String schema) throws OnapCommandException {
        return this.initializeSchema(schema, false);
    }

    public List<String> initializeSchema(OnapCommandSchemaInfo schema) throws OnapCommandException {
        return this.initializeSchema(schema.getSchemaName(), false);
    }

    public List<String> initializeSchema(String schema, boolean validate) throws OnapCommandException {
        this.setSchemaName(schema);

        Map<String, ?> schemaMap = OnapCommandSchemaMerger.mergeSchemas(this);
        List<String> errors = OnapCommandSchemaLoader.parseSchema(this, schemaMap, validate);
        errors.addAll(this.initializeProfileSchema(schemaMap, validate));
        this.isInitialzied = true;

        return errors;
    }
    /**
     * Any additional profile based such as http schema could be initialized.
     */
    protected List<String> initializeProfileSchema(Map<String, ?> schemaMap, boolean validate) throws OnapCommandException { //NOSONAR
        return new ArrayList<>();
    }

    /*
     * Validate input parameters. This can be overridden in derived commands
     */
    protected void validate() throws OnapCommandException {
        for (OnapCommandParameter param : this.getParameters()) {
             if (param.isInclude()) {
                 param.validate();
             }
         }
    }

    protected void preRun() throws OnapCommandException { //NOSONAR
        log.debug("{} PRE-RUN", this.getName());
    }

    protected void postRun() throws OnapCommandException { //NOSONAR
        log.debug("{} POST-RUN", this.getName());
    }

    /**
     * Oclip command execute with given parameters on service. Before calling this method, its mandatory to set all
     * parameters value.
     *
     * @throws OnapCommandException
     *             : General Command Exception
     */
    public OnapCommandResult execute() throws OnapCommandException {
        if (!this.isInitialzied) {
            throw new OnapCommandNotInitialized(this.getClass().getName());
        }

        log.info("CMD: {}", this.getName());

        Map<String, OnapCommandParameter> paramMap = this.getParametersMap();

        log.info("INPUT: {}", paramMap);

        // -h or --help is always higher precedence !, user can set this value to get help message
        if (Boolean.TRUE.equals(paramMap.get(OnapCommandConstants.DEFAULT_PARAMETER_HELP).getValue())) {
            this.cmdResult.setType(OnapCommandResultType.TEXT);
            this.cmdResult.setOutput(this.printHelp());
            return this.cmdResult;
        }

        // -v or --version is next higher precedence !, user can set this value to get help message
        if (Boolean.TRUE.equals(paramMap.get(OnapCommandConstants.DEFAULT_PARAMETER_VERSION).getValue())) {
            this.cmdResult.setType(OnapCommandResultType.TEXT);
            this.cmdResult.setOutput(this.printVersion());
            return this.cmdResult;
        }

        //set the artifact content path.
        for (OnapCommandParameter param: this.getParameters()) {
            if (!param.getParameterType().equals(OnapCommandParameterType.BINARY))
                continue;

            if (param.getValue().toString().matches("artifact://*:*")) { //NOSONAR
                String categoryAndName = param.getValue().toString().replaceFirst("artifact://", "");
                String[] categoryAndNameTokens = categoryAndName.split(":");
                Artifact a = OnapCommandArtifactStore.getStore().getArtifact(categoryAndNameTokens[1], categoryAndNameTokens[0]);
                param.setValue(a.getPath());
            }
        }

        // validate
        this.validate();

        // -f or --format
        this.cmdResult.setType(
                OnapCommandResultType.get(paramMap.get(OnapCommandConstants.DEFAULT_PARAMETER_OUTPUT_FORMAT).getValue().toString()));
        if (Boolean.TRUE.equals(paramMap.get(OnapCommandConstants.DEFAULT_PARAMETER_OUTPUT_ATTR_LONG).getValue())) {
            this.cmdResult.setScope(OnapCommandResultAttributeScope.LONG);
        }
        // --no-title
        if (Boolean.TRUE.equals(paramMap.get(OnapCommandConstants.DEFAULT_PARAMETER_OUTPUT_NO_TITLE).getValue())) {
            this.cmdResult.setIncludeTitle(false);
        }

        // --debug
        if (Boolean.TRUE.equals(paramMap.get(OnapCommandConstants.DEFAULT_PARAMETER_DEBUG).getValue())) {
            this.cmdResult.setDebug(true);
        }

        //pre-process result attributes for spl entries and input parameters
        for (OnapCommandResultAttribute attr: this.cmdResult.getRecords()) {
            if (!attr.getDefaultValue().isEmpty()) {
                attr.setDefaultValue(OnapCommandUtils.replaceLineForSpecialValues(attr.getDefaultValue()));
                attr.setDefaultValue(OnapCommandUtils.replaceLineFromInputParameters(
                        attr.getDefaultValue(), this.getParametersMap()));
            }
        }

        preRun();

        this.run();

        log.info("OUTPUT: {}", this.cmdResult.getRecords());

        postRun();


        return this.cmdResult;
    }

    /*
     * Each command implements run method to executing the command.
     *
     */
    protected abstract void run() throws OnapCommandException;

    /**
     * Returns the service service version it supports.
     *
     * @return version
     */
    public String printVersion() {
        return this.getInfo().getProduct() + "::" + this.getInfo().getService();
    }

    /**
     * Provides help message for this command.
     *
     * @return help message
     * @throws OnapCommandHelpFailed
     *             Failed to execute Help command.
     */
    public String printHelp() throws OnapCommandHelpFailed {
        return OnapCommandHelperUtils.help(this);
    }
    // (mrkanag) Add toString for all command, parameter, result, etc objects in JSON format

    public void setExecutionContext(ExecutionStoreContext executionStoreContext) {
        this.executionStoreContext = executionStoreContext;
    }

    public ExecutionStoreContext getExecutionContext() {
        return this.executionStoreContext;
    }
}