5c2e51809c4d7246adad9c8e3b3245dfc5006308
[sdnc/apps.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SDNC
4  * ================================================================================
5  * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
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 package org.onap.sdnc.apps.ms.gra.controllers;
22
23 import com.fasterxml.jackson.annotation.JsonInclude;
24 import com.fasterxml.jackson.core.JsonProcessingException;
25 import com.fasterxml.jackson.databind.JsonMappingException;
26 import com.fasterxml.jackson.databind.ObjectMapper;
27 import com.google.gson.JsonParser;
28
29 import org.onap.ccsdk.apps.services.RestException;
30 import org.onap.ccsdk.apps.services.SvcLogicFactory;
31 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
32 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
33 import org.onap.ccsdk.sli.core.sli.provider.base.SvcLogicServiceBase;
34 import org.onap.sdnc.apps.ms.gra.data.ConfigPreloadData;
35 import org.onap.sdnc.apps.ms.gra.data.ConfigPreloadDataRepository;
36 import org.onap.sdnc.apps.ms.gra.data.ConfigServices;
37 import org.onap.sdnc.apps.ms.gra.data.ConfigServicesRepository;
38 import org.onap.sdnc.apps.ms.gra.data.ConfigContrailRouteAllottedResources;
39 import org.onap.sdnc.apps.ms.gra.data.ConfigContrailRouteAllottedResourcesRepository;
40 import org.onap.sdnc.apps.ms.gra.data.ConfigPortMirrorConfigurations;
41 import org.onap.sdnc.apps.ms.gra.data.ConfigPortMirrorConfigurationsRepository;
42 import org.onap.sdnc.apps.ms.gra.data.OperationalPreloadData;
43 import org.onap.sdnc.apps.ms.gra.data.OperationalPreloadDataRepository;
44 import org.onap.sdnc.apps.ms.gra.data.OperationalServices;
45 import org.onap.sdnc.apps.ms.gra.data.OperationalServicesRepository;
46 import org.onap.sdnc.apps.ms.gra.data.OperationalContrailRouteAllottedResources;
47 import org.onap.sdnc.apps.ms.gra.data.OperationalContrailRouteAllottedResourcesRepository;
48 import org.onap.sdnc.apps.ms.gra.data.OperationalPortMirrorConfigurations;
49 import org.onap.sdnc.apps.ms.gra.data.OperationalPortMirrorConfigurationsRepository;
50 import org.onap.sdnc.apps.ms.gra.swagger.OperationsApi;
51 import org.onap.sdnc.apps.ms.gra.swagger.model.*;
52 import org.springframework.beans.factory.annotation.Autowired;
53 import org.springframework.boot.autoconfigure.domain.EntityScan;
54 import org.springframework.context.annotation.ComponentScan;
55 import org.springframework.context.annotation.Import;
56 import org.springframework.http.HttpStatus;
57 import org.springframework.http.ResponseEntity;
58 import org.springframework.stereotype.Controller;
59
60 import javax.servlet.http.HttpServletRequest;
61 import javax.validation.Valid;
62
63 import java.text.DateFormat;
64 import java.text.SimpleDateFormat;
65 import java.util.*;
66 import java.util.concurrent.atomic.AtomicBoolean;
67
68 @Controller
69 @ComponentScan(basePackages = { "org.onap.sdnc.apps.ms.gra.*", "org.onap.ccsdk.apps.services" })
70 @EntityScan("org.onap.sdnc.apps.ms.gra.*")
71 @Import(value = SvcLogicFactory.class)
72 public class OperationsApiController implements OperationsApi {
73
74     private static final String CALLED_STR = "{} called.";
75     private static final String MODULE_NAME = "GENERIC-RESOURCE-API";
76     private static final String SERVICE_OBJECT_PATH_PARAM = "service-object-path";
77     private static final String NETWORK_OBJECT_PATH_PARAM = "network-object-path";
78     private static final String VNF_OBJECT_PATH_PARAM = "vnf-object-path";
79     private static final String PNF_OBJECT_PATH_PARAM = "pnf-object-path";
80     private static final String VF_MODULE_OBJECT_PATH_PARAM = "vf-module-object-path";
81     private static final String PORT_MIRROR_OBJECT_PATH_PARAM = "port-mirror-object-path";
82     private static final String BACKGROUND_THREAD_STARTED_MESSAGE = "Start background thread";
83     private static final String BACKGROUND_THREAD_INFO = "Background thread: input conf_id is {}";
84     private static final String SKIP_MDSAL_UPDATE_PROP = "skip-mdsal-update";
85     private static final String ADDING_INPUT_DATA_LOG = "Adding INPUT data for {} [{}] input: {}";
86     private static final String ADDING_OPERATIONAL_DATA_LOG = "Adding OPERATIONAL data for {} [{}] operational-data: {}";
87
88     private final ObjectMapper objectMapper;
89
90     private final HttpServletRequest request;
91
92     @Autowired
93     protected SvcLogicServiceBase svc;
94
95     @Autowired
96     private ConfigPreloadDataRepository configPreloadDataRepository;
97
98     @Autowired
99     private OperationalPreloadDataRepository operationalPreloadDataRepository;
100
101     @Autowired
102     private ConfigServicesRepository configServicesRepository;
103
104     @Autowired
105     private OperationalServicesRepository operationalServicesRepository;
106
107     @Autowired
108     private ConfigContrailRouteAllottedResourcesRepository configContrailRouteAllottedResourcesRepository;
109
110     @Autowired
111     private OperationalContrailRouteAllottedResourcesRepository operationalContrailRouteAllottedResourcesRepository;
112
113     @Autowired
114     private ConfigPortMirrorConfigurationsRepository configPortMirrorConfigurationsRepository;
115
116     @Autowired
117     private OperationalPortMirrorConfigurationsRepository operationalPortMirrorConfigurationsRepository;
118
119     private static class Iso8601Util {
120
121         private static TimeZone timeZone = TimeZone.getTimeZone("UTC");
122         private static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
123
124         private Iso8601Util() {
125         }
126
127         static {
128             dateFormat.setTimeZone(timeZone);
129         }
130
131         private static String now() {
132             return dateFormat.format(new Date());
133         }
134     }
135
136     @org.springframework.beans.factory.annotation.Autowired
137     public OperationsApiController(ObjectMapper objectMapper, HttpServletRequest request) {
138         objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
139         objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
140         this.objectMapper = objectMapper;
141         this.request = request;
142     }
143
144     @Override
145     public Optional<ObjectMapper> getObjectMapper() {
146         return Optional.ofNullable(objectMapper);
147     }
148
149     @Override
150     public Optional<HttpServletRequest> getRequest() {
151         return Optional.ofNullable(request);
152     }
153
154     @Override
155     public ResponseEntity<GenericResourceApiPreloadNetworkTopologyOperation> operationsGENERICRESOURCEAPIpreloadNetworkTopologyOperationPost(
156             @Valid GenericResourceApiPreloadnetworktopologyoperationInputBodyparam graInput) {
157         final String svcOperation = "preload-network-topology-operation";
158         GenericResourceApiPreloadNetworkTopologyOperation retval = new GenericResourceApiPreloadNetworkTopologyOperation();
159         GenericResourceApiPreloadTopologyResponseBody resp = new GenericResourceApiPreloadTopologyResponseBody();
160
161         log.info(CALLED_STR, svcOperation);
162         if (hasInvalidPreloadNetwork(graInput)) {
163             log.debug("exiting {} because of null or empty preload-network-topology-information", svcOperation);
164
165             resp.setResponseCode("403");
166             resp.setResponseMessage("invalid input, null or empty preload-network-topology-information");
167             resp.setAckFinalIndicator("Y");
168
169             retval.setOutput(resp);
170
171             return new ResponseEntity<>(retval, HttpStatus.FORBIDDEN);
172         }
173
174         String preloadId = graInput.getInput().getPreloadNetworkTopologyInformation()
175                 .getNetworkTopologyIdentifierStructure().getNetworkId();
176         String preloadType = "network";
177
178         resp.setSvcRequestId(graInput.getInput().getSdncRequestHeader().getSvcRequestId());
179
180         SvcLogicContext ctxIn = new SvcLogicContext();
181
182         GenericResourceApiPreloaddataPreloadData preloadData = null;
183
184         // Add input to SvcLogicContext
185         try {
186             ctxIn.mergeJson(svcOperation + "-input", objectMapper.writeValueAsString(graInput.getInput()));
187         } catch (JsonProcessingException e) {
188             log.error("exiting {} due to parse error on input preload data", svcOperation);
189             resp.setResponseCode("500");
190             resp.setResponseMessage("internal error");
191             resp.setAckFinalIndicator("Y");
192             retval.setOutput(resp);
193             return new ResponseEntity<>(retval, HttpStatus.INTERNAL_SERVER_ERROR);
194         }
195
196         // Add config tree data to SvcLogicContext
197         try {
198             preloadData = getConfigPreloadData(preloadId, preloadType);
199             ctxIn.mergeJson("preload-data", objectMapper.writeValueAsString(preloadData));
200         } catch (JsonProcessingException e) {
201             log.error("exiting {} due to parse error on saved config preload data", svcOperation);
202             resp.setResponseCode("500");
203             resp.setResponseMessage("internal error");
204             resp.setAckFinalIndicator("Y");
205             retval.setOutput(resp);
206             return new ResponseEntity<>(retval, HttpStatus.INTERNAL_SERVER_ERROR);
207         }
208
209         // Add operational tree data to SvcLogicContext
210         try {
211             preloadData = getOperationalPreloadData(preloadId, preloadType);
212             ctxIn.mergeJson("operational-data", objectMapper.writeValueAsString(preloadData));
213         } catch (JsonProcessingException e) {
214             log.error("exiting {} due to parse error on saved operational preload data", svcOperation);
215             resp.setResponseCode("500");
216             resp.setResponseMessage("internal error");
217             resp.setAckFinalIndicator("Y");
218             retval.setOutput(resp);
219             return new ResponseEntity<>(retval, HttpStatus.INTERNAL_SERVER_ERROR);
220         }
221
222         // Call DG
223         try {
224             // Any of these can throw a nullpointer exception
225             // execute should only throw a SvcLogicException
226             SvcLogicContext ctxOut = svc.execute(MODULE_NAME, svcOperation, null, "sync", ctxIn);
227             Properties respProps = ctxOut.toProperties();
228
229             resp.setAckFinalIndicator(respProps.getProperty("ack-final-indicator", "Y"));
230             resp.setResponseCode(respProps.getProperty("error-code", "200"));
231             resp.setResponseMessage(respProps.getProperty("error-message", "SUCCESS"));
232
233             if ("200".equals(resp.getResponseCode())) {
234                 // If DG returns success, update database
235                 String ctxJson = ctxOut.toJsonString("preload-data");
236                 log.info("DG preload-data is {}", ctxJson);
237                 GenericResourceApiPreloaddataPreloadData preloadToLoad = objectMapper.readValue(ctxJson,
238                         GenericResourceApiPreloaddataPreloadData.class);
239                 saveConfigPreloadData(preloadId, preloadType, preloadToLoad);
240                 saveOperationalPreloadData(preloadId, preloadType, preloadToLoad);
241             }
242
243         } catch (NullPointerException npe) {
244             log.error("Caught NPE", npe);
245             resp.setAckFinalIndicator("true");
246             resp.setResponseCode("500");
247             resp.setResponseMessage("Check that you populated module, rpc and or mode correctly.");
248         } catch (SvcLogicException e) {
249             log.error("Caught SvcLogicException", e);
250             resp.setAckFinalIndicator("true");
251             resp.setResponseCode("500");
252             resp.setResponseMessage(e.getMessage());
253         } catch (JsonMappingException e) {
254             log.error("Caught JsonMappingException", e);
255             resp.setAckFinalIndicator("true");
256             resp.setResponseCode("500");
257             resp.setResponseMessage(e.getMessage());
258         } catch (JsonProcessingException e) {
259             log.error("Caught JsonProcessingException", e);
260             resp.setAckFinalIndicator("true");
261             resp.setResponseCode("500");
262             resp.setResponseMessage(e.getMessage());
263         }
264
265         retval.setOutput(resp);
266         return (new ResponseEntity<>(retval, HttpStatus.valueOf(Integer.parseInt(resp.getResponseCode()))));
267     }
268
269     @Override
270     public ResponseEntity<GenericResourceApiPreloadVfModuleTopologyOperation> operationsGENERICRESOURCEAPIpreloadVfModuleTopologyOperationPost(
271             @Valid GenericResourceApiPreloadvfmoduletopologyoperationInputBodyparam graInput) {
272         final String svcOperation = "preload-vf-module-topology-operation";
273         GenericResourceApiPreloadVfModuleTopologyOperation retval = new GenericResourceApiPreloadVfModuleTopologyOperation();
274         GenericResourceApiPreloadTopologyResponseBody resp = new GenericResourceApiPreloadTopologyResponseBody();
275
276         log.info(CALLED_STR, svcOperation);
277         if (hasInvalidPreloadNetwork(graInput)) {
278             log.debug("exiting {} because of null or empty preload-network-topology-information", svcOperation);
279
280             resp.setResponseCode("403");
281             resp.setResponseMessage("invalid input, null or empty preload-network-topology-information");
282             resp.setAckFinalIndicator("Y");
283
284             retval.setOutput(resp);
285
286             return new ResponseEntity<>(retval, HttpStatus.FORBIDDEN);
287         }
288
289         String preloadId = graInput.getInput().getPreloadVfModuleTopologyInformation().getVfModuleTopology()
290                 .getVfModuleTopologyIdentifier().getVfModuleName();
291         String preloadType = "vf-module";
292
293         resp.setSvcRequestId(graInput.getInput().getSdncRequestHeader().getSvcRequestId());
294
295         SvcLogicContext ctxIn = new SvcLogicContext();
296
297         GenericResourceApiPreloaddataPreloadData preloadData = null;
298
299         // Add input to SvcLogicContext
300         try {
301             ctxIn.mergeJson(svcOperation + "-input", objectMapper.writeValueAsString(graInput.getInput()));
302         } catch (JsonProcessingException e) {
303             log.error("exiting {} due to parse error on input preload data", svcOperation);
304             resp.setResponseCode("500");
305             resp.setResponseMessage("internal error");
306             resp.setAckFinalIndicator("Y");
307             retval.setOutput(resp);
308             return new ResponseEntity<>(retval, HttpStatus.INTERNAL_SERVER_ERROR);
309         }
310
311         // Add config tree data to SvcLogicContext
312         try {
313             preloadData = getConfigPreloadData(preloadId, preloadType);
314             ctxIn.mergeJson("preload-data", objectMapper.writeValueAsString(preloadData));
315         } catch (JsonProcessingException e) {
316             log.error("exiting {} due to parse error on saved config preload data", svcOperation);
317             resp.setResponseCode("500");
318             resp.setResponseMessage("internal error");
319             resp.setAckFinalIndicator("Y");
320             retval.setOutput(resp);
321             return new ResponseEntity<>(retval, HttpStatus.INTERNAL_SERVER_ERROR);
322         }
323
324         // Add operational tree data to SvcLogicContext
325         try {
326             preloadData = getOperationalPreloadData(preloadId, preloadType);
327             ctxIn.mergeJson("operational-data", objectMapper.writeValueAsString(preloadData));
328         } catch (JsonProcessingException e) {
329             log.error("exiting {} due to parse error on saved operational preload data", svcOperation);
330             resp.setResponseCode("500");
331             resp.setResponseMessage("internal error");
332             resp.setAckFinalIndicator("Y");
333             retval.setOutput(resp);
334             return new ResponseEntity<>(retval, HttpStatus.INTERNAL_SERVER_ERROR);
335         }
336
337         // Call DG
338         try {
339             // Any of these can throw a nullpointer exception
340             // execute should only throw a SvcLogicException
341             SvcLogicContext ctxOut = svc.execute(MODULE_NAME, svcOperation, null, "sync", ctxIn);
342             Properties respProps = ctxOut.toProperties();
343
344             resp.setAckFinalIndicator(respProps.getProperty("ack-final-indicator", "Y"));
345             resp.setResponseCode(respProps.getProperty("error-code", "200"));
346             resp.setResponseMessage(respProps.getProperty("error-message", "SUCCESS"));
347
348             if ("200".equals(resp.getResponseCode())) {
349                 // If DG returns success, update database
350                 String ctxJson = ctxOut.toJsonString("preload-data");
351                 GenericResourceApiPreloaddataPreloadData preloadToLoad = objectMapper.readValue(ctxJson,
352                         GenericResourceApiPreloaddataPreloadData.class);
353                 saveConfigPreloadData(preloadId, preloadType, preloadToLoad);
354                 saveOperationalPreloadData(preloadId, preloadType, preloadToLoad);
355             }
356
357         } catch (NullPointerException npe) {
358             resp.setAckFinalIndicator("true");
359             resp.setResponseCode("500");
360             resp.setResponseMessage("Check that you populated module, rpc and or mode correctly.");
361         } catch (SvcLogicException e) {
362             resp.setAckFinalIndicator("true");
363             resp.setResponseCode("500");
364             resp.setResponseMessage(e.getMessage());
365         } catch (JsonMappingException e) {
366             resp.setAckFinalIndicator("true");
367             resp.setResponseCode("500");
368             resp.setResponseMessage(e.getMessage());
369         } catch (JsonProcessingException e) {
370             resp.setAckFinalIndicator("true");
371             resp.setResponseCode("500");
372             resp.setResponseMessage(e.getMessage());
373         }
374
375         retval.setOutput(resp);
376         return (new ResponseEntity<>(retval, HttpStatus.valueOf(Integer.parseInt(resp.getResponseCode()))));
377     }
378
379     private boolean hasInvalidPreloadNetwork(
380             GenericResourceApiPreloadnetworktopologyoperationInputBodyparam preloadData) {
381         return ((preloadData == null) || (preloadData.getInput() == null)
382                 || (preloadData.getInput().getPreloadNetworkTopologyInformation() == null));
383     }
384
385     private boolean hasInvalidPreloadNetwork(
386             GenericResourceApiPreloadvfmoduletopologyoperationInputBodyparam preloadData) {
387         return ((preloadData == null) || (preloadData.getInput() == null)
388                 || (preloadData.getInput().getPreloadVfModuleTopologyInformation() == null));
389     }
390
391     private boolean hasInvalidServiceId(GenericResourceApiServiceOperationInformation input) {
392
393         return input == null || input.getServiceInformation() == null
394                 || input.getServiceInformation().getServiceInstanceId() == null
395                 || input.getServiceInformation().getServiceInstanceId().length() == 0;
396     }
397
398     private boolean hasInvalidServiceId(GenericResourceApiNetworkOperationInformation input) {
399
400         return input == null || input.getServiceInformation() == null
401                 || input.getServiceInformation().getServiceInstanceId() == null
402                 || input.getServiceInformation().getServiceInstanceId().length() == 0;
403     }
404
405     private boolean hasInvalidServiceId(GenericResourceApiVnfOperationInformation input) {
406
407         return input == null || input.getServiceInformation() == null
408                 || input.getServiceInformation().getServiceInstanceId() == null
409                 || input.getServiceInformation().getServiceInstanceId().length() == 0;
410     }
411
412     private boolean hasInvalidServiceId(GenericResourceApiVfModuleOperationInformation input) {
413
414         return input == null || input.getServiceInformation() == null
415                 || input.getServiceInformation().getServiceInstanceId() == null
416                 || input.getServiceInformation().getServiceInstanceId().length() == 0;
417     }
418
419     private boolean hasInvalidServiceId(GenericResourceApiPortMirrorTopologyOperationInformation input) {
420
421         return input == null || input.getServiceInformation() == null
422                 || input.getServiceInformation().getServiceInstanceId() == null
423                 || input.getServiceInformation().getServiceInstanceId().length() == 0;
424     }
425
426     private boolean hasInvalidServiceId(GenericResourceApiVnfgetresourcerequestInput input) {
427
428         return input == null || input.getServiceInformation() == null
429                 || input.getServiceInformation().getServiceInstanceId() == null
430                 || input.getServiceInformation().getServiceInstanceId().length() == 0;
431     }
432
433     private boolean hasInvalidVnfId(GenericResourceApiVfModuleOperationInformation input) {
434
435         return input == null || input.getVnfInformation() == null
436                 || input.getVnfInformation().getVnfId() == null
437                 || input.getVnfInformation().getVnfId().length() == 0;
438     }
439
440     private boolean hasInvalidConfigurationId(GenericResourceApiPortMirrorTopologyOperationInformation input) {
441         return input.getConfigurationInformation() == null
442                 || input.getConfigurationInformation().getConfigurationId() == null
443                 || input.getConfigurationInformation().getConfigurationId().length() == 0;
444     }
445
446     private boolean hasInvalidPolicyUpdateInput(GenericResourceApiPolicyupdatenotifyoperationInput input) {
447         return (input.getPolicyName() == null) || (input.getUpdateType() == null) || (input.getVersionId() == null);
448     }
449
450     private GenericResourceApiPreloaddataPreloadData getConfigPreloadData(String preloadId, String preloadType)
451             throws JsonProcessingException {
452
453         List<ConfigPreloadData> configPreloadData = configPreloadDataRepository.findByPreloadIdAndPreloadType(preloadId,
454                 preloadType);
455
456         if (configPreloadData.isEmpty()) {
457             return (null);
458         } else {
459             return (objectMapper.readValue(configPreloadData.get(0).getPreloadData(),
460                     GenericResourceApiPreloaddataPreloadData.class));
461         }
462     }
463
464     private GenericResourceApiPreloaddataPreloadData getOperationalPreloadData(String preloadId, String preloadType)
465             throws JsonProcessingException {
466
467         List<OperationalPreloadData> configPreloadData = operationalPreloadDataRepository
468                 .findByPreloadIdAndPreloadType(preloadId, preloadType);
469
470         if (configPreloadData.isEmpty()) {
471             return (null);
472         } else {
473             return (objectMapper.readValue(configPreloadData.get(0).getPreloadData(),
474                     GenericResourceApiPreloaddataPreloadData.class));
475         }
476     }
477
478     private void saveConfigPreloadData(String preloadId, String preloadType,
479             GenericResourceApiPreloaddataPreloadData preloadData) throws JsonProcessingException {
480
481         configPreloadDataRepository.deleteByPreloadIdAndPreloadType(preloadId, preloadType);
482         configPreloadDataRepository
483                 .save(new ConfigPreloadData(preloadId, preloadType, objectMapper.writeValueAsString(preloadData)));
484
485     }
486
487     private void saveOperationalPreloadData(String preloadId, String preloadType,
488             GenericResourceApiPreloaddataPreloadData preloadData) throws JsonProcessingException {
489
490         operationalPreloadDataRepository.deleteByPreloadIdAndPreloadType(preloadId, preloadType);
491         operationalPreloadDataRepository
492                 .save(new OperationalPreloadData(preloadId, preloadType, objectMapper.writeValueAsString(preloadData)));
493
494     }
495
496     private GenericResourceApiServicedataServiceData getConfigServiceData(String svcInstanceId)
497             throws JsonProcessingException {
498
499         List<ConfigServices> configServices = configServicesRepository.findBySvcInstanceId(svcInstanceId);
500
501         if (configServices.isEmpty()) {
502             return (null);
503         } else {
504             return (objectMapper.readValue(configServices.get(0).getSvcData(),
505                     GenericResourceApiServicedataServiceData.class));
506         }
507     }
508
509     @Override
510     public ResponseEntity<GenericResourceApiNetworkTopologyOperation> operationsGENERICRESOURCEAPInetworkTopologyOperationPost(
511             @Valid GenericResourceApiNetworkOperationInformationBodyparam input) throws RestException {
512         final String svcOperation = "network-topology-operation";
513         GenericResourceApiNetworkTopologyOperation retval = new GenericResourceApiNetworkTopologyOperation();
514         GenericResourceApiNetworktopologyoperationOutput resp = new GenericResourceApiNetworktopologyoperationOutput();
515
516         log.info(CALLED_STR, svcOperation);
517         // Verify input contains service instance id
518         if (hasInvalidServiceId(input.getInput())) {
519             log.debug("exiting {} because of null or empty service-instance-id", svcOperation);
520
521             resp.setResponseCode("404");
522             resp.setResponseMessage("null or empty service-instance-id");
523             resp.setAckFinalIndicator("Y");
524
525             retval.setOutput(resp);
526
527             return new ResponseEntity<>(retval, HttpStatus.OK);
528         }
529
530         String svcInstanceId = input.getInput().getServiceInformation().getServiceInstanceId();
531
532         SvcLogicContext ctxIn = new SvcLogicContext();
533
534         // Add input to SvcLogicContext
535         try {
536             ctxIn.mergeJson(svcOperation + "-input", objectMapper.writeValueAsString(input.getInput()));
537         } catch (JsonProcessingException e) {
538             log.error("exiting {} due to parse error on input data", svcOperation);
539             resp.setResponseCode("500");
540             resp.setResponseMessage("internal error");
541             resp.setAckFinalIndicator("Y");
542             retval.setOutput(resp);
543             return new ResponseEntity<>(retval, HttpStatus.INTERNAL_SERVER_ERROR);
544         }
545
546         // Add config tree data to SvcLogicContext
547         List<ConfigServices> configServices = configServicesRepository.findBySvcInstanceId(svcInstanceId);
548         ConfigServices configService = null;
549         String svcData = null;
550         if (configServices != null && !configServices.isEmpty()) {
551             configService = configServices.get(0);
552             svcData = configService.getSvcData();
553         }
554         if (svcData != null) {
555             ctxIn.mergeJson("service-data", configService.getSvcData());
556         } else {
557             log.debug("exiting {} because the service-instance does not have any service data in SDN", svcOperation);
558
559             resp.setResponseCode("404");
560             resp.setResponseMessage("invalid input: the service-instance does not have any service data in SDNC");
561             resp.setAckFinalIndicator("Y");
562
563             retval.setOutput(resp);
564
565             return new ResponseEntity<>(retval, HttpStatus.OK);
566         }
567
568         // Add operational tree data to SvcLogicContext
569         List<OperationalServices> operServices = operationalServicesRepository.findBySvcInstanceId(svcInstanceId);
570         OperationalServices operService = null;
571         boolean saveOperationalData = false;
572
573         if (operServices != null && !operServices.isEmpty()) {
574             operService = operServices.get(0);
575             ctxIn.mergeJson("operational-data", operService.getSvcData());
576         } else {
577             operService = new OperationalServices(svcInstanceId, null, null);
578         }
579
580         // Update service status info in config entry from input
581         configService.setServiceStatusAction(input.getInput().getRequestInformation().getRequestAction().toString());
582         configService.setServiceStatusRpcAction(input.getInput().getSdncRequestHeader().getSvcAction().toString());
583         configService.setServiceStatusRpcName(svcOperation);
584
585         // Call DG
586         try {
587             // Any of these can throw a nullpointer exception
588             // execute should only throw a SvcLogicException
589             SvcLogicContext ctxOut = svc.execute(MODULE_NAME, svcOperation, null, "sync", ctxIn);
590             Properties respProps = ctxOut.toProperties();
591
592             resp.setAckFinalIndicator(respProps.getProperty("ack-final-indicator", "Y"));
593             resp.setResponseCode(respProps.getProperty("error-code", "200"));
594             resp.setResponseMessage(respProps.getProperty("error-message", "SUCCESS"));
595
596
597
598             configService
599                     .setServiceStatusRequestStatus(GenericResourceApiRequestStatusEnumeration.SYNCCOMPLETE.toString());
600
601             if ("200".equals(resp.getResponseCode())) {
602
603                 GenericResourceApiInstanceReference serviceReference = new GenericResourceApiInstanceReference();
604                 serviceReference.setInstanceId(svcInstanceId);
605                 serviceReference.setObjectPath(respProps.getProperty(SERVICE_OBJECT_PATH_PARAM));
606                 resp.setServiceResponseInformation(serviceReference);
607     
608                 GenericResourceApiInstanceReference networkReference = new GenericResourceApiInstanceReference();
609                 networkReference.setInstanceId(respProps.getProperty("networkId"));
610                 networkReference.setObjectPath(respProps.getProperty(NETWORK_OBJECT_PATH_PARAM));
611                 resp.setNetworkResponseInformation(networkReference);
612
613                 // If DG returns success, update svcData in config and operational trees
614                 // and remember to save operational data.
615                 String ctxJson = ctxOut.toJsonString("service-data");
616                 configService.setSvcData(ctxJson);
617                 operService.setSvcData(ctxJson);
618                 saveOperationalData = true;
619             }
620
621         } catch (NullPointerException npe) {
622             resp.setAckFinalIndicator("true");
623             resp.setResponseCode("500");
624             resp.setResponseMessage("Check that you populated module, rpc and or mode correctly.");
625         } catch (SvcLogicException e) {
626             resp.setAckFinalIndicator("true");
627             resp.setResponseCode("500");
628             resp.setResponseMessage(e.getMessage());
629         }
630
631         // Update status in config services entry
632
633         configService.setServiceStatusFinalIndicator(resp.getAckFinalIndicator());
634         configService.setServiceStatusResponseCode(resp.getResponseCode());
635         configService.setServiceStatusResponseMessage(resp.getResponseMessage());
636         configService.setServiceStatusResponseTimestamp(Iso8601Util.now());
637
638         // Update config tree
639         configServicesRepository.save(configService);
640
641         // If necessary, sync status to operation service entry and save
642         if (saveOperationalData) {
643             operService.setServiceStatus(configService.getServiceStatus());
644             operationalServicesRepository.save(operService);
645         }
646         retval.setOutput(resp);
647         
648         return (new ResponseEntity<>(retval, HttpStatus.OK));
649     }
650
651     @Override
652     public ResponseEntity<GenericResourceApiServiceTopologyOperation> operationsGENERICRESOURCEAPIserviceTopologyOperationPost(
653             @Valid GenericResourceApiServiceOperationInformationBodyparam input) throws RestException {
654         final String svcOperation = "service-topology-operation";
655         GenericResourceApiServiceTopologyOperation retval = new GenericResourceApiServiceTopologyOperation();
656         GenericResourceApiServicetopologyoperationOutput resp = new GenericResourceApiServicetopologyoperationOutput();
657
658         log.info(CALLED_STR, svcOperation);
659
660         // Verify input contains service instance id
661         if (hasInvalidServiceId(input.getInput())) {
662             log.debug("exiting {} because of null or empty service-instance-id", svcOperation);
663
664             resp.setResponseCode("404");
665             resp.setResponseMessage("null or empty service-instance-id");
666             resp.setAckFinalIndicator("Y");
667
668             retval.setOutput(resp);
669
670             return new ResponseEntity<>(retval, HttpStatus.OK);
671         }
672
673         String svcInstanceId = input.getInput().getServiceInformation().getServiceInstanceId();
674
675         SvcLogicContext ctxIn = new SvcLogicContext();
676
677         // Add input to SvcLogicContext
678         try {
679             ctxIn.mergeJson(svcOperation + "-input", objectMapper.writeValueAsString(input.getInput()));
680         } catch (JsonProcessingException e) {
681             log.error("exiting {} due to parse error on input data", svcOperation);
682             resp.setResponseCode("500");
683             resp.setResponseMessage("internal error");
684             resp.setAckFinalIndicator("Y");
685             retval.setOutput(resp);
686             return new ResponseEntity<>(retval, HttpStatus.INTERNAL_SERVER_ERROR);
687         }
688
689         // Add config tree data to SvcLogicContext
690         List<ConfigServices> configServices = configServicesRepository.findBySvcInstanceId(svcInstanceId);
691         ConfigServices configService = null;
692         if (configServices != null && !configServices.isEmpty()) {
693             configService = configServices.get(0);
694             if (configService.getSvcData() != null) {
695                 ctxIn.mergeJson("service-data", configService.getSvcData());
696             }
697         } else {
698             configService = new ConfigServices(svcInstanceId, null);
699         }
700
701         // Add operational tree data to SvcLogicContext
702         List<OperationalServices> operServices = operationalServicesRepository.findBySvcInstanceId(svcInstanceId);
703         OperationalServices operService = null;
704         boolean saveOperationalData = false;
705
706         if (operServices != null && !operServices.isEmpty()) {
707             operService = operServices.get(0);
708             ctxIn.mergeJson("operational-data", operService.getSvcData());
709         } else {
710             operService = new OperationalServices(svcInstanceId, null, null);
711         }
712
713         // Update service status info in config entry from input
714         configService.setServiceStatusAction(input.getInput().getRequestInformation().getRequestAction().toString());
715         configService.setServiceStatusRpcAction(input.getInput().getSdncRequestHeader().getSvcAction().toString());
716         configService.setServiceStatusRpcName(svcOperation);
717
718         // Call DG
719         try {
720             // Any of these can throw a nullpointer exception
721             // execute should only throw a SvcLogicException
722             SvcLogicContext ctxOut = svc.execute(MODULE_NAME, svcOperation, null, "sync", ctxIn);
723             Properties respProps = ctxOut.toProperties();
724
725             resp.setAckFinalIndicator(respProps.getProperty("ack-final-indicator", "Y"));
726             resp.setResponseCode(respProps.getProperty("error-code", "200"));
727             resp.setResponseMessage(respProps.getProperty("error-message", "SUCCESS"));
728
729             configService
730                     .setServiceStatusRequestStatus(GenericResourceApiRequestStatusEnumeration.SYNCCOMPLETE.toString());
731
732             if ("200".equals(resp.getResponseCode())) {                 
733                 GenericResourceApiInstanceReference serviceReference = new GenericResourceApiInstanceReference();
734                 serviceReference.setInstanceId(svcInstanceId);
735                 serviceReference.setObjectPath(respProps.getProperty(SERVICE_OBJECT_PATH_PARAM));
736                 resp.setServiceResponseInformation(serviceReference);
737
738                 // If DG returns success, update svcData in config and operational trees
739                 // and remember to save operational data.
740                 String ctxJson = ctxOut.toJsonString("service-data");
741                 configService.setSvcData(ctxJson);
742                 operService.setSvcData(ctxJson);
743                 saveOperationalData = true;
744             }
745
746         } catch (NullPointerException npe) {
747             resp.setAckFinalIndicator("true");
748             resp.setResponseCode("500");
749             resp.setResponseMessage("Check that you populated module, rpc and or mode correctly.");
750         } catch (SvcLogicException e) {
751             resp.setAckFinalIndicator("true");
752             resp.setResponseCode("500");
753             resp.setResponseMessage(e.getMessage());
754         }
755
756         // Update status in config services entry
757
758         configService.setServiceStatusFinalIndicator(resp.getAckFinalIndicator());
759         configService.setServiceStatusResponseCode(resp.getResponseCode());
760         configService.setServiceStatusResponseMessage(resp.getResponseMessage());
761         configService.setServiceStatusResponseTimestamp(Iso8601Util.now());
762
763         // Update config tree
764         configServicesRepository.save(configService);
765
766         // If necessary, sync status to operation service entry and save
767         if (saveOperationalData) {
768             operService.setServiceStatus(configService.getServiceStatus());
769             operationalServicesRepository.save(operService);
770         }
771         retval.setOutput(resp);
772         return (new ResponseEntity<>(retval, HttpStatus.OK));
773
774     }
775
776     @Override
777     public ResponseEntity<GenericResourceApiVnfTopologyOperation> operationsGENERICRESOURCEAPIvnfTopologyOperationPost(
778             @Valid GenericResourceApiVnfOperationInformationBodyparam input)
779             throws RestException {
780         final String svcOperation = "vnf-topology-operation";
781         GenericResourceApiVnfTopologyOperation retval = new GenericResourceApiVnfTopologyOperation();
782         GenericResourceApiVnftopologyoperationOutput resp = new GenericResourceApiVnftopologyoperationOutput();
783
784         log.info(CALLED_STR, svcOperation);
785         // Verify input contains service instance id
786         if(hasInvalidServiceId(input.getInput())) {
787             log.debug("exiting {} because of null or empty service-instance-id", svcOperation);
788
789             resp.setResponseCode("404");
790             resp.setResponseMessage("null or empty service-instance-id");
791             resp.setAckFinalIndicator("Y");
792
793             retval.setOutput(resp);
794
795             return new ResponseEntity<>(retval, HttpStatus.OK);
796         }
797
798         String svcInstanceId = input.getInput().getServiceInformation().getServiceInstanceId();
799         String vnfId = null;
800
801         if((input.getInput() != null) && (input.getInput().getVnfInformation() != null)) {
802             vnfId = input.getInput().getVnfInformation().getVnfId();
803         }
804
805         SvcLogicContext ctxIn = new SvcLogicContext();
806
807         // Add input to SvcLogicContext
808         try {
809             ctxIn.mergeJson(svcOperation + "-input", objectMapper.writeValueAsString(input.getInput()));
810         }
811         catch(JsonProcessingException e) {
812             log.error("exiting {} due to parse error on input data", svcOperation);
813             resp.setResponseCode("500");
814             resp.setResponseMessage("internal error");
815             resp.setAckFinalIndicator("Y");
816             retval.setOutput(resp);
817             return new ResponseEntity<>(retval, HttpStatus.INTERNAL_SERVER_ERROR);
818         }
819
820         // Add config tree data to SvcLogicContext
821         List<ConfigServices> configServices = configServicesRepository.findBySvcInstanceId(svcInstanceId);
822         ConfigServices configService = null;
823         if(configServices != null && !configServices.isEmpty()) {
824             configService = configServices.get(0);
825             ctxIn.mergeJson("service-data", configService.getSvcData());
826         }
827         else {
828             log.debug("exiting {} because the service-instance does not have any service data in SDN", svcOperation);
829
830             resp.setResponseCode("404");
831             resp.setResponseMessage("invalid input: the service-instance does not have any service data in SDNC");
832             resp.setAckFinalIndicator("Y");
833
834             retval.setOutput(resp);
835
836             return new ResponseEntity<>(retval, HttpStatus.OK);
837         }
838
839         // Add operational tree data to SvcLogicContext
840         List<OperationalServices> operServices = operationalServicesRepository.findBySvcInstanceId(svcInstanceId);
841         OperationalServices operService = null;
842         boolean saveOperationalData = false;
843
844         if(operServices != null && !operServices.isEmpty()) {
845             operService = operServices.get(0);
846             ctxIn.mergeJson("operational-data", operService.getSvcData());
847         }
848         else {
849             operService = new OperationalServices(svcInstanceId, null, null);
850         }
851
852         // Update service status info in config entry from input
853         configService.setServiceStatusAction(input.getInput().getRequestInformation().getRequestAction().toString());
854         configService.setServiceStatusRpcAction(input.getInput().getSdncRequestHeader().getSvcAction().toString());
855         configService.setServiceStatusRpcName(svcOperation);
856
857
858         // Call DG
859         try {
860             // Any of these can throw a nullpointer exception
861             // execute should only throw a SvcLogicException
862             SvcLogicContext ctxOut = svc.execute(MODULE_NAME, svcOperation, null, "sync", ctxIn);
863             Properties respProps = ctxOut.toProperties();
864
865             resp.setAckFinalIndicator(respProps.getProperty("ack-final-indicator", "Y"));
866             resp.setResponseCode(respProps.getProperty("error-code", "200"));
867             resp.setResponseMessage(respProps.getProperty("error-message", "SUCCESS"));
868
869             configService
870                     .setServiceStatusRequestStatus(GenericResourceApiRequestStatusEnumeration.SYNCCOMPLETE.toString());
871
872             if("200".equals(resp.getResponseCode())) {
873
874                 GenericResourceApiInstanceReference serviceReference = new GenericResourceApiInstanceReference();
875                 serviceReference.setInstanceId(svcInstanceId);
876                 serviceReference.setObjectPath(respProps.getProperty(SERVICE_OBJECT_PATH_PARAM));
877                 resp.setServiceResponseInformation(serviceReference);
878
879                 if(vnfId == null) {
880                     vnfId = respProps.getProperty("vnfId");
881                 }
882                 GenericResourceApiInstanceReference vnfReference = new GenericResourceApiInstanceReference();
883                 vnfReference.setInstanceId(vnfId);
884                 vnfReference.setObjectPath(respProps.getProperty(VNF_OBJECT_PATH_PARAM));
885                 resp.setVnfResponseInformation(vnfReference);
886
887                 // If DG returns success, update svcData in config and operational trees
888                 // and remember to save operational data.
889                 String ctxJson = ctxOut.toJsonString("service-data");
890                 configService.setSvcData(ctxJson);
891                 operService.setSvcData(ctxJson);
892                 saveOperationalData = true;
893             }
894
895         }
896         catch(NullPointerException npe) {
897             resp.setAckFinalIndicator("Y");
898             resp.setResponseCode("500");
899             resp.setResponseMessage("Check that you populated module, rpc and or mode correctly.");
900         }
901         catch(SvcLogicException e) {
902             resp.setAckFinalIndicator("Y");
903             resp.setResponseCode("500");
904             resp.setResponseMessage(e.getMessage());
905         }
906
907         // Update status in config services entry
908
909         configService.setServiceStatusFinalIndicator(resp.getAckFinalIndicator());
910         configService.setServiceStatusResponseCode(resp.getResponseCode());
911         configService.setServiceStatusResponseMessage(resp.getResponseMessage());
912         configService.setServiceStatusResponseTimestamp(Iso8601Util.now());
913
914         // Update config tree
915         configServicesRepository.save(configService);
916
917         // If necessary, sync status to operation service entry and save
918         if(saveOperationalData) {
919             operService.setServiceStatus(configService.getServiceStatus());
920             operationalServicesRepository.save(operService);
921         }
922
923         if(resp.getAckFinalIndicator().equals("N")) {
924             // Spawn background thread to invoke the Async DG
925             Runnable backgroundThread = () -> {
926                 log.info(BACKGROUND_THREAD_STARTED_MESSAGE);
927                 processAsyncVnfTopologyOperation(svcOperation, input);
928             };
929             new Thread(backgroundThread).start();
930         }
931
932         log.info("Returned {} for {} [{}] {}.", resp.getResponseCode(), svcOperation, vnfId, resp.getResponseMessage());
933         retval.setOutput(resp);
934         return (new ResponseEntity<>(retval, HttpStatus.OK));
935     }
936
937     public void processAsyncVnfTopologyOperation(String parentOperation, @Valid GenericResourceApiVnfOperationInformationBodyparam input) {
938         final String svcOperation = "vnf-topology-operation-async";
939         GenericResourceApiVnftopologyoperationOutput resp = new GenericResourceApiVnftopologyoperationOutput();
940
941         log.info(CALLED_STR, svcOperation);
942         // Verify input contains service instance id
943         String svcInstanceId = input.getInput().getServiceInformation().getServiceInstanceId();
944         String vnfId = input.getInput().getVnfInformation().getVnfId();
945
946         SvcLogicContext ctxIn = new SvcLogicContext();
947
948         // Add input to SvcLogicContext
949         try {
950             ctxIn.mergeJson(parentOperation + "-input", objectMapper.writeValueAsString(input.getInput()));
951         } catch (JsonProcessingException e) {
952             log.error("exiting {} due to parse error on input data", svcOperation);
953             return;
954         }
955
956         // Add config tree data to SvcLogicContext
957         List<ConfigServices> configServices = configServicesRepository.findBySvcInstanceId(svcInstanceId);
958         ConfigServices configService = null;
959         if(configServices != null && !configServices.isEmpty()) {
960             configService = configServices.get(0);
961             ctxIn.mergeJson("service-data", configService.getSvcData());
962         }
963         else {
964             log.error("exiting {} because there is no service data with id [{}] in SDN", svcOperation, svcInstanceId);
965             return;
966         }
967
968         // Add operational tree data to SvcLogicContext
969         List<OperationalServices> operServices = operationalServicesRepository.findBySvcInstanceId(svcInstanceId);
970         OperationalServices operService = null;
971
972         if(operServices != null && !operServices.isEmpty()) {
973             operService = operServices.get(0);
974             ctxIn.mergeJson("operational-data", operService.getSvcData());
975         }
976         else {
977             operService = new OperationalServices(svcInstanceId, null, null);
978         }
979
980         // Update service status info in config entry from input
981         configService.setServiceStatusAction(input.getInput().getRequestInformation().getRequestAction().toString());
982         configService.setServiceStatusRpcAction(input.getInput().getSdncRequestHeader().getSvcAction().toString());
983         configService.setServiceStatusRpcName(svcOperation);
984
985
986         // Call DG
987         try {
988             // Any of these can throw a nullpointer exception
989             // execute should only throw a SvcLogicException
990             SvcLogicContext ctxOut = svc.execute(MODULE_NAME, svcOperation, null, "sync", ctxIn);
991             Properties respProps = ctxOut.toProperties();
992
993             resp.setAckFinalIndicator(respProps.getProperty("ack-final-indicator", "Y"));
994             resp.setResponseCode(respProps.getProperty("error-code", "200"));
995             resp.setResponseMessage(respProps.getProperty("error-message", "SUCCESS"));
996
997             configService
998                     .setServiceStatusRequestStatus(GenericResourceApiRequestStatusEnumeration.SYNCCOMPLETE.toString());
999
1000             if("200".equals(resp.getResponseCode())) {
1001                 GenericResourceApiInstanceReference serviceReference = new GenericResourceApiInstanceReference();
1002                 serviceReference.setInstanceId(svcInstanceId);
1003                 serviceReference.setObjectPath(respProps.getProperty(SERVICE_OBJECT_PATH_PARAM));
1004                 resp.setServiceResponseInformation(serviceReference);
1005
1006                 if(vnfId == null) {
1007                     vnfId = respProps.getProperty("vnfId");
1008                 }
1009                 GenericResourceApiInstanceReference vnfReference = new GenericResourceApiInstanceReference();
1010                 vnfReference.setInstanceId(vnfId);
1011                 vnfReference.setObjectPath(respProps.getProperty(VNF_OBJECT_PATH_PARAM));
1012                 resp.setVnfResponseInformation(vnfReference);
1013
1014                 // If DG returns success, update svcData in config and operational trees
1015                 // and remember to save operational data.
1016                 String ctxJson = ctxOut.toJsonString("service-data");
1017                 configService.setSvcData(ctxJson);
1018                 operService.setSvcData(ctxJson);
1019
1020                 // Update status in config services entry
1021
1022                 configService.setServiceStatusFinalIndicator(resp.getAckFinalIndicator());
1023                 configService.setServiceStatusResponseCode(resp.getResponseCode());
1024                 configService.setServiceStatusResponseMessage(resp.getResponseMessage());
1025                 configService.setServiceStatusResponseTimestamp(Iso8601Util.now());
1026
1027                 // Update config and operational tree
1028                 configServicesRepository.save(configService);
1029                 operService.setServiceStatus(configService.getServiceStatus());
1030                 operationalServicesRepository.save(operService);
1031                 log.info("Returned SUCCESS for {} [{}]", svcOperation, vnfId);
1032             }
1033         }
1034         catch(Exception e) {
1035             log.error("Caught Exception updating configuration status in SDN for {} [{}] \n", svcOperation, vnfId);
1036         }
1037     }
1038
1039     @Override
1040     public ResponseEntity<GenericResourceApiVfModuleTopologyOperation> operationsGENERICRESOURCEAPIvfModuleTopologyOperationPost(
1041             @Valid GenericResourceApiVfModuleOperationInformationBodyparam input)
1042             throws RestException {
1043         final String svcOperation = "vf-module-topology-operation";
1044         GenericResourceApiVfModuleTopologyOperation retval = new GenericResourceApiVfModuleTopologyOperation();
1045         GenericResourceApiVfmoduletopologyoperationOutput resp = new GenericResourceApiVfmoduletopologyoperationOutput();
1046
1047         log.info(CALLED_STR, svcOperation);
1048         // Verify input contains service instance id
1049         if (hasInvalidServiceId(input.getInput())) {
1050             log.debug("exiting {} because of null or empty service-instance-id", svcOperation);
1051
1052             resp.setResponseCode("404");
1053             resp.setResponseMessage("null or empty service-instance-id");
1054             resp.setAckFinalIndicator("Y");
1055             retval.setOutput(resp);
1056             return new ResponseEntity<>(retval, HttpStatus.OK);
1057         }
1058
1059         // Verify input contains vnf-id
1060         if (hasInvalidVnfId(input.getInput())) {
1061             log.debug("exiting {} because of null or empty vnf-id", svcOperation);
1062
1063             resp.setResponseCode("404");
1064             resp.setResponseMessage("null or empty vnf-id");
1065             resp.setAckFinalIndicator("Y");
1066             retval.setOutput(resp);
1067             return new ResponseEntity<>(retval, HttpStatus.OK);
1068         }
1069
1070         String svcInstanceId = input.getInput().getServiceInformation().getServiceInstanceId();
1071         String vnfId = input.getInput().getVnfInformation().getVnfId();
1072         String vfModuleId = input.getInput().getVfModuleInformation().getVfModuleId();
1073
1074         SvcLogicContext ctxIn = new SvcLogicContext();
1075
1076         // Add input to SvcLogicContext
1077         try {
1078             ctxIn.mergeJson(svcOperation + "-input", objectMapper.writeValueAsString(input.getInput()));
1079         } catch (JsonProcessingException e) {
1080             log.error("exiting {} due to parse error on input data", svcOperation);
1081             resp.setResponseCode("500");
1082             resp.setResponseMessage("internal error");
1083             resp.setAckFinalIndicator("Y");
1084             retval.setOutput(resp);
1085             return new ResponseEntity<>(retval, HttpStatus.INTERNAL_SERVER_ERROR);
1086         }
1087
1088         // Add config tree data to SvcLogicContext
1089         List<ConfigServices> configServices = configServicesRepository.findBySvcInstanceId(svcInstanceId);
1090         ConfigServices configService = null;
1091         if (configServices != null && !configServices.isEmpty()) {
1092             configService = configServices.get(0);
1093             log.info("Read ({}) data for [{}] service-data: {}",
1094                     "CONFIG_GRA_PORT_MIRROR_CONFIGURATIONS", svcInstanceId, configService.getSvcData().toString());
1095             ctxIn.mergeJson("service-data", configService.getSvcData());
1096         } else {
1097             log.debug("exiting {} because the service-instance does not have any service data in SDN", svcOperation);
1098             resp.setResponseCode("404");
1099             resp.setResponseMessage("invalid input: the service-instance does not have any service data in SDNC");
1100             resp.setAckFinalIndicator("Y");
1101             retval.setOutput(resp);
1102             return new ResponseEntity<>(retval, HttpStatus.OK);
1103         }
1104
1105         // Add operational tree data to SvcLogicContext
1106         List<OperationalServices> operServices = operationalServicesRepository.findBySvcInstanceId(svcInstanceId);
1107         OperationalServices operService = null;
1108
1109         if (operServices != null && !operServices.isEmpty()) {
1110             operService = operServices.get(0);
1111             log.info("Read ({}) data for [{}] operational-data: {}",
1112                     "OPERATIONAL_GRA_PORT_MIRROR_CONFIGURATIONS", svcInstanceId, operService.getSvcData().toString());
1113             //ctxIn.mergeJson("operational-data", operService.getSvcData());
1114         } else {
1115             log.info("No operational-data found in OPERATIONAL_GRA_PORT_MIRROR_CONFIGURATIONS for [{}]", svcInstanceId);
1116             operService = new OperationalServices(svcInstanceId, null, null);
1117         }
1118
1119         // Update service status info in config entry from input
1120         configService.setServiceStatusAction(input.getInput().getRequestInformation().getRequestAction().toString());
1121         configService.setServiceStatusRpcAction(input.getInput().getSdncRequestHeader().getSvcAction().toString());
1122         configService.setServiceStatusRpcName(svcOperation);
1123
1124         String ackFinal = "Y";
1125         String skipMdsalUpdate;
1126
1127         // Call DG
1128         try {
1129             // Any of these can throw a nullpointer exception
1130             // execute should only throw a SvcLogicException
1131             SvcLogicContext ctxOut = svc.execute(MODULE_NAME, svcOperation, null, "sync", ctxIn);
1132             Properties respProps = ctxOut.toProperties();
1133
1134             ackFinal = respProps.getProperty("ack-final", "Y");
1135             skipMdsalUpdate = respProps.getProperty("skip-mdsal-update", "N");
1136             log.info("ackFinal [{}], skipMdsalUpdate [{}]", ackFinal, skipMdsalUpdate);
1137
1138             resp.setAckFinalIndicator(ackFinal);
1139             resp.setResponseCode(respProps.getProperty("error-code", "200"));
1140             resp.setResponseMessage(respProps.getProperty("error-message", "SUCCESS"));
1141
1142             // Update status in config services entry
1143             configService.setServiceStatusFinalIndicator(resp.getAckFinalIndicator());
1144             configService.setServiceStatusResponseCode(resp.getResponseCode());
1145             configService.setServiceStatusResponseMessage(resp.getResponseMessage());
1146             configService.setServiceStatusResponseTimestamp(Iso8601Util.now());
1147             configService
1148                     .setServiceStatusRequestStatus(GenericResourceApiRequestStatusEnumeration.SYNCCOMPLETE.toString());
1149
1150             if ("200".equals(resp.getResponseCode())) {
1151
1152                 GenericResourceApiInstanceReference serviceReference = new GenericResourceApiInstanceReference();
1153                 serviceReference.setInstanceId(svcInstanceId);
1154                 serviceReference.setObjectPath(respProps.getProperty(SERVICE_OBJECT_PATH_PARAM));
1155                 resp.setServiceResponseInformation(serviceReference);
1156
1157                 GenericResourceApiInstanceReference vnfReference = new GenericResourceApiInstanceReference();
1158                 vnfReference.setInstanceId(vnfId);
1159                 vnfReference.setObjectPath(respProps.getProperty(VNF_OBJECT_PATH_PARAM));
1160                 resp.setVnfResponseInformation(vnfReference);
1161
1162                 GenericResourceApiInstanceReference vfModuleReference = new GenericResourceApiInstanceReference();
1163                 vnfReference.setInstanceId(vfModuleId);
1164                 vnfReference.setObjectPath(respProps.getProperty(VF_MODULE_OBJECT_PATH_PARAM));
1165                 resp.setVnfResponseInformation(vfModuleReference);
1166
1167                 if (skipMdsalUpdate.equals("N")) {
1168                     // If DG returns success,
1169                     // ONLY update svcData in config and operational trees
1170                     // and remember to save operational data when skip-mdsal-update is Y in ctx.
1171                     String ctxJson = ctxOut.toJsonString("service-data");
1172                     log.info("Saving service-data in SDN because skiMdsalUpdate is {}", skipMdsalUpdate);
1173                     configService.setSvcData(ctxJson);
1174                     configServicesRepository.save(configService);
1175
1176                     log.info("Copying service-data to operational-data");
1177                     operService.setSvcData(ctxJson);
1178                     operService.setServiceStatus(configService.getServiceStatus());
1179                     operationalServicesRepository.save(operService);
1180                 }
1181             }
1182         } catch (NullPointerException npe) {
1183             resp.setAckFinalIndicator("Y");
1184             resp.setResponseCode("500");
1185             resp.setResponseMessage("Check that you populated module, rpc and or mode correctly.");
1186         } catch (SvcLogicException e) {
1187             resp.setAckFinalIndicator("Y");
1188         }
1189
1190         if (ackFinal.equals("N")) {
1191             // Spawn background thread to invoke the Async DG
1192             Runnable backgroundThread = new Runnable() {
1193                 public void run() {
1194                     log.info(BACKGROUND_THREAD_STARTED_MESSAGE);
1195                     processAsyncVfModuleTopologyOperation(svcOperation, input);
1196                 }
1197             };
1198             new Thread(backgroundThread).start();
1199         }
1200         retval.setOutput(resp);
1201         return (new ResponseEntity<>(retval, HttpStatus.OK));
1202     }
1203
1204     public void processAsyncVfModuleTopologyOperation( String parentOperation,
1205             @Valid GenericResourceApiVfModuleOperationInformationBodyparam input) {
1206         log.info(BACKGROUND_THREAD_INFO, input.getInput().getVfModuleInformation().getVfModuleId());
1207         final String svcOperation = "vf-module-topology-operation-async";
1208
1209         log.info(CALLED_STR, svcOperation);
1210         String svcInstanceId = input.getInput().getServiceInformation().getServiceInstanceId();
1211         SvcLogicContext ctxIn = new SvcLogicContext();
1212
1213         // Add input to SvcLogicContext
1214         try {
1215             ctxIn.mergeJson(parentOperation + "-input", objectMapper.writeValueAsString(input.getInput()));
1216         } catch (JsonProcessingException e) {
1217             log.error("exiting {} due to parse error on input data", svcOperation);
1218             return;
1219         }
1220
1221         // Add config tree data to SvcLogicContext
1222         List<ConfigServices> configServices = configServicesRepository.findBySvcInstanceId(svcInstanceId);
1223         ConfigServices configService = null;
1224         if (configServices != null && !configServices.isEmpty()) {
1225             configService = configServices.get(0);
1226             log.info("Read ({}) data for [{}] service-data: {}",
1227                     "CONFIG_GRA_PORT_MIRROR_CONFIGURATIONS", svcInstanceId, configService.getSvcData().toString());
1228             ctxIn.mergeJson("service-data", configService.getSvcData());
1229         } else {
1230             log.debug("exiting {} because the service-instance does not have any service data in SDN", svcOperation);
1231             return;
1232         }
1233
1234         // Add operational tree data to SvcLogicContext
1235         List<OperationalServices> operServices = operationalServicesRepository.findBySvcInstanceId(svcInstanceId);
1236         OperationalServices operService = null;
1237
1238         if (operServices != null && !operServices.isEmpty()) {
1239             operService = operServices.get(0);
1240             log.info("Read ({}) data for [{}] operational-data: {}",
1241                     "OPERATIONAL_GRA_PORT_MIRROR_CONFIGURATIONS", svcInstanceId, operService.getSvcData().toString());
1242             //ctxIn.mergeJson("operational-data", operService.getSvcData());
1243         } else {
1244             log.info("No operational-data found in OPERATIONAL_GRA_PORT_MIRROR_CONFIGURATIONS for [{}]", svcInstanceId);
1245             operService = new OperationalServices(svcInstanceId, null, null);
1246         }
1247
1248         // Update service status info in config entry from input
1249         configService.setServiceStatusAction(input.getInput().getRequestInformation().getRequestAction().toString());
1250         configService.setServiceStatusRpcAction(input.getInput().getSdncRequestHeader().getSvcAction().toString());
1251         configService.setServiceStatusRpcName(svcOperation);
1252
1253         String respStatus = "SUCCESS";
1254         String errorMessage = null;
1255
1256         log.info ("Adding INPUT data for {} [{}] input: {}", svcOperation, svcInstanceId, input.getInput().toString());
1257
1258         // Call DG
1259         try {
1260             // Any of these can throw a nullpointer exception
1261             // execute should only throw a SvcLogicException
1262             SvcLogicContext ctxOut = svc.execute(MODULE_NAME, svcOperation, null, "sync", ctxIn);
1263             Properties respProps = ctxOut.toProperties();
1264
1265             String ackFinal = respProps.getProperty("ack-final-indicator", "Y");
1266             String errorCode = respProps.getProperty("error-code", "200");
1267             errorMessage = respProps.getProperty("error-message", "SUCCESS");
1268
1269             if (! "200".equals(errorCode)) {
1270                 respStatus = "FAILED";
1271             }
1272
1273             // Update status in config services entry
1274             configService.setServiceStatusFinalIndicator(ackFinal);
1275             configService.setServiceStatusResponseTimestamp(Iso8601Util.now());
1276             configService.setServiceStatusResponseCode(errorCode);
1277             configService.setServiceStatusResponseMessage(errorMessage);
1278             configService
1279                     .setServiceStatusRequestStatus(GenericResourceApiRequestStatusEnumeration.SYNCCOMPLETE.toString());
1280
1281             String ctxJson = ctxOut.toJsonString("service-data");
1282             configServicesRepository.save(configService);
1283
1284             operService.setSvcData(ctxJson);
1285             operService.setServiceStatus(configService.getServiceStatus());
1286             operationalServicesRepository.save(operService);
1287
1288         } catch (Exception ex) {
1289             log.error("Caught Exception updating service status in SDN for {} [{}] \n", svcOperation, svcInstanceId);
1290         }
1291         log.info("Returned {} for {} [{}] {}.", respStatus, svcOperation, svcInstanceId, errorMessage);
1292     }
1293
1294     @Override
1295     public ResponseEntity<GenericResourceApiPortMirrorTopologyOperation> operationsGENERICRESOURCEAPIportMirrorTopologyOperationPost(
1296             @Valid GenericResourceApiPortMirrorTopologyOperationInformationBodyparam input)
1297             throws RestException {
1298         final String svcOperation = "port-mirror-topology-operation";
1299         GenericResourceApiPortMirrorTopologyOperation retval = new GenericResourceApiPortMirrorTopologyOperation();
1300         GenericResourceApiPortmirrortopologyoperationOutput resp = new GenericResourceApiPortmirrortopologyoperationOutput();
1301
1302         log.info(CALLED_STR, svcOperation);
1303
1304         // Verify input contains configuration-id
1305         if (hasInvalidConfigurationId(input.getInput())) {
1306             log.debug("exiting {} because of null or empty configuration-id", svcOperation);
1307
1308             resp.setResponseCode("404");
1309             resp.setResponseMessage("null or empty configuration-id");
1310             resp.setAckFinalIndicator("Y");
1311             retval.setOutput(resp);
1312             return new ResponseEntity<>(retval, HttpStatus.OK);
1313         }
1314
1315         // Verify input contains service instance id
1316         if (hasInvalidServiceId(input.getInput())) {
1317             log.debug("exiting {} because of null or empty service-instance-id", svcOperation);
1318
1319             resp.setResponseCode("404");
1320             resp.setResponseMessage("null or empty service-instance-id");
1321             resp.setAckFinalIndicator("Y");
1322             retval.setOutput(resp);
1323             return new ResponseEntity<>(retval, HttpStatus.OK);
1324         }
1325
1326         String svcInstanceId = input.getInput().getServiceInformation().getServiceInstanceId();
1327         String configurationId = input.getInput().getConfigurationInformation().getConfigurationId();
1328
1329         SvcLogicContext ctxIn = new SvcLogicContext();
1330
1331         // Add input to SvcLogicContext
1332         try {
1333             ctxIn.mergeJson(svcOperation + "-input", objectMapper.writeValueAsString(input.getInput()));
1334         } catch (JsonProcessingException e) {
1335             log.error("exiting {} due to parse error on input data", svcOperation);
1336             resp.setResponseCode("500");
1337             resp.setResponseMessage("internal error");
1338             resp.setAckFinalIndicator("Y");
1339             retval.setOutput(resp);
1340             return new ResponseEntity<>(retval, HttpStatus.INTERNAL_SERVER_ERROR);
1341         }
1342
1343         // Add service instance config data to SvcLogicContext
1344         List<ConfigServices> configServices = configServicesRepository.findBySvcInstanceId(svcInstanceId);
1345         ConfigServices configService = null;
1346         if (configServices != null && !configServices.isEmpty()) {
1347             configService = configServices.get(0);
1348             log.info("Read ({}) data for [{}] service-data: {}", "CONFIG_GRA_SERVICES", svcInstanceId, configService.getSvcData());
1349             ctxIn.mergeJson("service-data", configService.getSvcData());
1350
1351         } else {
1352             log.debug("exiting {} because there is no service data with id [{}] in SDN", svcOperation, svcInstanceId);
1353             resp.setResponseCode("404");
1354             resp.setResponseMessage("invalid input: there is no service-instance with id [{}] in SDNC");
1355             resp.setAckFinalIndicator("Y");
1356
1357             retval.setOutput(resp);
1358             return new ResponseEntity<>(retval, HttpStatus.OK);
1359         }
1360
1361         // Add configuration config data to SvcLogicContext
1362         List<ConfigPortMirrorConfigurations> configPortMirrorConfigurations =
1363                 configPortMirrorConfigurationsRepository.findByConfigurationId(configurationId);
1364         ConfigPortMirrorConfigurations configPortMirrorConfiguration;
1365         if (configPortMirrorConfigurations != null && !configPortMirrorConfigurations.isEmpty()) {
1366             configPortMirrorConfiguration = configPortMirrorConfigurations.get(0);
1367             log.info("Read ({}) data for [{}] configuration-data: {}",
1368                     "CONFIG_GRA_PORT_MIRROR_CONFIGURATIONS", configurationId, configPortMirrorConfiguration.getPmcData());
1369             ctxIn.mergeJson("configuration-data", configPortMirrorConfiguration.getPmcData());
1370
1371         } else {
1372             log.info("No configuration-data found ({}) for [{}]", "CONFIG_GRA_PORT_MIRROR_CONFIGURATIONS", configurationId);
1373             configPortMirrorConfiguration = new ConfigPortMirrorConfigurations(configurationId, null);
1374         }
1375
1376         /*
1377         // Add configuration operational data to SvcLogicContext
1378         List<OperationalPortMirrorConfigurations> operPortMirrorConfigurations =
1379                 operationalPortMirrorConfigurationsRepository.findByConfigurationId(configurationId);
1380         OperationalPortMirrorConfigurations operPortMirrorConfiguration;
1381
1382         if (operPortMirrorConfigurations != null && !operPortMirrorConfigurations.isEmpty()) {
1383             operPortMirrorConfiguration = operPortMirrorConfigurations.get(0);
1384             log.info("Read ({}) data for [{}] operational-data: {}",
1385                     "OPERATIONAL_GRA_PORT_MIRROR_CONFIGURATIONS", configurationId, operPortMirrorConfiguration.getPmcData());
1386             ctxIn.mergeJson("operational-data", operPortMirrorConfiguration.getPmcData());
1387
1388         } else {
1389             log.info("No operational-data found ({}) for [{}]", "OPERATIONAL_GRA_PORT_MIRROR_CONFIGURATIONS", configurationId);
1390             operPortMirrorConfiguration = new OperationalPortMirrorConfigurations(configurationId, null, null);
1391         }
1392
1393         */
1394
1395         String ackFinal = "Y";
1396         String errorCode = "200";
1397         String errorMessage = "SUCCESS";
1398         String respStatus = "SUCCESS";
1399
1400         log.info(ADDING_INPUT_DATA_LOG, svcOperation, svcInstanceId, input.getInput().toString());
1401         //log.info(ADDING_INPUT_DATA_LOG, svcOperation, svcInstanceId, input.toString());
1402
1403         // Update service status info in config entry from input
1404         configPortMirrorConfiguration.setPortMirrorConfigurationStatusAction(input.getInput().getRequestInformation().getRequestAction().toString());
1405         configPortMirrorConfiguration.setPortMirrorConfigurationStatusRpcAction(input.getInput().getSdncRequestHeader().getSvcAction().toString());
1406         configPortMirrorConfiguration.setPortMirrorConfigurationStatusRpcName(svcOperation);
1407
1408         // Call DG
1409         try {
1410             // Any of these can throw a nullpointer exception
1411             // execute should only throw a SvcLogicException
1412             SvcLogicContext ctxOut = svc.execute(MODULE_NAME, svcOperation, null, "sync", ctxIn);
1413             Properties respProps = ctxOut.toProperties();
1414
1415             ackFinal = respProps.getProperty("ack-final", "Y");
1416             errorCode = respProps.getProperty("error-code", "200");
1417             errorMessage = respProps.getProperty("error-message", "SUCCESS");
1418             log.info("ackFinal [{}], error-code [{}], error-message [{}]", ackFinal, errorCode, errorMessage);
1419
1420             resp.setAckFinalIndicator(ackFinal);
1421             resp.setResponseCode(errorCode);
1422             resp.setResponseMessage(errorMessage);
1423
1424             // Update status in config services entry
1425             configPortMirrorConfiguration
1426                     .setPortMirrorConfigurationStatusRequestStatus(GenericResourceApiRequestStatusEnumeration.SYNCCOMPLETE.toString());
1427             configPortMirrorConfiguration.setPortMirrorConfigurationStatusFinalIndicator(resp.getAckFinalIndicator());
1428             configPortMirrorConfiguration.setPortMirrorConfigurationStatusResponseCode(resp.getResponseCode());
1429             configPortMirrorConfiguration.setPortMirrorConfigurationStatusResponseMessage(resp.getResponseMessage());
1430             configPortMirrorConfiguration.setPortMirrorConfigurationStatusResponseTimestamp(Iso8601Util.now());
1431
1432             GenericResourceApiInstanceReference serviceReference = new GenericResourceApiInstanceReference();
1433             serviceReference.setInstanceId(svcInstanceId);
1434             serviceReference.setObjectPath(respProps.getProperty(SERVICE_OBJECT_PATH_PARAM));
1435             resp.setServiceResponseInformation(serviceReference);
1436
1437             GenericResourceApiInstanceReference pmcReference = new GenericResourceApiInstanceReference();
1438             serviceReference.setInstanceId(configurationId);
1439             serviceReference.setObjectPath(respProps.getProperty(PORT_MIRROR_OBJECT_PATH_PARAM));
1440             resp.setServiceResponseInformation(pmcReference);
1441
1442             retval.setOutput(resp);
1443
1444             // ONLY update pmcData in config and operational trees
1445             //String ctxJson = ctxOut.toJsonString("configuration-data");
1446             //configPortMirrorConfiguration.setPmcData(ctxJson);
1447             //operPortMirrorConfiguration.setPmcData(ctxJson);
1448             //configPortMirrorConfigurationsRepository.save(configPortMirrorConfiguration);
1449
1450             // If necessary, sync status to operation service entry and save
1451             //operPortMirrorConfiguration.setPortMirrorConfigurationStatus(configPortMirrorConfiguration.getPortMirrorConfigurationStatus());
1452             //operationalPortMirrorConfigurationsRepository.save(operPortMirrorConfiguration);
1453
1454             if (! "200".equals(errorCode)) {
1455                 respStatus = "FAILED";
1456             }
1457
1458         } catch (NullPointerException npe) {
1459             resp.setAckFinalIndicator("Y");
1460             resp.setResponseCode("500");
1461             resp.setResponseMessage("Check that you populated module, rpc and or mode correctly.");
1462         } catch (SvcLogicException e) {
1463             resp.setAckFinalIndicator("Y");
1464         }
1465
1466         if (ackFinal.equals("N")) {
1467             // Spawn background thread to invoke the Async DG
1468             Runnable backgroundThread = new Runnable() {
1469                 public void run() {
1470                     log.info(BACKGROUND_THREAD_STARTED_MESSAGE);
1471                     processAsyncPortMirrorTopologyOperation(svcOperation, input);
1472                  }
1473             };
1474             new Thread(backgroundThread).start();
1475         }
1476         log.info("Returned {} for {} [{}] {}.", respStatus, svcOperation, configurationId, errorMessage);
1477         return (new ResponseEntity<>(retval, HttpStatus.OK));
1478     }
1479
1480     public void processAsyncPortMirrorTopologyOperation( String parentOperation,
1481              @Valid GenericResourceApiPortMirrorTopologyOperationInformationBodyparam input) {
1482         log.info(BACKGROUND_THREAD_INFO, input.getInput().getConfigurationInformation().getConfigurationId());
1483         final String svcOperation = "port-mirror-topology-operation-async";
1484
1485         log.info(CALLED_STR, svcOperation);
1486         String svcInstanceId = input.getInput().getServiceInformation().getServiceInstanceId();
1487         String configurationId = input.getInput().getConfigurationInformation().getConfigurationId();
1488         SvcLogicContext ctxIn = new SvcLogicContext();
1489
1490         String errorMessage;
1491
1492         // Add input to SvcLogicContext
1493         try {
1494             ctxIn.mergeJson(parentOperation + "-input", objectMapper.writeValueAsString(input.getInput()));
1495         } catch (JsonProcessingException e) {
1496             log.error("exiting {} due to parse error on input data", svcOperation);
1497             return;
1498         }
1499
1500         // Add service instance config data to SvcLogicContext
1501         List<ConfigServices> configServices = configServicesRepository.findBySvcInstanceId(svcInstanceId);
1502         ConfigServices configService = null;
1503         if (configServices != null && !configServices.isEmpty()) {
1504             configService = configServices.get(0);
1505             ctxIn.mergeJson("service-data", configService.getSvcData());
1506         } else {
1507             log.error("exiting {} because there is no service data with id [{}] in SDN", svcOperation, svcInstanceId);
1508             return;
1509         }
1510
1511         // Add config tree data to SvcLogicContext
1512         List<ConfigPortMirrorConfigurations> configPortMirrorConfigurations = configPortMirrorConfigurationsRepository.findByConfigurationId(configurationId);
1513         ConfigPortMirrorConfigurations configPortMirrorConfiguration;
1514         if (configPortMirrorConfigurations != null && !configPortMirrorConfigurations.isEmpty()) {
1515             configPortMirrorConfiguration = configPortMirrorConfigurations.get(0);
1516             ctxIn.mergeJson("configuration-data", configPortMirrorConfiguration.getPmcData());
1517         } else {
1518             configPortMirrorConfiguration = new ConfigPortMirrorConfigurations(configurationId, null);
1519         }
1520
1521         /*
1522         // Add operational tree data to SvcLogicContext
1523         List<OperationalPortMirrorConfigurations> operPortMirrorConfigurations = operationalPortMirrorConfigurationsRepository.findByConfigurationId(configurationId);
1524         OperationalPortMirrorConfigurations operPortMirrorConfiguration = null;
1525
1526         if (operPortMirrorConfigurations != null && !operPortMirrorConfigurations.isEmpty()) {
1527             operPortMirrorConfiguration = operPortMirrorConfigurations.get(0);
1528             ctxIn.mergeJson("operational-data", operPortMirrorConfiguration.getPmcData());
1529         } else {
1530             operPortMirrorConfiguration = new OperationalPortMirrorConfigurations(configurationId, null, null);
1531         }
1532
1533         */
1534
1535         // Update service status info in config entry from input
1536         configPortMirrorConfiguration.setPortMirrorConfigurationStatusAction(input.getInput().getRequestInformation().getRequestAction().toString());
1537         configPortMirrorConfiguration.setPortMirrorConfigurationStatusRpcAction(input.getInput().getSdncRequestHeader().getSvcAction().toString());
1538         configPortMirrorConfiguration.setPortMirrorConfigurationStatusRpcName(parentOperation);
1539
1540         log.info("Adding INPUT data for {} [{}] input: {}", svcOperation, svcInstanceId, input.toString());
1541
1542         // Call DG
1543         try {
1544             // Any of these can throw a nullpointer exception
1545             // execute should only throw a SvcLogicException
1546             SvcLogicContext ctxOut = svc.execute(MODULE_NAME, svcOperation, null, "sync", ctxIn);
1547             Properties respProps = ctxOut.toProperties();
1548
1549             String ackFinalIndicator = respProps.getProperty("ack-final-indicator", "Y");
1550             String errorCode = respProps.getProperty("error-code", "200");
1551             errorMessage = respProps.getProperty("error-message", "SUCCESS");
1552
1553             // Update status in config services entry
1554             configPortMirrorConfiguration.setPortMirrorConfigurationStatusFinalIndicator(ackFinalIndicator);
1555             configPortMirrorConfiguration.setPortMirrorConfigurationStatusResponseTimestamp(Iso8601Util.now());
1556             configPortMirrorConfiguration.setPortMirrorConfigurationStatusResponseCode(errorCode);
1557             configPortMirrorConfiguration.setPortMirrorConfigurationStatusResponseMessage(errorMessage);
1558             configPortMirrorConfiguration
1559                     .setPortMirrorConfigurationStatusRequestStatus(GenericResourceApiRequestStatusEnumeration.SYNCCOMPLETE.toString());
1560
1561             // ONLY update status
1562             //String ctxJson = ctxOut.toJsonString("configuration-data");
1563             //configPortMirrorConfiguration.setPmcData(ctxJson);
1564             //configPortMirrorConfiguration.setPmcData(ctxJson);
1565
1566             // Update config tree
1567             configPortMirrorConfigurationsRepository.save(configPortMirrorConfiguration);
1568
1569             //update operational tree
1570             //operPortMirrorConfiguration.setPortMirrorConfigurationStatus(configPortMirrorConfiguration.getPortMirrorConfigurationStatus());
1571             //operationalPortMirrorConfigurationsRepository.save(operPortMirrorConfiguration);
1572
1573         } catch (Exception e) {
1574             log.error("Caught Exception updating configuration status in SDN for {} [{}] \n", svcOperation, configurationId);
1575         }
1576         log.info("Returned SUCCESS for {} [{}]", svcOperation, configurationId);
1577     }
1578
1579     @Override
1580     public ResponseEntity<GenericResourceApiVnfGetResourceRequest> operationsGENERICRESOURCEAPIvnfGetResourceRequestPost(
1581             @Valid GenericResourceApiVnfgetresourcerequestInputBodyparam input)
1582             throws RestException {
1583         final String svcOperation = "vnf-get-resource-request";
1584         GenericResourceApiVnfGetResourceRequest retval = new GenericResourceApiVnfGetResourceRequest();
1585
1586         log.info(CALLED_STR, svcOperation);
1587         // Verify input contains service instance id
1588         if (hasInvalidServiceId(input.getInput())) {
1589             log.debug("exiting {} because of null or empty service-instance-id", svcOperation);
1590             //return new ResponseEntity<>(retval, HttpStatus.OK);
1591         }
1592
1593         String svcInstanceId = input.getInput().getServiceInformation().getServiceInstanceId();
1594         SvcLogicContext ctxIn = new SvcLogicContext();
1595
1596         // Add input to SvcLogicContext
1597         try {
1598             ctxIn.mergeJson(svcOperation + "-input", objectMapper.writeValueAsString(input.getInput()));
1599         } catch (JsonProcessingException e) {
1600             log.error("exiting {} due to parse error on input data", svcOperation);
1601             return new ResponseEntity<>(retval, HttpStatus.INTERNAL_SERVER_ERROR);
1602         }
1603
1604         // Add config tree data to SvcLogicContext
1605         List<ConfigServices> configServices = configServicesRepository.findBySvcInstanceId(svcInstanceId);
1606         ConfigServices configService = null;
1607         if (configServices != null && !configServices.isEmpty()) {
1608             configService = configServices.get(0);
1609             ctxIn.mergeJson("service-data", configService.getSvcData());
1610         } else {
1611             log.debug("exiting {} because the service-instance does not have any service data in SDN", svcOperation);
1612             return new ResponseEntity<>(retval, HttpStatus.OK);
1613         }
1614
1615         // Call DG
1616         try {
1617             // Any of these can throw a nullpointer exception
1618             // execute should only throw a SvcLogicException
1619             SvcLogicContext ctxOut = svc.execute(MODULE_NAME, svcOperation, null, "sync", ctxIn);
1620             Properties respProps = ctxOut.toProperties();
1621
1622             /* For debugging Only
1623             respProps.forEach((k,v) -> {
1624                 log.debug("prop: {} -> {}",k,v);
1625             });
1626             */
1627
1628             String ctxJson = ctxOut.toJsonString("vnf-get-resource-request-output");
1629             GenericResourceApiVnfgetresourcerequestOutput vnfgetresourcerequestOutput =
1630                     objectMapper.readValue(ctxJson, GenericResourceApiVnfgetresourcerequestOutput.class);
1631
1632             retval.setOutput(vnfgetresourcerequestOutput);
1633
1634         } catch (Exception e) {
1635             return (new ResponseEntity<>(retval, HttpStatus.INTERNAL_SERVER_ERROR));
1636         }
1637         return (new ResponseEntity<>(retval, HttpStatus.OK));
1638     }
1639
1640     @Override
1641     public ResponseEntity<GenericResourceApiPolicyUpdateNotifyOperation> operationsGENERICRESOURCEAPIpolicyUpdateNotifyOperationPost(
1642             @Valid GenericResourceApiPolicyupdatenotifyoperationInputBodyparam input)
1643             throws RestException {
1644         final String svcOperation = "policy-update-notify-operation";
1645         GenericResourceApiPolicyUpdateNotifyOperation retval = new GenericResourceApiPolicyUpdateNotifyOperation();
1646         GenericResourceApiPolicyupdatenotifyoperationOutput resp = new GenericResourceApiPolicyupdatenotifyoperationOutput();
1647
1648         log.info(CALLED_STR, svcOperation);
1649         // Verify required input elements
1650         if (hasInvalidPolicyUpdateInput(input.getInput())) {
1651             log.debug("exiting {} because policy name, update type, or version id was not provided", svcOperation);
1652             resp.setErrorCode("404");
1653             resp.setErrorMsg("policy-name, update-type, and/or version-id is null or empty");
1654             retval.setOutput(resp);
1655             return new ResponseEntity<>(retval, HttpStatus.OK);
1656         }
1657
1658         SvcLogicContext ctxIn = new SvcLogicContext();
1659
1660         // Add input to SvcLogicContext
1661         try {
1662             ctxIn.mergeJson(svcOperation + "-input", objectMapper.writeValueAsString(input.getInput()));
1663         } catch (JsonProcessingException e) {
1664             log.error("exiting {} due to parse error on input data", svcOperation);
1665             resp.setErrorCode("500");
1666             resp.setErrorMsg("internal error");
1667             retval.setOutput(resp);
1668             return new ResponseEntity<>(retval, HttpStatus.INTERNAL_SERVER_ERROR);
1669         }
1670
1671         // Call DG
1672         try {
1673             // Any of these can throw a nullpointer exception
1674             // execute should only throw a SvcLogicException
1675             SvcLogicContext ctxOut = svc.execute(MODULE_NAME, svcOperation, null, "sync", ctxIn);
1676             Properties respProps = ctxOut.toProperties();
1677             resp.setErrorCode(respProps.getProperty("error-code", "200"));
1678             resp.setErrorMsg(respProps.getProperty("error-message", "SUCCESS"));
1679
1680             /* For debugging Only
1681             respProps.forEach((k,v) -> {
1682                 log.debug("prop: {} -> {}",k,v);
1683             });
1684              */
1685
1686         } catch (NullPointerException npe) {
1687             resp.setErrorCode("500");
1688             resp.setErrorMsg("Check that you populated module, rpc and or mode correctly.");
1689         } catch (SvcLogicException e) {
1690             resp.setErrorCode("500");
1691             resp.setErrorMsg(e.getMessage());
1692         }
1693
1694         retval.setOutput(resp);
1695         return (new ResponseEntity<>(retval, HttpStatus.OK));
1696     }
1697
1698 }