Aharoni, Pavel (pa0916) | ca3cb01 | 2018-10-22 15:29:57 +0300 | [diff] [blame] | 1 | /*-
|
| 2 | * ============LICENSE_START=======================================================
|
| 3 | * OSAM
|
| 4 | * ================================================================================
|
| 5 | * Copyright (C) 2018 AT&T
|
| 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
|
| 10 | *
|
| 11 | * http://www.apache.org/licenses/LICENSE-2.0
|
| 12 | *
|
| 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=========================================================
|
| 19 | */
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 | package org.onap.osam.mso;
|
| 24 |
|
| 25 | import com.att.eelf.configuration.EELFLogger;
|
| 26 | import com.fasterxml.jackson.databind.ObjectMapper;
|
| 27 | import org.apache.commons.codec.binary.Base64;
|
| 28 | import org.eclipse.jetty.util.security.Password;
|
| 29 | import org.onap.osam.aai.util.HttpClientMode;
|
| 30 | import org.onap.osam.aai.util.HttpsAuthClient;
|
| 31 | import org.onap.osam.client.HttpBasicClient;
|
| 32 | import org.onap.osam.exceptions.GenericUncheckedException;
|
| 33 | import org.onap.osam.mso.rest.RequestDetailsWrapper;
|
| 34 | import org.onap.osam.mso.rest.RestInterface;
|
| 35 | import org.onap.osam.utils.Logging;
|
| 36 | import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
|
| 37 | import org.onap.portalsdk.core.util.SystemProperties;
|
| 38 | import org.springframework.beans.factory.annotation.Autowired;
|
| 39 | import org.springframework.http.HttpMethod;
|
| 40 |
|
| 41 | import javax.ws.rs.client.Client;
|
| 42 | import javax.ws.rs.client.Entity;
|
| 43 | import javax.ws.rs.client.Invocation;
|
| 44 | import javax.ws.rs.core.MediaType;
|
| 45 | import javax.ws.rs.core.MultivaluedHashMap;
|
| 46 | import javax.ws.rs.core.Response;
|
| 47 | import java.text.DateFormat;
|
| 48 | import java.text.SimpleDateFormat;
|
| 49 | import java.util.Collections;
|
| 50 | import java.util.Date;
|
| 51 | import java.util.UUID;
|
| 52 |
|
| 53 | import static org.onap.osam.utils.Logging.*;
|
| 54 |
|
| 55 | public class RestMsoImplementation implements RestInterface {
|
| 56 |
|
| 57 | public static final String START_LOG = " start";
|
| 58 | public static final String APPLICATION_JSON = "application/json";
|
| 59 | public static final String WITH_STATUS = " with status=";
|
| 60 | public static final String URL_LOG = ", url=";
|
| 61 | public static final String NO_RESPONSE_ENTITY_LOG = " No response entity, this is probably ok, e=";
|
| 62 | public static final String WITH_URL_LOG = " with url=";
|
| 63 | public static final String EXCEPTION_LOG = ", Exception: ";
|
| 64 | public static final String REST_API_SUCCESSFULL_LOG = " REST api was successfull!";
|
| 65 | public static final String REST_API_POST_WAS_SUCCESSFUL_LOG = " REST api POST was successful!";
|
| 66 | EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RestMsoImplementation.class);
|
| 67 | private final EELFLogger outgoingRequestsLogger = Logging.getRequestsLogger("mso");
|
| 68 |
|
| 69 | /**
|
| 70 | * The Constant dateFormat.
|
| 71 | */
|
| 72 | static final DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:SSSS");
|
| 73 |
|
| 74 | /** The client. */
|
| 75 | private Client client = null;
|
| 76 |
|
| 77 | @Autowired
|
| 78 | HttpsAuthClient httpsAuthClient;
|
| 79 |
|
| 80 | /** The common headers. */
|
| 81 | /**
|
| 82 | * Instantiates a new mso rest interface.
|
| 83 | */
|
| 84 |
|
| 85 | @SuppressWarnings("Duplicates")
|
| 86 | @Override
|
| 87 | public MultivaluedHashMap<String, Object> initMsoClient()
|
| 88 | {
|
| 89 | final String methodname = "initRestClient()";
|
| 90 |
|
| 91 | final String username = SystemProperties.getProperty(MsoProperties.MSO_USER_NAME);
|
| 92 | final String password = SystemProperties.getProperty(MsoProperties.MSO_PASSWORD);
|
| 93 | final String mso_url = SystemProperties.getProperty(MsoProperties.MSO_SERVER_URL);
|
| 94 | final String decrypted_password = Password.deobfuscate(password);
|
| 95 |
|
| 96 | String authString = username + ":" + decrypted_password;
|
| 97 |
|
| 98 | byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
|
| 99 | String authStringEnc = new String(authEncBytes);
|
| 100 |
|
| 101 | MultivaluedHashMap<String, Object> commonHeaders = new MultivaluedHashMap();
|
| 102 | commonHeaders.put("Authorization", Collections.singletonList(("Basic " + authStringEnc)));
|
| 103 | //Pass calling application identifier to SO
|
| 104 | commonHeaders.put("X-FromAppId", Collections.singletonList(SystemProperties.getProperty(SystemProperties.APP_DISPLAY_NAME)));
|
| 105 | try {
|
| 106 | commonHeaders.put(REQUEST_ID_HEADER_KEY, Collections.singletonList(Logging.extractOrGenerateRequestId()));
|
| 107 | }
|
| 108 | catch (IllegalStateException e){
|
| 109 | //in async jobs we don't have any HttpServletRequest
|
| 110 | commonHeaders.put(REQUEST_ID_HEADER_KEY, Collections.singletonList(UUID.randomUUID().toString()));
|
| 111 | }
|
| 112 |
|
| 113 |
|
| 114 | boolean useSsl = true;
|
| 115 | if ( (mso_url != null) && ( !(mso_url.isEmpty()) ) ) {
|
| 116 | useSsl = mso_url.startsWith("https");
|
| 117 | }
|
| 118 | if (client == null) {
|
| 119 |
|
| 120 | try {
|
| 121 | if ( useSsl ) {
|
| 122 | client = httpsAuthClient.getClient(HttpClientMode.WITHOUT_KEYSTORE);
|
| 123 | }
|
| 124 | else {
|
| 125 | client = HttpBasicClient.getClient();
|
| 126 | }
|
| 127 | } catch (Exception e) {
|
| 128 | logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + methodname + " Unable to get the SSL client");
|
| 129 | }
|
| 130 | }
|
| 131 |
|
| 132 | return commonHeaders;
|
| 133 | }
|
| 134 |
|
| 135 | public <T> void Get (T t, String sourceId, String path, RestObject<T> restObject ) {
|
| 136 | String methodName = "Get";
|
| 137 |
|
| 138 | logger.debug(EELFLoggerDelegate.debugLogger, methodName + START_LOG);
|
| 139 |
|
| 140 | String url="";
|
| 141 | restObject.set(t);
|
| 142 |
|
| 143 | url = SystemProperties.getProperty(MsoProperties.MSO_SERVER_URL) + path;
|
| 144 |
|
| 145 | MultivaluedHashMap<String, Object> commonHeaders = initMsoClient();
|
| 146 | Logging.logRequest(outgoingRequestsLogger, HttpMethod.GET, url);
|
| 147 | final Response cres = client.target(url)
|
| 148 | .request()
|
| 149 | .accept(APPLICATION_JSON)
|
| 150 | .headers(commonHeaders)
|
| 151 | .get();
|
| 152 | Logging.logResponse(outgoingRequestsLogger, HttpMethod.GET, url, cres);
|
| 153 | int status = cres.getStatus();
|
| 154 | restObject.setStatusCode (status);
|
| 155 |
|
| 156 | if (status == 200 || status == 202) {
|
| 157 | t = (T) cres.readEntity(t.getClass());
|
| 158 | restObject.set(t);
|
| 159 | logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + methodName + REST_API_SUCCESSFULL_LOG);
|
| 160 |
|
| 161 | } else {
|
| 162 | throw new GenericUncheckedException(methodName + WITH_STATUS + status + ", url= " + url );
|
| 163 | }
|
| 164 |
|
| 165 | logger.debug(EELFLoggerDelegate.debugLogger,methodName + " received status=" + status );
|
| 166 |
|
| 167 | return;
|
| 168 | }
|
| 169 |
|
| 170 | public <T> RestObject<T> GetForObject(String sourceID, String path, Class<T> clazz) {
|
| 171 | final String methodName = getMethodName();
|
| 172 | logger.debug(EELFLoggerDelegate.debugLogger, "start {}->{}({}, {}, {})", getMethodCallerName(), methodName, sourceID, path, clazz);
|
| 173 |
|
| 174 | String url = SystemProperties.getProperty(MsoProperties.MSO_SERVER_URL) + path;
|
| 175 | logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " sending request to url= " + url);
|
| 176 |
|
| 177 | MultivaluedHashMap<String, Object> commonHeaders = initMsoClient();
|
| 178 | Logging.logRequest(outgoingRequestsLogger, HttpMethod.GET, url);
|
| 179 | final Response cres = client.target(url)
|
| 180 | .request()
|
| 181 | .accept(APPLICATION_JSON)
|
| 182 | .headers(commonHeaders)
|
| 183 | .get();
|
| 184 | Logging.logResponse(outgoingRequestsLogger, HttpMethod.GET, url, cres);
|
| 185 | final RestObject<T> restObject = cresToRestObject(cres, clazz);
|
| 186 | int status = cres.getStatus();
|
| 187 |
|
| 188 | if (status == 200 || status == 202) {
|
| 189 | logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + methodName + REST_API_SUCCESSFULL_LOG);
|
| 190 | } else {
|
| 191 | logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + WITH_STATUS +status+ URL_LOG +url);
|
| 192 | }
|
| 193 |
|
| 194 | logger.debug(EELFLoggerDelegate.debugLogger,methodName + " received status=" + status );
|
| 195 |
|
| 196 | return restObject;
|
| 197 | }
|
| 198 |
|
| 199 | @Override
|
| 200 | public <T> void Delete(T t, Object r, String sourceID, String path, RestObject<T> restObject) {
|
| 201 |
|
| 202 | String methodName = "Delete";
|
| 203 | String url="";
|
| 204 | Response cres = null;
|
| 205 |
|
| 206 | logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + START_LOG);
|
| 207 |
|
| 208 | try {
|
| 209 | MultivaluedHashMap<String, Object> commonHeaders = initMsoClient();
|
| 210 |
|
| 211 | url = SystemProperties.getProperty(MsoProperties.MSO_SERVER_URL) + path;
|
| 212 | Logging.logRequest(outgoingRequestsLogger, HttpMethod.DELETE, url, r);
|
| 213 | cres = client.target(url)
|
| 214 | .request()
|
| 215 |
|
| 216 | .accept(APPLICATION_JSON)
|
| 217 | .headers(commonHeaders)
|
| 218 | //.entity(r)
|
| 219 | .build("DELETE", Entity.entity(r, MediaType.APPLICATION_JSON)).invoke();
|
| 220 | Logging.logResponse(outgoingRequestsLogger, HttpMethod.DELETE, url, cres);
|
| 221 | int status = cres.getStatus();
|
| 222 | restObject.setStatusCode (status);
|
| 223 |
|
| 224 | if (status == 404) { // resource not found
|
| 225 | String msg = "Resource does not exist...: " + cres.getStatus();
|
| 226 | logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + msg);
|
| 227 | } else if (status == 200 || status == 204){
|
| 228 | logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + "Resource " + url + " deleted");
|
| 229 | } else if (status == 202) {
|
| 230 | String msg = "Delete in progress: " + status;
|
| 231 | logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + msg);
|
| 232 | }
|
| 233 | else {
|
| 234 | String msg = "Deleting Resource failed: " + status;
|
| 235 | logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + msg);
|
| 236 | }
|
| 237 |
|
| 238 | try {
|
| 239 | t = (T) cres.readEntity(t.getClass());
|
| 240 | restObject.set(t);
|
| 241 | }
|
| 242 | catch ( Exception e ) {
|
| 243 | logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + NO_RESPONSE_ENTITY_LOG
|
| 244 | + e.getMessage());
|
| 245 | }
|
| 246 |
|
| 247 | }
|
| 248 | catch (Exception e)
|
| 249 | {
|
| 250 | logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + WITH_URL_LOG +url+ EXCEPTION_LOG + e.toString());
|
| 251 | throw e;
|
| 252 |
|
| 253 | }
|
| 254 | }
|
| 255 |
|
| 256 | public <T> RestObject<T> PostForObject(Object requestDetails, String sourceID, String path, Class<T> clazz) {
|
| 257 | logger.debug(EELFLoggerDelegate.debugLogger, "start {}->{}({}, {}, {}, {})", getMethodCallerName(), getMethodName(), requestDetails, sourceID, path, clazz);
|
| 258 | RestObject<T> restObject = new RestObject<>();
|
| 259 | Post(clazz, requestDetails, path, restObject);
|
| 260 | return restObject;
|
| 261 | }
|
| 262 |
|
| 263 | @Override
|
| 264 | public <T> void Post(T t, Object r, String sourceID, String path, RestObject<T> restObject) {
|
| 265 | logger.debug(EELFLoggerDelegate.debugLogger, "start {}->{}({}, {}, {}, {})", getMethodCallerName(), getMethodName(), t.getClass(), r, sourceID, path);
|
| 266 | Post(t.getClass(), r, path, restObject);
|
| 267 | }
|
| 268 |
|
| 269 | public Invocation.Builder prepareClient(String path, String methodName) {
|
| 270 | MultivaluedHashMap<String, Object> commonHeaders = initMsoClient();
|
| 271 |
|
| 272 | String url = SystemProperties.getProperty(MsoProperties.MSO_SERVER_URL) + path;
|
| 273 | logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + " sending request to url= " + url);
|
| 274 | // Change the content length
|
| 275 | return client.target(url)
|
| 276 | .request()
|
| 277 | .accept(APPLICATION_JSON)
|
| 278 | .headers(commonHeaders);
|
| 279 | }
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 | public <T> void Post(Class<?> tClass, Object requestDetails, String path, RestObject<T> restObject) {
|
| 284 | String methodName = "Post";
|
| 285 | String url="";
|
| 286 |
|
| 287 | try {
|
| 288 |
|
| 289 | MultivaluedHashMap<String, Object> commonHeaders = initMsoClient();
|
| 290 |
|
| 291 | url = SystemProperties.getProperty(MsoProperties.MSO_SERVER_URL) + path;
|
| 292 | Logging.logRequest(outgoingRequestsLogger, HttpMethod.POST, url, requestDetails);
|
| 293 | // Change the content length
|
| 294 | final Response cres = client.target(url)
|
| 295 | .request()
|
| 296 | .accept(APPLICATION_JSON)
|
| 297 | .headers(commonHeaders)
|
| 298 | .post(Entity.entity(requestDetails, MediaType.APPLICATION_JSON));
|
| 299 | Logging.logResponse(outgoingRequestsLogger, HttpMethod.POST, url, cres);
|
| 300 | final RestObject<T> cresToRestObject = cresToRestObject(cres, tClass);
|
| 301 | restObject.set(cresToRestObject.get());
|
| 302 | restObject.setStatusCode(cresToRestObject.getStatusCode());
|
| 303 | restObject.setRaw(cresToRestObject.getRaw());
|
| 304 |
|
| 305 | int status = cres.getStatus();
|
| 306 | restObject.setStatusCode (status);
|
| 307 |
|
| 308 | if ( status >= 200 && status <= 299 ) {
|
| 309 | logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + "<== " + methodName + REST_API_POST_WAS_SUCCESSFUL_LOG);
|
| 310 | logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + REST_API_POST_WAS_SUCCESSFUL_LOG);
|
| 311 |
|
| 312 | } else {
|
| 313 | logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + WITH_STATUS +status+ URL_LOG +url);
|
| 314 | }
|
| 315 |
|
| 316 | } catch (Exception e)
|
| 317 | {
|
| 318 | logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + WITH_URL_LOG +url+ EXCEPTION_LOG + e.toString());
|
| 319 | throw e;
|
| 320 |
|
| 321 | }
|
| 322 |
|
| 323 | logger.debug(EELFLoggerDelegate.debugLogger, "end {}() => ({}){}", getMethodName(), tClass, restObject);
|
| 324 | }
|
| 325 |
|
| 326 | private <T> RestObject<T> cresToRestObject(Response cres, Class<?> tClass) {
|
| 327 | RestObject<T> restObject = new RestObject<>();
|
| 328 |
|
| 329 | String rawEntity = null;
|
| 330 | try {
|
| 331 | cres.bufferEntity();
|
| 332 | rawEntity = cres.readEntity(String.class);
|
| 333 | restObject.setRaw(rawEntity);
|
| 334 | T t = (T) new ObjectMapper().readValue(rawEntity, tClass);
|
| 335 | restObject.set(t);
|
| 336 | }
|
| 337 | catch ( Exception e ) {
|
| 338 | try {
|
| 339 | logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + getMethodCallerName() + " Error reading response entity as " + tClass + ": , e="
|
| 340 | + e.getMessage() + ", Entity=" + rawEntity);
|
| 341 | } catch (Exception e2) {
|
| 342 | logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + getMethodCallerName() + NO_RESPONSE_ENTITY_LOG
|
| 343 | + e.getMessage());
|
| 344 | }
|
| 345 | }
|
| 346 |
|
| 347 | int status = cres.getStatus();
|
| 348 | restObject.setStatusCode (status);
|
| 349 |
|
| 350 | return restObject;
|
| 351 |
|
| 352 | }
|
| 353 |
|
| 354 | @Override
|
| 355 | public <T> void Put(T t, RequestDetailsWrapper r, String sourceID, String path, RestObject<T> restObject) {
|
| 356 |
|
| 357 | String methodName = "Put";
|
| 358 | String url="";
|
| 359 |
|
| 360 | logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + START_LOG);
|
| 361 |
|
| 362 | try {
|
| 363 |
|
| 364 | MultivaluedHashMap<String, Object> commonHeaders = initMsoClient();
|
| 365 |
|
| 366 | url = SystemProperties.getProperty(MsoProperties.MSO_SERVER_URL) + path;
|
| 367 | Logging.logRequest(outgoingRequestsLogger, HttpMethod.PUT, url, r);
|
| 368 | // Change the content length
|
| 369 | final Response cres = client.target(url)
|
| 370 | .request()
|
| 371 | .accept(APPLICATION_JSON)
|
| 372 | .headers(commonHeaders)
|
| 373 | //.header("content-length", 201)
|
| 374 | //.header("X-FromAppId", sourceID)
|
| 375 | .put(Entity.entity(r, MediaType.APPLICATION_JSON));
|
| 376 |
|
| 377 | Logging.logResponse(outgoingRequestsLogger, HttpMethod.PUT, url, cres);
|
| 378 |
|
| 379 | try {
|
| 380 | t = (T) cres.readEntity(t.getClass());
|
| 381 | restObject.set(t);
|
| 382 | }
|
| 383 | catch ( Exception e ) {
|
| 384 | logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + NO_RESPONSE_ENTITY_LOG
|
| 385 | + e.getMessage());
|
| 386 | }
|
| 387 |
|
| 388 | int status = cres.getStatus();
|
| 389 | restObject.setStatusCode (status);
|
| 390 |
|
| 391 | if ( status >= 200 && status <= 299 ) {
|
| 392 | logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + "<== " + methodName + REST_API_POST_WAS_SUCCESSFUL_LOG);
|
| 393 | logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + REST_API_POST_WAS_SUCCESSFUL_LOG);
|
| 394 |
|
| 395 | } else {
|
| 396 | logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + WITH_STATUS +status+ URL_LOG +url);
|
| 397 | }
|
| 398 |
|
| 399 | } catch (Exception e)
|
| 400 | {
|
| 401 | logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + WITH_URL_LOG +url+ EXCEPTION_LOG + e.toString());
|
| 402 | throw e;
|
| 403 |
|
| 404 | }
|
| 405 | }
|
| 406 | }
|