1 package org.onap.vid.mso;
3 import com.att.eelf.configuration.EELFLogger;
4 import com.fasterxml.jackson.databind.ObjectMapper;
5 import org.apache.commons.codec.binary.Base64;
6 import org.apache.http.HttpException;
7 import org.eclipse.jetty.util.security.Password;
8 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
9 import org.onap.portalsdk.core.util.SystemProperties;
10 import org.onap.vid.aai.ExceptionWithRequestInfo;
11 import org.onap.vid.aai.util.HttpClientMode;
12 import org.onap.vid.aai.util.HttpsAuthClient;
13 import org.onap.vid.client.HttpBasicClient;
14 import org.onap.vid.exceptions.GenericUncheckedException;
15 import org.onap.vid.mso.rest.RestInterface;
16 import org.onap.vid.utils.Logging;
17 import org.springframework.beans.factory.annotation.Autowired;
18 import org.springframework.http.HttpMethod;
20 import javax.ws.rs.client.Client;
21 import javax.ws.rs.client.Entity;
22 import javax.ws.rs.client.Invocation;
23 import javax.ws.rs.core.MediaType;
24 import javax.ws.rs.core.MultivaluedHashMap;
25 import javax.ws.rs.core.Response;
26 import java.util.Collections;
27 import java.util.Optional;
29 import static org.onap.vid.utils.Logging.*;
32 * Created by pickjonathan on 26/06/2017.
34 public abstract class RestMsoImplementation implements RestInterface {
39 protected EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RestMsoImplementation.class);
40 private final EELFLogger outgoingRequestsLogger = Logging.getRequestsLogger("mso");
43 private Client client = null;
46 protected HttpsAuthClient httpsAuthClient;
48 private static final String START_LOG = " start";
49 private static final String APPLICATION_JSON = "application/json";
50 private static final String WITH_STATUS = " with status=";
51 private static final String URL_LOG = ", url=";
52 private static final String NO_RESPONSE_ENTITY_LOG = " No response entity, this is probably ok, e=";
53 private static final String WITH_URL_LOG = " with url=";
54 private static final String EXCEPTION_LOG = ", Exception: ";
55 private static final String REST_API_SUCCESSFULL_LOG = " REST api was successfull!";
56 private static final String REST_MSG_TEMPLATE = "start {}->{}({}, {}, {})";
57 /** The common headers. */
59 * Instantiates a new mso rest interface.
62 @SuppressWarnings("Duplicates")
63 protected MultivaluedHashMap<String, Object> initMsoClient()
65 final String methodname = "initRestClient()";
67 final String username = SystemProperties.getProperty(MsoProperties.MSO_USER_NAME);
68 final String password = SystemProperties.getProperty(MsoProperties.MSO_PASSWORD);
69 final String mso_url = SystemProperties.getProperty(MsoProperties.MSO_SERVER_URL);
70 final String decrypted_password = Password.deobfuscate(password);
72 String authString = username + ":" + decrypted_password;
74 byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
75 String authStringEnc = new String(authEncBytes);
77 MultivaluedHashMap<String, Object> commonHeaders = new MultivaluedHashMap();
78 commonHeaders.put("Authorization", Collections.singletonList(("Basic " + authStringEnc)));
79 commonHeaders.put("X-ONAP-PartnerName", Collections.singletonList("VID"));
81 String requestIdValue = Logging.extractOrGenerateRequestId();
82 commonHeaders.put(REQUEST_ID_HEADER_KEY, Collections.singletonList(requestIdValue));
83 commonHeaders.put(ONAP_REQUEST_ID_HEADER_KEY, Collections.singletonList(requestIdValue));
86 boolean useSsl = true;
87 if ( (mso_url != null) && ( !(mso_url.isEmpty()) ) ) {
88 useSsl = mso_url.startsWith("https");
94 client = httpsAuthClient.getClient(HttpClientMode.WITHOUT_KEYSTORE);
97 client = HttpBasicClient.getClient();
99 } catch (Exception e) {
100 logger.info(EELFLoggerDelegate.errorLogger,methodname + " Unable to get the SSL client");
104 return commonHeaders;
107 public <T> RestObjectWithRequestInfo<T> Get(T t, String path, RestObject<T> restObject, boolean warpException) {
108 String methodName = "Get";
110 logger.debug(EELFLoggerDelegate.debugLogger, methodName + START_LOG);
113 String rawData = null;
114 Integer status = null;
118 url = SystemProperties.getProperty(MsoProperties.MSO_SERVER_URL) + path;
120 MultivaluedHashMap<String, Object> commonHeaders = initMsoClient();
121 Logging.logRequest(outgoingRequestsLogger, HttpMethod.GET, url);
122 final Response cres = client.target(url)
124 .accept(APPLICATION_JSON)
125 .headers(commonHeaders)
127 Logging.logResponse(outgoingRequestsLogger, HttpMethod.GET, url, cres);
130 status = cres.getStatus();
131 rawData = cres.readEntity(String.class);
133 restObject.setStatusCode(status);
135 if (status == 200 || status == 202) {
136 t = (T) cres.readEntity(t.getClass());
138 logger.debug(EELFLoggerDelegate.debugLogger, methodName + REST_API_SUCCESSFULL_LOG);
141 throw new GenericUncheckedException(new HttpException(methodName + WITH_STATUS + status + " (200 or 202 expected), url= " + url));
144 logger.debug(EELFLoggerDelegate.debugLogger, methodName + " received status=" + status);
146 return new RestObjectWithRequestInfo<>(HttpMethod.GET, url, restObject, status, rawData);
147 } catch (RuntimeException e) {
148 throw warpException ? new ExceptionWithRequestInfo(HttpMethod.GET, url, rawData, status, e) : e;
153 public <T> RestObject<T> GetForObject(String path, Class<T> clazz) {
154 final String methodName = getMethodName();
155 logger.debug(EELFLoggerDelegate.debugLogger, "start {}->{}({}, {})", getMethodCallerName(), methodName, path, clazz);
157 String url = SystemProperties.getProperty(MsoProperties.MSO_SERVER_URL) + path;
158 logger.debug(EELFLoggerDelegate.debugLogger, "<== " + methodName + " sending request to url= " + url);
160 MultivaluedHashMap<String, Object> commonHeaders = initMsoClient();
161 Logging.logRequest(outgoingRequestsLogger, HttpMethod.GET, url);
162 final Response cres = client.target(url)
164 .accept(APPLICATION_JSON)
165 .headers(commonHeaders)
167 Logging.logResponse(outgoingRequestsLogger, HttpMethod.GET, url, cres);
168 final RestObject<T> restObject = cresToRestObject(cres, clazz);
169 int status = cres.getStatus();
171 if (status == 200 || status == 202) {
172 logger.debug(EELFLoggerDelegate.debugLogger, methodName + REST_API_SUCCESSFULL_LOG);
174 logger.debug(EELFLoggerDelegate.debugLogger,"<== " + methodName + WITH_STATUS +status+ URL_LOG +url);
177 logger.debug(EELFLoggerDelegate.debugLogger,methodName + " received status=" + status );
183 public <T> void Delete(T t, Object r, String path, RestObject<T> restObject) {
185 String methodName = "Delete";
189 logger.debug(EELFLoggerDelegate.debugLogger,"<== " + methodName + START_LOG);
192 MultivaluedHashMap<String, Object> commonHeaders = initMsoClient();
194 url = SystemProperties.getProperty(MsoProperties.MSO_SERVER_URL) + path;
195 Logging.logRequest(outgoingRequestsLogger, HttpMethod.DELETE, url, r);
196 cres = client.target(url)
199 .accept(APPLICATION_JSON)
200 .headers(commonHeaders)
202 .build("DELETE", Entity.entity(r, MediaType.APPLICATION_JSON)).invoke();
203 Logging.logResponse(outgoingRequestsLogger, HttpMethod.DELETE, url, cres);
204 int status = cres.getStatus();
205 restObject.setStatusCode (status);
207 if (status == 404) { // resource not found
208 String msg = "Resource does not exist...: " + cres.getStatus();
209 logger.debug(EELFLoggerDelegate.debugLogger,"<== " + msg);
210 } else if (status == 200 || status == 204){
211 logger.debug(EELFLoggerDelegate.debugLogger,"<== " + "Resource " + url + " deleted");
212 } else if (status == 202) {
213 String msg = "Delete in progress: " + status;
214 logger.debug(EELFLoggerDelegate.debugLogger,"<== " + msg);
217 String msg = "Deleting Resource failed: " + status;
218 logger.debug(EELFLoggerDelegate.debugLogger,"<== " + msg);
222 t = (T) cres.readEntity(t.getClass());
225 catch ( Exception e ) {
226 logger.debug(EELFLoggerDelegate.debugLogger,"<== " + methodName + NO_RESPONSE_ENTITY_LOG
233 logger.debug(EELFLoggerDelegate.debugLogger,"<== " + methodName + WITH_URL_LOG +url+ EXCEPTION_LOG + e.toString());
239 public <T> RestObject<T> PostForObject(Object requestDetails, String path, Class<T> clazz) {
240 logger.debug(EELFLoggerDelegate.debugLogger, REST_MSG_TEMPLATE, getMethodCallerName(), getMethodName(), requestDetails, path, clazz);
241 return restCall(HttpMethod.POST, clazz, requestDetails, path);
244 public <T> RestObject<T> DeleteForObject(Object requestDetails, String path, Class<T> clazz) {
245 logger.debug(EELFLoggerDelegate.debugLogger, REST_MSG_TEMPLATE, getMethodCallerName(), getMethodName(), requestDetails, path, clazz);
246 return restCall(HttpMethod.DELETE, clazz, requestDetails, path);
250 public void Post(String t, Object r, String path, RestObject<String> restObject) {
251 logger.debug(EELFLoggerDelegate.debugLogger, REST_MSG_TEMPLATE, getMethodCallerName(), getMethodName(), t.getClass(), r, path);
252 restObject.copyFrom(restCall(HttpMethod.POST, String.class, r, path));
255 public Invocation.Builder prepareClient(String path, String methodName) {
256 MultivaluedHashMap<String, Object> commonHeaders = initMsoClient();
258 String url = SystemProperties.getProperty(MsoProperties.MSO_SERVER_URL) + path;
259 logger.debug(EELFLoggerDelegate.debugLogger,"<== " + methodName + " sending request to url= " + url);
260 // Change the content length
261 return client.target(url)
263 .accept(APPLICATION_JSON)
264 .headers(commonHeaders);
267 public <T> RestObject<T> restCall(HttpMethod httpMethod, Class<T> tClass, Object payload, String path) {
268 return restCall(httpMethod, tClass, payload, path, Optional.empty());
273 user id is needed to be pass as X-RequestorID in new MSO flows like Delete instanceGroup
275 public <T> RestObject<T> restCall(HttpMethod httpMethod, Class<T> tClass, Object payload, String path, Optional<String> userId) {
276 String methodName = httpMethod.name();
281 MultivaluedHashMap<String, Object> commonHeaders = initMsoClient();
282 userId.ifPresent(id->commonHeaders.put("X-RequestorID", Collections.singletonList(id)));
284 url = SystemProperties.getProperty(MsoProperties.MSO_SERVER_URL) + path;
285 Logging.logRequest(outgoingRequestsLogger, httpMethod, url, payload);
286 // Change the content length
287 final Invocation.Builder restBuilder = client.target(url)
289 .accept(APPLICATION_JSON)
290 .headers(commonHeaders);
292 Invocation restInvocation = payload==null ?
293 restBuilder.build(httpMethod.name()) :
294 restBuilder.build(httpMethod.name(), Entity.entity(payload, MediaType.APPLICATION_JSON));
295 final Response cres = restInvocation.invoke();
297 Logging.logResponse(outgoingRequestsLogger, httpMethod, url, cres);
298 return cresToRestObject(cres, tClass);
300 catch (Exception e) {
301 logger.debug(EELFLoggerDelegate.debugLogger,"<== " + methodName + WITH_URL_LOG +url+ EXCEPTION_LOG + e.toString());
307 private <T> RestObject<T> cresToRestObject(Response cres, Class<?> tClass) {
308 RestObject<T> restObject = new RestObject<>();
310 String rawEntity = null;
313 rawEntity = cres.readEntity(String.class);
314 restObject.setRaw(rawEntity);
315 T t = (T) new ObjectMapper().readValue(rawEntity, tClass);
318 catch ( Exception e ) {
320 logger.debug(EELFLoggerDelegate.debugLogger, "<== " + getMethodCallerName() + " Error reading response entity as " + tClass + ": , e="
321 + e.getMessage() + ", Entity=" + rawEntity);
322 } catch (Exception e2) {
323 logger.debug(EELFLoggerDelegate.debugLogger, "<== " + getMethodCallerName() + NO_RESPONSE_ENTITY_LOG
328 int status = cres.getStatus();
329 restObject.setStatusCode (status);
336 public <T> void Put(T t, org.onap.vid.changeManagement.RequestDetailsWrapper r, String path, RestObject<T> restObject) {
338 String methodName = "Put";
341 logger.debug(EELFLoggerDelegate.debugLogger,"<== " + methodName + START_LOG);
345 MultivaluedHashMap<String, Object> commonHeaders = initMsoClient();
347 url = SystemProperties.getProperty(MsoProperties.MSO_SERVER_URL) + path;
348 Logging.logRequest(outgoingRequestsLogger, HttpMethod.PUT, url, r);
349 // Change the content length
350 final Response cres = client.target(url)
352 .accept(APPLICATION_JSON)
353 .headers(commonHeaders)
354 //.header("content-length", 201)
355 .put(Entity.entity(r, MediaType.APPLICATION_JSON));
357 Logging.logResponse(outgoingRequestsLogger, HttpMethod.PUT, url, cres);
360 t = (T) cres.readEntity(t.getClass());
363 catch ( Exception e ) {
364 logger.debug(EELFLoggerDelegate.debugLogger,"<== " + methodName + NO_RESPONSE_ENTITY_LOG
368 int status = cres.getStatus();
369 restObject.setStatusCode (status);
371 if ( status >= 200 && status <= 299 ) {
372 logger.info(EELFLoggerDelegate.errorLogger, "<== " + methodName + REST_API_SUCCESSFULL_LOG);
373 logger.debug(EELFLoggerDelegate.debugLogger,"<== " + methodName + REST_API_SUCCESSFULL_LOG);
376 logger.debug(EELFLoggerDelegate.debugLogger,"<== " + methodName + WITH_STATUS +status+ URL_LOG +url);
379 } catch (Exception e)
381 logger.debug(EELFLoggerDelegate.debugLogger,"<== " + methodName + WITH_URL_LOG +url+ EXCEPTION_LOG + e.toString());