aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/main/java/org/onap/cli/fw/OnapCommand.java
blob: 9be9b88e3e4969af08a67a7f42bca95710c09974 (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
/*
 * 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;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.onap.cli.fw.ad.OnapService;
import org.onap.cli.fw.cmd.CommandType;
import org.onap.cli.fw.conf.Constants;
import org.onap.cli.fw.conf.OnapCommandConfg;
import org.onap.cli.fw.error.OnapCommandException;
import org.onap.cli.fw.error.OnapCommandHelpFailed;
import org.onap.cli.fw.error.OnapCommandInvalidParameterType;
import org.onap.cli.fw.error.OnapCommandInvalidPrintDirection;
import org.onap.cli.fw.error.OnapCommandInvalidResultAttributeScope;
import org.onap.cli.fw.error.OnapCommandInvalidSchema;
import org.onap.cli.fw.error.OnapCommandInvalidSchemaVersion;
import org.onap.cli.fw.error.OnapCommandNotInitialized;
import org.onap.cli.fw.error.OnapCommandParameterMissing;
import org.onap.cli.fw.error.OnapCommandParameterNameConflict;
import org.onap.cli.fw.error.OnapCommandParameterOptionConflict;
import org.onap.cli.fw.error.OnapCommandRegistrationFailed;
import org.onap.cli.fw.error.OnapCommandSchemaNotFound;
import org.onap.cli.fw.input.OnapCommandParameter;
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.ResultType;
import org.onap.cli.fw.utils.OnapCommandUtils;

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

    private String cmdDescription;

    private String cmdName;

    private String cmdSchemaName;

    private String productVersion;

    private OnapService onapService = new OnapService();

    private List<OnapCommandParameter> cmdParameters = new ArrayList<>();

    private OnapCommandResult cmdResult = new OnapCommandResult();

    protected boolean isInitialzied = false;

    protected CommandType type = CommandType.CMD;

    public String getSchemaVersion() {
        return Constants.OPEN_CLI_SCHEMA_VERSION_VALUE;
    }

    /**
     * Onap command description, defined by derived command.
     */
    public String getDescription() {
        return this.cmdDescription;
    }

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

    /*
     * Onap command name like user-create, ns-list, etc , defined by derived command
     */
    public String getName() {
        return this.cmdName;
    }

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

    public boolean isCommandInternal() {
        return onapService.getName() != null
                && onapService.getName().equalsIgnoreCase(OnapCommandConfg.getInternalCmd())
                && this.type.equals(CommandType.CMD);
    }

    /*
     * Onap service, this command uses to execute it. , defined by derived command
     */
    public OnapService getService() {
        return this.onapService;
    }

    public void setService(OnapService service) {
        this.onapService = service;
    }

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

    /*
     * Onap command input parameters, defined by derived command
     */
    public List<OnapCommandParameter> getParameters() {
        return this.cmdParameters;
    }

    /*
     * Onap command input parameters, defined by derived command
     */
    public Map<String, OnapCommandParameter> getParametersMap() {
        return OnapCommandUtils.getInputMap(this.getParameters());
    }

    /*
     * Onap command output results, defined by derived command
     */
    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;
    }

    public CommandType getType() {
        return this.type;
    }

    public void setType(CommandType type) {
        this.type = type;
    }

    /**
     * Initialize this command from command schema.
     *
     * @throws OnapCommandRegistrationFailed
     *             Command Registration Exception
     * @throws OnapCommandInvalidResultAttributeScope
     *             InvalidResultAttribute Exception
     * @throws OnapCommandInvalidPrintDirection
     *             InvalidPrintDirection Exception
     * @throws OnapCommandInvalidParameterType
     *             InvalidParameterType Exception
     * @throws OnapCommandSchemaNotFound
     *             SchemaNotFound Exception
     * @throws OnapCommandInvalidSchema
     *             InvalidSchema Exception
     * @throws OnapCommandParameterOptionConflict
     *             ParameterOptionConflict Exception
     * @throws OnapCommandParameterNameConflict
     *             ParameterNameConflict Exception
     * @throws OnapCommandInvalidSchemaVersion
     *             InvalidSchemaVersion Exception
     */
    public void initializeSchema(String schema) throws OnapCommandException {
        this.setSchemaName(schema);
        OnapCommandUtils.loadSchema(this, schema, true, false);
        this.initializeProfileSchema();
        this.isInitialzied = true;
    }

    /**
     * Any additional profile based such as http/swagger schema could be initialized.
     */
    protected void initializeProfileSchema() throws OnapCommandException {
    }

    /*
     * Validate input parameters. This can be overridden in derived commands
     */
    protected void validate() throws OnapCommandException {
        for (OnapCommandParameter param : this.getParameters()) {
            try {
                param.validate();
            } catch (OnapCommandParameterMissing e) {
                if (OnapCommandConfg.getExcludeParamsForNoAuthEnableExternalCmd().contains(param.getName())) {
                    Optional<OnapCommandParameter> noAuthParamOpt = this.getParameters().stream().filter(p -> p.getName()
                            .equalsIgnoreCase(Constants.DEFAULT_PARAMETER_OUTPUT_NO_AUTH)).findFirst();

                    if (noAuthParamOpt.isPresent() && "true".equalsIgnoreCase(noAuthParamOpt.get().getValue().toString())) {
                        continue;
                    }
                }
                throw e;
            } catch (OnapCommandException e) {
                throw e;
            }

        }
    }

    /**
     * Onap 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());
        }

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

        // -h or --help is always higher precedence !, user can set this value to get help message
        if ("true".equals(paramMap.get(Constants.DEFAULT_PARAMETER_HELP).getValue())) {
            OnapCommandResult result = new OnapCommandResult();
            result.setType(ResultType.TEXT);
            result.setOutput(this.printHelp());
            return result;
        }

        // -v or --version is next higher precedence !, user can set this value to get help message
        if ("true".equals(paramMap.get(Constants.DEFAULT_PARAMETER_VERSION).getValue())) {
            OnapCommandResult result = new OnapCommandResult();
            result.setType(ResultType.TEXT);
            result.setOutput(this.printVersion());
            return result;
        }

        // validate
        this.validate();

        // -f or --format
        this.cmdResult.setType(
                ResultType.get(paramMap.get(Constants.DEFAULT_PARAMETER_OUTPUT_FORMAT).getValue().toString()));
        if ("true".equals(paramMap.get(Constants.DEFAULT_PARAMETER_OUTPUT_ATTR_LONG).getValue())) {
            this.cmdResult.setScope(OnapCommandResultAttributeScope.LONG);
        }
        // --no-title
        if ("true".equals(paramMap.get(Constants.DEFAULT_PARAMETER_OUTPUT_NO_TITLE).getValue())) {
            this.cmdResult.setIncludeTitle(false);
        }

        // --debug
        if ("true".equals(paramMap.get(Constants.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()));
            }
        }

        this.run();

        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.getService().toString();
    }

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

    public void setVersion(String version) {
        this.productVersion = version;
    }

    public String getVersion() {
        return this.productVersion;
    }
}