Basic auth login and logout command
[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.getServiceUrl() + 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         results = OnapCommandUtils.populateOutputsFromInputParameters(results, this.getParametersMap());
99
100         for (OnapCommandResultAttribute attr : this.getResult().getRecords()) {
101             attr.setValues(results.get(attr.getName()));
102         }
103         generateJsonMock(httpInput, output, this.getSchemaName());
104     }
105
106     private void generateJsonMock(HttpInput httpInput, HttpResult httpResult, String schemaName)
107             throws OnapCommandFailedMocoGenerate {
108
109         if (OnapCommandConfg.isSampleGenerateEnabled()) {
110             try {
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());
116
117                 MockResponse mockResponse = new MockResponse();
118                 mockResponse.setStatus(httpResult.getStatus());
119                 mockResponse.setJson(httpResult.getBody());
120
121                 MockJsonGenerator.generateMocking(mockRequest, mockResponse, OnapCommandConfg.getSampleGenerateTargetFolder()
122                         + "/" + schemaName.replace(".yaml", "") + "-moco.json");
123             } catch (IOException error) {
124                 throw new OnapCommandFailedMocoGenerate(schemaName, error);
125             }
126         }
127     }
128 }