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