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.ad.OnapAuthClient;
27 import org.onap.cli.fw.ad.OnapService;
28 import org.onap.cli.fw.conf.Constants;
29 import org.onap.cli.fw.conf.OnapCommandConfg;
30 import org.onap.cli.fw.error.OnapCommandException;
31 import org.onap.cli.fw.error.OnapCommandExecutionFailed;
32 import org.onap.cli.fw.error.OnapCommandFailedMocoGenerate;
33 import org.onap.cli.fw.http.HttpInput;
34 import org.onap.cli.fw.http.HttpResult;
35 import org.onap.cli.fw.input.OnapCommandParameter;
36 import org.onap.cli.fw.output.OnapCommandResultAttribute;
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;
46 public class OnapHttpCommand extends OnapCommand {
48 private HttpInput input = new HttpInput();
50 private List<Integer> successStatusCodes = new ArrayList<>();
52 private Map<String, String> resultMap = new HashMap<>();
54 protected OnapAuthClient authClient;
56 private OnapService onapService = new OnapService();
58 public void setInput(HttpInput input) {
63 public String getSchemaVersion() {
64 return Constants.OPEN_CLI_SCHEMA_VERSION_VALUE_1_0;
67 public void setSuccessStatusCodes(List<Integer> successStatusCodes) {
68 this.successStatusCodes = successStatusCodes;
71 public void setResultMap(Map<String, String> resultMap) {
72 this.resultMap = resultMap;
75 public HttpInput getInput() {
79 public List<Integer> getSuccessStatusCodes() {
80 return successStatusCodes;
83 public Map<String, String> getResultMap() {
88 * Onap service, this command uses to execute it.
90 public OnapService getService() {
91 return this.onapService;
94 public void setService(OnapService service) {
95 this.onapService = service;
99 protected void initializeProfileSchema() throws OnapCommandException {
100 OnapCommandUtils.loadHttpSchema(this, this.getSchemaName(), true, false);
104 protected void validate() throws OnapCommandException {
105 if (! this.isAuthRequired()) {
106 if (this.getParametersMap().containsKey(Constants.DEAFULT_PARAMETER_USERNAME)) {
107 this.getParametersMap().get(Constants.DEAFULT_PARAMETER_USERNAME).setOptional(true);
109 if (this.getParametersMap().containsKey(Constants.DEAFULT_PARAMETER_PASSWORD)) {
110 this.getParametersMap().get(Constants.DEAFULT_PARAMETER_PASSWORD).setOptional(true);
117 private boolean isAuthRequired() {
118 return !this.getService().isNoAuth()
119 && "false".equals(this.getParametersMap().get(Constants.DEFAULT_PARAMETER_NO_AUTH).getValue())
120 && this.getInfo().getCommandType().equals(CommandType.CMD);
124 protected void run() throws OnapCommandException {
126 // For auth/catalog type commands, login and logout logic is not required
127 boolean isAuthRequired = this.isAuthRequired();
129 this.authClient = new OnapAuthClient(
131 this.getResult().isDebug());
133 if (isAuthRequired) {
134 this.authClient.login();
137 this.processRequest();
139 if (isAuthRequired) {
140 this.authClient.logout();
143 if (this.getResult().isDebug() && authClient != null) {
144 this.getResult().setDebugInfo(this.authClient.getDebugInfo());
146 } catch (OnapCommandException e) {
147 if (this.getResult().isDebug() && authClient != null) {
148 this.getResult().setDebugInfo(this.authClient.getDebugInfo());
154 protected void processRequest() throws OnapCommandException {
156 HttpInput httpInput = OnapCommandUtils.populateParameters(this.getParametersMap(), this.getInput());
157 httpInput.setUri(this.authClient.getServiceUrl() + httpInput.getUri());
159 HttpResult output = this.authClient.run(httpInput);
161 this.getResult().setOutput(output);
162 if (!this.getSuccessStatusCodes().contains(output.getStatus())) {
163 throw new OnapCommandExecutionFailed(this.getName(), output.getBody(), output.getStatus());
166 Map<String, ArrayList<String>> results = OnapCommandUtils.populateOutputs(this.getResultMap(), output);
167 results = OnapCommandUtils.populateOutputsFromInputParameters(results, this.getParametersMap());
169 for (OnapCommandResultAttribute attr : this.getResult().getRecords()) {
170 attr.setValues(results.get(attr.getName()));
172 generateJsonMock(httpInput, output, this.getSchemaName());
175 private void generateJsonMock(HttpInput httpInput, HttpResult httpResult, String schemaName)
176 throws OnapCommandFailedMocoGenerate {
178 if (OnapCommandConfg.isSampleGenerateEnabled()) {
180 MockRequest mockRequest = new MockRequest();
181 mockRequest.setMethod(httpInput.getMethod());
182 mockRequest.setUri(httpInput.getUri());
183 mockRequest.setHeaders(httpInput.getReqHeaders());
184 mockRequest.setJson(httpInput.getBody());
186 MockResponse mockResponse = new MockResponse();
187 mockResponse.setStatus(httpResult.getStatus());
188 mockResponse.setJson(httpResult.getBody());
190 MockJsonGenerator.generateMocking(mockRequest, mockResponse, OnapCommandConfg.getSampleGenerateTargetFolder()
191 + "/" + schemaName.replace(".yaml", "") + "-moco.json");
192 } catch (IOException error) {
193 throw new OnapCommandFailedMocoGenerate(schemaName, error);