Code improvement for pending sonar issues
[cli.git] / profiles / http / src / main / java / org / onap / cli / fw / http / auth / OnapCommandHttpAuthClient.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.http.auth;
18
19 import java.util.HashMap;
20 import java.util.Map;
21
22 import org.onap.cli.fw.cmd.OnapCommand;
23 import org.onap.cli.fw.conf.OnapCommandConfig;
24 import org.onap.cli.fw.error.OnapCommandException;
25 import org.onap.cli.fw.http.cmd.OnapHttpCommand;
26 import org.onap.cli.fw.http.conf.OnapCommandHttpConstants;
27 import org.onap.cli.fw.http.connect.HttpInput;
28 import org.onap.cli.fw.http.connect.HttpResult;
29 import org.onap.cli.fw.http.connect.OnapHttpConnection;
30 import org.onap.cli.fw.http.error.OnapCommandHttpFailure;
31 import org.onap.cli.fw.http.schema.OnapCommandSchemaHttpLoader;
32 import org.onap.cli.fw.output.OnapCommandResultAttribute;
33 import org.onap.cli.fw.registrar.OnapCommandRegistrar;
34 import org.onap.cli.fw.utils.OnapCommandUtils;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import java.io.IOException;
38 import org.onap.cli.fw.http.error.OnapCommandLoginFailed;
39
40 /**
41  * Oclip Auth client helps to do login and logout.
42  *
43  */
44 public class OnapCommandHttpAuthClient {
45
46     private static Logger logger = LoggerFactory.getLogger(OnapCommandHttpAuthClient.class); //NOSONAR
47     private OnapHttpCommand cmd = null;
48
49     private OnapHttpConnection http = null;
50
51     private Map<String, String> loginCache = new HashMap<>();
52
53     public OnapCommandHttpAuthClient(OnapHttpCommand cmd) {
54         this.cmd = cmd;
55         this.http = new OnapHttpConnection();
56     }
57
58     /**
59      * Login.
60      *
61      * @throws OnapCommandException
62      *             exception
63      */
64     public void login() throws OnapCommandException {
65
66         // For development purpose, its introduced and is not supported for production
67         if (Boolean.parseBoolean(OnapCommandConfig.getPropertyValue(OnapCommandHttpConstants.OPEN_IGNORE_AUTH))) {
68             return;
69         }
70
71         OnapCommand login = OnapCommandSchemaHttpLoader.findAuthCommand(this.cmd, "login");
72
73         OnapCommandUtils.copyParamsFrom(this.cmd, login);
74         login.execute();
75
76         //It is safely assumed that all outputs are considered as common http headers.
77         for (OnapCommandResultAttribute    attr: login.getResult().getRecords()) {
78             String headerValue = attr.getValues().get(0);
79             if (headerValue != null && !headerValue.isEmpty()) {
80                 this.loginCache.put(attr.getName(), attr.getValues().get(0));
81             }
82         }
83
84         this.http.setCommonHeaders(this.loginCache);
85     }
86
87     /**
88      * Logout.
89      *
90      * @throws OnapCommandException
91      *             exception
92      */
93     public void logout() throws OnapCommandException {
94         // For development purpose, its introduced and is not supported for production
95         if (Boolean.parseBoolean(OnapCommandConfig.getPropertyValue(OnapCommandHttpConstants.OPEN_IGNORE_AUTH))) {
96             return;
97         }
98
99         OnapCommand logout = OnapCommandSchemaHttpLoader.findAuthCommand(this.cmd, "logout");
100
101         //for logout, share the login cache, which would be used for logout such a token
102         OnapCommandUtils.copyParamsFrom(this.cmd, logout, this.loginCache);
103
104         logout.execute();
105
106         try {
107             this.http.close();
108         } catch (IOException e) {
109             throw new OnapCommandLoginFailed("Exception when closing httpclient");
110         }
111     }
112
113     /**
114      * Find given service base path.
115      *
116      * @throws OnapCommandException
117      *             exception
118      */
119     public String getServiceUrl() throws OnapCommandException {
120         return this.getServiceUrl(this.cmd);
121     }
122
123     private String getServiceUrl(OnapHttpCommand cmd) throws OnapCommandException {
124         if (cmd.getService().isModeDirect() ||
125             Boolean.parseBoolean(cmd.getParametersMap().get(OnapCommandHttpConstants.DEFAULT_PARAMETER_NO_CATALOG).getValue().toString())){
126             return cmd.getParametersMap().get(OnapCommandHttpConstants.DEAFULT_PARAMETER_HOST_URL).getValue().toString();
127         } else { //Catalog mode
128             OnapCommand catalog = OnapCommandRegistrar.getRegistrar().get("catalog", cmd.getInfo().getProduct());
129
130             Map<String, String> paramsOverrides = new HashMap<>();
131             paramsOverrides.put(OnapCommandHttpConstants.CATALOG_SERVICE_NAME, cmd.getService().getName());
132             paramsOverrides.put(OnapCommandHttpConstants.CATALOG_SERVICE_VERSION, cmd.getService().getVersion());
133
134             OnapCommandUtils.copyParamsFrom(cmd, catalog, paramsOverrides);
135
136             catalog.execute();
137
138             String hostUrl = catalog.getResult().getRecordsMap().get(OnapCommandHttpConstants.CATALOG_SERVICE_HOST_URL).getValues().get(0);
139             hostUrl = hostUrl.trim();
140             if (hostUrl.endsWith("/")) {
141                 hostUrl = hostUrl.substring(0, hostUrl.length()-1);
142             }
143
144             String basePath = catalog.getResult().getRecordsMap().get(OnapCommandHttpConstants.CATALOG_SERVICE_BASE_PATH).getValues().get(0);
145             basePath = basePath.trim();
146             if (basePath.startsWith("/")) {
147                 basePath = basePath.substring(1);
148             }
149
150             return hostUrl + "/" + basePath;
151         }
152     }
153
154     /**
155      * Http call to external service.
156      *
157      * @param input
158      *            http input
159      * @return http result
160      * @throws OnapCommandHttpFailure
161      *             exception
162      */
163     public HttpResult run(HttpInput input) throws OnapCommandHttpFailure {
164         return this.http.request(input);
165     }
166 }