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.cmd;
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.HashMap;
22 import java.util.List;
25 import org.onap.cli.fw.OnapCommand;
26 import org.onap.cli.fw.conf.Constants;
27 import org.onap.cli.fw.conf.OnapCommandConfg;
28 import org.onap.cli.fw.error.OnapCommandException;
29 import org.onap.cli.fw.error.OnapCommandExecutionFailed;
30 import org.onap.cli.fw.error.OnapCommandFailedMocoGenerate;
31 import org.onap.cli.fw.http.HttpInput;
32 import org.onap.cli.fw.http.HttpResult;
33 import org.onap.cli.fw.output.OnapCommandResultAttribute;
34 import org.onap.cli.fw.utils.OnapCommandUtils;
35 import org.onap.cli.http.mock.MockJsonGenerator;
36 import org.onap.cli.http.mock.MockRequest;
37 import org.onap.cli.http.mock.MockResponse;
43 public class OnapHttpCommand extends OnapCommand {
45 private HttpInput input = new HttpInput();
47 private List<Integer> successStatusCodes = new ArrayList<>();
49 private Map<String, String> resultMap = new HashMap<>();
51 public void setInput(HttpInput input) {
56 public String getSchemaVersion() {
57 return Constants.OPEN_CLI_SCHEMA_VERSION_VALUE;
60 public void setSuccessStatusCodes(List<Integer> successStatusCodes) {
61 this.successStatusCodes = successStatusCodes;
64 public void setResultMap(Map<String, String> resultMap) {
65 this.resultMap = resultMap;
68 public HttpInput getInput() {
72 public List<Integer> getSuccessStatusCodes() {
73 return successStatusCodes;
76 public Map<String, String> getResultMap() {
81 protected void initializeProfileSchema() throws OnapCommandException {
82 OnapCommandUtils.loadHTTPSchemaSection(this, this.getSchemaName(), false);
86 protected void run() throws OnapCommandException {
87 HttpInput httpInput = OnapCommandUtils.populateParameters(this.getParametersMap(), this.getInput());
88 httpInput.setUri(this.authClient.getServiceUrl() + httpInput.getUri());
90 HttpResult output = this.authClient.run(httpInput);
92 this.getResult().setOutput(output);
93 if (!this.getSuccessStatusCodes().contains(output.getStatus())) {
94 throw new OnapCommandExecutionFailed(this.getName(), output.getBody(), output.getStatus());
97 Map<String, ArrayList<String>> results = OnapCommandUtils.populateOutputs(this.getResultMap(), output);
98 results = OnapCommandUtils.populateOutputsFromInputParameters(results, this.getParametersMap());
100 for (OnapCommandResultAttribute attr : this.getResult().getRecords()) {
101 attr.setValues(results.get(attr.getName()));
103 generateJsonMock(httpInput, output, this.getSchemaName());
106 private void generateJsonMock(HttpInput httpInput, HttpResult httpResult, String schemaName)
107 throws OnapCommandFailedMocoGenerate {
109 if (OnapCommandConfg.isSampleGenerateEnabled()) {
111 MockRequest mockRequest = new MockRequest();
112 mockRequest.setMethod(httpInput.getMethod());
113 mockRequest.setUri(httpInput.getUri());
114 mockRequest.setHeaders(httpInput.getReqHeaders());
115 mockRequest.setJson(httpInput.getBody());
117 MockResponse mockResponse = new MockResponse();
118 mockResponse.setStatus(httpResult.getStatus());
119 mockResponse.setJson(httpResult.getBody());
121 MockJsonGenerator.generateMocking(mockRequest, mockResponse, OnapCommandConfg.getSampleGenerateTargetFolder()
122 + "/" + schemaName.replace(".yaml", "") + "-moco.json");
123 } catch (IOException error) {
124 throw new OnapCommandFailedMocoGenerate(schemaName, error);