e5f17becb49e1b0fa967629bdc6e32b48741465d
[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
36 /**
37  * Oclip Auth client helps to do login and logout.
38  *
39  */
40 public class OnapCommandHttpAuthClient {
41
42     private OnapHttpCommand cmd = null;
43
44     private OnapHttpConnection http = null;
45
46     private Map<String, String> loginCache = new HashMap<>();
47
48     public OnapCommandHttpAuthClient(OnapHttpCommand cmd) {
49         this.cmd = cmd;
50         this.http = new OnapHttpConnection();
51     }
52
53     /**
54      * Login.
55      *
56      * @throws OnapCommandException
57      *             exception
58      */
59     public void login() throws OnapCommandException {
60
61         // For development purpose, its introduced and is not supported for production
62         if (Boolean.parseBoolean(OnapCommandConfig.getPropertyValue(OnapCommandHttpConstants.OPEN_IGNORE_AUTH))) {
63             return;
64         }
65
66         OnapCommand login = OnapCommandSchemaHttpLoader.findAuthCommand(this.cmd, "login");
67
68         OnapCommandUtils.copyParamsFrom(this.cmd, login);
69         login.execute();
70
71         //It is safely assumed that all outputs are considered as common http headers.
72         for (OnapCommandResultAttribute    attr: login.getResult().getRecords()) {
73             String headerValue = attr.getValues().get(0);
74             if (headerValue != null && !headerValue.isEmpty()) {
75                 this.loginCache.put(attr.getName(), attr.getValues().get(0));
76             }
77         }
78
79         this.http.setCommonHeaders(this.loginCache);
80     }
81
82     /**
83      * Logout.
84      *
85      * @throws OnapCommandException
86      *             exception
87      */
88     public void logout() throws OnapCommandException {
89         // For development purpose, its introduced and is not supported for production
90         if (Boolean.parseBoolean(OnapCommandConfig.getPropertyValue(OnapCommandHttpConstants.OPEN_IGNORE_AUTH))) {
91             return;
92         }
93
94         OnapCommand logout = OnapCommandSchemaHttpLoader.findAuthCommand(this.cmd, "logout");
95
96         //for logout, share the login cache, which would be used for logout such a token
97         OnapCommandUtils.copyParamsFrom(this.cmd, logout, this.loginCache);
98
99         logout.execute();
100
101         this.http.close();
102     }
103
104     /**
105      * Find given service base path.
106      *
107      * @throws OnapCommandException
108      *             exception
109      */
110     public String getServiceUrl() throws OnapCommandException {
111         return this.getServiceUrl(this.cmd);
112     }
113
114     private String getServiceUrl(OnapHttpCommand cmd) throws OnapCommandException {
115         if (cmd.getService().isModeDirect() ||
116             Boolean.parseBoolean(cmd.getParametersMap().get(OnapCommandHttpConstants.DEFAULT_PARAMETER_NO_CATALOG).getValue().toString())){
117             return cmd.getParametersMap().get(OnapCommandHttpConstants.DEAFULT_PARAMETER_HOST_URL).getValue().toString();
118         } else { //Catalog mode
119             OnapCommand catalog = OnapCommandRegistrar.getRegistrar().get("catalog", cmd.getInfo().getProduct());
120
121             Map<String, String> paramsOverrides = new HashMap<>();
122             paramsOverrides.put(OnapCommandHttpConstants.CATALOG_SERVICE_NAME, cmd.getService().getName());
123             paramsOverrides.put(OnapCommandHttpConstants.CATALOG_SERVICE_VERSION, cmd.getService().getVersion());
124
125             OnapCommandUtils.copyParamsFrom(cmd, catalog, paramsOverrides);
126
127             catalog.execute();
128
129             String hostUrl = catalog.getResult().getRecordsMap().get(OnapCommandHttpConstants.CATALOG_SERVICE_HOST_URL).getValues().get(0);
130             hostUrl = hostUrl.trim();
131             if (hostUrl.endsWith("/")) {
132                 hostUrl = hostUrl.substring(0, hostUrl.length()-1);
133             }
134
135             String basePath = catalog.getResult().getRecordsMap().get(OnapCommandHttpConstants.CATALOG_SERVICE_BASE_PATH).getValues().get(0);
136             basePath = basePath.trim();
137             if (basePath.startsWith("/")) {
138                 basePath = basePath.substring(1);
139             }
140
141             return hostUrl + "/" + basePath;
142         }
143     }
144
145     /**
146      * Http call to external service.
147      *
148      * @param input
149      *            http input
150      * @return http result
151      * @throws OnapCommandHttpFailure
152      *             exception
153      */
154     public HttpResult run(HttpInput input) throws OnapCommandHttpFailure {
155         return this.http.request(input);
156     }
157 }