90ac961ef55363d5b9d2767ea423dee4252658ae
[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.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;
38
39 /**
40  * Onap Command.
41  *
42  */
43 public class OnapHttpCommand extends OnapCommand {
44
45     private HttpInput input = new HttpInput();
46
47     private List<Integer> successStatusCodes = new ArrayList<>();
48
49     private Map<String, String> resultMap = new HashMap<>();
50
51     public void setInput(HttpInput input) {
52         this.input = input;
53     }
54
55     @Override
56     public String getSchemaVersion() {
57         return Constants.OPEN_CLI_SCHEMA_VERSION_VALUE;
58     }
59
60     public void setSuccessStatusCodes(List<Integer> successStatusCodes) {
61         this.successStatusCodes = successStatusCodes;
62     }
63
64     public void setResultMap(Map<String, String> resultMap) {
65         this.resultMap = resultMap;
66     }
67
68     public HttpInput getInput() {
69         return input;
70     }
71
72     public List<Integer> getSuccessStatusCodes() {
73         return successStatusCodes;
74     }
75
76     public Map<String, String> getResultMap() {
77         return resultMap;
78     }
79
80     @Override
81     protected void initializeProfileSchema() throws OnapCommandException {
82         OnapCommandUtils.loadHTTPSchemaSection(this, this.getSchemaName(), false);
83     }
84
85     @Override
86     protected void run() throws OnapCommandException {
87         HttpInput httpInput = OnapCommandUtils.populateParameters(this.getParametersMap(), this.getInput());
88         httpInput.setUri(this.authClient.getServiceBasePath(this.getService()) + httpInput.getUri());
89
90         HttpResult output = this.authClient.run(httpInput);
91
92         this.getResult().setOutput(output);
93         if (!this.getSuccessStatusCodes().contains(output.getStatus())) {
94             throw new OnapCommandExecutionFailed(this.getName(), output.getBody(), output.getStatus());
95         }
96
97         Map<String, ArrayList<String>> results = OnapCommandUtils.populateOutputs(this.getResultMap(), output);
98
99         for (OnapCommandResultAttribute attr : this.getResult().getRecords()) {
100             attr.setValues(results.get(attr.getName()));
101         }
102         generateJsonMock(httpInput, output, this.getSchemaName());
103     }
104
105     private void generateJsonMock(HttpInput httpInput, HttpResult httpResult, String schemaName)
106             throws OnapCommandFailedMocoGenerate {
107
108         if (OnapCommandConfg.isSampleGenerateEnabled()) {
109             try {
110                 MockRequest mockRequest = new MockRequest();
111                 mockRequest.setMethod(httpInput.getMethod());
112                 mockRequest.setUri(httpInput.getUri());
113                 mockRequest.setHeaders(httpInput.getReqHeaders());
114                 mockRequest.setJson(httpInput.getBody());
115
116                 MockResponse mockResponse = new MockResponse();
117                 mockResponse.setStatus(httpResult.getStatus());
118                 mockResponse.setJson(httpResult.getBody());
119
120                 MockJsonGenerator.generateMocking(mockRequest, mockResponse, OnapCommandConfg.getSampleGenerateTargetFolder()
121                         + "/" + schemaName.replace(".yaml", "") + "-moco.json");
122             } catch (IOException error) {
123                 throw new OnapCommandFailedMocoGenerate(schemaName, error);
124             }
125         }
126     }
127 }