2 * Copyright 2017 Huawei Technologies Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.cli.fw.http.cmd;
19 import org.onap.cli.fw.cmd.OnapCommand;
20 import org.onap.cli.fw.cmd.OnapCommandType;
21 import org.onap.cli.fw.conf.OnapCommandConfig;
22 import org.onap.cli.fw.conf.OnapCommandConstants;
23 import org.onap.cli.fw.error.OnapCommandException;
24 import org.onap.cli.fw.error.OnapCommandExecutionFailed;
25 import org.onap.cli.fw.http.auth.OnapCommandHttpAuthClient;
26 import org.onap.cli.fw.http.auth.OnapCommandHttpService;
27 import org.onap.cli.fw.http.conf.OnapCommandHttpConstants;
28 import org.onap.cli.fw.http.connect.HttpInput;
29 import org.onap.cli.fw.http.connect.HttpResult;
30 import org.onap.cli.fw.http.error.OnapCommandFailedMocoGenerate;
31 import org.onap.cli.fw.http.mock.MocoServer;
32 import org.onap.cli.fw.http.schema.OnapCommandSchemaHttpLoader;
33 import org.onap.cli.fw.http.utils.OnapCommandHttpUtils;
34 import org.onap.cli.fw.input.OnapCommandParameter;
35 import org.onap.cli.fw.output.OnapCommandResultAttribute;
36 import org.onap.cli.fw.schema.OnapCommandSchema;
37 import org.onap.cli.fw.utils.OnapCommandUtils;
38 import org.onap.cli.http.mock.MockJsonGenerator;
39 import org.onap.cli.http.mock.MockRequest;
40 import org.onap.cli.http.mock.MockResponse;
42 import java.io.IOException;
43 import java.util.ArrayList;
44 import java.util.HashMap;
45 import java.util.List;
47 import java.util.Map.Entry;
48 import java.util.Optional;
54 @OnapCommandSchema(type = OnapCommandHttpConstants.HTTP_SCHEMA_PROFILE)
55 public class OnapHttpCommand extends OnapCommand {
57 private HttpInput input = new HttpInput();
59 private List<Integer> successStatusCodes = new ArrayList<>();
61 private Map<String, String> resultMap = new HashMap<>();
63 protected OnapCommandHttpAuthClient authClient;
65 private OnapCommandHttpService oclipService = new OnapCommandHttpService();
67 private MocoServer mocoServer;
69 boolean shouldVerify = false;
71 boolean mockingEnabled;
73 public OnapHttpCommand() {
74 super.addDefaultSchemas(OnapCommandHttpConstants.DEFAULT_PARAMETER_HTTP_FILE_NAME);
77 public void setInput(HttpInput input) {
82 public String getSchemaVersion() {
83 return OnapCommandConstants.OPEN_CLI_SCHEMA_VERSION_VALUE_1_0;
86 public void setSuccessStatusCodes(List<Integer> successStatusCodes) {
87 this.successStatusCodes = successStatusCodes;
90 public void setResultMap(Map<String, String> resultMap) {
91 this.resultMap = resultMap;
94 public HttpInput getInput() {
98 public List<Integer> getSuccessStatusCodes() {
99 return successStatusCodes;
102 public Map<String, String> getResultMap() {
107 * Oclip service, this command uses to execute it.
109 public OnapCommandHttpService getService() {
110 return this.oclipService;
113 public void setService(OnapCommandHttpService service) {
114 this.oclipService = service;
118 protected List<String> initializeProfileSchema(Map<String, ?> schemaMap, boolean validate) throws OnapCommandException {
119 return OnapCommandSchemaHttpLoader.parseHttpSchema(this, schemaMap, validate);
123 protected void validate() throws OnapCommandException {
124 if (! this.isAuthRequired()) {
125 if (this.getParametersMap().containsKey(OnapCommandHttpConstants.DEAFULT_PARAMETER_USERNAME)) {
126 this.getParametersMap().get(OnapCommandHttpConstants.DEAFULT_PARAMETER_USERNAME).setOptional(true);
128 if (this.getParametersMap().containsKey(OnapCommandHttpConstants.DEAFULT_PARAMETER_PASSWORD)) {
129 this.getParametersMap().get(OnapCommandHttpConstants.DEAFULT_PARAMETER_PASSWORD).setOptional(true);
136 private boolean isAuthRequired() {
137 return !this.getService().isNoAuth()
138 && !(Boolean) (this.getParametersMap().get(OnapCommandHttpConstants.DEFAULT_PARAMETER_NO_AUTH).getValue())
139 && (this.getInfo().getCommandType().equals(OnapCommandType.CMD) ||
140 this.getInfo().getCommandType().equals(OnapCommandType.CATALOG));
143 private Optional<OnapCommandParameter> findParameterByName(String parameterName) {
144 return this.getParameters().stream()
145 .filter(e -> e.getName().equals(parameterName))
150 protected void preRun() throws OnapCommandException {
151 Optional<OnapCommandParameter> verifyOpt = this.getParameters().stream()
152 .filter(e -> "verify".equals(e.getName()))
154 if(verifyOpt.isPresent()) {
155 shouldVerify = (boolean) verifyOpt.get().getValue();
159 Optional<OnapCommandParameter> hostUrlParamOpt = findParameterByName(OnapCommandHttpConstants.VERIFY_HOST_PARAMETER_OPT);
160 Optional<OnapCommandParameter> noAuthParamOpt = findParameterByName(OnapCommandHttpConstants.VERIFY_NO_AUTH_PARAMETER_OPT);
162 if (hostUrlParamOpt.isPresent()) {
163 OnapCommandParameter onapCommandParameter = hostUrlParamOpt.get();
164 onapCommandParameter.setValue(
165 OnapCommandConfig.getPropertyValue(OnapCommandHttpConstants.VERIFY_MOCO_HOST)
166 + ":" + OnapCommandConfig.getPropertyValue(OnapCommandHttpConstants.VERIFY_MOCO_PORT));
169 if (noAuthParamOpt.isPresent()) {
170 OnapCommandParameter onapCommandParameter = noAuthParamOpt.get();
171 onapCommandParameter.setValue(true);
175 Optional<OnapCommandParameter> contextOpt = this.getParameters().stream()
176 .filter(e -> e.getName().equals(OnapCommandConstants.VERIFY_CONTEXT_PARAM))
179 if (contextOpt.isPresent()) {
180 OnapCommandParameter context = contextOpt.get();
181 Map<String, String> map = (Map<String, String>) context.getValue();
183 mockingEnabled = map.containsKey(OnapCommandHttpConstants.VERIFY_DISABLE_MOCKING)
184 && "true".equals(map.get(OnapCommandHttpConstants.VERIFY_DISABLE_MOCKING)) ? false : true;
186 if (mockingEnabled) {
187 String mockedFile = ((Map<String, String>) context.getValue()).get(OnapCommandConstants.VERIFY_MOCO);
188 mocoServer = new MocoServer(mockedFile);
196 protected void postRun() throws OnapCommandException {
197 if (shouldVerify && mockingEnabled) {
203 protected void run() throws OnapCommandException {
205 // For auth/catalog type commands, login and logout logic is not required
206 boolean isAuthRequired = this.isAuthRequired();
208 this.authClient = new OnapCommandHttpAuthClient(
210 this.getResult().isDebug());
212 if (isAuthRequired) {
213 this.authClient.login();
216 this.processRequest();
218 if (isAuthRequired) {
219 this.authClient.logout();
222 if (this.getResult().isDebug() && authClient != null) {
223 this.getResult().setDebugInfo(this.authClient.getDebugInfo());
225 } catch (OnapCommandException e) {
226 if (this.getResult().isDebug() && authClient != null) {
227 this.getResult().setDebugInfo(this.authClient.getDebugInfo());
233 protected void processRequest() throws OnapCommandException {
235 HttpInput httpInput = OnapCommandHttpUtils.populateParameters(this.getParametersMap(), this.getInput());
236 httpInput.setUri(this.authClient.getServiceUrl() + httpInput.getUri());
238 HttpResult output = this.authClient.run(httpInput);
240 this.getResult().setOutput(output);
241 if (!this.getSuccessStatusCodes().contains(output.getStatus())) {
242 throw new OnapCommandExecutionFailed(this.getName(), output.getBody(), output.getStatus());
245 //pre-process result map for spl entries and input parameters
246 for (Entry<String, String> resultMapEntry : this.getResultMap().entrySet()) {
247 String value = OnapCommandUtils.replaceLineForSpecialValues(resultMapEntry.getValue());
248 value = OnapCommandUtils.replaceLineFromInputParameters(value, this.getParametersMap());
249 this.resultMap.put(resultMapEntry.getKey(), value);
252 Map<String, ArrayList<String>> results = OnapCommandHttpUtils.populateOutputs(this.getResultMap(), output);
253 results = OnapCommandUtils.populateOutputsFromInputParameters(results, this.getParametersMap());
255 for (OnapCommandResultAttribute attr : this.getResult().getRecords()) {
256 attr.setValues(results.get(attr.getName()));
258 generateJsonMock(httpInput, output, this.getSchemaName());
261 private void generateJsonMock(HttpInput httpInput, HttpResult httpResult, String schemaName)
262 throws OnapCommandFailedMocoGenerate {
264 if (Boolean.parseBoolean(OnapCommandConfig.getPropertyValue(OnapCommandConstants.SAMPLE_GEN_ENABLED))) {
266 MockRequest mockRequest = new MockRequest();
267 mockRequest.setMethod(httpInput.getMethod());
268 mockRequest.setUri(httpInput.getUri());
269 mockRequest.setHeaders(httpInput.getReqHeaders());
270 mockRequest.setJson(httpInput.getBody());
272 MockResponse mockResponse = new MockResponse();
273 mockResponse.setStatus(httpResult.getStatus());
274 mockResponse.setJson(httpResult.getBody());
276 MockJsonGenerator.generateMocking(mockRequest, mockResponse,
277 OnapCommandConfig.getPropertyValue(OnapCommandConstants.SAMPLE_GEN_TARGET_FOLDER)
278 + "/" + schemaName.replace(".yaml", "") + "-moco.json");
279 } catch (IOException error) {
280 throw new OnapCommandFailedMocoGenerate(schemaName, error);