Normalize OnapCommandSchema
[cli.git] / framework / src / main / java / org / onap / cli / fw / cmd / OnapHttpCommand.java
1 /*
2  * Copyright 2017 Huawei Technologies Co., Ltd.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.cli.fw.cmd;
18
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24
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;
41
42 /**
43  * Onap Command.
44  *
45  */
46 public class OnapHttpCommand extends OnapCommand {
47
48     private HttpInput input = new HttpInput();
49
50     private List<Integer> successStatusCodes = new ArrayList<>();
51
52     private Map<String, String> resultMap = new HashMap<>();
53
54     protected OnapAuthClient authClient;
55
56     private OnapService onapService = new OnapService();
57
58     public void setInput(HttpInput input) {
59         this.input = input;
60     }
61
62     @Override
63     public String getSchemaVersion() {
64         return Constants.OPEN_CLI_SCHEMA_VERSION_VALUE_1_0;
65     }
66
67     public void setSuccessStatusCodes(List<Integer> successStatusCodes) {
68         this.successStatusCodes = successStatusCodes;
69     }
70
71     public void setResultMap(Map<String, String> resultMap) {
72         this.resultMap = resultMap;
73     }
74
75     public HttpInput getInput() {
76         return input;
77     }
78
79     public List<Integer> getSuccessStatusCodes() {
80         return successStatusCodes;
81     }
82
83     public Map<String, String> getResultMap() {
84         return resultMap;
85     }
86
87     /*
88      * Onap service, this command uses to execute it.
89      */
90     public OnapService getService() {
91         return this.onapService;
92     }
93
94     public void setService(OnapService service) {
95         this.onapService = service;
96     }
97
98     @Override
99     protected void initializeProfileSchema() throws OnapCommandException {
100         OnapCommandUtils.loadHttpSchema(this, this.getSchemaName(), true, false);
101     }
102
103     @Override
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);
108             }
109             if (this.getParametersMap().containsKey(Constants.DEAFULT_PARAMETER_PASSWORD)) {
110                 this.getParametersMap().get(Constants.DEAFULT_PARAMETER_PASSWORD).setOptional(true);
111             }
112         }
113
114         super.validate();
115     }
116
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);
121     }
122
123     @Override
124     protected void run() throws OnapCommandException {
125         try {
126             // For auth/catalog type commands, login and logout logic is not required
127             boolean isAuthRequired = this.isAuthRequired();
128
129             this.authClient = new OnapAuthClient(
130                     this,
131                     this.getResult().isDebug());
132
133             if (isAuthRequired) {
134                 this.authClient.login();
135             }
136
137             this.processRequest();
138
139             if (isAuthRequired) {
140                 this.authClient.logout();
141             }
142
143             if (this.getResult().isDebug() && authClient != null) {
144                 this.getResult().setDebugInfo(this.authClient.getDebugInfo());
145             }
146         } catch (OnapCommandException e) {
147             if (this.getResult().isDebug() && authClient != null) {
148                 this.getResult().setDebugInfo(this.authClient.getDebugInfo());
149             }
150             throw e;
151         }
152     }
153
154     protected void processRequest() throws OnapCommandException {
155
156         HttpInput httpInput = OnapCommandUtils.populateParameters(this.getParametersMap(), this.getInput());
157         httpInput.setUri(this.authClient.getServiceUrl() + httpInput.getUri());
158
159         HttpResult output = this.authClient.run(httpInput);
160
161         this.getResult().setOutput(output);
162         if (!this.getSuccessStatusCodes().contains(output.getStatus())) {
163             throw new OnapCommandExecutionFailed(this.getName(), output.getBody(), output.getStatus());
164         }
165
166         Map<String, ArrayList<String>> results = OnapCommandUtils.populateOutputs(this.getResultMap(), output);
167         results = OnapCommandUtils.populateOutputsFromInputParameters(results, this.getParametersMap());
168
169         for (OnapCommandResultAttribute attr : this.getResult().getRecords()) {
170             attr.setValues(results.get(attr.getName()));
171         }
172         generateJsonMock(httpInput, output, this.getSchemaName());
173     }
174
175     private void generateJsonMock(HttpInput httpInput, HttpResult httpResult, String schemaName)
176             throws OnapCommandFailedMocoGenerate {
177
178         if (OnapCommandConfg.isSampleGenerateEnabled()) {
179             try {
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());
185
186                 MockResponse mockResponse = new MockResponse();
187                 mockResponse.setStatus(httpResult.getStatus());
188                 mockResponse.setJson(httpResult.getBody());
189
190                 MockJsonGenerator.generateMocking(mockRequest, mockResponse, OnapCommandConfg.getSampleGenerateTargetFolder()
191                         + "/" + schemaName.replace(".yaml", "") + "-moco.json");
192             } catch (IOException error) {
193                 throw new OnapCommandFailedMocoGenerate(schemaName, error);
194             }
195         }
196     }
197 }