2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.vid.mso;
23 import com.att.eelf.configuration.EELFLogger;
24 import com.fasterxml.jackson.databind.ObjectMapper;
25 import org.apache.commons.codec.binary.Base64;
26 import org.apache.http.HttpException;
27 import org.eclipse.jetty.util.security.Password;
28 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
29 import org.onap.vid.aai.ExceptionWithRequestInfo;
30 import org.onap.vid.aai.util.HttpClientMode;
31 import org.onap.vid.aai.util.HttpsAuthClient;
32 import org.onap.vid.client.HttpBasicClient;
33 import org.onap.vid.exceptions.GenericUncheckedException;
34 import org.onap.vid.mso.rest.RestInterface;
35 import org.onap.vid.utils.Logging;
36 import org.onap.vid.utils.SystemPropertiesWrapper;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.http.HttpMethod;
40 import javax.ws.rs.client.Client;
41 import javax.ws.rs.client.Entity;
42 import javax.ws.rs.client.Invocation;
43 import javax.ws.rs.core.MediaType;
44 import javax.ws.rs.core.MultivaluedHashMap;
45 import javax.ws.rs.core.Response;
46 import java.util.Collections;
47 import java.util.Optional;
49 import static org.onap.vid.utils.Logging.*;
52 * Created by pickjonathan on 26/06/2017.
54 public class RestMsoImplementation implements RestInterface {
59 protected EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RestMsoImplementation.class);
60 private final EELFLogger outgoingRequestsLogger = Logging.getRequestsLogger("mso");
63 private Client client = null;
66 protected HttpsAuthClient httpsAuthClient;
67 protected SystemPropertiesWrapper systemProperties;
69 private static final String START_LOG = " start";
70 private static final String APPLICATION_JSON = "application/json";
71 private static final String WITH_STATUS = " with status=";
72 private static final String URL_LOG = ", url=";
73 private static final String NO_RESPONSE_ENTITY_LOG = " No response entity, this is probably ok, e=";
74 private static final String WITH_URL_LOG = " with url=";
75 private static final String EXCEPTION_LOG = ", Exception: ";
76 private static final String REST_API_SUCCESSFULL_LOG = " REST api was successfull!";
77 private static final String REST_MSG_TEMPLATE = "start {}->{}({}, {}, {})";
78 /** The common headers. */
80 * Instantiates a new mso rest interface.
84 protected RestMsoImplementation(HttpsAuthClient httpsAuthClient, SystemPropertiesWrapper systemProperties){
85 this.httpsAuthClient=httpsAuthClient;
86 this.systemProperties = systemProperties;
89 @SuppressWarnings("Duplicates")
90 protected MultivaluedHashMap<String, Object> initMsoClient()
92 final String methodname = "initRestClient()";
94 final String username = systemProperties.getProperty(MsoProperties.MSO_USER_NAME);
95 final String password = systemProperties.getProperty(MsoProperties.MSO_PASSWORD);
96 final String mso_url = systemProperties.getProperty(MsoProperties.MSO_SERVER_URL);
97 final String decrypted_password = Password.deobfuscate(password);
99 String authString = username + ":" + decrypted_password;
101 byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
102 String authStringEnc = new String(authEncBytes);
104 MultivaluedHashMap<String, Object> commonHeaders = new MultivaluedHashMap();
105 commonHeaders.put("Authorization", Collections.singletonList(("Basic " + authStringEnc)));
106 commonHeaders.put("X-ONAP-PartnerName", Collections.singletonList("VID"));
108 String requestIdValue = Logging.extractOrGenerateRequestId();
109 commonHeaders.put(REQUEST_ID_HEADER_KEY, Collections.singletonList(requestIdValue));
110 commonHeaders.put(ONAP_REQUEST_ID_HEADER_KEY, Collections.singletonList(requestIdValue));
113 boolean useSsl = true;
114 if ( (mso_url != null) && ( !(mso_url.isEmpty()) ) ) {
115 useSsl = mso_url.startsWith("https");
117 if (client == null) {
121 client = httpsAuthClient.getClient(HttpClientMode.WITHOUT_KEYSTORE);
124 client = HttpBasicClient.getClient();
126 } catch (Exception e) {
127 logger.info(EELFLoggerDelegate.errorLogger,methodname + " Unable to get the SSL client");
131 return commonHeaders;
134 public <T> RestObjectWithRequestInfo<T> Get(T t, String path, RestObject<T> restObject, boolean warpException) {
135 String methodName = "Get";
137 logger.debug(EELFLoggerDelegate.debugLogger, methodName + START_LOG);
140 String rawData = null;
141 Integer status = null;
145 url = systemProperties.getProperty(MsoProperties.MSO_SERVER_URL) + path;
147 MultivaluedHashMap<String, Object> commonHeaders = initMsoClient();
148 Logging.logRequest(outgoingRequestsLogger, HttpMethod.GET, url);
149 final Response cres = client.target(url)
151 .accept(APPLICATION_JSON)
152 .headers(commonHeaders)
154 Logging.logResponse(outgoingRequestsLogger, HttpMethod.GET, url, cres);
157 status = cres.getStatus();
158 rawData = cres.readEntity(String.class);
160 restObject.setStatusCode(status);
162 if (status == 200 || status == 202) {
163 t = (T) cres.readEntity(t.getClass());
165 logger.debug(EELFLoggerDelegate.debugLogger, methodName + REST_API_SUCCESSFULL_LOG);
168 throw new GenericUncheckedException(new HttpException(methodName + WITH_STATUS + status + " (200 or 202 expected), url= " + url));
171 logger.debug(EELFLoggerDelegate.debugLogger, methodName + " received status=" + status);
173 return new RestObjectWithRequestInfo<>(HttpMethod.GET, url, restObject, status, rawData);
174 } catch (RuntimeException e) {
175 throw warpException ? new ExceptionWithRequestInfo(HttpMethod.GET, url, rawData, status, e) : e;
180 public <T> RestObject<T> GetForObject(String path, Class<T> clazz) {
181 final String methodName = getMethodName();
182 logger.debug(EELFLoggerDelegate.debugLogger, "start {}->{}({}, {})", getMethodCallerName(), methodName, path, clazz);
184 String url = systemProperties.getProperty(MsoProperties.MSO_SERVER_URL) + path;
185 logger.debug(EELFLoggerDelegate.debugLogger, "<== " + methodName + " sending request to url= " + url);
187 MultivaluedHashMap<String, Object> commonHeaders = initMsoClient();
188 Logging.logRequest(outgoingRequestsLogger, HttpMethod.GET, url);
189 final Response cres = client.target(url)
191 .accept(APPLICATION_JSON)
192 .headers(commonHeaders)
194 Logging.logResponse(outgoingRequestsLogger, HttpMethod.GET, url, cres);
195 final RestObject<T> restObject = cresToRestObject(cres, clazz);
196 int status = cres.getStatus();
198 if (status == 200 || status == 202) {
199 logger.debug(EELFLoggerDelegate.debugLogger, methodName + REST_API_SUCCESSFULL_LOG);
201 logger.debug(EELFLoggerDelegate.debugLogger,"<== " + methodName + WITH_STATUS +status+ URL_LOG +url);
204 logger.debug(EELFLoggerDelegate.debugLogger,methodName + " received status=" + status );
210 public <T> void Delete(T t, Object r, String path, RestObject<T> restObject) {
212 String methodName = "Delete";
216 logger.debug(EELFLoggerDelegate.debugLogger,"<== " + methodName + START_LOG);
219 MultivaluedHashMap<String, Object> commonHeaders = initMsoClient();
221 url = systemProperties.getProperty(MsoProperties.MSO_SERVER_URL) + path;
222 Logging.logRequest(outgoingRequestsLogger, HttpMethod.DELETE, url, r);
223 cres = client.target(url)
226 .accept(APPLICATION_JSON)
227 .headers(commonHeaders)
229 .build("DELETE", Entity.entity(r, MediaType.APPLICATION_JSON))
231 Logging.logResponse(outgoingRequestsLogger, HttpMethod.DELETE, url, cres);
232 int status = cres.getStatus();
233 restObject.setStatusCode (status);
235 if (status == 404) { // resource not found
236 String msg = "Resource does not exist...: " + cres.getStatus();
237 logger.debug(EELFLoggerDelegate.debugLogger,"<== " + msg);
238 } else if (status == 200 || status == 204){
239 logger.debug(EELFLoggerDelegate.debugLogger,"<== " + "Resource " + url + " deleted");
240 } else if (status == 202) {
241 String msg = "Delete in progress: " + status;
242 logger.debug(EELFLoggerDelegate.debugLogger,"<== " + msg);
245 String msg = "Deleting Resource failed: " + status;
246 logger.debug(EELFLoggerDelegate.debugLogger,"<== " + msg);
250 t = (T) cres.readEntity(t.getClass());
253 catch ( Exception e ) {
254 logger.debug(EELFLoggerDelegate.debugLogger,"<== " + methodName + NO_RESPONSE_ENTITY_LOG
262 logger.debug(EELFLoggerDelegate.debugLogger,"<== " + methodName + WITH_URL_LOG +url+ EXCEPTION_LOG + e.toString());
267 public <T> RestObject<T> PostForObject(Object requestDetails, String path, Class<T> clazz) {
268 logger.debug(EELFLoggerDelegate.debugLogger, REST_MSG_TEMPLATE, getMethodCallerName(), getMethodName(), requestDetails, path, clazz);
269 return restCall(HttpMethod.POST, clazz, requestDetails, path);
272 public <T> RestObject<T> DeleteForObject(Object requestDetails, String path, Class<T> clazz) {
273 logger.debug(EELFLoggerDelegate.debugLogger, REST_MSG_TEMPLATE, getMethodCallerName(), getMethodName(), requestDetails, path, clazz);
274 return restCall(HttpMethod.DELETE, clazz, requestDetails, path);
278 public void Post(String t, Object r, String path, RestObject<String> restObject) {
279 logger.debug(EELFLoggerDelegate.debugLogger, REST_MSG_TEMPLATE, getMethodCallerName(), getMethodName(), t.getClass(), r, path);
280 restObject.copyFrom(restCall(HttpMethod.POST, String.class, r, path));
283 public Invocation.Builder prepareClient(String path, String methodName) {
284 MultivaluedHashMap<String, Object> commonHeaders = initMsoClient();
286 String url = systemProperties.getProperty(MsoProperties.MSO_SERVER_URL) + path;
287 logger.debug(EELFLoggerDelegate.debugLogger,"<== " + methodName + " sending request to url= " + url);
288 // Change the content length
289 return client.target(url)
291 .accept(APPLICATION_JSON)
292 .headers(commonHeaders);
295 public <T> RestObject<T> restCall(HttpMethod httpMethod, Class<T> tClass, Object payload, String path) {
296 return restCall(httpMethod, tClass, payload, path, Optional.empty());
301 user id is needed to be pass as X-RequestorID in new MSO flows like Delete instanceGroup
303 public <T> RestObject<T> restCall(HttpMethod httpMethod, Class<T> tClass, Object payload, String path, Optional<String> userId) {
304 String methodName = httpMethod.name();
309 MultivaluedHashMap<String, Object> commonHeaders = initMsoClient();
310 userId.ifPresent(id->commonHeaders.put("X-RequestorID", Collections.singletonList(id)));
312 url = systemProperties.getProperty(MsoProperties.MSO_SERVER_URL) + path;
313 Logging.logRequest(outgoingRequestsLogger, httpMethod, url, payload);
314 // Change the content length
315 final Invocation.Builder restBuilder = client.target(url)
317 .accept(APPLICATION_JSON)
318 .headers(commonHeaders);
320 Invocation restInvocation = payload==null ?
321 restBuilder.build(httpMethod.name()) :
322 restBuilder.build(httpMethod.name(), Entity.entity(payload, MediaType.APPLICATION_JSON));
323 final Response cres = restInvocation.invoke();
325 Logging.logResponse(outgoingRequestsLogger, httpMethod, url, cres);
326 return cresToRestObject(cres, tClass);
328 catch (Exception e) {
329 logger.debug(EELFLoggerDelegate.debugLogger,"<== " + methodName + WITH_URL_LOG +url+ EXCEPTION_LOG + e.toString());
335 private <T> RestObject<T> cresToRestObject(Response cres, Class<?> tClass) {
336 RestObject<T> restObject = new RestObject<>();
338 String rawEntity = null;
341 rawEntity = cres.readEntity(String.class);
342 restObject.setRaw(rawEntity);
343 T t = (T) new ObjectMapper().readValue(rawEntity, tClass);
346 catch ( Exception e ) {
348 logger.debug(EELFLoggerDelegate.debugLogger, "<== " + getMethodCallerName() + " Error reading response entity as " + tClass + ": , e="
349 + e.getMessage() + ", Entity=" + rawEntity);
350 } catch (Exception e2) {
351 logger.debug(EELFLoggerDelegate.debugLogger, "<== " + getMethodCallerName() + NO_RESPONSE_ENTITY_LOG
356 int status = cres.getStatus();
357 restObject.setStatusCode (status);
363 public <T> void Put(T t, org.onap.vid.changeManagement.RequestDetailsWrapper r, String path, RestObject<T> restObject) {
365 String methodName = "Put";
368 logger.debug(EELFLoggerDelegate.debugLogger,"<== " + methodName + START_LOG);
372 MultivaluedHashMap<String, Object> commonHeaders = initMsoClient();
374 url = systemProperties.getProperty(MsoProperties.MSO_SERVER_URL) + path;
375 Logging.logRequest(outgoingRequestsLogger, HttpMethod.PUT, url, r);
376 // Change the content length
377 final Response cres = client.target(url)
379 .accept(APPLICATION_JSON)
380 .headers(commonHeaders)
381 //.header("content-length", 201)
382 .put(Entity.entity(r, MediaType.APPLICATION_JSON));
384 Logging.logResponse(outgoingRequestsLogger, HttpMethod.PUT, url, cres);
387 t = (T) cres.readEntity(t.getClass());
390 catch ( Exception e ) {
391 logger.debug(EELFLoggerDelegate.debugLogger,"<== " + methodName + NO_RESPONSE_ENTITY_LOG
396 int status = cres.getStatus();
397 restObject.setStatusCode (status);
399 if ( status >= 200 && status <= 299 ) {
400 logger.info(EELFLoggerDelegate.errorLogger, "<== " + methodName + REST_API_SUCCESSFULL_LOG);
401 logger.debug(EELFLoggerDelegate.debugLogger,"<== " + methodName + REST_API_SUCCESSFULL_LOG);
404 logger.debug(EELFLoggerDelegate.debugLogger,"<== " + methodName + WITH_STATUS +status+ URL_LOG +url);
407 } catch (Exception e)
409 logger.debug(EELFLoggerDelegate.debugLogger,"<== " + methodName + WITH_URL_LOG +url+ EXCEPTION_LOG + e.toString());