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.ad;
19 import java.util.HashMap;
20 import java.util.List;
23 import org.apache.http.HttpStatus;
24 import org.apache.http.auth.UsernamePasswordCredentials;
25 import org.apache.http.impl.auth.BasicScheme;
26 import org.onap.cli.fw.OnapCommand;
27 import org.onap.cli.fw.OnapCommandRegistrar;
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.OnapCommandHttpFailure;
33 import org.onap.cli.fw.error.OnapCommandInvalidParameterValue;
34 import org.onap.cli.fw.error.OnapCommandLoginFailed;
35 import org.onap.cli.fw.error.OnapCommandLogoutFailed;
36 import org.onap.cli.fw.error.OnapCommandNotFound;
37 import org.onap.cli.fw.error.OnapCommandServiceNotFound;
38 import org.onap.cli.fw.http.HttpInput;
39 import org.onap.cli.fw.http.HttpResult;
40 import org.onap.cli.fw.http.OnapHttpConnection;
41 import org.onap.cli.fw.input.OnapCommandParameter;
42 import org.onap.cli.fw.output.OnapCommandResultAttribute;
43 import org.onap.cli.fw.utils.OnapCommandUtils;
45 import com.jayway.jsonpath.JsonPath;
48 * Onap Auth client helps to do login and logout.
51 public class OnapAuthClient {
53 private OnapCommand cmd = null;
55 private OnapHttpConnection http = null;
57 public OnapAuthClient(OnapCommand cmd, boolean debug) throws OnapCommandHttpFailure, OnapCommandInvalidParameterValue {
59 this.http = new OnapHttpConnection(debug);
65 * @throws OnapCommandException
68 public void login() throws OnapCommandException {
70 // For development purpose, its introduced and is not supported for production
71 if (OnapCommandConfg.isAuthIgnored()) {
75 OnapCommand login = this.findAuthCommand("login");
77 OnapCommandUtils.copyParamsFrom(this.cmd, login);
78 login.getParametersMap().get(Constants.DEAFULT_PARAMETER_HOST_URL).setValue(this.getServiceUrl(login));
81 //It is safely assumed that all outputs are considered as common http headers.
82 Map<String, String> headers = new HashMap<>();
83 for (OnapCommandResultAttribute attr: login.getResult().getRecords()) {
84 String headerValue = attr.getValues().get(0);
85 if (headerValue != null && !headerValue.isEmpty()) {
86 headers.put(attr.getName(), attr.getValues().get(0));
90 this.http.setCommonHeaders(headers);
96 * @throws OnapCommandException
99 public void logout() throws OnapCommandException {
100 // For development purpose, its introduced and is not supported for production
101 if (OnapCommandConfg.isAuthIgnored()) {
105 OnapCommand logout = this.findAuthCommand("logout");
107 OnapCommandUtils.copyParamsFrom(this.cmd, logout);
115 * Find given service base path.
117 * @throws OnapCommandException
120 public String getServiceUrl() throws OnapCommandException {
121 return this.getServiceUrl(this.cmd);
124 private String getServiceUrl(OnapCommand cmd) throws OnapCommandException {
125 if (cmd.getService().isModeDirect()){
126 return cmd.getParametersMap().get(Constants.DEAFULT_PARAMETER_HOST_URL).getValue().toString();
127 } else { //Catalog mode
128 OnapCommand catalog = OnapCommandRegistrar.getRegistrar().get("catalog");
130 OnapCommandUtils.copyParamsFrom(cmd, catalog);
134 String hostUrl = catalog.getResult().getRecordsMap().get(Constants.CATALOG_SERVICE_HOST_URL).getValues().get(0);
135 hostUrl = hostUrl.trim();
136 if (hostUrl.endsWith("/")) {
137 hostUrl = hostUrl.substring(0, hostUrl.length()-1);
140 String basePath = catalog.getResult().getRecordsMap().get(Constants.CATALOG_SERVICE_BASE_PATH).getValues().get(0);
141 basePath = basePath.trim();
142 if (basePath.startsWith("/")) {
143 basePath = basePath.substring(1);
146 return hostUrl + "/" + basePath;
151 public String getDebugInfo() {
152 return this.http.getDebugInfo();
156 * Http call to external service.
160 * @return http result
161 * @throws OnapCommandHttpFailure
164 public HttpResult run(HttpInput input) throws OnapCommandHttpFailure {
165 return this.http.request(input);
168 private OnapCommand findAuthCommand(String authAction) throws OnapCommandException {
169 OnapCommand auth = null;
171 //Find the auth command for the given service and version under current enabled product
172 auth = OnapCommandRegistrar.getRegistrar().get(
173 this.cmd.getService().getName() + "-" +
174 this.cmd.getService().getVersion() + "-" +
175 this.cmd.getService().getAuthType() + "-" + authAction);
176 } catch (OnapCommandNotFound e) {
178 //Find the auth command for the given service under current enabled product
179 auth = OnapCommandRegistrar.getRegistrar().get(
180 this.cmd.getService().getName() + "-" +
181 this.cmd.getService().getAuthType() + "-" + authAction);
182 } catch (OnapCommandNotFound e1) {
183 //Find the auth command for current enabled product
184 auth = OnapCommandRegistrar.getRegistrar().get(
185 this.cmd.getService().getAuthType() + "-" + authAction);