2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2018 Nokia. All rights reserved.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 package org.onap.vid.mso.rest;
23 import com.google.common.collect.ImmutableMap;
24 import io.joshworks.restclient.http.HttpResponse;
25 import java.util.HashMap;
27 import javax.ws.rs.core.HttpHeaders;
28 import javax.ws.rs.core.MediaType;
29 import org.apache.commons.codec.binary.Base64;
30 import org.eclipse.jetty.util.security.Password;
31 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
32 import org.onap.portalsdk.core.util.SystemProperties;
33 import org.onap.vid.changeManagement.MsoRequestDetails;
34 import org.onap.vid.changeManagement.RequestDetailsWrapper;
35 import org.onap.vid.client.SyncRestClient;
36 import org.onap.vid.model.RequestReferencesContainer;
37 import org.onap.vid.mso.*;
39 import java.text.DateFormat;
40 import java.text.SimpleDateFormat;
41 import java.util.Date;
42 import org.onap.vid.utils.Logging;
46 * Created by pickjonathan on 21/06/2017.
48 public class MsoRestClientNew implements MsoInterface {
51 * The Constant dateFormat.
53 public static final String X_FROM_APP_ID = "X-FromAppId";
54 final static DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:SSSS");
55 private static final String START = " start";
56 private final SyncRestClient client;
57 private final String baseUrl;
58 private final Map<String, String> commonHeaders;
62 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MsoRestClientNew.class);
64 public MsoRestClientNew(SyncRestClient client, String baseUrl) {
66 this.baseUrl = baseUrl;
67 this.commonHeaders = initCommonHeaders();
71 public MsoResponseWrapper createSvcInstance(RequestDetails requestDetails, String endpoint) {
72 String methodName = "createSvcInstance ";
73 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + START);
74 String path = baseUrl + endpoint;
76 return createInstance(requestDetails, path);
80 public MsoResponseWrapper createE2eSvcInstance(Object requestDetails, String endpoint) {
81 String methodName = "createE2eSvcInstance ";
82 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + START);
83 String path = baseUrl + endpoint;
85 return createInstance(requestDetails, path);
89 public MsoResponseWrapper createVnf(RequestDetails requestDetails, String endpoint) {
91 String methodName = "createVnf";
92 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + START);
93 String path = baseUrl + endpoint;
95 return createInstance(requestDetails, path);
99 public MsoResponseWrapper createNwInstance(RequestDetails requestDetails, String endpoint) {
101 String methodName = "createNwInstance";
102 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + START);
103 String path = baseUrl + endpoint;
105 return createInstance(requestDetails, path);
109 public MsoResponseWrapper createVolumeGroupInstance(RequestDetails requestDetails, String endpoint) {
110 String methodName = "createVolumeGroupInstance";
111 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + START);
112 String path = baseUrl + endpoint;
114 return createInstance(requestDetails, path);
118 public MsoResponseWrapper createVfModuleInstance(RequestDetails requestDetails, String endpoint) {
119 String methodName = "createVfModuleInstance";
120 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + START);
121 String path = baseUrl + endpoint;
123 return createInstance(requestDetails, path);
127 public MsoResponseWrapper createConfigurationInstance(org.onap.vid.mso.rest.RequestDetailsWrapper requestDetailsWrapper, String endpoint) {
128 String methodName = "createConfigurationInstance";
129 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + START);
130 String path = baseUrl + endpoint;
132 return createInstance(requestDetailsWrapper, path);
136 public MsoResponseWrapper deleteE2eSvcInstance(Object requestDetails, String endpoint) {
137 String methodName = "deleteE2eSvcInstance";
138 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + START);
139 String path = baseUrl + endpoint;
140 return deleteInstance(requestDetails, path);
144 public MsoResponseWrapper deleteSvcInstance(RequestDetails requestDetails, String endpoint) {
145 String methodName = "deleteSvcInstance";
146 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + START);
147 String path = baseUrl + endpoint;
148 return deleteInstance(requestDetails, path);
152 public MsoResponseWrapper unassignSvcInstance(RequestDetails requestDetails, String endpoint) {
153 String methodName = "unassignSvcInstance";
154 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + START);
155 HttpResponse<String> response = client.post(endpoint, commonHeaders, requestDetails, String.class);
156 return MsoUtil.wrapResponse(response);
160 public MsoResponseWrapper deleteVnf(RequestDetails requestDetails, String endpoint) {
161 String methodName = "deleteVnf";
162 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + START);
163 String path = baseUrl + endpoint;
165 return deleteInstance(requestDetails, path);
169 public MsoResponseWrapper deleteVfModule(RequestDetails requestDetails, String endpoint) {
170 String methodName = "deleteVfModule";
171 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + START);
172 String path = baseUrl + endpoint;
174 return deleteInstance(requestDetails, path);
178 public MsoResponseWrapper deleteVolumeGroupInstance(RequestDetails requestDetails, String endpoint) {
179 String methodName = "deleteVolumeGroupInstance";
180 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + START);
181 String path = baseUrl + endpoint;
183 return deleteInstance(requestDetails, path);
187 public MsoResponseWrapper deleteNwInstance(RequestDetails requestDetails, String endpoint) {
188 String methodName = "deleteNwInstance";
189 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + START);
190 String path = baseUrl + endpoint;
192 return deleteInstance(requestDetails, path);
196 public MsoResponseWrapper getOrchestrationRequest(String endpoint) {
197 String path = baseUrl + endpoint;
199 HttpResponse<String> response = client.get(path, commonHeaders, new HashMap<>(), String.class);
200 return MsoUtil.wrapResponse(response);
203 public MsoResponseWrapper getManualTasks(String endpoint) {
204 String path = baseUrl + endpoint;
206 HttpResponse<String> response = client.get(path, commonHeaders, new HashMap<>(), String.class);
207 return MsoUtil.wrapResponse(response);
210 public MsoResponseWrapper getOrchestrationRequestsForDashboard(String t, String sourceId, String path, RestObject restObject) {
211 String methodName = "getOrchestrationRequestsForDashboard";
212 logger.debug(dateFormat.format(new Date()) + "<== " + methodName + START);
215 MsoResponseWrapper w = getOrchestrationRequest(path);
216 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " w=" + w.getResponse());
220 } catch (Exception e) {
221 logger.error(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + "<== " + "." + methodName + e.toString());
222 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + "." + methodName + e.toString());
227 public MsoResponseWrapper getManualTasksByRequestId(String t, String sourceId, String endpoint, RestObject restObject) {
228 String methodName = "getManualTasksByRequestId";
229 logger.debug(dateFormat.format(new Date()) + "<== " + methodName + START);
232 String path = baseUrl + endpoint;
234 MsoResponseWrapper w =getManualTasks(path);
235 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " w=" + w.getResponse());
239 } catch (Exception e) {
240 logger.error(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + "<== " + "." + methodName + e.toString());
241 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + "." + methodName + e.toString());
247 public MsoResponseWrapper completeManualTask(RequestDetails requestDetails, String t, String sourceId, String endpoint, RestObject restObject) {
248 String methodName = "completeManualTask";
249 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " calling Complete ");
251 String path = baseUrl + endpoint;
253 HttpResponse<String> response = client.post(path, commonHeaders, requestDetails, String.class);
254 MsoResponseWrapper w = MsoUtil.wrapResponse(response);
256 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " w=" + w.getResponse());
259 } catch (Exception e) {
260 logger.error(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + "<== " + "." + methodName + e.toString());
261 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + "." + methodName + e.toString());
267 public MsoResponseWrapper replaceVnf(org.onap.vid.changeManagement.RequestDetails requestDetails, String endpoint) {
268 String methodName = "replaceVnf";
269 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + START);
270 String path = baseUrl + endpoint;
271 return replaceInstance(requestDetails, path);
275 public MsoResponseWrapper deleteConfiguration(org.onap.vid.mso.rest.RequestDetailsWrapper requestDetailsWrapper, String pmc_endpoint) {
276 String methodName = "deleteConfiguration";
277 logger.debug(EELFLoggerDelegate.debugLogger,
278 dateFormat.format(new Date()) + "<== " + methodName + START);
279 String path = baseUrl + pmc_endpoint;
281 return deleteInstance(requestDetailsWrapper, path);
285 public MsoResponseWrapper setConfigurationActiveStatus(RequestDetails request, String endpoint) {
286 String methodName = "setConfigurationActiveStatus";
287 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + START);
290 String path = baseUrl + endpoint;
292 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== "
293 + methodName + " calling change configuration active status, path =[" + path + "]");
294 HttpResponse<String> response = client.post(path, commonHeaders, request, String.class);
295 return MsoUtil.wrapResponse(response);
296 } catch (Exception e) {
297 logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + "<== " + "." + methodName + e.toString());
298 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + "." + methodName + e.toString());
304 public MsoResponseWrapper setPortOnConfigurationStatus(RequestDetails request, String endpoint) {
305 String methodName = "setPortOnConfigurationStatus";
306 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + START);
309 String path = baseUrl + endpoint;
310 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== "
311 + methodName + " calling change port configuration status, path =[" + path + "]");
312 HttpResponse<String> response = client.post(path, commonHeaders, request, String.class);
313 return MsoUtil.wrapResponse(response);
314 } catch (Exception e) {
315 logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + "<== " + "." + methodName + e.toString());
316 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + "." + methodName + e.toString());
322 public MsoResponseWrapperInterface changeManagementUpdate(RequestDetailsWrapper requestDetails, String endpoint) {
323 String path = baseUrl + endpoint;
324 HttpResponse<RequestReferencesContainer> response = client.post(path, commonHeaders, requestDetails, RequestReferencesContainer.class);
325 return MsoUtil.wrapResponse(response);
328 public MsoResponseWrapper replaceInstance(org.onap.vid.changeManagement.RequestDetails request, String path) {
329 String methodName = "replaceInstance";
330 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + START);
333 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " calling Replace VNF, path =[" + path + "]");
335 HttpResponse<String> response = client.post(path, commonHeaders, request, String.class);
336 MsoResponseWrapper msoResponseWrapperObject = MsoUtil.wrapResponse(response);
337 int status = msoResponseWrapperObject.getStatus();
339 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName +
340 ",post succeeded, msoResponseWrapperObject response:" + msoResponseWrapperObject.getResponse());
342 logger.error(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName +
343 ": post failed, msoResponseWrapperObject status" + status + ", response:" + msoResponseWrapperObject.getResponse());
347 return msoResponseWrapperObject;
349 } catch (Exception e) {
350 logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + "<== " + "." + methodName + e.toString());
351 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + "." + methodName + e.toString());
358 public MsoResponseWrapper updateVnf(org.onap.vid.changeManagement.RequestDetails requestDetails, String endpoint) {
359 String methodName = "updateVnf";
360 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + START);
361 String path = baseUrl + endpoint;
363 RequestDetailsWrapper wrapper = new RequestDetailsWrapper();
364 wrapper.requestDetails = new MsoRequestDetails(requestDetails);
365 return updateInstance(requestDetails, path);
368 public MsoResponseWrapper updateInstance(org.onap.vid.changeManagement.RequestDetails request, String path) {
369 String methodName = "updateInstance";
370 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + START);
373 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " calling Delete, path =[" + path + "]");
375 HttpResponse<String> response = client.post(path, commonHeaders, request, String.class);
376 MsoResponseWrapper w = MsoUtil.wrapResponse(response);
378 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " w=" + w.getResponse());
381 } catch (Exception e) {
382 logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + "<== " + "." + methodName + e.toString());
383 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + "." + methodName + e.toString());
389 public void setServiceInstanceStatus(RequestDetails requestDetails, String t, String sourceId, String endpoint, RestObject<String> restObject) {
390 String methodName = "activateServiceInstance";
391 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " start ");
393 String path = baseUrl + endpoint;
394 HttpResponse<String> response = client.post(path, commonHeaders, requestDetails, String.class);
395 MsoResponseWrapper w = MsoUtil.wrapResponse(response);
396 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " w =" + w.getResponse());
398 } catch (Exception e) {
399 logger.error(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + "<== " + "." + methodName + e.toString());
400 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + "." + methodName + e.toString());
406 public MsoResponseWrapper removeRelationshipFromServiceInstance(RequestDetails requestDetails, String endpoint) {
407 String methodName = "removeRelationshipFromServiceInstance";
408 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + START);
411 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " calling Remove relationship from service instance, path =[" + endpoint + "]");
412 String path = baseUrl + endpoint;
413 HttpResponse<String> response = client.post(path, commonHeaders, requestDetails, String.class);
414 return MsoUtil.wrapResponse(response);
415 } catch (Exception e) {
416 logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + "<== " + "." + methodName + e.toString());
417 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + "." + methodName + e.toString());
423 public MsoResponseWrapper addRelationshipToServiceInstance(RequestDetails requestDetails, String addRelationshipsPath) {
424 String methodName = "addRelationshipToServiceInstance";
425 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + START);
428 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " calling Add relationship to service instance, path =[" + addRelationshipsPath + "]");
429 String path = baseUrl + addRelationshipsPath;
431 HttpResponse<String> response = client.post(path, commonHeaders, requestDetails, String.class);
432 return MsoUtil.wrapResponse(response);
433 } catch (Exception e) {
434 logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + "<== " + "." + methodName + e.toString());
435 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + "." + methodName + e.toString());
441 public <T> HttpResponse<T> get(String path, Class<T> responseClass) {
442 return client.get(path, commonHeaders, new HashMap<>(), responseClass);
446 public <T> HttpResponse<T> post(String path, RequestDetailsWrapper<?> requestDetailsWrapper,
447 Class<T> responseClass) {
448 return client.post(path, commonHeaders, requestDetailsWrapper, responseClass);
452 private MsoResponseWrapper createInstance(Object request, String endpoint) {
453 String methodName = "createInstance";
454 logger.debug(dateFormat.format(new Date()) + "<== " + methodName + START);
457 HttpResponse<String> response = client.post(endpoint, commonHeaders, request, String.class);
458 return MsoUtil.wrapResponse(response);
459 } catch (Exception e) {
460 logger.error(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + "<== " + "." + methodName + e.toString());
461 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + "." + methodName + e.toString());
469 * @param request the request
470 * @param path the path
471 * @return the mso response wrapper
472 * @throws Exception the exception
474 private MsoResponseWrapper deleteInstance(Object request, String path) {
475 String methodName = "deleteInstance";
476 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + START);
479 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " calling Delete, path =[" + path + "]");
481 HttpResponse<String> response = client.delete(path, commonHeaders, String.class);
482 MsoResponseWrapper w = MsoUtil.wrapResponse(response);
484 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " w=" + w.getResponse());
487 } catch (Exception e) {
488 logger.error(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + "<== " + "." + methodName + e.toString());
489 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + "." + methodName + e.toString());
495 private Map<String, String> initCommonHeaders() {
496 String username = SystemProperties.getProperty(MsoProperties.MSO_USER_NAME);
497 String password = SystemProperties.getProperty(MsoProperties.MSO_PASSWORD);
498 String decrypted_password = Password.deobfuscate(password);
500 String authString = username + ":" + decrypted_password;
502 byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
503 String authStringEnc = new String(authEncBytes);
505 Map<String, String> map = new HashMap<>();
506 map.put(HttpHeaders.AUTHORIZATION, "Basic " + authStringEnc);
507 map.put(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON);
508 map.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
509 map.put(X_FROM_APP_ID, SystemProperties.getProperty(SystemProperties.APP_DISPLAY_NAME));
510 map.put(SystemProperties.ECOMP_REQUEST_ID, Logging.extractOrGenerateRequestId());
511 return ImmutableMap.copyOf(map);