org.onap migration
[vid.git] / vid-app-common / src / main / java / org / onap / vid / mso / MsoBusinessLogicImpl.java
1 package org.onap.vid.mso;
2
3 import com.fasterxml.jackson.core.JsonProcessingException;
4 import com.fasterxml.jackson.databind.DeserializationFeature;
5 import com.fasterxml.jackson.databind.ObjectMapper;
6 import com.fasterxml.jackson.databind.SerializationFeature;
7 import com.google.common.collect.ImmutableList;
8 import com.google.common.collect.ImmutableMap;
9 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
10 import org.openecomp.portalsdk.core.util.SystemProperties;
11 import org.onap.vid.changeManagement.ChangeManagementRequest;
12 import org.onap.vid.changeManagement.RequestDetailsWrapper;
13 import org.onap.vid.controller.OperationalEnvironmentController;
14 import org.onap.vid.domain.mso.RequestInfo;
15 import org.onap.vid.mso.model.OperationalEnvironmentActivateInfo;
16 import org.onap.vid.mso.model.OperationalEnvironmentDeactivateInfo;
17 import org.onap.vid.mso.rest.OperationalEnvironment.OperationEnvironmentRequestDetails;
18 import org.onap.vid.mso.rest.*;
19 import org.springframework.beans.factory.annotation.Autowired;
20
21 import javax.ws.rs.BadRequestException;
22 import java.text.DateFormat;
23 import java.text.SimpleDateFormat;
24 import java.util.*;
25 import java.util.regex.Pattern;
26
27 import static org.onap.vid.changeManagement.ChangeManagementRequest.MsoChangeManagementRequest;
28 import static org.onap.vid.controller.MsoController.*;
29 import static org.onap.vid.mso.MsoProperties.*;
30
31 public class MsoBusinessLogicImpl implements MsoBusinessLogic {
32
33     /**
34      * The Constant dateFormat.
35      */
36     final static DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:SSSS");
37     final static Pattern SOFTWARE_VERSION_PATTERN = Pattern.compile("^[A-Za-z0-9.\\-]+$");
38     final static Pattern NUMBER_PATTERN = Pattern.compile("^[0-9]+$");
39     private static final String ACTIVATE = "/activate";
40     private static final String DEACTIVATE = "/deactivate";
41     private static final String ENABLE_PORT = "/enablePort";
42     private static final String DISABLE_PORT = "/disablePort";
43     private final static String RESOURCE_TYPE_OPERATIONAL_ENVIRONMENT = "operationalEnvironment";
44     private final static String SOURCE_OPERATIONAL_ENVIRONMENT = "VID";
45     final static private ObjectMapper objectMapper = new ObjectMapper();
46     /**
47      * The Mso REST client
48      * This should be replaced with mso client factory.
49      */
50     private final MsoInterface msoClientInterface;
51     /**
52      * The logger.
53      */
54     private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MsoBusinessLogicImpl.class);
55
56     @Autowired
57     public MsoBusinessLogicImpl(MsoInterface msoClientInterface) {
58         this.msoClientInterface = msoClientInterface;
59     }
60
61     static String validateEndpointPath(String endpointEnvVariable) {
62         String endpoint = SystemProperties.getProperty(endpointEnvVariable);
63         if (endpoint == null || endpoint.isEmpty()) {
64             throw new RuntimeException(endpointEnvVariable + " env variable is not defined");
65         }
66         return endpoint;
67     }
68
69     // this function should get params from tosca and send them to instance at mso, then return success response.
70     @Override
71     public MsoResponseWrapper createSvcInstance(RequestDetails msoRequest) throws Exception {
72         String methodName = "createSvcInstance ";
73         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " start");
74
75         String endpoint;
76         try {
77             endpoint = validateEndpointPath(MsoProperties.MSO_REST_API_SVC_INSTANCE);
78         } catch (Exception exception) {
79             throw exception;
80         }
81
82         return msoClientInterface.createSvcInstance(msoRequest, endpoint);
83     }
84
85     void validateLineOfBusiness(RequestDetails requestDetails) {
86
87         Object value = requestDetails.getAdditionalProperties();
88
89         for(String prop: ImmutableList.of("requestDetails", "lineOfBusiness", "lineOfBusinessName")) {
90             if(value==null ||!(value instanceof Map)) {
91                 value = null;
92                 break;
93             }
94             else {
95                 value = ((Map)value).get(prop);
96             }
97         }
98
99         if(value == null || value.toString().isEmpty()) {
100             throw new BadRequestException("lineOfBusiness is required");
101         }
102
103     }
104
105     @Override
106     public MsoResponseWrapper createVnf(RequestDetails requestDetails, String serviceInstanceId) throws Exception {
107         String methodName = "createVnf";
108         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " start");
109
110         String endpoint;
111         try {
112             endpoint = validateEndpointPath(MsoProperties.MSO_REST_API_VNF_INSTANCE);
113         } catch (Exception exception) {
114             throw exception;
115         }
116
117         String vnf_endpoint = endpoint.replaceFirst(SVC_INSTANCE_ID, serviceInstanceId);
118         return msoClientInterface.createVnf(requestDetails, vnf_endpoint);
119     }
120
121     @Override
122     public MsoResponseWrapper createNwInstance(RequestDetails requestDetails, String serviceInstanceId) throws Exception {
123         String methodName = "createNwInstance";
124         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " start");
125
126         String endpoint;
127         try {
128             endpoint = validateEndpointPath(MsoProperties.MSO_REST_API_NETWORK_INSTANCE);
129         } catch (Exception exception) {
130             throw exception;
131         }
132
133         String nw_endpoint = endpoint.replaceFirst(SVC_INSTANCE_ID, serviceInstanceId);
134         return msoClientInterface.createNwInstance(requestDetails, nw_endpoint);
135     }
136
137     @Override
138     public MsoResponseWrapper createVolumeGroupInstance(RequestDetails requestDetails, String serviceInstanceId, String vnfInstanceId) throws Exception {
139         String methodName = "createVolumeGroupInstance";
140         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " start");
141
142         String endpoint;
143         try {
144             endpoint = validateEndpointPath(MsoProperties.MSO_REST_API_VOLUME_GROUP_INSTANCE);
145         } catch (Exception exception) {
146             throw exception;
147         }
148
149         String vnf_endpoint = endpoint.replaceFirst(SVC_INSTANCE_ID, serviceInstanceId);
150         vnf_endpoint = vnf_endpoint.replaceFirst(VNF_INSTANCE_ID, vnfInstanceId);
151
152         return msoClientInterface.createVolumeGroupInstance(requestDetails, vnf_endpoint);
153     }
154
155     @Override
156     public MsoResponseWrapper createVfModuleInstance(RequestDetails requestDetails, String serviceInstanceId, String vnfInstanceId) throws Exception {
157         String methodName = "createVfModuleInstance";
158         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " start");
159
160         String endpoint;
161         try {
162             endpoint = validateEndpointPath(MsoProperties.MSO_REST_API_VF_MODULE_INSTANCE);
163         } catch (Exception exception) {
164             throw exception;
165         }
166
167         String partial_endpoint = endpoint.replaceFirst(SVC_INSTANCE_ID, serviceInstanceId);
168         String vf_module_endpoint = partial_endpoint.replaceFirst(VNF_INSTANCE_ID, vnfInstanceId);
169
170         return msoClientInterface.createVfModuleInstance(requestDetails, vf_module_endpoint);
171     }
172
173     @Override
174     public MsoResponseWrapper createConfigurationInstance(RequestDetails requestDetails, String serviceInstanceId) throws Exception {
175         String methodName = "createConfigurationInstance";
176         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " start");
177
178         String endpoint = validateEndpointPath(MsoProperties.MSO_REST_API_CONFIGURATIONS);
179         endpoint = endpoint.replace(SVC_INSTANCE_ID, serviceInstanceId);
180
181         return msoClientInterface.createConfigurationInstance(requestDetails, endpoint);
182     }
183
184     @Override
185     public MsoResponseWrapper deleteSvcInstance(RequestDetails requestDetails, String serviceInstanceId) throws Exception {
186         String methodName = "deleteSvcInstance";
187         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " start");
188
189         String endpoint;
190         try {
191             endpoint = validateEndpointPath(MsoProperties.MSO_REST_API_SVC_INSTANCE);
192         } catch (Exception exception) {
193             throw exception;
194         }
195
196         String svc_endpoint = endpoint + "/" + serviceInstanceId;
197
198         return msoClientInterface.deleteSvcInstance(requestDetails, svc_endpoint);
199     }
200
201     @Override
202     public MsoResponseWrapper deleteVnf(RequestDetails requestDetails, String serviceInstanceId, String vnfInstanceId) throws Exception {
203         String methodName = "deleteVnf";
204         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " start");
205
206         String endpoint;
207         try {
208             endpoint = validateEndpointPath(MsoProperties.MSO_REST_API_VNF_INSTANCE);
209         } catch (Exception exception) {
210             throw exception;
211         }
212         String vnf_endpoint = endpoint.replaceFirst(SVC_INSTANCE_ID, serviceInstanceId);
213         vnf_endpoint = vnf_endpoint + '/' + vnfInstanceId;
214
215         return msoClientInterface.deleteVnf(requestDetails, vnf_endpoint);
216     }
217
218     @Override
219     public MsoResponseWrapper deleteVfModule(RequestDetails requestDetails, String serviceInstanceId, String vnfInstanceId, String vfModuleId) throws Exception {
220         String methodName = "deleteVfModule";
221         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " start");
222
223         String endpoint;
224         try {
225             endpoint = validateEndpointPath(MsoProperties.MSO_REST_API_VF_MODULE_INSTANCE);
226         } catch (Exception exception) {
227             throw exception;
228         }
229
230         String vf__modules_endpoint = endpoint.replaceFirst(SVC_INSTANCE_ID, serviceInstanceId).replaceFirst(VNF_INSTANCE_ID, vnfInstanceId);
231
232         String delete_vf_endpoint = vf__modules_endpoint + '/' + vfModuleId;
233
234         return msoClientInterface.deleteVfModule(requestDetails, delete_vf_endpoint);
235     }
236
237     @Override
238     public MsoResponseWrapper deleteVolumeGroupInstance(RequestDetails requestDetails, String serviceInstanceId, String vnfInstanceId, String volumeGroupId) throws Exception {
239         String methodName = "deleteVolumeGroupInstance";
240         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " start");
241
242         String endpoint;
243         try {
244             endpoint = validateEndpointPath(MsoProperties.MSO_REST_API_VOLUME_GROUP_INSTANCE);
245         } catch (Exception exception) {
246             throw exception;
247         }
248
249         String svc_endpoint = endpoint.replaceFirst(SVC_INSTANCE_ID, serviceInstanceId);
250         String vnf_endpoint = svc_endpoint.replaceFirst(VNF_INSTANCE_ID, vnfInstanceId);
251         String delete_volume_group_endpoint = vnf_endpoint + "/" + volumeGroupId;
252
253         return msoClientInterface.deleteVolumeGroupInstance(requestDetails, delete_volume_group_endpoint);
254     }
255
256     @Override
257     public MsoResponseWrapper deleteNwInstance(RequestDetails requestDetails, String serviceInstanceId, String networkInstanceId) throws Exception {
258         String methodName = "deleteNwInstance";
259         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " start");
260
261         String endpoint;
262         try {
263             endpoint = validateEndpointPath(MsoProperties.MSO_REST_API_NETWORK_INSTANCE);
264         } catch (Exception exception) {
265             throw exception;
266         }
267
268         String svc_endpoint = endpoint.replaceFirst(SVC_INSTANCE_ID, serviceInstanceId);
269         String delete_nw_endpoint = svc_endpoint + "/" + networkInstanceId;
270
271         return msoClientInterface.deleteNwInstance(requestDetails, delete_nw_endpoint);
272     }
273
274     @Override
275     public MsoResponseWrapper getOrchestrationRequest(String requestId) throws Exception {
276         String methodName = "getOrchestrationRequest";
277         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " start");
278         MsoResponseWrapper w = null;
279         try {
280             String p = SystemProperties.getProperty(MsoProperties.MSO_REST_API_GET_ORC_REQ);
281             String path = p + "/" + requestId;
282
283             RestObject<String> restObjStr = new RestObject<String>();
284             String str = new String();
285             restObjStr.set(str);
286
287             msoClientInterface.getOrchestrationRequest(str, "", path, restObjStr);
288
289             return MsoUtil.wrapResponse(restObjStr);
290
291         } catch (Exception e) {
292             logger.error(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + "<== " + "." + methodName + e.toString());
293             logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + "." + methodName + e.toString());
294             throw e;
295         }
296     }
297
298     @Override
299     public MsoResponseWrapper getOrchestrationRequests(String filterString) throws Exception {
300         String methodName = "getOrchestrationRequest";
301         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " start");
302         MsoResponseWrapper w = null;
303         try {
304             String p = SystemProperties.getProperty(MsoProperties.MSO_REST_API_GET_ORC_REQS);
305             String path = p + filterString;
306
307             RestObject<String> restObjStr = new RestObject<String>();
308             String str = new String();
309             restObjStr.set(str);
310
311             msoClientInterface.getOrchestrationRequest(str, "", path, restObjStr);
312
313             return MsoUtil.wrapResponse(restObjStr);
314
315         } catch (Exception e) {
316             logger.error(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + "<== " + "." + methodName + e.toString());
317             logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + "." + methodName + e.toString());
318             throw e;
319         }
320     }
321
322     @Override
323     public List<Request> getOrchestrationRequestsForDashboard() throws Exception {
324         String methodName = "getOrchestrationRequestsForDashboard";
325         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " start");
326         List<Request> filteredOrchestrationRequests = new ArrayList<>();
327         try {
328             String path = SystemProperties.getProperty(MsoProperties.MSO_REST_API_GET_ORC_REQS);
329             path += "filter=modelType:EQUALS:vnf";
330             RestObject<String> restObjStr = new RestObject<String>();
331             String str = new String();
332             restObjStr.set(str);
333
334             MsoResponseWrapper msoResponseWrapper = msoClientInterface.getOrchestrationRequestsForDashboard(str, "", path, restObjStr);
335             List<RequestWrapper> allOrchestrationRequests = deserializeOrchestrationRequestsJson(msoResponseWrapper.getEntity());
336
337             ;
338             for (RequestWrapper currentRequest : allOrchestrationRequests) {
339                 if ((currentRequest.getRequest() != null) && (currentRequest.getRequest().getRequestScope() == Request.RequestScope.VNF) && ((currentRequest.getRequest().getRequestType() ==
340                         Request.RequestType.REPLACE_INSTANCE) || (currentRequest.getRequest().getRequestType() ==
341                         Request.RequestType.UPDATE_INSTANCE))) {
342                     filteredOrchestrationRequests.add(currentRequest.getRequest());
343                 }
344             }
345         } catch (Exception e) {
346             logger.error(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + "<== " + "." + methodName + e.toString());
347             logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + "." + methodName + e.toString());
348         }
349         return filteredOrchestrationRequests;
350
351     }
352
353     private List<RequestWrapper> deserializeOrchestrationRequestsJson(String orchestrationRequestsJson) throws Exception {
354         String methodName = "deserializeOrchestrationRequestsJson";
355         logger.debug(dateFormat.format(new Date()) + "<== " + methodName + " start");
356
357         ObjectMapper mapper = new ObjectMapper();
358         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
359         mapper.configure(DeserializationFeature.READ_ENUMS_USING_TO_STRING, true);
360         RequestList requestList = mapper.readValue(orchestrationRequestsJson, RequestList.class);
361         return requestList.getRequestList();
362     }
363
364
365     @Override
366     public List<Task> getManualTasksByRequestId(String originalRequestId) throws Exception {
367         String methodName = "getManualTasksByRequestId";
368         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " start");
369
370         try {
371             String p = SystemProperties.getProperty(MsoProperties.MSO_REST_API_GET_MAN_TASKS);
372             String path = p + "?originalRequestId=" + originalRequestId;
373
374             RestObject<String> restObjStr = new RestObject<String>();
375             String str = new String();
376             restObjStr.set(str);
377
378             MsoResponseWrapper msoResponseWrapper = msoClientInterface.getManualTasksByRequestId(str, "", path, restObjStr);
379             return deserializeManualTasksJson(msoResponseWrapper.getEntity());
380
381         } catch (Exception e) {
382             logger.error(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + "<== " + "." + methodName + e.toString());
383             logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + "." + methodName + e.toString());
384             throw e;
385         }
386     }
387
388     private List<Task> deserializeManualTasksJson(String manualTasksJson) throws Exception {
389         String methodName = "deserializeManualTasksJson";
390         logger.debug(dateFormat.format(new Date()) + "<== " + methodName + " start");
391
392         ObjectMapper mapper = new ObjectMapper();
393         TaskList taskList = mapper.readValue(manualTasksJson, TaskList.class);
394         return taskList.getTaskList();
395     }
396
397
398     @Override
399     public MsoResponseWrapper completeManualTask(RequestDetails requestDetails, String taskId) throws Exception {
400         String methodName = "completeManualTask";
401         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " start");
402         MsoResponseWrapper w = null;
403         try {
404             String p = SystemProperties.getProperty(MsoProperties.MSO_REST_API_GET_MAN_TASKS);
405             String path = p + "/" + taskId + "/complete";
406
407             RestObject<String> restObjStr = new RestObject<String>();
408             String str = new String();
409             restObjStr.set(str);
410
411             msoClientInterface.completeManualTask(requestDetails, str, "", path, restObjStr);
412
413             return MsoUtil.wrapResponse(restObjStr);
414
415         } catch (Exception e) {
416             logger.error(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + "<== " + "." + methodName + e.toString());
417             logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + "." + methodName + e.toString());
418             throw e;
419         }
420     }
421
422     @Override
423     public MsoResponseWrapper activateServiceInstance(RequestDetails requestDetails, String serviceInstanceId) throws Exception {
424         String methodName = "activateServiceInstance";
425         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " start");
426         try {
427             String serviceEndpoint = SystemProperties.getProperty(MsoProperties.MSO_REST_API_SVC_INSTANCE);
428             String activateServicePath = serviceEndpoint + "/" + serviceInstanceId + "/activate";
429
430             RestObject<String> restObjStr = new RestObject<>();
431             String str = "";
432             restObjStr.set(str);
433
434             msoClientInterface.setServiceInstanceStatus(requestDetails, str, "", activateServicePath, restObjStr);
435
436             return MsoUtil.wrapResponse(restObjStr);
437
438         } catch (Exception e) {
439             logger.error(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + "<== " + "." + methodName + e.toString());
440             logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + "." + methodName + e.toString());
441             throw e;
442         }
443     }
444
445
446     @Override
447     public MsoResponseWrapperInterface updateVnf(org.onap.vid.changeManagement.RequestDetails requestDetails, String serviceInstanceId, String vnfInstanceId) throws Exception {
448         String methodName = "updateVnf";
449         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " start");
450
451         String endpoint;
452         try {
453             endpoint = validateEndpointPath(MsoProperties.MSO_REST_API_VNF_INSTANCE);
454         } catch (Exception exception) {
455             throw exception;
456         }
457         String vnf_endpoint = endpoint.replaceFirst(SVC_INSTANCE_ID, serviceInstanceId);
458         vnf_endpoint = vnf_endpoint + '/' + vnfInstanceId;
459         return msoClientInterface.updateVnf(requestDetails, vnf_endpoint);
460     }
461
462     @Override
463     public MsoResponseWrapperInterface replaceVnf(org.onap.vid.changeManagement.RequestDetails requestDetails, String serviceInstanceId, String vnfInstanceId) throws Exception {
464         String methodName = "replaceVnf";
465         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " start");
466
467         String endpoint;
468         try {
469             endpoint = validateEndpointPath(MsoProperties.MSO_REST_API_VNF_CHANGE_MANAGEMENT_INSTANCE);
470         } catch (Exception exception) {
471             throw exception;
472         }
473         String vnf_endpoint = endpoint.replaceFirst(SVC_INSTANCE_ID, serviceInstanceId);
474         vnf_endpoint = vnf_endpoint.replace(VNF_INSTANCE_ID, vnfInstanceId);
475         vnf_endpoint = vnf_endpoint.replace(REQUEST_TYPE, MsoChangeManagementRequest.REPLACE);
476         return msoClientInterface.replaceVnf(requestDetails, vnf_endpoint);
477     }
478
479     public RequestDetailsWrapper generateInPlaceMsoRequest(org.onap.vid.changeManagement.RequestDetails requestDetails) throws Exception {
480         validateUpdateVnfSoftwarePayload(requestDetails);
481         RequestDetails inPlaceSoftwareUpdateRequest = new RequestDetails();
482         inPlaceSoftwareUpdateRequest.setCloudConfiguration(requestDetails.getCloudConfiguration());
483         inPlaceSoftwareUpdateRequest.setRequestParameters(requestDetails.getRequestParameters());
484         inPlaceSoftwareUpdateRequest.setRequestInfo(requestDetails.getRequestInfo());
485         RequestDetailsWrapper requestDetailsWrapper = new RequestDetailsWrapper();
486         requestDetailsWrapper.requestDetails = inPlaceSoftwareUpdateRequest;
487         return requestDetailsWrapper;
488     }
489
490     @Override
491     public RequestDetailsWrapper generateConfigMsoRequest(org.onap.vid.changeManagement.RequestDetails requestDetails) throws Exception {
492         validateUpdateVnfConfig(requestDetails);
493         RequestDetails ConfigUpdateRequest = new RequestDetails();
494         ConfigUpdateRequest.setRequestParameters(requestDetails.getRequestParameters());
495         ConfigUpdateRequest.setRequestInfo(requestDetails.getRequestInfo());
496         RequestDetailsWrapper requestDetailsWrapper = new RequestDetailsWrapper();
497         requestDetailsWrapper.requestDetails = ConfigUpdateRequest;
498         return requestDetailsWrapper;
499     }
500
501     @Override
502     public MsoResponseWrapperInterface updateVnfSoftware(org.onap.vid.changeManagement.RequestDetails requestDetails, String serviceInstanceId, String vnfInstanceId) throws Exception {
503         String methodName = "updateVnfSoftware";
504         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " start");
505         String vnf_endpoint = getChangeManagementEndpoint(serviceInstanceId, vnfInstanceId, MsoChangeManagementRequest.SOFTWARE_UPDATE); //workflow name in mso is different than workflow name in vid UI
506         RequestDetailsWrapper finalRequestDetails = generateInPlaceMsoRequest(requestDetails);
507         return msoClientInterface.changeManagementUpdate(finalRequestDetails, vnf_endpoint);
508     }
509
510     @Override
511     public MsoResponseWrapperInterface updateVnfConfig(org.onap.vid.changeManagement.RequestDetails requestDetails, String serviceInstanceId, String vnfInstanceId) throws Exception {
512         String methodName = "updateVnfConfig";
513         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " start");
514         RequestDetailsWrapper finalRequestDetails = generateConfigMsoRequest(requestDetails);
515         String vnf_endpoint = getChangeManagementEndpoint(serviceInstanceId, vnfInstanceId, MsoChangeManagementRequest.CONFIG_UPDATE);
516         return msoClientInterface.changeManagementUpdate(finalRequestDetails, vnf_endpoint);
517     }
518
519     private String getChangeManagementEndpoint(String serviceInstanceId, String vnfInstanceId, String vnfRequestType) {
520         String endpoint  = validateEndpointPath(MsoProperties.MSO_REST_API_VNF_CHANGE_MANAGEMENT_INSTANCE);
521         String vnf_endpoint = endpoint.replaceFirst(SVC_INSTANCE_ID, serviceInstanceId);
522         vnf_endpoint = vnf_endpoint.replace(VNF_INSTANCE_ID, vnfInstanceId);
523         vnf_endpoint = vnf_endpoint.replace(REQUEST_TYPE, vnfRequestType);
524         return vnf_endpoint;
525     }
526
527     private Map getChangeManagementPayload(RequestDetails requestDetails, String message) throws Exception{
528         if(requestDetails.getRequestParameters()==null||requestDetails.getRequestParameters().getAdditionalProperties()==null){
529             throw new BadRequestException(message);
530         }
531         Object payloadRaw=requestDetails.getRequestParameters().getAdditionalProperties().get("payload");
532         try{
533             return objectMapper.readValue((String)payloadRaw,Map.class);
534         }
535         catch(Exception exception){
536             throw new BadRequestException(message);
537         }
538     }
539
540     private void validateUpdateVnfSoftwarePayload(RequestDetails requestDetails) throws Exception {
541         final String noValidPayloadMsg = "No valid payload in " + ChangeManagementRequest.VNF_IN_PLACE_SOFTWARE_UPDATE + " request";
542
543         Map payload = getChangeManagementPayload(requestDetails, noValidPayloadMsg);
544         validateUpdateVnfSoftwarePayloadProperty(payload, noValidPayloadMsg, "existing-software-version", SOFTWARE_VERSION_PATTERN);
545         validateUpdateVnfSoftwarePayloadProperty(payload, noValidPayloadMsg, "new-software-version", SOFTWARE_VERSION_PATTERN);
546
547         //if "operations-timeout" is not integer, trying to read it as String that represent a number
548         if (!(payload.get("operations-timeout") instanceof Integer)) {
549             validateUpdateVnfSoftwarePayloadProperty(payload, noValidPayloadMsg, "operations-timeout", NUMBER_PATTERN);
550         }
551
552     }
553
554     private void validateUpdateVnfSoftwarePayloadProperty(Map payload, String noValidPayloadMsg, String propertyName, Pattern pattern) {
555         Object forValidation = payload.get(propertyName);
556         final String noValidPayloadPropertyMsg = noValidPayloadMsg + ", " + propertyName + " property is not valid";
557         if (!(forValidation instanceof String)) {
558             throw new BadRequestException(noValidPayloadPropertyMsg);
559         }
560         if (!pattern.matcher((String) forValidation).matches()) {
561             throw new BadRequestException(noValidPayloadPropertyMsg);
562         }
563     }
564
565     private void validateUpdateVnfConfig(RequestDetails requestDetails) throws Exception {
566         final String noValidPayloadMsg = "No valid payload in " + ChangeManagementRequest.CONFIG_UPDATE + " request";
567
568         Map payload = getChangeManagementPayload(requestDetails, noValidPayloadMsg);
569         validateConfigUpdateVnfPayloadProperty(payload, noValidPayloadMsg, "request-parameters");
570         validateConfigUpdateVnfPayloadProperty(payload, noValidPayloadMsg, "configuration-parameters");
571     }
572
573     private void validateConfigUpdateVnfPayloadProperty(Map payload, String noValidPayloadMsg, String propertyName) {
574         Object forValidation = payload.get(propertyName);
575         final String noValidPayloadPropertyMsg = noValidPayloadMsg+ ", "+ propertyName + " property is not valid";
576         if(!payload.containsKey(propertyName)) {
577             throw new BadRequestException( noValidPayloadPropertyMsg);
578         }
579     }
580
581     @Override
582     public MsoResponseWrapper deleteConfiguration(
583             RequestDetails requestDetails,
584             String serviceInstanceId,
585             String configurationId) throws Exception {
586
587         String methodName = "deleteConfiguration";
588         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " start");
589
590         String endpoint = validateEndpointPath(MsoProperties.MSO_REST_API_CONFIGURATION_INSTANCE);
591         endpoint = endpoint.replace(SVC_INSTANCE_ID, serviceInstanceId);
592         endpoint = endpoint.replace(CONFIGURATION_ID, configurationId);
593
594         return msoClientInterface.deleteConfiguration(requestDetails, endpoint);
595     }
596
597     @Override
598     public MsoResponseWrapper setConfigurationActiveStatus(
599             RequestDetails requestDetails,
600             String serviceInstanceId,
601             String configurationId,
602             boolean isActivate) throws Exception {
603
604         String methodName = "setConfigurationActiveStatus";
605         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " start");
606
607         String endpoint = validateEndpointPath(MsoProperties.MSO_REST_API_CONFIGURATION_INSTANCE);
608         endpoint = endpoint.replace(SVC_INSTANCE_ID, serviceInstanceId);
609         endpoint = endpoint.replace(CONFIGURATION_ID, configurationId);
610
611         String isActivateState = (isActivate ? ACTIVATE : DEACTIVATE);
612         endpoint = endpoint + isActivateState;
613
614         return msoClientInterface.setConfigurationActiveStatus(requestDetails, endpoint);
615     }
616
617     @Override
618     public MsoResponseWrapper setServiceInstanceStatus(RequestDetails requestDetails , String serviceInstanceId, boolean isActivate)throws Exception{
619         String methodName = "setServiceInstanceStatus";
620         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " start");
621         try {
622             String serviceEndpoint = validateEndpointPath(MsoProperties.MSO_REST_API_SVC_INSTANCE);
623             String endpoint = serviceEndpoint + "/" + serviceInstanceId;
624
625             String isActivateState = (isActivate ? ACTIVATE : DEACTIVATE);
626             endpoint = endpoint + isActivateState;
627
628
629             RestObject<String> restObjStr = new RestObject<>();
630             String str = "";
631             restObjStr.set(str);
632
633             msoClientInterface.setServiceInstanceStatus(requestDetails , str, "", endpoint, restObjStr);
634
635             return MsoUtil.wrapResponse(restObjStr);
636
637         } catch (Exception e) {
638             logger.error(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + "<== " + "." + methodName + e.toString());
639             logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + "." + methodName + e.toString());
640             throw e;
641         }
642     }
643
644     @Override
645     public MsoResponseWrapper setPortOnConfigurationStatus(
646             RequestDetails requestDetails,
647             String serviceInstanceId,
648             String configurationId,
649             boolean isEnable) throws Exception {
650         String methodName = "setPortOnConfigurationStatus";
651         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " start");
652
653         String endpoint = validateEndpointPath(MsoProperties.MSO_REST_API_CONFIGURATION_INSTANCE);
654         endpoint = endpoint.replace(SVC_INSTANCE_ID, serviceInstanceId);
655         endpoint = endpoint.replace(CONFIGURATION_ID, configurationId);
656
657         String isEnablePortStatus = (isEnable ? ENABLE_PORT : DISABLE_PORT);
658         endpoint = endpoint + isEnablePortStatus;
659
660         return msoClientInterface.setPortOnConfigurationStatus(requestDetails, endpoint);
661     }
662
663
664     @Override
665     public  RequestDetailsWrapper<RequestDetails> createOperationalEnvironmentActivationRequestDetails(OperationalEnvironmentActivateInfo details) {
666         RequestDetails requestDetails = new RequestDetails();
667         RequestInfo requestInfo = new RequestInfo();
668         requestInfo.setAdditionalProperty("resourceType", RESOURCE_TYPE_OPERATIONAL_ENVIRONMENT);
669         requestInfo.setSource(SOURCE_OPERATIONAL_ENVIRONMENT);
670         requestInfo.setRequestorId(details.getUserId());
671         requestDetails.setRequestInfo(requestInfo);
672
673         org.onap.vid.domain.mso.RelatedInstance relatedInstance = new org.onap.vid.domain.mso.RelatedInstance();
674         relatedInstance.setAdditionalProperty("resourceType", RESOURCE_TYPE_OPERATIONAL_ENVIRONMENT);
675         relatedInstance.setInstanceId(details.getRelatedInstanceId());
676         relatedInstance.setInstanceName(details.getRelatedInstanceName());
677         requestDetails.setAdditionalProperty("relatedInstanceList", Collections.singletonList(ImmutableMap.of("relatedInstance", relatedInstance)));
678
679         org.onap.vid.domain.mso.RequestParameters requestParameters = new org.onap.vid.domain.mso.RequestParameters();
680         requestParameters.setUserParams(null);
681         requestParameters.setAdditionalProperty("operationalEnvironmentType", "VNF");
682         requestParameters.setAdditionalProperty("workloadContext", details.getWorkloadContext());
683         requestParameters.setAdditionalProperty("manifest", details.getManifest());
684         requestDetails.setRequestParameters(requestParameters);
685
686         RequestDetailsWrapper<RequestDetails> requestDetailsWrapper = new RequestDetailsWrapper<>(requestDetails);
687
688         debugRequestDetails(requestDetailsWrapper);
689
690         return requestDetailsWrapper;
691     }
692
693     @Override
694     public String getOperationalEnvironmentActivationPath(OperationalEnvironmentActivateInfo details) {
695         String path = validateEndpointPath(MSO_REST_API_OPERATIONAL_ENVIRONMENT_ACTIVATE);
696         path = path.replace("<operational_environment_id>", details.getOperationalEnvironmentId());
697         return path;
698     }
699
700     @Override
701     public RequestDetailsWrapper<RequestDetails> createOperationalEnvironmentDeactivationRequestDetails(OperationalEnvironmentDeactivateInfo details) {
702         RequestDetails requestDetails = new RequestDetails();
703
704         RequestInfo requestInfo = new RequestInfo();
705         requestInfo.setAdditionalProperty("resourceType", RESOURCE_TYPE_OPERATIONAL_ENVIRONMENT);
706         requestInfo.setSource(SOURCE_OPERATIONAL_ENVIRONMENT);
707         requestInfo.setRequestorId(details.getUserId());
708         requestDetails.setRequestInfo(requestInfo);
709
710         org.onap.vid.domain.mso.RequestParameters requestParameters = new org.onap.vid.domain.mso.RequestParameters();
711         requestParameters.setUserParams(null);
712         requestParameters.setAdditionalProperty("operationalEnvironmentType", "VNF");
713         requestDetails.setRequestParameters(requestParameters);
714         RequestDetailsWrapper<RequestDetails> requestDetailsWrapper = new RequestDetailsWrapper<>(requestDetails);
715         debugRequestDetails(requestDetailsWrapper);
716         return requestDetailsWrapper;
717     }
718
719     @Override
720     public String getCloudResourcesRequestsStatusPath(String requestId) {
721         String path = validateEndpointPath(MSO_REST_API_CLOUD_RESOURCES_REQUEST_STATUS);
722         path = path.replace("<request_id>", requestId);
723         return path;
724     }
725
726     @Override
727     public String getOperationalEnvironmentDeactivationPath(OperationalEnvironmentDeactivateInfo details) {
728         String path = validateEndpointPath(MSO_REST_API_OPERATIONAL_ENVIRONMENT_DEACTIVATE);
729         path = path.replace("<operational_environment_id>", details.getOperationalEnvironmentId());
730         return path;
731     }
732
733     private void debugRequestDetails(Object requestDetails) {
734         if (logger.isDebugEnabled()) {
735             String requestDetailsAsString;
736             try {
737                 requestDetailsAsString = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT).writeValueAsString(requestDetails);
738             } catch (JsonProcessingException e) {
739                 requestDetailsAsString = "error: cannot stringify RequestDetails";
740             }
741             logger.debug(EELFLoggerDelegate.debugLogger, "requestDetailsAsString: {}", requestDetailsAsString);
742         }
743     }
744
745     @Override
746     public String getOperationalEnvironmentCreationPath() {
747         String path = validateEndpointPath(MSO_REST_API_OPERATIONAL_ENVIRONMENT_CREATE);
748         return path;
749     }
750
751     @Override
752     public RequestDetailsWrapper<OperationEnvironmentRequestDetails> convertParametersToRequestDetails(OperationalEnvironmentController.OperationalEnvironmentCreateBody input, String userId) {
753         OperationEnvironmentRequestDetails.RequestInfo requestInfo = new OperationEnvironmentRequestDetails.RequestInfo(
754                 RESOURCE_TYPE_OPERATIONAL_ENVIRONMENT,
755                 input.getInstanceName(),
756                 SOURCE_OPERATIONAL_ENVIRONMENT,
757                 userId);
758
759         OperationEnvironmentRequestDetails.RelatedInstance relatedInstance = new OperationEnvironmentRequestDetails.RelatedInstance(
760                 RESOURCE_TYPE_OPERATIONAL_ENVIRONMENT,
761                 input.getEcompInstanceId(),
762                 input.getEcompInstanceName());
763
764         List<OperationEnvironmentRequestDetails.RelatedInstance> relatedInstanceList = Collections.singletonList((relatedInstance));
765
766         OperationEnvironmentRequestDetails.RequestParameters requestParameters = new OperationEnvironmentRequestDetails.RequestParameters(
767                 input.getOperationalEnvironmentType(),
768                 input.getTenantContext(),
769                 input.getWorkloadContext());
770
771         OperationEnvironmentRequestDetails requestDetails = new OperationEnvironmentRequestDetails(requestInfo, relatedInstanceList, requestParameters);
772         RequestDetailsWrapper<OperationEnvironmentRequestDetails> requestDetailsWrapper = new RequestDetailsWrapper<>(requestDetails);
773         debugRequestDetails(requestDetailsWrapper);
774         return requestDetailsWrapper;
775     }
776
777     @Override
778     public MsoResponseWrapper removeRelationshipFromServiceInstance(RequestDetails requestDetails, String serviceInstanceId) throws Exception {
779         String methodName = "removeRelationshipFromServiceInstance";
780         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " start");
781
782         String serviceEndpoint = SystemProperties.getProperty(MsoProperties.MSO_REST_API_SVC_INSTANCE);
783         String removeRelationshipsPath = serviceEndpoint + "/" + serviceInstanceId + "/removeRelationships";
784
785         return msoClientInterface.removeRelationshipFromServiceInstance(requestDetails, removeRelationshipsPath);
786     }
787
788     @Override
789     public MsoResponseWrapper addRelationshipToServiceInstance(RequestDetails requestDetails, String serviceInstanceId) throws Exception {
790         String methodName = "addRelationshipToServiceInstance";
791         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " start");
792
793         String serviceEndpoint = SystemProperties.getProperty(MsoProperties.MSO_REST_API_SVC_INSTANCE);
794         String addRelationshipsPath = serviceEndpoint + "/" + serviceInstanceId + "/addRelationships";
795
796         return msoClientInterface.addRelationshipToServiceInstance(requestDetails, addRelationshipsPath);
797     }
798
799
800 }