2cef1a0ec186181bacb08c9d2ede567a4a76c854
[sdnc/apps.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SDNC
4  * ================================================================================
5  * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.sdnc.apps.ms.gra.controllers;
22
23 import com.fasterxml.jackson.annotation.JsonInclude;
24 import com.fasterxml.jackson.core.JsonProcessingException;
25 import com.fasterxml.jackson.databind.JsonMappingException;
26 import com.fasterxml.jackson.databind.ObjectMapper;
27 import org.onap.ccsdk.apps.services.RestApplicationException;
28 import org.onap.ccsdk.apps.services.RestException;
29 import org.onap.ccsdk.apps.services.RestProtocolError;
30 import org.onap.ccsdk.apps.services.RestProtocolException;
31 import org.onap.sdnc.apps.ms.gra.data.ConfigPreloadData;
32 import org.onap.sdnc.apps.ms.gra.data.ConfigPreloadDataRepository;
33 import org.onap.sdnc.apps.ms.gra.data.ConfigServices;
34 import org.onap.sdnc.apps.ms.gra.data.ConfigServicesRepository;
35 import org.onap.sdnc.apps.ms.gra.data.ConfigPortMirrorConfigurations;
36 import org.onap.sdnc.apps.ms.gra.data.ConfigPortMirrorConfigurationsRepository;
37 import org.onap.sdnc.apps.ms.gra.data.ConfigContrailRouteAllottedResources;
38 import org.onap.sdnc.apps.ms.gra.data.ConfigContrailRouteAllottedResourcesRepository;
39 import org.onap.sdnc.apps.ms.gra.swagger.ConfigApi;
40 import org.onap.sdnc.apps.ms.gra.swagger.model.*;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.boot.autoconfigure.domain.EntityScan;
45 import org.springframework.context.annotation.ComponentScan;
46 import org.springframework.http.HttpStatus;
47 import org.springframework.http.ResponseEntity;
48 import org.springframework.stereotype.Controller;
49
50 import javax.servlet.http.HttpServletRequest;
51 import javax.validation.Valid;
52 import java.util.ArrayList;
53 import java.util.Iterator;
54 import java.util.LinkedList;
55 import java.util.List;
56 import java.util.Optional;
57 import java.util.concurrent.atomic.AtomicBoolean;
58 import java.util.stream.Collectors;
59 import java.util.stream.Stream;
60
61 @Controller
62 @ComponentScan(basePackages = { "org.onap.sdnc.apps.ms.gra.*" })
63 @EntityScan("org.onap.sdnc.apps.ms.gra.springboot.*")
64 public class ConfigApiController implements ConfigApi {
65     private static final Logger log = LoggerFactory.getLogger(ConfigApiController.class);
66
67     private final ObjectMapper objectMapper;
68
69     private final HttpServletRequest request;
70
71     @Autowired
72     private ConfigPreloadDataRepository configPreloadDataRepository;
73
74     @Autowired
75     private ConfigServicesRepository configServicesRepository;
76
77     @Autowired
78     private ConfigPortMirrorConfigurationsRepository configPortMirrorConfigurationsRepository;
79
80     @Autowired
81     private ConfigContrailRouteAllottedResourcesRepository configContrailRouteAllottedResourcesRepository;
82
83
84     @Autowired
85     public ConfigApiController(ObjectMapper objectMapper, HttpServletRequest request) {
86         objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
87         objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
88         this.objectMapper = objectMapper;
89         this.request = request;
90     }
91
92     @Override
93     public Optional<ObjectMapper> getObjectMapper() {
94         return Optional.ofNullable(objectMapper);
95     }
96
97     @Override
98     public Optional<HttpServletRequest> getRequest() {
99         return Optional.ofNullable(request);
100     }
101
102     @Override
103     public ResponseEntity<Void> configGENERICRESOURCEAPIpreloadInformationDelete() {
104         configPreloadDataRepository.deleteAll();
105         return (new ResponseEntity<>(HttpStatus.NO_CONTENT));
106     }
107
108     /**
109      * Extracts port-mirror configuration data from CONFIG_GRA_PORT_MIRROR_CONFIGURATIONS for a given, configuration-id
110      * <p>
111      * Maps to /config/GENERIC-RESOURCE-API:port-mirror-configurations/port-mirror-configuration/{configuration-id}/
112      * @param configurationId the configuration ID for a port-mirror
113      * @return HttpStatus.OK (200) if the data is found.
114      * @throws RestException if the data does not exist.
115      */
116     public ResponseEntity<GenericResourceApiPortmirrorconfigurationsPortMirrorConfiguration>
117                 configGENERICRESOURCEAPIportMirrorConfigurationsPortMirrorConfigurationConfigurationIdGet(
118                     String configurationId) throws RestApplicationException {
119         GenericResourceApiPortmirrorconfigurationsPortMirrorConfiguration retval = null;
120
121         List<ConfigPortMirrorConfigurations> pmConfigurations = configPortMirrorConfigurationsRepository.findByConfigurationId(configurationId);
122
123         if (pmConfigurations.isEmpty()) {
124             log.info("No configuration data found with id [{}]",configurationId);
125             throw new RestApplicationException("data-missing",
126                     "Request could not be completed because the relevant data model content does not exist",
127                     HttpStatus.NOT_FOUND.value());
128         } else {
129             ConfigPortMirrorConfigurations pmConfiguration = pmConfigurations.get(0);
130             retval = new GenericResourceApiPortmirrorconfigurationsPortMirrorConfiguration();
131             retval.setConfigurationId(configurationId);
132             retval.setConfigurationStatus(pmConfiguration.getPortMirrorConfigurationStatus());
133             try {
134                 retval.setConfigurationData(objectMapper.readValue(pmConfiguration.getPmcData(), GenericResourceApiPortmirrorconfigurationsPortmirrorconfigurationConfigurationData.class));
135             } catch (JsonProcessingException e) {
136                 log.error("Could not deserialize service data for service instance id {}", configurationId, e);
137                 throw new RestApplicationException("data-conversion", "Request could not be completed due to internal error", e, HttpStatus.INTERNAL_SERVER_ERROR.value());
138             }
139         }
140         return new ResponseEntity<>(retval, HttpStatus.OK);
141     }
142
143     @Override
144     public ResponseEntity<Void> configGENERICRESOURCEAPIportMirrorConfigurationsPortMirrorConfigurationConfigurationIdPut(
145             String configurationId, @Valid GenericResourceApiPortmirrorconfigurationsPortMirrorConfiguration newConfiguration)
146                 throws RestApplicationException {
147
148         boolean dataExists = false;
149
150         String newConfigurationId = newConfiguration.getConfigurationId();
151
152         ConfigPortMirrorConfigurations portMirrorConfiguration = null;
153         List<ConfigPortMirrorConfigurations> existingConfiguration = configPortMirrorConfigurationsRepository.findByConfigurationId(configurationId);
154         if ((existingConfiguration != null) && !existingConfiguration.isEmpty()) {
155             dataExists = true;
156             portMirrorConfiguration = existingConfiguration.get(0);
157         } else {
158             portMirrorConfiguration = new ConfigPortMirrorConfigurations();
159             portMirrorConfiguration.setConfigureationId(configurationId);
160         }
161
162         try {
163             portMirrorConfiguration.setPmcData(objectMapper.writeValueAsString(newConfiguration.getConfigurationData()));
164         } catch (JsonProcessingException e) {
165             log.error("Could not serialize porr-mirror configuration data for {}", portMirrorConfiguration.getConfigureationId(), e);
166             throw new RestApplicationException("data-conversion", "Request could not be completed due to internal error", e, HttpStatus.INTERNAL_SERVER_ERROR.value());
167
168         }
169         portMirrorConfiguration.setPortMirrorConfigurationStatus(newConfiguration.getConfigurationStatus());
170         configPortMirrorConfigurationsRepository.save(portMirrorConfiguration);
171
172         if (dataExists) {
173             return new ResponseEntity<>(HttpStatus.NO_CONTENT);
174         } else {
175             return new ResponseEntity<>(HttpStatus.CREATED);
176         }
177     }
178
179     @Override
180     public ResponseEntity<GenericResourceApiPortmirrorconfigurationtopologyPortMirrorConfigurationTopology>
181             configGENERICRESOURCEAPIportMirrorConfigurationsPortMirrorConfigurationConfigurationIdConfigurationDataPortMirrorConfigurationTopologyGet(
182                     String configurationId) throws RestApplicationException, RestProtocolException {
183         @Valid GenericResourceApiPortmirrorconfigurationtopologyPortMirrorConfigurationTopology portMirrorConfigurationTopology = null;
184         GenericResourceApiPortmirrorconfigurationsPortmirrorconfigurationConfigurationData portMirrorConfigurationData = null;
185
186         List<ConfigPortMirrorConfigurations> configPortMirrorConfigurations = configPortMirrorConfigurationsRepository.findByConfigurationId(configurationId);
187         if ((configPortMirrorConfigurations == null) || (configPortMirrorConfigurations.isEmpty())) {
188             log.info("No configuration data found with id [{}]", configurationId);
189             throw new RestApplicationException("data-missing",
190                     "Request could not be completed because the relevant data model content does not exist",
191                     HttpStatus.NOT_FOUND.value());
192         }
193
194         try {
195             if ( configPortMirrorConfigurations.get(0).getPmcData().isEmpty()) {
196                 throw new RestProtocolException("data-missing", "No configuration-data entry found", HttpStatus.NOT_FOUND.value());
197             } else {
198                 portMirrorConfigurationData = objectMapper.readValue(configPortMirrorConfigurations.get(0).getPmcData(), GenericResourceApiPortmirrorconfigurationsPortmirrorconfigurationConfigurationData.class);
199                 portMirrorConfigurationTopology = portMirrorConfigurationData.getPortMirrorConfigurationTopology();
200             }
201             if (portMirrorConfigurationTopology == null) {
202                 throw new RestProtocolException("data-missing", "No service-topology entry found", HttpStatus.NOT_FOUND.value());
203             }
204             return new ResponseEntity<>(portMirrorConfigurationTopology, HttpStatus.OK);
205         } catch (JsonProcessingException e) {
206             log.error("Could not parse service data", e);
207             throw new RestApplicationException("data-conversion", "Request could not be completed due to internal error", e, HttpStatus.INTERNAL_SERVER_ERROR.value());
208         }
209     }
210
211
212     @Override
213     public ResponseEntity<Void> configGENERICRESOURCEAPIportMirrorConfigurationsPortMirrorConfigurationConfigurationIdDelete(String configurationId) {
214         configPortMirrorConfigurationsRepository.deleteByConfigurationId(configurationId);
215         return new ResponseEntity<>(HttpStatus.NO_CONTENT);
216     }
217
218     /**
219      * Extracts contrail-route-allotted-resource data from CONFIG_GRA_CONTRAIL_ROUTE_ALLOTTED_RESOURCES for a given allottedResourceId
220      * <p>
221      * Maps to /config/GENERIC-RESOURCE-API:contrail-route-allotted-resources/contrail-route-allotted-resource/{allotted-resource-id}
222      * @param allottedResourceId the allotted-resource-id for a contrail-route
223      * @return HttpStatus.OK (200) if the data is found.
224      * @throws RestException if the data does not exist.
225      */
226     public ResponseEntity<GenericResourceApiContrailrouteallottedresourcesContrailRouteAllottedResource>
227                 configGENERICRESOURCEAPIcontrailRouteAllottedResourcesContrailRouteAllottedResourceAllottedResourceIdGet(
228                         String allottedResourceId) throws RestApplicationException {
229         GenericResourceApiContrailrouteallottedresourcesContrailRouteAllottedResource retval = null;
230
231         List<ConfigContrailRouteAllottedResources> allottedResources = configContrailRouteAllottedResourcesRepository.findByAllottedResourceId(allottedResourceId);
232
233         if (allottedResources.isEmpty()) {
234             log.info("No contrail-route-allotted-resource found with id [{}]", allottedResourceId);
235             throw new RestApplicationException("data-missing",
236                     "Request could not be completed because the relevant data model content does not exist",
237                     HttpStatus.NOT_FOUND.value());
238         }
239         else {
240             ConfigContrailRouteAllottedResources allottedResource = allottedResources.get(0);
241             retval = new GenericResourceApiContrailrouteallottedresourcesContrailRouteAllottedResource();
242             retval.setAllottedResourceId(allottedResourceId);
243             retval.setAllottedResourceStatus(allottedResource.getAllottedResourceStatus());
244             try {
245                 retval.setAllottedResourceData(objectMapper.readValue(allottedResource.getArData(),
246                         GenericResourceApiContrailrouteallottedresourcesContrailrouteallottedresourceAllottedResourceData.class));
247             } catch (JsonProcessingException e) {
248                 log.error("Could not deserialize service data for service instance id {}", allottedResourceId, e);
249                 throw new RestApplicationException("data-conversion", "Request could not be completed due to internal error", e, HttpStatus.INTERNAL_SERVER_ERROR.value());
250             }
251         }
252         return new ResponseEntity<>(retval, HttpStatus.OK);
253     }
254
255     /**
256      * PUT contrail-route-allotted-resource data from CONFIG_GRA_CONTRAIL_ROUTE_ALLOTTED_RESOURCES for a given allottedResourceId
257      * <p>
258      * Maps to /config/GENERIC-RESOURCE-API:contrail-route-allotted-resources/contrail-route-allotted-resource/{allotted-resource-id}
259      * @param allottedResourceId the allotted-resource-id for a contrail-route
260      * @return HttpStatus.OK (200) if the data is found.
261      * @throws RestException if the data does not exist.
262      */
263     @Override
264     public ResponseEntity<Void> configGENERICRESOURCEAPIcontrailRouteAllottedResourcesContrailRouteAllottedResourceAllottedResourceIdPut(
265             String allottedResourceId, @Valid GenericResourceApiContrailrouteallottedresourcesContrailRouteAllottedResource newAllottedResource)
266             throws RestApplicationException {
267
268         boolean dataExists = false;
269
270         String newAllottedResourceId = newAllottedResource.getAllottedResourceId();
271
272         ConfigContrailRouteAllottedResources allottedResource = null;
273         List<ConfigContrailRouteAllottedResources> existingAllottedResource =
274                 configContrailRouteAllottedResourcesRepository.findByAllottedResourceId(allottedResourceId);
275
276         if ((existingAllottedResource != null) && !existingAllottedResource.isEmpty()) {
277             dataExists = true;
278             allottedResource = existingAllottedResource.get(0);
279         } else {
280             allottedResource = new ConfigContrailRouteAllottedResources();
281             allottedResource.setAllottedResourceId(allottedResourceId);
282         }
283
284         try {
285             allottedResource.setArData(objectMapper.writeValueAsString(newAllottedResource.getAllottedResourceData()));
286         } catch (JsonProcessingException e) {
287             log.error("Could not serialize porr-mirror configuration data for {}", allottedResource.getAllottedResourceId(), e);
288             throw new RestApplicationException("data-conversion", "Request could not be completed due to internal error", e, HttpStatus.INTERNAL_SERVER_ERROR.value());
289
290         }
291         allottedResource.setAllottedResourceStatus(newAllottedResource.getAllottedResourceStatus());
292         configContrailRouteAllottedResourcesRepository.save(allottedResource);
293
294         if (dataExists) {
295             return new ResponseEntity<>(HttpStatus.NO_CONTENT);
296         } else {
297             return new ResponseEntity<>(HttpStatus.CREATED);
298         }
299     }
300
301     /**
302      * Extracts contrail-route-topology data from CONFIG_GRA_CONTRAIL_ROUTE_ALLOTTED_RESOURCES for a given allottedResourceId
303      * <p>
304      * Maps to /config/GENERIC-RESOURCE-API:contrail-route-allotted-resources/contrail-route-allotted-resource/{allotted-resource-id}/allotted-resource-data/contrail-route-topology/
305      * @param allottedResourceId the allotted-resource-id for a contrail-route
306      * @return HttpStatus.OK (200) if the data is found.
307      * @throws RestException if the data does not exist.
308      */
309     @Override
310     public ResponseEntity<GenericResourceApiContrailroutetopologyContrailRouteTopology>
311             configGENERICRESOURCEAPIcontrailRouteAllottedResourcesContrailRouteAllottedResourceAllottedResourceIdAllottedResourceDataContrailRouteTopologyGet(
312                 String allottedResourceId) throws RestApplicationException, RestProtocolException {
313         @Valid GenericResourceApiContrailroutetopologyContrailRouteTopology contrailRouteTopology = null;
314         GenericResourceApiContrailrouteallottedresourcesContrailrouteallottedresourceAllottedResourceData allottedResourceData = null;
315
316         List<ConfigContrailRouteAllottedResources> configContrailRouteAllottedResources =
317                 configContrailRouteAllottedResourcesRepository.findByAllottedResourceId(allottedResourceId);
318
319         if ((configContrailRouteAllottedResources == null) || (configContrailRouteAllottedResources.isEmpty())) {
320             log.info("No contrail-route-allotted-resoure data found with id [{}]", allottedResourceId);
321             throw new RestApplicationException("data-missing",
322                     "Request could not be completed because the relevant data model content does not exist",
323                     HttpStatus.NOT_FOUND.value());
324         }
325
326         try {
327             if ( configContrailRouteAllottedResources.get(0).getArData().isEmpty()) {
328                 throw new RestProtocolException("data-missing", "No allotted-resource-data entry found", HttpStatus.NOT_FOUND.value());
329             } else {
330                 allottedResourceData = objectMapper.readValue(configContrailRouteAllottedResources.get(0).getArData(),
331                         GenericResourceApiContrailrouteallottedresourcesContrailrouteallottedresourceAllottedResourceData.class);
332
333                 contrailRouteTopology = allottedResourceData.getContrailRouteTopology();
334             }
335             if (contrailRouteTopology == null) {
336                 throw new RestProtocolException("data-missing", "No contrail-route-topology entry found", HttpStatus.NOT_FOUND.value());
337             }
338             return new ResponseEntity<>(contrailRouteTopology, HttpStatus.OK);
339         } catch (JsonProcessingException e) {
340             log.error("Could not parse port-mirror-configuration data", e);
341             throw new RestApplicationException("data-conversion", "Request could not be completed due to internal error", e, HttpStatus.INTERNAL_SERVER_ERROR.value());
342         }
343     }
344
345
346     /**
347      * DELETE allotted-resource data from CONFIG_GRA_CONTRAIL_ROUTE_ALLOTTED_RESOURCES for a given allottedResourceId
348      * <p>
349      * Maps to /config/GENERIC-RESOURCE-API:contrail-route-allotted-resources/contrail-route-allotted-resource/{allotted-resource-id}
350      * @param allottedResourceId the allotted-resource-id for a contrail-route
351      * @return HttpStatus.NO_CONTENT (204) if the data is found.
352      */
353     @Override
354     public ResponseEntity<Void> configGENERICRESOURCEAPIcontrailRouteAllottedResourcesContrailRouteAllottedResourceAllottedResourceIdDelete(
355             String allottedResourceId) {
356         configContrailRouteAllottedResourcesRepository.deleteByAllottedResourceId(allottedResourceId);
357         return new ResponseEntity<>(HttpStatus.NO_CONTENT);
358     }
359
360
361     @Override
362     public ResponseEntity<GenericResourceApiPreloadModelInformation> configGENERICRESOURCEAPIpreloadInformationGet()
363             throws RestApplicationException {
364         GenericResourceApiPreloadModelInformation genericResourceApiPreloadModelInformation = new GenericResourceApiPreloadModelInformation();
365
366         if (configPreloadDataRepository.count() == 0) {
367             throw new RestApplicationException("data-missing",
368                     "Request could not be completed because the relevant data model content does not exist",
369                     HttpStatus.NOT_FOUND.value());
370         }
371
372         for (ConfigPreloadData configPreloadData : configPreloadDataRepository.findAll()) {
373             GenericResourceApiPreloadmodelinformationPreloadList preloadListItem = new GenericResourceApiPreloadmodelinformationPreloadList();
374
375             preloadListItem.setPreloadId(configPreloadData.getPreloadId());
376             preloadListItem.setPreloadType(configPreloadData.getPreloadType());
377             try {
378                 preloadListItem.setPreloadData(objectMapper.readValue(configPreloadData.getPreloadData(),
379                         GenericResourceApiPreloaddataPreloadData.class));
380             } catch (JsonProcessingException e) {
381                 log.error("Could not convert preload data", e);
382                 throw new RestApplicationException("data-conversion",
383                         "Request could not be completed due to internal error", e,
384                         HttpStatus.INTERNAL_SERVER_ERROR.value());
385             }
386             genericResourceApiPreloadModelInformation.addPreloadListItem(preloadListItem);
387         }
388
389         return new ResponseEntity<>(genericResourceApiPreloadModelInformation, HttpStatus.OK);
390     }
391
392     @Override
393     public ResponseEntity<GenericResourceApiPreloadnetworktopologyinformationPreloadNetworkTopologyInformation> configGENERICRESOURCEAPIpreloadInformationPreloadListPreloadIdPreloadTypePreloadDataPreloadNetworkTopologyInformationGet(
394             String preloadId, String preloadType) throws RestException {
395         GenericResourceApiPreloadnetworktopologyinformationPreloadNetworkTopologyInformation netTopoInfo = null;
396
397         if (configPreloadDataRepository.count() == 0) {
398             throw new RestApplicationException("data-missing",
399                     "Request could not be completed because the relevant data model content does not exist",
400                     HttpStatus.NOT_FOUND.value());
401         }
402
403         for (ConfigPreloadData configPreloadData : configPreloadDataRepository.findAll()) {
404
405             try {
406                 GenericResourceApiPreloaddataPreloadData preloadDataItem = objectMapper
407                         .readValue(configPreloadData.getPreloadData(), GenericResourceApiPreloaddataPreloadData.class);
408                 netTopoInfo = preloadDataItem.getPreloadNetworkTopologyInformation();
409             } catch (JsonProcessingException e) {
410                 log.error("Could not convert preload data", e);
411                 throw new RestApplicationException("data-conversion",
412                         "Request could not be completed due to internal error", e,
413                         HttpStatus.INTERNAL_SERVER_ERROR.value());
414             }
415         }
416         
417         return new ResponseEntity<>(netTopoInfo, HttpStatus.OK);
418     }
419
420
421     @Override
422     public ResponseEntity<Void> configGENERICRESOURCEAPIpreloadInformationPost(
423             @Valid GenericResourceApiPreloadModelInformation graPreloadModelInfo)
424             throws RestApplicationException, RestProtocolException {
425
426         List<GenericResourceApiPreloadmodelinformationPreloadList> preloadList = graPreloadModelInfo.getPreloadList();
427         List<ConfigPreloadData> newPreloadData = new LinkedList<>();
428
429         if (preloadList != null) {
430             // Verification pass - if any items already exist, return an error
431             for (GenericResourceApiPreloadmodelinformationPreloadList curItem : preloadList) {
432
433                 List<ConfigPreloadData> curPreloadData = configPreloadDataRepository
434                         .findByPreloadIdAndPreloadType(curItem.getPreloadId(), curItem.getPreloadType());
435                 if ((curPreloadData != null) && (!curPreloadData.isEmpty())) {
436                     log.error("Preload data already exists for {}:{}", curItem.getPreloadId(),
437                             curItem.getPreloadType());
438                     throw new RestProtocolException("data-exists",
439                             "Data already exists for " + curItem.getPreloadId() + ":" + curItem.getPreloadType(),
440                             HttpStatus.CONFLICT.value());
441                 } else {
442                     try {
443                         newPreloadData.add(new ConfigPreloadData(curItem.getPreloadId(), curItem.getPreloadType(),
444                                 objectMapper.writeValueAsString(curItem.getPreloadData())));
445                     } catch (JsonProcessingException e) {
446                         log.error("Cannot convert preload data");
447                         throw new RestApplicationException("data-conversion",
448                                 "Request could not be completed due to internal error", e,
449                                 HttpStatus.INTERNAL_SERVER_ERROR.value());
450
451                     }
452                 }
453             }
454
455             // Update pass
456             for (ConfigPreloadData newDataItem : newPreloadData) {
457                 log.info("Adding preload data for {}:{}", newDataItem.getPreloadId(), newDataItem.getPreloadType());
458                 configPreloadDataRepository.save(newDataItem);
459             }
460         } else {
461             throw new RestProtocolException("data-missing", "No preload-list entries found to add",
462                     HttpStatus.CONFLICT.value());
463         }
464
465         return new ResponseEntity<>(HttpStatus.CREATED);
466     }
467
468     @Override
469     public ResponseEntity<Void> configGENERICRESOURCEAPIpreloadInformationPut(
470             @Valid GenericResourceApiPreloadModelInformation graPreloadModelInfo) throws RestApplicationException {
471
472         boolean addedNew = false;
473         List<GenericResourceApiPreloadmodelinformationPreloadList> preloadList = graPreloadModelInfo.getPreloadList();
474
475         if (preloadList != null) {
476             Iterator<GenericResourceApiPreloadmodelinformationPreloadList> iter = preloadList.iterator();
477             while (iter.hasNext()) {
478                 GenericResourceApiPreloadmodelinformationPreloadList curItem = iter.next();
479                 List<ConfigPreloadData> curPreloadData = configPreloadDataRepository
480                         .findByPreloadIdAndPreloadType(curItem.getPreloadId(), curItem.getPreloadType());
481                 if ((curPreloadData == null) || curPreloadData.isEmpty()) {
482                     addedNew = true;
483                 }
484
485                 try {
486                     configPreloadDataRepository.save(new ConfigPreloadData(curItem.getPreloadId(),
487                             curItem.getPreloadType(), objectMapper.writeValueAsString(curItem.getPreloadData())));
488                 } catch (JsonProcessingException e) {
489                     log.error("Cannot convert preload data", e);
490                     throw new RestApplicationException("data-conversion",
491                             "Request could not be completed due to internal error", e,
492                             HttpStatus.INTERNAL_SERVER_ERROR.value());
493
494                 }
495             }
496         }
497
498         if (addedNew) {
499             return new ResponseEntity<>(HttpStatus.CREATED);
500         } else {
501             return new ResponseEntity<>(HttpStatus.NO_CONTENT);
502         }
503
504     }
505
506     @Override
507     public ResponseEntity<Void> configGENERICRESOURCEAPIpreloadInformationPreloadListPost(
508             @Valid GenericResourceApiPreloadmodelinformationPreloadList preloadListItem) throws RestProtocolException {
509
510         throw new RestProtocolException("data-missing", "Missing key for list \"preload-list\"",
511                 HttpStatus.NOT_FOUND.value());
512     }
513
514     @Override
515     public ResponseEntity<Void> configGENERICRESOURCEAPIpreloadInformationPreloadListPreloadIdPreloadTypeDelete(
516             String preloadId, String preloadType) {
517         configPreloadDataRepository.deleteByPreloadIdAndPreloadType(preloadId, preloadType);
518         return new ResponseEntity<>(HttpStatus.NO_CONTENT);
519     }
520
521     @Override
522     public ResponseEntity<GenericResourceApiPreloadmodelinformationPreloadList> configGENERICRESOURCEAPIpreloadInformationPreloadListPreloadIdPreloadTypeGet(
523             String preloadId, String preloadType) throws RestApplicationException {
524         List<ConfigPreloadData> preloadData = configPreloadDataRepository.findByPreloadIdAndPreloadType(preloadId,
525                 preloadType);
526         if (preloadData != null) {
527             if (!preloadData.isEmpty()) {
528                 ConfigPreloadData preloadDataItem = preloadData.get(0);
529                 GenericResourceApiPreloadmodelinformationPreloadList preloadDataList = new GenericResourceApiPreloadmodelinformationPreloadList();
530                 preloadDataList.setPreloadId(preloadDataItem.getPreloadId());
531                 preloadDataList.setPreloadType(preloadDataItem.getPreloadType());
532                 try {
533                     preloadDataList.setPreloadData(objectMapper.readValue(preloadDataItem.getPreloadData(),
534                             GenericResourceApiPreloaddataPreloadData.class));
535                 } catch (JsonProcessingException e) {
536                     log.error("Cannot convert preload data", e);
537                     throw new RestApplicationException("data-conversion",
538                             "Request could not be completed due to internal error", e,
539                             HttpStatus.INTERNAL_SERVER_ERROR.value());
540                 }
541                 return new ResponseEntity<>(preloadDataList, HttpStatus.OK);
542             }
543         }
544         return new ResponseEntity<>(HttpStatus.NOT_FOUND);
545     }
546
547     @Override
548     public ResponseEntity<Void> configGENERICRESOURCEAPIpreloadInformationPreloadListPreloadIdPreloadTypePost(
549             String preloadId, String preloadType,
550             @Valid GenericResourceApiPreloadmodelinformationPreloadList preloadListItem)
551             throws RestApplicationException, RestProtocolException {
552         List<ConfigPreloadData> preloadDataItems = configPreloadDataRepository.findByPreloadIdAndPreloadType(preloadId,
553                 preloadType);
554
555         if ((preloadDataItems != null) && !preloadDataItems.isEmpty()) {
556             log.error("Preload data already exists for {}:{}", preloadId, preloadType);
557             throw new RestProtocolException("data-exists", "Data already exists for " + preloadId + ":" + preloadType,
558                     HttpStatus.CONFLICT.value());
559         }
560
561         try {
562             log.info("Adding preload data for {}:{}", preloadId, preloadType);
563             configPreloadDataRepository.save(new ConfigPreloadData(preloadId, preloadType,
564                     objectMapper.writeValueAsString(preloadListItem.getPreloadData())));
565         } catch (JsonProcessingException e) {
566             log.error("Cannot convert preload data", e);
567             throw new RestApplicationException("data-conversion",
568                     "Request could not be completed due to internal error", e,
569                     HttpStatus.INTERNAL_SERVER_ERROR.value());
570
571         }
572         return new ResponseEntity<>(HttpStatus.CREATED);
573     }
574
575     @Override
576     public ResponseEntity<Void> configGENERICRESOURCEAPIpreloadInformationPreloadListPreloadIdPreloadTypePut(
577             String preloadId, String preloadType,
578             @Valid GenericResourceApiPreloadmodelinformationPreloadList preloadListItem)
579             throws RestApplicationException, RestProtocolException {
580         List<ConfigPreloadData> preloadDataItems = configPreloadDataRepository.findByPreloadIdAndPreloadType(preloadId,
581                 preloadType);
582         boolean dataExists = false;
583         if ((preloadDataItems != null) && !preloadDataItems.isEmpty()) {
584             dataExists = true;
585         }
586
587         if ((preloadListItem.getPreloadId() == null) || (preloadListItem.getPreloadType() == null)
588                 || (preloadListItem.getPreloadData() == null)) {
589             log.error("Invalid list item received: {}", preloadListItem);
590             throw new RestProtocolException("bad-attribute", "Invalid data received", HttpStatus.BAD_REQUEST.value());
591         }
592
593         try {
594             if (dataExists) {
595                 log.info("Updating preload data for {}:{} -> {}", preloadId, preloadType,
596                         objectMapper.writeValueAsString(preloadListItem));
597
598             } else {
599                 log.info("Adding preload data for {}:{}", preloadId, preloadType);
600             }
601
602             configPreloadDataRepository.save(new ConfigPreloadData(preloadId, preloadType,
603                     objectMapper.writeValueAsString(preloadListItem.getPreloadData())));
604         } catch (JsonProcessingException e) {
605             log.error("Cannot convert preload data", e);
606             throw new RestApplicationException("data-conversion",
607                     "Request could not be completed due to internal error", e,
608                     HttpStatus.INTERNAL_SERVER_ERROR.value());
609
610         }
611
612         if (dataExists) {
613             return new ResponseEntity<>(HttpStatus.NO_CONTENT);
614         } else {
615             return new ResponseEntity<>(HttpStatus.CREATED);
616         }
617     }
618
619     @Override
620     public ResponseEntity<Void> configGENERICRESOURCEAPIpreloadInformationPreloadListPreloadIdPreloadTypePreloadDataDelete(
621             String preloadId, String preloadType) throws RestProtocolException {
622         List<ConfigPreloadData> preloadData = configPreloadDataRepository.findByPreloadIdAndPreloadType(preloadId,
623                 preloadType);
624
625         if ((preloadData == null) || preloadData.isEmpty()) {
626             throw new RestProtocolException("data-missing", "No preload entry found", HttpStatus.NOT_FOUND.value());
627         }
628
629         ConfigPreloadData preloadDataItem = preloadData.get(0);
630
631         if (preloadDataItem.getPreloadData() == null) {
632             throw new RestProtocolException("data-missing", "No preload-data found", HttpStatus.NOT_FOUND.value());
633         }
634         preloadDataItem.setPreloadData(null);
635         configPreloadDataRepository.save(preloadDataItem);
636
637         return new ResponseEntity<>(HttpStatus.NO_CONTENT);
638     }
639
640     @Override
641     public ResponseEntity<GenericResourceApiPreloaddataPreloadData> configGENERICRESOURCEAPIpreloadInformationPreloadListPreloadIdPreloadTypePreloadDataGet(
642             String preloadId, String preloadType) throws RestApplicationException, RestProtocolException {
643         List<ConfigPreloadData> preloadData = configPreloadDataRepository.findByPreloadIdAndPreloadType(preloadId,
644                 preloadType);
645
646         if ((preloadData == null) || preloadData.isEmpty()) {
647             throw new RestProtocolException("data-missing", "No preload entry found", HttpStatus.NOT_FOUND.value());
648         }
649
650         ConfigPreloadData preloadDataItem = preloadData.get(0);
651
652         if (preloadDataItem.getPreloadData() == null) {
653             throw new RestProtocolException("data-missing", "No preload-data found", HttpStatus.NOT_FOUND.value());
654         }
655         try {
656             return new ResponseEntity<>(objectMapper.readValue(preloadDataItem.getPreloadData(),
657                     GenericResourceApiPreloaddataPreloadData.class), HttpStatus.OK);
658         } catch (JsonProcessingException e) {
659             log.error("Cannot convert preload data", e);
660             throw new RestApplicationException("data-conversion",
661                     "Request could not be completed due to internal error", e,
662                     HttpStatus.INTERNAL_SERVER_ERROR.value());
663         }
664     }
665
666     @Override
667     public ResponseEntity<Void> configGENERICRESOURCEAPIpreloadInformationPreloadListPreloadIdPreloadTypePreloadDataPost(
668             String preloadId, String preloadType, @Valid GenericResourceApiPreloaddataPreloadData preloadData)
669             throws RestApplicationException, RestProtocolException {
670         List<ConfigPreloadData> preloadDataEntries = configPreloadDataRepository
671                 .findByPreloadIdAndPreloadType(preloadId, preloadType);
672
673         List<ConfigPreloadData> preloadDataItems = configPreloadDataRepository.findByPreloadIdAndPreloadType(preloadId,
674                 preloadType);
675         if ((preloadDataItems == null) || (preloadDataItems.isEmpty())) {
676             throw new RestProtocolException("data-missing", "No preload entry found", HttpStatus.NOT_FOUND.value());
677         }
678
679         if ((preloadData == null) || (preloadData.getPreloadNetworkTopologyInformation() == null)) {
680             throw new RestProtocolException("bad-attribute", "Invalid preloadData received",
681                     HttpStatus.BAD_REQUEST.value());
682         }
683
684         ConfigPreloadData preloadDataItem = preloadDataItems.get(0);
685
686         if (preloadDataItem.getPreloadData() != null) {
687             log.error("Preload data already exists for {}:{} ", preloadId, preloadType);
688             throw new RestProtocolException("data-exists", "Data already exists for " + preloadId + ":" + preloadType,
689                     HttpStatus.CONFLICT.value());
690         }
691
692         try {
693             preloadDataItem.setPreloadData(objectMapper.writeValueAsString(preloadData));
694             configPreloadDataRepository.save(preloadDataItem);
695         } catch (JsonProcessingException e) {
696             log.error("Cannot convert preload data", e);
697             throw new RestApplicationException("data-conversion",
698                     "Request could not be completed due to internal error", e,
699                     HttpStatus.INTERNAL_SERVER_ERROR.value());
700         }
701
702         return new ResponseEntity<>(HttpStatus.CREATED);
703     }
704
705     @Override
706     public ResponseEntity<Void> configGENERICRESOURCEAPIpreloadInformationPreloadListPreloadIdPreloadTypePreloadDataPut(
707             String preloadId, String preloadType, @Valid GenericResourceApiPreloaddataPreloadData preloadData)
708             throws RestApplicationException, RestProtocolException {
709         boolean dataExists = false;
710         List<ConfigPreloadData> preloadDataItems = configPreloadDataRepository.findByPreloadIdAndPreloadType(preloadId,
711                 preloadType);
712         if ((preloadDataItems == null) || (preloadDataItems.isEmpty())) {
713             throw new RestProtocolException("data-missing", "No preload entry found", HttpStatus.NOT_FOUND.value());
714         }
715
716         if ((preloadData == null) || (preloadData.getPreloadNetworkTopologyInformation() == null)) {
717             throw new RestProtocolException("bad-attribute", "Invalid preloadData received",
718                     HttpStatus.BAD_REQUEST.value());
719         }
720
721         ConfigPreloadData preloadDataItem = preloadDataItems.get(0);
722
723         if (preloadDataItem.getPreloadData() != null) {
724             dataExists = true;
725         }
726
727         try {
728             preloadDataItem.setPreloadData(objectMapper.writeValueAsString(preloadData));
729             configPreloadDataRepository.save(preloadDataItem);
730         } catch (JsonProcessingException e) {
731             log.error("Cannot convert preload data", e);
732             throw new RestApplicationException("data-conversion",
733                     "Request could not be completed due to internal error", e,
734                     HttpStatus.INTERNAL_SERVER_ERROR.value());
735         }
736
737         if (dataExists) {
738             return new ResponseEntity<>(HttpStatus.NO_CONTENT);
739         } else {
740             return new ResponseEntity<>(HttpStatus.CREATED);
741         }
742     }
743
744     @Override
745     public ResponseEntity<Void> configGENERICRESOURCEAPIservicesDelete() {
746         configServicesRepository.deleteAll();
747         return new ResponseEntity<>(HttpStatus.NO_CONTENT);
748     }
749
750     @Override
751     public ResponseEntity<GenericResourceApiServiceModelInfrastructure> configGENERICRESOURCEAPIservicesGet()
752             throws RestApplicationException {
753         GenericResourceApiServiceModelInfrastructure modelInfrastructure = new GenericResourceApiServiceModelInfrastructure();
754
755         if (configServicesRepository.count() == 0) {
756             throw new RestApplicationException("data-missing",
757                     "Request could not be completed because the relevant data model content does not exist",
758                     HttpStatus.NOT_FOUND.value());
759         }
760
761         for (ConfigServices service : configServicesRepository.findAll()) {
762             GenericResourceApiServicemodelinfrastructureService serviceItem = new GenericResourceApiServicemodelinfrastructureService();
763             serviceItem.setServiceInstanceId(service.getSvcInstanceId());
764             if (service.getSvcData() != null) {
765                 try {
766                     serviceItem.setServiceData(objectMapper.readValue(service.getSvcData(),
767                             GenericResourceApiServicedataServiceData.class));
768                 } catch (JsonProcessingException e) {
769                     log.error("Could not deserialize service data for {}", service.getSvcInstanceId(), e);
770                     throw new RestApplicationException("data-conversion",
771                             "Request could not be completed due to internal error", e,
772                             HttpStatus.INTERNAL_SERVER_ERROR.value());
773
774                 }
775             }
776             serviceItem.setServiceStatus(service.getServiceStatus());
777             modelInfrastructure.addServiceItem(serviceItem);
778         }
779
780         return new ResponseEntity<>(modelInfrastructure, HttpStatus.OK);
781
782     }
783
784     @Override
785     public ResponseEntity<Void> configGENERICRESOURCEAPIservicesPost(
786             @Valid GenericResourceApiServiceModelInfrastructure modelInfrastructure)
787             throws RestApplicationException, RestProtocolException {
788         List<ConfigServices> newServices = new LinkedList<>();
789
790         for (GenericResourceApiServicemodelinfrastructureService serviceItem : modelInfrastructure.getService()) {
791             String svcInstanceId = serviceItem.getServiceInstanceId();
792             List<ConfigServices> existingService = configServicesRepository.findBySvcInstanceId(svcInstanceId);
793             if ((existingService != null) && !existingService.isEmpty()) {
794                 log.error("Service data already exists for {}", svcInstanceId);
795                 throw new RestProtocolException("data-exists",
796                         "Data already exists for service-instance-id " + svcInstanceId, HttpStatus.CONFLICT.value());
797             }
798             ConfigServices service = new ConfigServices();
799             service.setSvcInstanceId(svcInstanceId);
800             try {
801                 service.setSvcData(objectMapper.writeValueAsString(serviceItem.getServiceData()));
802             } catch (JsonProcessingException e) {
803                 log.error("Could not serialize service data for {}", service.getSvcInstanceId(), e);
804                 throw new RestApplicationException("data-conversion",
805                         "Request could not be completed due to internal error", e,
806                         HttpStatus.INTERNAL_SERVER_ERROR.value());
807
808             }
809             service.setServiceStatus(serviceItem.getServiceStatus());
810             newServices.add(service);
811         }
812
813         for (ConfigServices service : newServices) {
814             configServicesRepository.save(service);
815         }
816
817         return new ResponseEntity<>(HttpStatus.CREATED);
818
819     }
820
821     @Override
822     public ResponseEntity<Void> configGENERICRESOURCEAPIservicesPut(
823             @Valid GenericResourceApiServiceModelInfrastructure modelInfrastructure) throws RestApplicationException {
824
825         List<ConfigServices> newServices = new LinkedList<>();
826         boolean dataExists = false;
827
828         for (GenericResourceApiServicemodelinfrastructureService serviceItem : modelInfrastructure.getService()) {
829             String svcInstanceId = serviceItem.getServiceInstanceId();
830             List<ConfigServices> existingService = configServicesRepository.findBySvcInstanceId(svcInstanceId);
831             if ((existingService != null) && !existingService.isEmpty()) {
832                 dataExists = true;
833             }
834             ConfigServices service = new ConfigServices();
835             service.setSvcInstanceId(svcInstanceId);
836             try {
837                 service.setSvcData(objectMapper.writeValueAsString(serviceItem.getServiceData()));
838             } catch (JsonProcessingException e) {
839                 log.error("Could not serialize service data for {}", service.getSvcInstanceId(), e);
840                 throw new RestApplicationException("data-conversion",
841                         "Request could not be completed due to internal error", e,
842                         HttpStatus.INTERNAL_SERVER_ERROR.value());
843
844             }
845             service.setServiceStatus(serviceItem.getServiceStatus());
846             newServices.add(service);
847         }
848
849         for (ConfigServices service : newServices) {
850             configServicesRepository.save(service);
851         }
852
853         if (dataExists) {
854             return new ResponseEntity<>(HttpStatus.NO_CONTENT);
855         } else {
856             return new ResponseEntity<>(HttpStatus.CREATED);
857         }
858
859     }
860
861     @Override
862     public ResponseEntity<Void> configGENERICRESOURCEAPIservicesServicePost(
863             @Valid GenericResourceApiServicemodelinfrastructureService servicesData) throws RestApplicationException {
864         String svcInstanceId = servicesData.getServiceInstanceId();
865         try {
866             String svcData = objectMapper.writeValueAsString(servicesData.getServiceData());
867             ConfigServices configService = new ConfigServices(svcInstanceId, svcData, servicesData.getServiceStatus());
868             configServicesRepository.deleteBySvcInstanceId(svcInstanceId);
869             configServicesRepository.save(configService);
870         } catch (JsonProcessingException e) {
871             log.error("Cannot convert service data", e);
872             throw new RestApplicationException("data-conversion",
873                     "Request could not be completed due to internal error", e,
874                     HttpStatus.INTERNAL_SERVER_ERROR.value());
875
876         }
877         return new ResponseEntity<>(HttpStatus.OK);
878     }
879
880     @Override
881     public ResponseEntity<Void> configGENERICRESOURCEAPIservicesServiceServiceInstanceIdDelete(
882             String serviceInstanceId) {
883         configServicesRepository.deleteBySvcInstanceId(serviceInstanceId);
884         return new ResponseEntity<>(HttpStatus.NO_CONTENT);
885     }
886
887     @Override
888     public ResponseEntity<GenericResourceApiServicemodelinfrastructureService> configGENERICRESOURCEAPIservicesServiceServiceInstanceIdGet(
889             String serviceInstanceId) throws RestApplicationException {
890         GenericResourceApiServicemodelinfrastructureService retval = null;
891
892         List<ConfigServices> services = configServicesRepository.findBySvcInstanceId(serviceInstanceId);
893
894         if (services.isEmpty()) {
895             return new ResponseEntity<>(HttpStatus.NOT_FOUND);
896         } else {
897             ConfigServices service = services.get(0);
898             retval = new GenericResourceApiServicemodelinfrastructureService();
899             retval.setServiceInstanceId(serviceInstanceId);
900             retval.setServiceStatus(service.getServiceStatus());
901             try {
902                 retval.setServiceData(
903                         objectMapper.readValue(service.getSvcData(), GenericResourceApiServicedataServiceData.class));
904             } catch (JsonProcessingException e) {
905                 log.error("Could not deserialize service data for service instance id {}", serviceInstanceId, e);
906                 throw new RestApplicationException("data-conversion",
907                         "Request could not be completed due to internal error", e,
908                         HttpStatus.INTERNAL_SERVER_ERROR.value());
909
910             }
911         }
912
913         return new ResponseEntity<>(retval, HttpStatus.OK);
914
915     }
916
917     @Override
918     public ResponseEntity<Void> configGENERICRESOURCEAPIservicesServiceServiceInstanceIdPost(String svcInstanceId,
919             @Valid GenericResourceApiServicemodelinfrastructureService newService)
920             throws RestApplicationException, RestProtocolException {
921
922         List<ConfigServices> existingService = configServicesRepository.findBySvcInstanceId(svcInstanceId);
923         if ((existingService != null) && !existingService.isEmpty()) {
924             log.error("Service data already exists for {}", svcInstanceId);
925             throw new RestProtocolException("data-exists",
926                     "Data already exists for service-instance-id " + svcInstanceId, HttpStatus.CONFLICT.value());
927         }
928         ConfigServices service = new ConfigServices();
929         service.setSvcInstanceId(svcInstanceId);
930         try {
931             service.setSvcData(objectMapper.writeValueAsString(newService.getServiceData()));
932         } catch (JsonProcessingException e) {
933             log.error("Could not serialize service data for {}", service.getSvcInstanceId(), e);
934             throw new RestApplicationException("data-conversion",
935                     "Request could not be completed due to internal error", e,
936                     HttpStatus.INTERNAL_SERVER_ERROR.value());
937
938         }
939         service.setServiceStatus(newService.getServiceStatus());
940         configServicesRepository.save(service);
941
942         return new ResponseEntity<>(HttpStatus.CREATED);
943     }
944
945     @Override
946     public ResponseEntity<Void> configGENERICRESOURCEAPIservicesServiceServiceInstanceIdPut(String serviceInstanceId,
947             @Valid GenericResourceApiServicemodelinfrastructureService newService) throws RestApplicationException {
948
949         boolean dataExists = false;
950
951         String svcInstanceId = newService.getServiceInstanceId();
952
953         ConfigServices service = null;
954         List<ConfigServices> existingService = configServicesRepository.findBySvcInstanceId(svcInstanceId);
955         if ((existingService != null) && !existingService.isEmpty()) {
956             dataExists = true;
957             service = existingService.get(0);
958         } else {
959             service = new ConfigServices();
960             service.setSvcInstanceId(svcInstanceId);
961         }
962
963         try {
964             service.setSvcData(objectMapper.writeValueAsString(newService.getServiceData()));
965         } catch (JsonProcessingException e) {
966             log.error("Could not serialize service data for {}", service.getSvcInstanceId(), e);
967             throw new RestApplicationException("data-conversion",
968                     "Request could not be completed due to internal error", e,
969                     HttpStatus.INTERNAL_SERVER_ERROR.value());
970
971         }
972         service.setServiceStatus(newService.getServiceStatus());
973         configServicesRepository.save(service);
974
975         if (dataExists) {
976             return new ResponseEntity<>(HttpStatus.NO_CONTENT);
977         } else {
978             return new ResponseEntity<>(HttpStatus.CREATED);
979         }
980     }
981
982     @Override
983     public ResponseEntity<Void> configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceDataDelete(
984             String serviceInstanceId) throws RestProtocolException {
985         List<ConfigServices> services = configServicesRepository.findBySvcInstanceId(serviceInstanceId);
986
987         if ((services == null) || (services.isEmpty())) {
988             throw new RestProtocolException("data-missing", "No service entry found", HttpStatus.NOT_FOUND.value());
989         }
990
991         ConfigServices service = services.get(0);
992         if (service.getSvcData() == null) {
993             throw new RestProtocolException("data-missing", "No service-data found", HttpStatus.NOT_FOUND.value());
994         }
995         service.setSvcData(null);
996         configServicesRepository.save(service);
997
998         return new ResponseEntity<>(HttpStatus.NO_CONTENT);
999     }
1000
1001     @Override
1002     public ResponseEntity<GenericResourceApiServicedataServiceData> configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceDataGet(
1003             String serviceInstanceId) throws RestApplicationException, RestProtocolException {
1004         GenericResourceApiServicedataServiceData serviceData = null;
1005
1006         List<ConfigServices> services = configServicesRepository.findBySvcInstanceId(serviceInstanceId);
1007         if ((services == null) || (services.isEmpty())) {
1008             throw new RestProtocolException("data-missing", "No service entry found", HttpStatus.NOT_FOUND.value());
1009         }
1010
1011         try {
1012             serviceData = objectMapper.readValue(services.get(0).getSvcData(),
1013                     GenericResourceApiServicedataServiceData.class);
1014             return new ResponseEntity<>(serviceData, HttpStatus.OK);
1015         } catch (JsonProcessingException e) {
1016             log.error("Could not parse service data", e);
1017             throw new RestApplicationException("data-conversion",
1018                     "Request could not be completed due to internal error", e,
1019                     HttpStatus.INTERNAL_SERVER_ERROR.value());
1020         }
1021
1022     }
1023
1024     @Override
1025     public ResponseEntity<Void> configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceDataPost(
1026             String serviceInstanceId, @Valid GenericResourceApiServicedataServiceData serviceData)
1027             throws RestApplicationException, RestProtocolException {
1028         ConfigServices service;
1029         List<ConfigServices> services = configServicesRepository.findBySvcInstanceId(serviceInstanceId);
1030         if ((services == null) || (services.isEmpty())) {
1031             throw new RestProtocolException("data-missing", "No service entry found", HttpStatus.NOT_FOUND.value());
1032         }
1033
1034         if ((serviceData == null) || (serviceData.getServiceInformation() == null)) {
1035             throw new RestProtocolException("bad-attribute", "Invalid service-data received",
1036                     HttpStatus.BAD_REQUEST.value());
1037
1038         }
1039         service = services.get(0);
1040
1041         if ((service.getSvcData() != null) && (service.getSvcData().length() > 0)) {
1042             log.error("service-data already exists for svcInstanceId {}", serviceInstanceId);
1043             throw new RestProtocolException("data-exists", "Data already exists for " + serviceInstanceId,
1044                     HttpStatus.CONFLICT.value());
1045         }
1046
1047         try {
1048             service.setSvcData(objectMapper.writeValueAsString(serviceData));
1049             configServicesRepository.save(service);
1050         } catch (JsonProcessingException e) {
1051             log.error("Could not serialize service data for svc instance id {}", serviceInstanceId, e);
1052             throw new RestApplicationException("data-conversion",
1053                     "Request could not be completed due to internal error", e,
1054                     HttpStatus.INTERNAL_SERVER_ERROR.value());
1055         }
1056
1057         return new ResponseEntity<>(HttpStatus.CREATED);
1058
1059     }
1060
1061     @Override
1062     public ResponseEntity<Void> configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceDataPut(
1063             String serviceInstanceId, @Valid GenericResourceApiServicedataServiceData serviceData)
1064             throws RestApplicationException, RestProtocolException {
1065         ConfigServices service;
1066         boolean dataExists = false;
1067
1068         List<ConfigServices> services = configServicesRepository.findBySvcInstanceId(serviceInstanceId);
1069         if ((services == null) || (services.isEmpty())) {
1070             throw new RestProtocolException("data-missing", "No service entry found", HttpStatus.NOT_FOUND.value());
1071         }
1072
1073         if ((serviceData == null) || (serviceData.getServiceInformation() == null)) {
1074             throw new RestProtocolException("bad-attribute", "Invalid service-data received",
1075                     HttpStatus.BAD_REQUEST.value());
1076
1077         }
1078         service = services.get(0);
1079
1080         if ((service.getSvcData() != null) && (service.getSvcData().length() > 0)) {
1081             dataExists = true;
1082         }
1083
1084         try {
1085             service.setSvcData(objectMapper.writeValueAsString(serviceData));
1086             configServicesRepository.save(service);
1087         } catch (JsonProcessingException e) {
1088             log.error("Could not serialize service data for svc instance id {}", serviceInstanceId, e);
1089             throw new RestApplicationException("data-conversion",
1090                     "Request could not be completed due to internal error", e,
1091                     HttpStatus.INTERNAL_SERVER_ERROR.value());
1092         }
1093
1094         if (dataExists) {
1095             return new ResponseEntity<>(HttpStatus.NO_CONTENT);
1096         } else {
1097             return new ResponseEntity<>(HttpStatus.CREATED);
1098         }
1099     }
1100
1101     @Override
1102     public ResponseEntity<Void> configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceStatusDelete(
1103             String serviceInstanceId) throws RestProtocolException {
1104         List<ConfigServices> services = configServicesRepository.findBySvcInstanceId(serviceInstanceId);
1105
1106         if ((services == null) || (services.isEmpty())) {
1107             throw new RestProtocolException("data-missing", "No service entry found", HttpStatus.NOT_FOUND.value());
1108         }
1109
1110         ConfigServices service = services.get(0);
1111         if (service.getServiceStatus() == null) {
1112             throw new RestProtocolException("data-missing", "No service-status found", HttpStatus.NOT_FOUND.value());
1113         }
1114         service.setServiceStatus(null);
1115         configServicesRepository.save(service);
1116
1117         return new ResponseEntity<>(HttpStatus.NO_CONTENT);
1118
1119     }
1120
1121     @Override
1122     public ResponseEntity<GenericResourceApiServicestatusServiceStatus> configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceStatusGet(
1123             String serviceInstanceId) throws RestApplicationException, RestProtocolException {
1124         GenericResourceApiServicestatusServiceStatus serviceStatus = null;
1125
1126         List<ConfigServices> services = configServicesRepository.findBySvcInstanceId(serviceInstanceId);
1127         if ((services == null) || (services.isEmpty())) {
1128             throw new RestProtocolException("data-missing", "No service entry found", HttpStatus.NOT_FOUND.value());
1129         }
1130
1131         serviceStatus = services.get(0).getServiceStatus();
1132         return new ResponseEntity<>(serviceStatus, HttpStatus.OK);
1133     }
1134
1135     @Override
1136     public ResponseEntity<Void> configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceStatusPost(
1137             String serviceInstanceId, @Valid GenericResourceApiServicestatusServiceStatus serviceStatus)
1138             throws RestProtocolException {
1139         ConfigServices service;
1140         List<ConfigServices> services = configServicesRepository.findBySvcInstanceId(serviceInstanceId);
1141         if ((services == null) || (services.isEmpty())) {
1142             throw new RestProtocolException("data-missing", "No service entry found", HttpStatus.NOT_FOUND.value());
1143         }
1144
1145         if ((serviceStatus == null) || (serviceStatus.getAction() == null)) {
1146             throw new RestProtocolException("bad-attribute", "Invalid service-status received",
1147                     HttpStatus.BAD_REQUEST.value());
1148
1149         }
1150         service = services.get(0);
1151
1152         if (service.getServiceStatus() != null) {
1153             log.error("service-status already exists for svcInstanceId {}", serviceInstanceId);
1154             throw new RestProtocolException("data-exists", "Data already exists for " + serviceInstanceId,
1155                     HttpStatus.CONFLICT.value());
1156         }
1157
1158         service.setServiceStatus(serviceStatus);
1159         configServicesRepository.save(service);
1160
1161         return new ResponseEntity<>(HttpStatus.CREATED);
1162
1163     }
1164
1165     @Override
1166     public ResponseEntity<Void> configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceStatusPut(
1167             String serviceInstanceId, @Valid GenericResourceApiServicestatusServiceStatus serviceStatus)
1168             throws RestProtocolException {
1169         ConfigServices service;
1170         boolean dataExists = false;
1171
1172         List<ConfigServices> services = configServicesRepository.findBySvcInstanceId(serviceInstanceId);
1173         if ((services == null) || (services.isEmpty())) {
1174             throw new RestProtocolException("data-missing", "No service entry found", HttpStatus.NOT_FOUND.value());
1175         }
1176
1177         if ((serviceStatus == null) || (serviceStatus.getAction() == null)) {
1178             throw new RestProtocolException("bad-attribute", "Invalid service-status received",
1179                     HttpStatus.BAD_REQUEST.value());
1180
1181         }
1182         service = services.get(0);
1183
1184         if (service.getServiceStatus() != null) {
1185             dataExists = true;
1186         }
1187
1188         service.setServiceStatus(serviceStatus);
1189         configServicesRepository.save(service);
1190
1191         if (dataExists) {
1192             return new ResponseEntity<>(HttpStatus.NO_CONTENT);
1193         } else {
1194             return new ResponseEntity<>(HttpStatus.CREATED);
1195         }
1196     }
1197
1198     /**
1199      * Deletes VNF data from the Config table specified Service Instance.
1200      * <p>
1201      * Maps to
1202      * /config/GENERIC-RESOURCE-API:services/service/{service-instance-id}/service-data/vnfs/vnf/{vnf-id}/
1203      * 
1204      * @param serviceInstanceId the Service Instance ID to perform the delete on
1205      * @param vnfId             the VNF ID of the VNF to delete
1206      * @return HttpStatus.NO_CONTENT (204) on successful delete
1207      *         <p>
1208      *         HttpStatus.BAD_REQUEST (400) if unmarshalling Service Data from the
1209      *         database fails, there is no VNF data for {@code vnfId}, or writing
1210      *         Service Data back to the database fails.
1211      *         <p>
1212      *         HttpStatus.NOT_FOUND (404) if {@code serviceInstanceId} does not
1213      *         exist.
1214      */
1215     @Override
1216     public ResponseEntity<Void> configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceDataVnfsVnfVnfIdDelete(
1217             String serviceInstanceId, String vnfId) throws RestException {
1218         log.info("DELETE | VNF Data for ({})", vnfId);
1219
1220         /*
1221          * The logic may need to be moved inside of this check or this check may need to
1222          * be removed.
1223          */
1224         if (getObjectMapper().isPresent() && getAcceptHeader().isPresent()) {
1225             log.info("Something with header.");
1226         } else {
1227             log.warn(
1228                     "ObjectMapper or HttpServletRequest not configured in default ConfigApi interface so no example is generated");
1229         }
1230
1231         List<ConfigServices> services = configServicesRepository.findBySvcInstanceId(serviceInstanceId);
1232         ConfigServices data;
1233         if ((services == null) || (services.isEmpty())) {
1234             log.info("Could not find data for ({}).", serviceInstanceId);
1235             // Or throw the data not found error?
1236             throw new RestProtocolException("data-missing", "Service Instance ID not found.",
1237                     HttpStatus.NOT_FOUND.value());
1238         } else {
1239             data = services.get(0);
1240         }
1241
1242         GenericResourceApiServicedataServiceData svcData;
1243         try {
1244             svcData = objectMapper.readValue(data.getSvcData(), GenericResourceApiServicedataServiceData.class);
1245         } catch (JsonProcessingException e) {
1246             // Or throw the data not found error?
1247             log.error("Could not map service data for ({})", serviceInstanceId);
1248             return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
1249         }
1250         if (svcData == null) {
1251             // Or throw the data not found error?
1252             log.info("Could not find Service Data for ({}).", serviceInstanceId);
1253             throw new RestProtocolException("data-missing", "Service data not found.", HttpStatus.NOT_FOUND.value());
1254         }
1255
1256         GenericResourceApiServicedataServicedataVnfs vnfs = svcData.getVnfs();
1257         if (vnfs == null) {
1258             // Or throw the data not found error?
1259             log.info("VNF List not found for ({}).", serviceInstanceId);
1260             throw new RestProtocolException("data-missing", "VNFs not found.", HttpStatus.NOT_FOUND.value());
1261         }
1262
1263         Stream<GenericResourceApiServicedataServicedataVnfsVnf> vnfStream = svcData.getVnfs().getVnf().stream();
1264         if (vnfStream.noneMatch(targetVnf -> targetVnf.getVnfId().equals(vnfId))) {
1265             // Data was not found
1266             log.error("Did not find VNF ({}) in data.", vnfId);
1267             throw new RestProtocolException("data-missing", "VNF ID not found.", HttpStatus.NOT_FOUND.value());
1268         }
1269         // Recreate the stream per Sonar?
1270         vnfStream = svcData.getVnfs().getVnf().stream();
1271         svcData.getVnfs().setVnf(
1272                 vnfStream.filter(targetVnf -> !targetVnf.getVnfId().equals(vnfId)).collect(Collectors.toList()));
1273
1274         // Map and save the new data
1275         try {
1276             data.setSvcData(objectMapper.writeValueAsString(svcData));
1277             configServicesRepository.save(data);
1278             return new ResponseEntity<>(HttpStatus.NO_CONTENT);
1279         } catch (JsonProcessingException e) {
1280             log.error("Error mapping object to JSON", e);
1281             // Should probably be a 500 INTERNAL_SERVICE_ERROR
1282             throw new RestProtocolException("internal-service-error", "Failed to save data.",
1283                     HttpStatus.BAD_REQUEST.value());
1284         }
1285     }
1286
1287     /**
1288      * Extracts VNF data from the Config table specified Service Instance.
1289      * <p>
1290      * Maps to
1291      * /config/GENERIC-RESOURCE-API:services/service/{service-instance-id}/service-data/vnfs/vnf/{vnf-id}/
1292      * 
1293      * @param serviceInstanceId the Service Instance ID to lookup data for
1294      * @param vnfId             the VNF ID of the VNF to return
1295      * @return HttpStatus.OK (200) if the data is found.
1296      * @throws RestException if the data does not exist.
1297      */
1298     @Override
1299     public ResponseEntity<GenericResourceApiServicedataServicedataVnfsVnf> configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceDataVnfsVnfVnfIdGet(
1300             String serviceInstanceId, String vnfId) throws RestException {
1301         log.info("GET | VNF Data for ({})", vnfId);
1302         if (getObjectMapper().isPresent() && getAcceptHeader().isPresent()) {
1303             if (getAcceptHeader().get().contains("application/json")) {
1304             }
1305         } else {
1306             log.warn(
1307                     "ObjectMapper or HttpServletRequest not configured in default ConfigApi interface so no example is generated");
1308         }
1309         List<ConfigServices> services = configServicesRepository.findBySvcInstanceId(serviceInstanceId);
1310         if ((services == null) || (services.isEmpty())) {
1311             throw new RestProtocolException("data-missing", "No service entry found", HttpStatus.NOT_FOUND.value());
1312         }
1313
1314         Optional<GenericResourceApiServicedataServicedataVnfsVnf> vnf = getVnfObject(services.get(0), vnfId);
1315         if (vnf.isPresent()) {
1316             return new ResponseEntity<>(vnf.get(), HttpStatus.OK);
1317         } else {
1318             log.info("No information found for {}", vnfId);
1319             throw new RestApplicationException("data-missing",
1320                     "Request could not be completed because the relevant data model content does not exist",
1321                     HttpStatus.NOT_FOUND.value());
1322         }
1323     }
1324
1325     /**
1326      * Extracts a vf-module object from the database,
1327      * @param configServices A Config Services option created from a Service
1328      *                       Instance ID
1329      * @param vnfId the target VNF ID
1330      * @param vfModuleId the target vf-module ID
1331      * @return An empty Optional if the Service Data does not exist, an empty
1332      *         Optional if the VNF is not found, or an optional containing the
1333      *         found VNF.
1334      */
1335     private Optional<GenericResourceApiServicedataServicedataVnfsVnfVnfdataVfmodulesVfModule> getVfModuleObject (
1336             ConfigServices configServices, String vnfId, String vfModuleId) {
1337         // Map the Marshall the JSON String into a Java Object
1338         log.info("Getting vf-module Data for ({})", vfModuleId);
1339
1340         Optional<GenericResourceApiServicedataServicedataVnfsVnf> vnf = getVnfObject(configServices, vnfId);
1341         GenericResourceApiServicedataServicedataVnfsVnfVnfData vnfData = vnf.get().getVnfData();
1342
1343         return vnfData.getVfModules().getVfModule()
1344                 .stream()
1345                 .filter(vf -> vf.getVfModuleId().equals(vfModuleId))
1346                 .findFirst();
1347     }
1348
1349     /**
1350      * Creates or updates VNF data in the Config table for a specified Service
1351      * Instance. If it is a new Service Instance or a new VNF, creates all necessary
1352      * parent data containers, then performs the updates.
1353      * <p>
1354      * Maps to /config/GENERIC-RESOURCE-API:services/service/{service-instance-id}/service-data/vnfs/vnf/{vnf-id}/
1355      * @param serviceInstanceId the Service Instance ID to perform the delete on
1356      * @param vnfId the VNF ID of the VNF to delete
1357      * @param genericResourceApiServicedataServicedataVnfsVnfBodyParam the playload
1358      * @return HttpStatus.CREATED (201) on successful create
1359      * <p>
1360      * HttpStatus.NO_CONTENT (204) on successful update
1361      * <p>
1362      * HttpStatus.BAD_REQUEST (400) if {@code vnfId} does not match what is specified in the
1363      * {@code genericResourceApiServicedataServicedataVnfsVnfBodyParam} , or if updating the database fails.
1364      * @throws RestException
1365      */
1366     @Override
1367     public ResponseEntity<Void> configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceDataVnfsVnfVnfIdPut(
1368             String serviceInstanceId, String vnfId,
1369             GenericResourceApiServicedataServicedataVnfsVnf genericResourceApiServicedataServicedataVnfsVnfBodyParam)
1370             throws RestException {
1371         log.info("PUT | VNF Data for ({})", vnfId);
1372         if (!vnfId.equals(genericResourceApiServicedataServicedataVnfsVnfBodyParam.getVnfId())) {
1373             throw new RestProtocolException("bad-attribute", "vnf-id mismatch", HttpStatus.BAD_REQUEST.value());
1374         }
1375         if (getObjectMapper().isPresent() && getAcceptHeader().isPresent()) {
1376             log.info("Something with header");
1377         } else {
1378             log.warn(
1379                     "ObjectMapper or HttpServletRequest not configured in default ConfigApi interface so no example is generated");
1380         }
1381
1382         List<ConfigServices> services = configServicesRepository.findBySvcInstanceId(serviceInstanceId);
1383         ConfigServices data;
1384         if ((services == null) || (services.isEmpty())) {
1385             log.info("Could not find data for ({}). Creating new Service Object.", serviceInstanceId);
1386             data = new ConfigServices();
1387             data.setSvcInstanceId(serviceInstanceId);
1388         } else {
1389             data = services.get(0);
1390         }
1391
1392         GenericResourceApiServicedataServiceData svcData = null;
1393         try {
1394             svcData = objectMapper.readValue(data.getSvcData(), GenericResourceApiServicedataServiceData.class);
1395         } catch (JsonProcessingException e) {
1396             log.error("Could not map service data for ({})", serviceInstanceId);
1397         }
1398         if (svcData == null) {
1399             log.info("Could not find Service Data for ({}). Creating new Service Data Container", serviceInstanceId);
1400             svcData = new GenericResourceApiServicedataServiceData();
1401         }
1402         if (svcData.getVnfs() == null) {
1403             log.info("VNF List not found for ({}). Creating new VNF List Container.", serviceInstanceId);
1404             svcData.setVnfs(new GenericResourceApiServicedataServicedataVnfs());
1405             svcData.getVnfs().setVnf(new ArrayList<>());
1406         }
1407
1408         GenericResourceApiServicedataServicedataVnfs vnflist = new GenericResourceApiServicedataServicedataVnfs();
1409         HttpStatus responseStatus = HttpStatus.NO_CONTENT;
1410         if (svcData.getVnfs().getVnf().isEmpty()) {
1411             log.info("Creating VNF data for ({})", vnfId);
1412             vnflist.addVnfItem(genericResourceApiServicedataServicedataVnfsVnfBodyParam);
1413             responseStatus = HttpStatus.CREATED;
1414         } else {
1415             log.info("Updating VNF data for ({})", vnfId);
1416             // Filter out all of the other vnf objects into a new VNF List
1417             // Replace if a delete method exists
1418             svcData.getVnfs().getVnf().stream().filter(targetVnf -> !targetVnf.getVnfId().equals(vnfId))
1419                     .forEach(vnflist::addVnfItem);
1420             vnflist.addVnfItem(genericResourceApiServicedataServicedataVnfsVnfBodyParam);
1421         }
1422         svcData.setVnfs(vnflist);
1423         // Map and save the new data
1424         try {
1425             data.setSvcData(objectMapper.writeValueAsString(svcData));
1426             configServicesRepository.save(data);
1427             return new ResponseEntity<>(responseStatus);
1428         } catch (JsonProcessingException e) {
1429             log.error("Error mapping object to JSON", e);
1430             // Should probably be a 500 INTERNAL_SERVICE_ERROR
1431             return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
1432         }
1433     }
1434
1435     /**
1436      * Extracts VNF Topology data from the Config table specified Service Instance
1437      * and VNF ID.
1438      * <p>
1439      * Maps to
1440      * /config/GENERIC-RESOURCE-API:services/service/{service-instance-id}/service-data/vnfs/vnf/{vnf-id}/vnf-data/vnf-topology/
1441      * 
1442      * @param serviceInstanceId the Service Instance ID to lookup data for
1443      * @param vnfId             the VNF ID of the VNF to extract topology data from.
1444      * @return HttpStatus.OK (200) if the data is found.
1445      * @throws RestException if the data does not exist.
1446      */
1447     @Override
1448     public ResponseEntity<GenericResourceApiVnftopologyVnfTopology> configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceDataVnfsVnfVnfIdVnfDataVnfTopologyGet(
1449             String serviceInstanceId, String vnfId) throws RestException {
1450         log.info("GET | VNF Topology for ({})", vnfId);
1451         if (getObjectMapper().isPresent() && getAcceptHeader().isPresent()) {
1452             if (getAcceptHeader().get().contains("application/json")) {
1453
1454             }
1455         } else {
1456             log.warn(
1457                     "ObjectMapper or HttpServletRequest not configured in default ConfigApi interface so no example is generated");
1458         }
1459         List<ConfigServices> services = configServicesRepository.findBySvcInstanceId(serviceInstanceId);
1460         if ((services == null) || (services.isEmpty())) {
1461             throw new RestProtocolException("data-missing", "No service entry found", HttpStatus.NOT_FOUND.value());
1462         }
1463
1464         Optional<GenericResourceApiServicedataServicedataVnfsVnf> vnf = getVnfObject(services.get(0), vnfId);
1465         // Drill down to find the data
1466         if (vnf.isPresent() && vnf.get().getVnfData() != null && vnf.get().getVnfData().getVnfTopology() != null) {
1467             return new ResponseEntity<>(vnf.get().getVnfData().getVnfTopology(), HttpStatus.OK);
1468         } else {
1469             log.info("No information found for {}", vnfId);
1470             throw new RestApplicationException("data-missing",
1471                     "Request could not be completed because the relevant data model content does not exist",
1472                     HttpStatus.NOT_FOUND.value());
1473         }
1474     }
1475
1476     /**
1477      * Creates or updates VNF Level Operation Status data in the Config table for a
1478      * specified Service Instance. If it is a new Service Instance or a new VNF,
1479      * creates all necessary parent data containers, then performs the updates.
1480      * <p>
1481      * Maps to
1482      * /config/GENERIC-RESOURCE-API:services/service/{service-instance-id}/service-data/vnfs/vnf/{vnf-id}/vnf-data/vnf-level-oper-status/
1483      * 
1484      * @param serviceInstanceId                         the Service Instance ID to
1485      *                                                  perform the delete on
1486      * @param vnfId                                     the VNF ID of the VNF to
1487      *                                                  delete
1488      * @param genericResourceApiOperStatusDataBodyParam the payload
1489      * @return HttpStatus.CREATED (201) on successful create.
1490      *         <p>
1491      *         HttpStatus.NO_CONTENT (204) on successful update.
1492      *         <p>
1493      *         HttpStatus.BAD_REQUEST (400) if updating the database fails.
1494      * @throws RestException
1495      */
1496     @Override
1497     public ResponseEntity<Void> configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceDataVnfsVnfVnfIdVnfDataVnfLevelOperStatusPut(
1498             String serviceInstanceId, String vnfId,
1499             GenericResourceApiOperStatusData genericResourceApiOperStatusDataBodyParam) throws RestException {
1500         log.info("PUT | VNF Level Oper Status ({})", vnfId);
1501         if (getObjectMapper().isPresent() && getAcceptHeader().isPresent()) {
1502         } else {
1503             log.warn(
1504                     "ObjectMapper or HttpServletRequest not configured in default ConfigApi interface so no example is generated");
1505         }
1506
1507         List<ConfigServices> services = configServicesRepository.findBySvcInstanceId(serviceInstanceId);
1508         ConfigServices data;
1509         if ((services == null) || (services.isEmpty())) {
1510             log.info("Could not find data for ({}). Creating new Service Object.", serviceInstanceId);
1511             data = new ConfigServices();
1512             data.setSvcInstanceId(serviceInstanceId);
1513         } else {
1514             data = services.get(0);
1515         }
1516
1517         GenericResourceApiServicedataServiceData svcData = null;
1518         try {
1519             svcData = objectMapper.readValue(data.getSvcData(), GenericResourceApiServicedataServiceData.class);
1520         } catch (JsonProcessingException e) {
1521             log.error("Could not map service data for ({})", serviceInstanceId);
1522         }
1523         if (svcData == null) {
1524             log.info("Could not find Service Data for ({}). Creating new Service Data Container", serviceInstanceId);
1525             svcData = new GenericResourceApiServicedataServiceData();
1526         }
1527         if (svcData.getVnfs() == null) {
1528             log.info("VNF List not found for ({}). Creating new VNF List Container.", serviceInstanceId);
1529             svcData.setVnfs(new GenericResourceApiServicedataServicedataVnfs());
1530             svcData.getVnfs().setVnf(new ArrayList<>());
1531         }
1532
1533         GenericResourceApiServicedataServicedataVnfs vnflist = new GenericResourceApiServicedataServicedataVnfs();
1534         HttpStatus responseStatus = HttpStatus.NO_CONTENT;
1535         if (svcData.getVnfs().getVnf().isEmpty()) {
1536             log.info("Creating VNF data for ({})", vnfId);
1537             GenericResourceApiServicedataServicedataVnfsVnf vnf = new GenericResourceApiServicedataServicedataVnfsVnf();
1538             vnf.setVnfId(vnfId);
1539             vnf.setVnfData(new GenericResourceApiServicedataServicedataVnfsVnfVnfData());
1540             vnf.getVnfData().setVnfLevelOperStatus(genericResourceApiOperStatusDataBodyParam);
1541             vnflist.addVnfItem(vnf);
1542             responseStatus = HttpStatus.CREATED;
1543         } else {
1544             log.info("Updating VNF data for ({})", vnfId);
1545             // Filter out all of the other vnf objects into a new VNF List
1546             // Replace if a delete method exists
1547             svcData.getVnfs().getVnf().stream().filter(targetVnf -> !targetVnf.getVnfId().equals(vnfId))
1548                     .forEach(vnflist::addVnfItem);
1549             GenericResourceApiServicedataServicedataVnfsVnf vnf = new GenericResourceApiServicedataServicedataVnfsVnf();
1550             // If the vnf exists, set it up with new data
1551             Optional<GenericResourceApiServicedataServicedataVnfsVnf> vnfOptional = getVnfObject(data, vnfId);
1552             if (vnfOptional.isPresent()) {
1553                 vnf = vnfOptional.get();
1554             }
1555             if (vnf.getVnfData() == null) {
1556                 vnf.setVnfData(new GenericResourceApiServicedataServicedataVnfsVnfVnfData());
1557                 responseStatus = HttpStatus.CREATED;
1558             }
1559
1560             vnf.getVnfData().setVnfLevelOperStatus(genericResourceApiOperStatusDataBodyParam);
1561             vnflist.addVnfItem(vnf);
1562         }
1563
1564         svcData.setVnfs(vnflist);
1565         // Map and save the new data
1566         try {
1567             data.setSvcData(objectMapper.writeValueAsString(svcData));
1568             configServicesRepository.save(data);
1569             return new ResponseEntity<>(responseStatus);
1570         } catch (JsonProcessingException e) {
1571             log.error("Error mapping object to JSON", e);
1572             // Should probably be a 500 INTERNAL_SERVICE_ERROR
1573             return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
1574         }
1575     }
1576
1577     /**
1578      * Creates or updates VNF Onap Model Information data in the Config table for a
1579      * specified Service Instance. If it is a new Service Instance or a new VNF,
1580      * creates all necessary parent data containers, then performs the updates.
1581      * <p>
1582      * Maps to
1583      * /config/GENERIC-RESOURCE-API:services/service/{service-instance-id}/service-data/vnfs/vnf/{vnf-id}/vnf-data/vnf-topology/onap-model-information/
1584      * 
1585      * @param serviceInstanceId                                                   the
1586      *                                                                            Service
1587      *                                                                            Instance
1588      *                                                                            ID
1589      *                                                                            to
1590      *                                                                            perform
1591      *                                                                            the
1592      *                                                                            delete
1593      *                                                                            on
1594      * @param vnfId                                                               the
1595      *                                                                            VNF
1596      *                                                                            ID
1597      *                                                                            of
1598      *                                                                            the
1599      *                                                                            VNF
1600      *                                                                            to
1601      *                                                                            delete
1602      * @param genericResourceApiOnapmodelinformationOnapModelInformationBodyParam the
1603      *                                                                            payload
1604      * @return HttpStatus.CREATED (201) on successful create.
1605      *         <p>
1606      *         HttpStatus.NO_CONTENT (204) on successful update.
1607      *         <p>
1608      *         HttpStatus.BAD_REQUEST (400) if updating the database fails.
1609      * @throws RestException
1610      */
1611     @Override
1612     public ResponseEntity<Void> configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceDataVnfsVnfVnfIdVnfDataVnfTopologyOnapModelInformationPut(
1613             String serviceInstanceId, String vnfId,
1614             GenericResourceApiOnapmodelinformationOnapModelInformation genericResourceApiOnapmodelinformationOnapModelInformationBodyParam)
1615             throws RestException {
1616         log.info("PUT | VNF Topology Onap Model Information ({})", vnfId);
1617         if (getObjectMapper().isPresent() && getAcceptHeader().isPresent()) {
1618         } else {
1619             log.warn(
1620                     "ObjectMapper or HttpServletRequest not configured in default ConfigApi interface so no example is generated");
1621         }
1622
1623         List<ConfigServices> services = configServicesRepository.findBySvcInstanceId(serviceInstanceId);
1624         ConfigServices data;
1625         if ((services == null) || (services.isEmpty())) {
1626             log.info("Could not find data for ({}). Creating new Service Object.", serviceInstanceId);
1627             data = new ConfigServices();
1628             data.setSvcInstanceId(serviceInstanceId);
1629         } else {
1630             data = services.get(0);
1631         }
1632
1633         GenericResourceApiServicedataServiceData svcData = null;
1634         try {
1635             svcData = objectMapper.readValue(data.getSvcData(), GenericResourceApiServicedataServiceData.class);
1636         } catch (JsonProcessingException e) {
1637             log.error("Could not map service data for ({})", serviceInstanceId);
1638         }
1639         if (svcData == null) {
1640             log.info("Could not find Service Data for ({}). Creating new Service Data Container", serviceInstanceId);
1641             svcData = new GenericResourceApiServicedataServiceData();
1642         }
1643         if (svcData.getVnfs() == null) {
1644             log.info("VNF List not found for ({}). Creating new VNF List Container.", serviceInstanceId);
1645             svcData.setVnfs(new GenericResourceApiServicedataServicedataVnfs());
1646             svcData.getVnfs().setVnf(new ArrayList<>());
1647         }
1648
1649         GenericResourceApiServicedataServicedataVnfs vnflist = new GenericResourceApiServicedataServicedataVnfs();
1650         HttpStatus responseStatus = HttpStatus.NO_CONTENT;
1651         if (svcData.getVnfs().getVnf().isEmpty()) {
1652             log.info("Creating VNF data for ({})", vnfId);
1653             GenericResourceApiServicedataServicedataVnfsVnf vnf = new GenericResourceApiServicedataServicedataVnfsVnf();
1654             vnf.setVnfId(vnfId);
1655             vnf.setVnfData(new GenericResourceApiServicedataServicedataVnfsVnfVnfData());
1656             vnf.getVnfData().setVnfTopology(new GenericResourceApiVnftopologyVnfTopology());
1657             vnf.getVnfData().getVnfTopology()
1658                     .setOnapModelInformation(genericResourceApiOnapmodelinformationOnapModelInformationBodyParam);
1659             vnflist.addVnfItem(vnf);
1660             responseStatus = HttpStatus.CREATED;
1661         } else {
1662             log.info("Updating VNF data for ({})", vnfId);
1663             // Filter out all of the other vnf objects into a new VNF List
1664             // Replace if a delete method exists
1665             svcData.getVnfs().getVnf().stream().filter(targetVnf -> !targetVnf.getVnfId().equals(vnfId))
1666                     .forEach(vnflist::addVnfItem);
1667             GenericResourceApiServicedataServicedataVnfsVnf vnf = new GenericResourceApiServicedataServicedataVnfsVnf();
1668             // If the vnf exists, set it up with new data
1669             Optional<GenericResourceApiServicedataServicedataVnfsVnf> vnfOptional = getVnfObject(data, vnfId);
1670             if (vnfOptional.isPresent()) {
1671                 vnf = vnfOptional.get();
1672             }
1673             if (vnf.getVnfData() == null) {
1674                 vnf.setVnfData(new GenericResourceApiServicedataServicedataVnfsVnfVnfData());
1675             }
1676             if (vnf.getVnfData().getVnfTopology() == null) {
1677                 vnf.getVnfData().setVnfTopology(new GenericResourceApiVnftopologyVnfTopology());
1678                 responseStatus = HttpStatus.CREATED;
1679             }
1680
1681             vnf.getVnfData().getVnfTopology()
1682                     .setOnapModelInformation(genericResourceApiOnapmodelinformationOnapModelInformationBodyParam);
1683             vnflist.addVnfItem(vnf);
1684         }
1685
1686         svcData.setVnfs(vnflist);
1687         // Map and save the new data
1688         try {
1689             data.setSvcData(objectMapper.writeValueAsString(svcData));
1690             configServicesRepository.save(data);
1691             return new ResponseEntity<>(responseStatus);
1692         } catch (JsonProcessingException e) {
1693             log.error("Error mapping object to JSON", e);
1694             // Should probably be a 500 INTERNAL_SERVICE_ERROR
1695             return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
1696         }
1697     }
1698
1699     /**
1700      * Creates or updates VNF Network data in the Config table for a specified
1701      * Service Instance. If it is a new Service Instance or a new VNF, creates all
1702      * necessary parent data containers, then performs the updates.
1703      * <p>
1704      * Maps to /config/GENERIC-RESOURCE-API:services/service/{service-instance-id}/service-data/vnfs/vnf/{vnf-id}/vnf-data/vnf-topology/vnf-resource-assignments/vnf-networks/
1705      * @param serviceInstanceId the Service Instance ID to perform the delete on
1706      * @param vnfId the VNF ID of the VNF to delete
1707      * @param genericResourceApiVnfresourceassignmentsVnfresourceassignmentsVnfNetworksBodyParam the * payload
1708      * @return HttpStatus.CREATED (201) on successful create.
1709      * <p>
1710      * HttpStatus.NO_CONTENT (204) on successful update.
1711      * <p>
1712      * HttpStatus.BAD_REQUEST (400) if updating the database fails.
1713      * @throws RestException
1714      */
1715     @Override
1716     public ResponseEntity<Void> configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceDataVnfsVnfVnfIdVnfDataVnfTopologyVnfResourceAssignmentsVnfNetworksPut(
1717             String serviceInstanceId, String vnfId,
1718             GenericResourceApiVnfresourceassignmentsVnfresourceassignmentsVnfNetworks genericResourceApiVnfresourceassignmentsVnfresourceassignmentsVnfNetworksBodyParam)
1719             throws RestException {
1720         log.info("PUT | VNF Topology VNF Resource Assignments VNF Networks ({})", vnfId);
1721         if (getObjectMapper().isPresent() && getAcceptHeader().isPresent()) {
1722         } else {
1723             log.warn(
1724                     "ObjectMapper or HttpServletRequest not configured in default ConfigApi interface so no example is generated");
1725         }
1726
1727         List<ConfigServices> services = configServicesRepository.findBySvcInstanceId(serviceInstanceId);
1728         ConfigServices data;
1729         if ((services == null) || (services.isEmpty())) {
1730             log.info("Could not find data for ({}). Creating new Service Object.", serviceInstanceId);
1731             data = new ConfigServices();
1732             data.setSvcInstanceId(serviceInstanceId);
1733         } else {
1734             data = services.get(0);
1735         }
1736
1737         GenericResourceApiServicedataServiceData svcData = null;
1738         try {
1739             svcData = objectMapper.readValue(data.getSvcData(), GenericResourceApiServicedataServiceData.class);
1740         } catch (JsonProcessingException e) {
1741             log.error("Could not map service data for ({})", serviceInstanceId);
1742         }
1743         if (svcData == null) {
1744             log.info("Could not find Service Data for ({}). Creating new Service Data Container", serviceInstanceId);
1745             svcData = new GenericResourceApiServicedataServiceData();
1746         }
1747         if (svcData.getVnfs() == null) {
1748             log.info("VNF List not found for ({}). Creating new VNF List Container.", serviceInstanceId);
1749             svcData.setVnfs(new GenericResourceApiServicedataServicedataVnfs());
1750             svcData.getVnfs().setVnf(new ArrayList<>());
1751         }
1752
1753         GenericResourceApiServicedataServicedataVnfs vnflist = new GenericResourceApiServicedataServicedataVnfs();
1754         HttpStatus responseStatus = HttpStatus.NO_CONTENT;
1755         if (svcData.getVnfs().getVnf().isEmpty()) {
1756             log.info("Creating VNF data for ({})", vnfId);
1757             GenericResourceApiServicedataServicedataVnfsVnf vnf = new GenericResourceApiServicedataServicedataVnfsVnf();
1758             vnf.setVnfId(vnfId);
1759             vnf.setVnfData(new GenericResourceApiServicedataServicedataVnfsVnfVnfData());
1760             vnf.getVnfData().setVnfTopology(new GenericResourceApiVnftopologyVnfTopology());
1761             vnf.getVnfData().getVnfTopology()
1762                     .setVnfResourceAssignments(new GenericResourceApiVnfresourceassignmentsVnfResourceAssignments());
1763             vnf.getVnfData().getVnfTopology().getVnfResourceAssignments()
1764                     .setVnfNetworks(genericResourceApiVnfresourceassignmentsVnfresourceassignmentsVnfNetworksBodyParam);
1765             vnflist.addVnfItem(vnf);
1766             responseStatus = HttpStatus.CREATED;
1767         } else {
1768             log.info("Updating VNF data for ({})", vnfId);
1769             // Filter out all of the other vnf objects into a new VNF List
1770             // Replace if a delete method exists
1771             svcData.getVnfs().getVnf().stream().filter(targetVnf -> !targetVnf.getVnfId().equals(vnfId))
1772                     .forEach(vnflist::addVnfItem);
1773             GenericResourceApiServicedataServicedataVnfsVnf vnf = new GenericResourceApiServicedataServicedataVnfsVnf();
1774             // If the vnf exists, set it up with new data
1775             Optional<GenericResourceApiServicedataServicedataVnfsVnf> vnfOptional = getVnfObject(data, vnfId);
1776             if (vnfOptional.isPresent()) {
1777                 vnf = vnfOptional.get();
1778             }
1779             if (vnf.getVnfData() == null) {
1780                 vnf.setVnfData(new GenericResourceApiServicedataServicedataVnfsVnfVnfData());
1781             }
1782             if (vnf.getVnfData().getVnfTopology() == null) {
1783                 vnf.getVnfData().setVnfTopology(new GenericResourceApiVnftopologyVnfTopology());
1784             }
1785             if (vnf.getVnfData().getVnfTopology().getVnfResourceAssignments() == null) {
1786                 vnf.getVnfData().getVnfTopology().setVnfResourceAssignments(
1787                         new GenericResourceApiVnfresourceassignmentsVnfResourceAssignments());
1788                 responseStatus = HttpStatus.CREATED;
1789             }
1790
1791             vnf.getVnfData().getVnfTopology().getVnfResourceAssignments()
1792                     .setVnfNetworks(genericResourceApiVnfresourceassignmentsVnfresourceassignmentsVnfNetworksBodyParam);
1793             vnflist.addVnfItem(vnf);
1794         }
1795
1796         svcData.setVnfs(vnflist);
1797         // Map and save the new data
1798         try {
1799             data.setSvcData(objectMapper.writeValueAsString(svcData));
1800             configServicesRepository.save(data);
1801             return new ResponseEntity<>(responseStatus);
1802         } catch (JsonProcessingException e) {
1803             log.error("Error mapping object to JSON", e);
1804             // Should probably be a 500 INTERNAL_SERVICE_ERROR
1805             return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
1806         }
1807     }
1808
1809     /**
1810      * Creates or updates VNF Network Role data in the Config table for a specified
1811      * Service Instance. If it is a new Service Instance or a new VNF, creates all
1812      * necessary parent data containers, then performs the updates.
1813      * <p>
1814      * Maps to
1815      * /config/GENERIC-RESOURCE-API:services/service/{service-instance-id}/service-data/vnfs/vnf/{vnf-id}/vnf-data/vnf-topology/vnf-resource-assignments/vnf-networks/vnf-network/{network-role}/
1816      * 
1817      * @param serviceInstanceId                         the Service Instance ID to
1818      *                                                  perform the delete on
1819      * @param vnfId                                     the VNF ID of the VNF to
1820      *                                                  delete
1821      * @param genericResourceApiVnfNetworkDataBodyParam the payload
1822      * @return HttpStatus.CREATED (201) on successful create.
1823      *         <p>
1824      *         HttpStatus.NO_CONTENT (204) on successful update.
1825      *         <p>
1826      *         HttpStatus.BAD_REQUEST (400) if updating the database fails.
1827      * @throws RestException
1828      */
1829     @Override
1830     public ResponseEntity<Void> configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceDataVnfsVnfVnfIdVnfDataVnfTopologyVnfResourceAssignmentsVnfNetworksVnfNetworkNetworkRolePut(
1831             String serviceInstanceId, String vnfId, String networkRole,
1832             GenericResourceApiVnfNetworkData genericResourceApiVnfNetworkDataBodyParam) throws RestException {
1833         log.info("PUT | VNF Network Network Role ({})", vnfId);
1834         if (!networkRole.equals(genericResourceApiVnfNetworkDataBodyParam.getNetworkRole())) {
1835             throw new RestProtocolException("bad-attribute", "network-role mismatch", HttpStatus.BAD_REQUEST.value());
1836         }
1837         if (getObjectMapper().isPresent() && getAcceptHeader().isPresent()) {
1838         } else {
1839             log.warn(
1840                     "ObjectMapper or HttpServletRequest not configured in default ConfigApi interface so no example is generated");
1841         }
1842
1843         List<ConfigServices> services = configServicesRepository.findBySvcInstanceId(serviceInstanceId);
1844         ConfigServices data;
1845         if ((services == null) || (services.isEmpty())) {
1846             log.info("Could not find data for ({}). Creating new Service Object.", serviceInstanceId);
1847             data = new ConfigServices();
1848             data.setSvcInstanceId(serviceInstanceId);
1849         } else {
1850             data = services.get(0);
1851         }
1852
1853         GenericResourceApiServicedataServiceData svcData = null;
1854         try {
1855             svcData = objectMapper.readValue(data.getSvcData(), GenericResourceApiServicedataServiceData.class);
1856         } catch (JsonProcessingException e) {
1857             log.error("Could not map service data for ({})", serviceInstanceId);
1858         }
1859         if (svcData == null) {
1860             log.info("Could not find Service Data for ({}). Creating new Service Data Container", serviceInstanceId);
1861             svcData = new GenericResourceApiServicedataServiceData();
1862         }
1863         if (svcData.getVnfs() == null) {
1864             log.info("VNF List not found for ({}). Creating new VNF List Container.", serviceInstanceId);
1865             svcData.setVnfs(new GenericResourceApiServicedataServicedataVnfs());
1866             svcData.getVnfs().setVnf(new ArrayList<>());
1867         }
1868
1869         GenericResourceApiServicedataServicedataVnfs vnflist = new GenericResourceApiServicedataServicedataVnfs();
1870         HttpStatus responseStatus = HttpStatus.NO_CONTENT;
1871         if (svcData.getVnfs().getVnf().isEmpty()) {
1872             log.info("Creating VNF data for ({})", vnfId);
1873             GenericResourceApiServicedataServicedataVnfsVnf vnf = new GenericResourceApiServicedataServicedataVnfsVnf();
1874             vnf.setVnfId(vnfId);
1875             vnf.setVnfData(new GenericResourceApiServicedataServicedataVnfsVnfVnfData());
1876             vnf.getVnfData().setVnfTopology(new GenericResourceApiVnftopologyVnfTopology());
1877             vnf.getVnfData().getVnfTopology()
1878                     .setVnfResourceAssignments(new GenericResourceApiVnfresourceassignmentsVnfResourceAssignments());
1879             vnf.getVnfData().getVnfTopology().getVnfResourceAssignments()
1880                     .setVnfNetworks(new GenericResourceApiVnfresourceassignmentsVnfresourceassignmentsVnfNetworks());
1881             vnf.getVnfData().getVnfTopology().getVnfResourceAssignments().getVnfNetworks()
1882                     .setVnfNetwork(new ArrayList<>());
1883             vnf.getVnfData().getVnfTopology().getVnfResourceAssignments().getVnfNetworks()
1884                     .addVnfNetworkItem(genericResourceApiVnfNetworkDataBodyParam);
1885             vnflist.addVnfItem(vnf);
1886             responseStatus = HttpStatus.CREATED;
1887         } else {
1888             log.info("Updating VNF data for ({})", vnfId);
1889             // Filter out all of the other vnf objects into a new VNF List
1890             // Replace if a delete method exists
1891             svcData.getVnfs().getVnf().stream().filter(targetVnf -> !targetVnf.getVnfId().equals(vnfId))
1892                     .forEach(vnflist::addVnfItem);
1893             GenericResourceApiServicedataServicedataVnfsVnf vnf = new GenericResourceApiServicedataServicedataVnfsVnf();
1894             // If the vnf exists, set it up with new data
1895             Optional<GenericResourceApiServicedataServicedataVnfsVnf> vnfOptional = getVnfObject(data, vnfId);
1896             if (vnfOptional.isPresent()) {
1897                 vnf = vnfOptional.get();
1898             }
1899             if (vnf.getVnfData() == null) {
1900                 vnf.setVnfData(new GenericResourceApiServicedataServicedataVnfsVnfVnfData());
1901             }
1902             if (vnf.getVnfData().getVnfTopology() == null) {
1903                 vnf.getVnfData().setVnfTopology(new GenericResourceApiVnftopologyVnfTopology());
1904             }
1905             if (vnf.getVnfData().getVnfTopology().getVnfResourceAssignments() == null) {
1906                 vnf.getVnfData().getVnfTopology().setVnfResourceAssignments(
1907                         new GenericResourceApiVnfresourceassignmentsVnfResourceAssignments());
1908             }
1909             if (vnf.getVnfData().getVnfTopology().getVnfResourceAssignments().getVnfNetworks() == null) {
1910                 log.info("Creating new VnfNetworks");
1911                 vnf.getVnfData().getVnfTopology().getVnfResourceAssignments().setVnfNetworks(
1912                         new GenericResourceApiVnfresourceassignmentsVnfresourceassignmentsVnfNetworks());
1913             }
1914
1915             GenericResourceApiVnfresourceassignmentsVnfresourceassignmentsVnfNetworks networkList = new GenericResourceApiVnfresourceassignmentsVnfresourceassignmentsVnfNetworks();
1916             if (vnf.getVnfData().getVnfTopology().getVnfResourceAssignments().getVnfNetworks().getVnfNetwork()
1917                     .isEmpty()) {
1918                 log.info("First entry into network info.");
1919                 vnf.getVnfData().getVnfTopology().getVnfResourceAssignments().getVnfNetworks()
1920                         .addVnfNetworkItem(genericResourceApiVnfNetworkDataBodyParam);
1921                 responseStatus = HttpStatus.CREATED;
1922             } else {
1923                 log.info("Found networks. Filtering.");
1924                 vnf.getVnfData().getVnfTopology().getVnfResourceAssignments().getVnfNetworks().getVnfNetwork().stream()
1925                         .filter(targetNetwork -> !targetNetwork.getNetworkRole().equals(networkRole))
1926                         .forEach(networkList::addVnfNetworkItem);
1927                 networkList.addVnfNetworkItem(genericResourceApiVnfNetworkDataBodyParam);
1928
1929                 if (networkList.getVnfNetwork().size() != vnf.getVnfData().getVnfTopology().getVnfResourceAssignments()
1930                         .getVnfNetworks().getVnfNetwork().size()) {
1931                     log.info("Added a new Item");
1932                     responseStatus = HttpStatus.CREATED;
1933                 }
1934                 vnf.getVnfData().getVnfTopology().getVnfResourceAssignments().setVnfNetworks(networkList);
1935             }
1936
1937             vnflist.addVnfItem(vnf);
1938         }
1939
1940         svcData.setVnfs(vnflist);
1941         // Map and save the new data
1942         try {
1943             data.setSvcData(objectMapper.writeValueAsString(svcData));
1944             configServicesRepository.save(data);
1945             return new ResponseEntity<>(responseStatus);
1946         } catch (JsonProcessingException e) {
1947             log.error("Error mapping object to JSON", e);
1948             // Should probably be a 500 INTERNAL_SERVICE_ERROR
1949             return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
1950         }
1951     }
1952
1953     /**
1954      * Extracts a VNF object from the database,
1955      * 
1956      * @param configServices A Config Services option created from a Service
1957      *                       Instance ID
1958      * @param vnfId          the target VNF ID
1959      * @return An empty Optional if the Service Data does not exist, an empty
1960      *         Optional if the VNF is not found, or an optional containing the found
1961      *         VNF.
1962      */
1963     private Optional<GenericResourceApiServicedataServicedataVnfsVnf> getVnfObject(ConfigServices configServices,
1964             String vnfId) {
1965         // Map the Marshall the JSON String into a Java Object
1966         log.info("Getting VNF Data for ({})", vnfId);
1967         GenericResourceApiServicedataServiceData svcData;
1968         try {
1969             svcData = objectMapper.readValue(configServices.getSvcData(),
1970                     GenericResourceApiServicedataServiceData.class);
1971         } catch (JsonProcessingException e) {
1972             log.error("Error", e);
1973             return Optional.empty();
1974         }
1975
1976         /*
1977          * Get a stream of the VNF Objects and return the target if it's found, assuming
1978          * that each VNF ID is unique within a Service Instance Object
1979          */
1980         return svcData.getVnfs().getVnf().stream().filter(targetVnf -> targetVnf.getVnfId().equals(vnfId)).findFirst();
1981     }
1982
1983     @Override
1984     public ResponseEntity<GenericResourceApiServicetopologyServiceTopology> configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceDataServiceTopologyGet(
1985             String serviceInstanceId) throws RestApplicationException, RestProtocolException {
1986         GenericResourceApiServicetopologyServiceTopology serviceTopology = null;
1987         GenericResourceApiServicedataServiceData serviceData = null;
1988
1989         List<ConfigServices> services = configServicesRepository.findBySvcInstanceId(serviceInstanceId);
1990         if ((services == null) || (services.isEmpty())) {
1991             throw new RestProtocolException("data-missing", "No service entry found", HttpStatus.NOT_FOUND.value());
1992         }
1993
1994         try {
1995             if (services.get(0).getSvcData().isEmpty()) {
1996                 throw new RestProtocolException("data-missing", "No service-data entry found",
1997                         HttpStatus.NOT_FOUND.value());
1998             } else {
1999                 serviceData = objectMapper.readValue(services.get(0).getSvcData(),
2000                         GenericResourceApiServicedataServiceData.class);
2001                 serviceTopology = serviceData.getServiceTopology();
2002             }
2003             if (serviceTopology == null) {
2004                 throw new RestProtocolException("data-missing", "No service-topology entry found",
2005                         HttpStatus.NOT_FOUND.value());
2006             }
2007             return new ResponseEntity<>(serviceTopology, HttpStatus.OK);
2008         } catch (JsonProcessingException e) {
2009             log.error("Could not parse service data", e);
2010             throw new RestApplicationException("data-conversion",
2011                     "Request could not be completed due to internal error", e,
2012                     HttpStatus.INTERNAL_SERVER_ERROR.value());
2013         }
2014
2015     }
2016
2017     /**
2018      * Extracts VF MODULE data from CONFIG_GRA_SERVICES for a given, service-instance-id, vnf-id, and vf-module-id
2019      * <p>
2020      * Maps to /config/GENERIC-RESOURCE-API:services/service/{service-instance-id}/service-data/vnfs/vnf/{vnf-id}/vnf-data/vf-modules/vf-module/{vf-module-id}/
2021      * @param serviceInstanceId the Service Instance ID to lookup data for
2022      * @param vnfId the VNF ID of the VNF to return
2023      * @param vfModuleId the vf-moudle ID of a specific VNF to return
2024      * @return HttpStatus.OK (200) if the data is found.
2025      * @throws RestException if the data does not exist.
2026      */
2027     @Override
2028     public ResponseEntity<GenericResourceApiServicedataServicedataVnfsVnfVnfdataVfmodulesVfModule>
2029     configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceDataVnfsVnfVnfIdVnfDataVfModulesVfModuleVfModuleIdGet(
2030             String serviceInstanceId, String vnfId, String vfModuleId) throws RestException {
2031
2032         log.info("GET | Vf Module Data for ({})", vfModuleId);
2033
2034         if(getObjectMapper().isPresent() && getAcceptHeader().isPresent()) {
2035             if(getAcceptHeader().get().contains("application/json")) {
2036             }
2037         } else {
2038             log.warn("ObjectMapper or HttpServletRequest not configured in default ConfigApi interface so no example is generated");
2039         }
2040         List<ConfigServices> services = configServicesRepository.findBySvcInstanceId(serviceInstanceId);
2041         if((services == null) || (services.isEmpty())) {
2042             throw new RestProtocolException("data-missing", "No service entry found", HttpStatus.NOT_FOUND.value());
2043         }
2044
2045         Optional<GenericResourceApiServicedataServicedataVnfsVnfVnfdataVfmodulesVfModule> vfModule =
2046                 getVfModuleObject(services.get(0), vnfId, vfModuleId);
2047         if(vfModule.isPresent()) {
2048             return new ResponseEntity<>(vfModule.get(), HttpStatus.OK);
2049         } else {
2050             log.info("No vf-module found for [{}]", vfModuleId);
2051             throw new RestApplicationException("data-missing",
2052                     "Request could not be completed because the relevant data model content does not exist",
2053                     HttpStatus.NOT_FOUND.value());
2054         }
2055     }
2056
2057     /**
2058      * PUT VF MODULE data into CONFIG_GRA_SERVICES of a given, service-instance-id, vnf-id
2059      * <p>
2060      * Maps to /config/GENERIC-RESOURCE-API:services/service/{service-instance-id}/service-data/vnfs/vnf/{vnf-id}/vnf-data/vf-modules/vf-module/{vf-module-id}/
2061      * @param serviceInstanceId the Service Instance ID
2062      * @param vnfId the VNF ID as the parent of the specified vf-module-id and child of the specified service-instance
2063      * @param vfModuleId the vf-moudle ID as a child of the specified VNF
2064      * @return HttpStatus.OK (200) if the data is found.
2065      * @throws RestException if the data does not exist.
2066      */
2067     @Override
2068     public ResponseEntity<Void> configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceDataVnfsVnfVnfIdVnfDataVfModulesVfModuleVfModuleIdPut(
2069             String serviceInstanceId, String vnfId, String vfModuleId,
2070             GenericResourceApiServicedataServicedataVnfsVnfVnfdataVfmodulesVfModule genericResourceApiServicedataServicedataVnfsVnfVnfdataVfmodulesVfModuleBodyParam)
2071             throws RestException {
2072         log.info("PUT | vf-module Data of ({}) for vnf ({}) in service({})", vfModuleId, vnfId, serviceInstanceId);
2073
2074         if(! vfModuleId.equals(genericResourceApiServicedataServicedataVnfsVnfVnfdataVfmodulesVfModuleBodyParam.getVfModuleId())) {
2075             throw new RestProtocolException("bad-attribute", "vf-module-id mismatch", HttpStatus.BAD_REQUEST.value());
2076         }
2077         if(getObjectMapper().isPresent() && getAcceptHeader().isPresent()) {
2078             log.info("Something with header");
2079         } else {
2080             log.warn("ObjectMapper or HttpServletRequest not configured in default ConfigApi interface so no example is generated");
2081         }
2082
2083         HttpStatus responseStatus = HttpStatus.NO_CONTENT;
2084         ConfigServices service;
2085         GenericResourceApiServicedataServiceData svcData;
2086         List<ConfigServices> services = configServicesRepository.findBySvcInstanceId(serviceInstanceId);
2087
2088         if((services == null) || (services.isEmpty())) {
2089             log.error("service-instance-id ({}) not found in SDN.", serviceInstanceId);
2090             return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
2091         } else {
2092             service = services.get(0);
2093         }
2094
2095         try {
2096             svcData = objectMapper.readValue(service.getSvcData(), GenericResourceApiServicedataServiceData.class);
2097         } catch(JsonProcessingException e) {
2098             log.error("Error", e);
2099             return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
2100         }
2101
2102         GenericResourceApiServicedataServicedataVnfs vnfs = svcData.getVnfs();
2103         Optional<GenericResourceApiServicedataServicedataVnfsVnf> vnf =
2104                 vnfs.getVnf()
2105                         .stream()
2106                         .filter(targetVnf -> targetVnf.getVnfId().equals(vnfId))
2107                         .findFirst();
2108
2109         if(! vnf.isPresent()) {
2110             log.error("vnf-id ({}) not found in SDN.", vnfId);
2111             return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
2112         }
2113
2114         GenericResourceApiServicedataServicedataVnfsVnfVnfData vnfData = vnf.get().getVnfData();
2115         GenericResourceApiServicedataServicedataVnfsVnfVnfdataVfModules existingVfModules = vnfData.getVfModules();
2116         GenericResourceApiServicedataServicedataVnfsVnfVnfdataVfModules newVfModules = new GenericResourceApiServicedataServicedataVnfsVnfVnfdataVfModules();
2117
2118         if (existingVfModules == null || existingVfModules.getVfModule().isEmpty()) {
2119             log.info("No existing vf-module found. Creating the first vf-module for vnf [{}]", vnfId);
2120             newVfModules.addVfModuleItem(genericResourceApiServicedataServicedataVnfsVnfVnfdataVfmodulesVfModuleBodyParam);
2121             responseStatus = HttpStatus.CREATED;
2122         }
2123         else {
2124             ArrayList<String> vfModuleIds = new ArrayList<>();
2125             for (GenericResourceApiServicedataServicedataVnfsVnfVnfdataVfmodulesVfModule vf : existingVfModules.getVfModule()) {
2126                 vfModuleIds.add(vf.getVfModuleId());
2127             }
2128             log.info("[{}] vf-module(s) {} found in vnf [{}]", existingVfModules.getVfModule().size(), vfModuleIds, vnfId);
2129             if (!vfModuleIds.isEmpty() && vfModuleIds.contains(vfModuleId)) {
2130                 log.info("Overwriting vf-module [{}] in vnf [{}]",  vfModuleId, vnfId);
2131             } else {
2132                 log.info("Adding vf-module [{}] to vnf [{}]", vfModuleId, vnfId);
2133             }
2134             existingVfModules.getVfModule()
2135                     .stream()
2136                     .filter(vf -> ! vf.getVfModuleId().equals(vfModuleId))
2137                     .forEach(newVfModules::addVfModuleItem);
2138             newVfModules.addVfModuleItem(genericResourceApiServicedataServicedataVnfsVnfVnfdataVfmodulesVfModuleBodyParam);
2139             responseStatus = HttpStatus.OK;
2140         }
2141         vnfData.setVfModules(newVfModules);
2142         // Map and save the new data
2143         try {
2144             service.setSvcData(objectMapper.writeValueAsString(svcData));
2145             configServicesRepository.save(service);
2146             return new ResponseEntity<>(responseStatus);
2147         } catch(JsonProcessingException e) {
2148             log.error("Error mapping object to JSON", e);
2149             // Should probably be a 500 INTERNAL_SERVICE_ERROR
2150             return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
2151         }
2152     }
2153
2154     /**
2155      * Extracts VF MODULE Topology data from the Config table specified Service
2156      * Instance and VNF ID.
2157      * <p>
2158      * Maps to /config/GENERIC-RESOURCE-API:services/service/{service-instance-id}/service-data/vnfs/vnf/{vnf-id}/vnf-data/vf-modules/vf-module/{vf-module-id}/vf-module-data/vf-module-topology/
2159      * @param serviceInstanceId the Service Instance ID to lookup data for
2160      * @param vnfId the VNF ID of the VNF to extract topology data from.
2161      * @param vfModuleId the vf-module-idof the vf-module to extract topology data from.
2162      * @return HttpStatus.OK (200) if the data is found.
2163      * @throws RestException if the data does not exist.
2164      */
2165     @Override
2166     public ResponseEntity<GenericResourceApiVfmoduletopologyVfModuleTopology>
2167     configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceDataVnfsVnfVnfIdVnfDataVfModulesVfModuleVfModuleIdVfModuleDataVfModuleTopologyGet(
2168             String serviceInstanceId, String vnfId, String vfModuleId) throws RestException {
2169         log.info("GET | vf-module-topology for ({})", vfModuleId);
2170         if(getObjectMapper().isPresent() && getAcceptHeader().isPresent()) {
2171             if (getAcceptHeader().get().contains("application/json")) {
2172                 log.info("Something with header");
2173             }
2174         } else {
2175             log.warn("ObjectMapper or HttpServletRequest not configured in default ConfigApi interface so no example is generated");
2176         }
2177         List<ConfigServices> services = configServicesRepository.findBySvcInstanceId(serviceInstanceId);
2178         if((services == null) || (services.isEmpty())) {
2179             throw new RestApplicationException("data-missing",
2180                     "Request could not be completed because the relevant data model content does not exist",
2181                     HttpStatus.NOT_FOUND.value());
2182         }
2183
2184         Optional<GenericResourceApiServicedataServicedataVnfsVnfVnfdataVfmodulesVfModule> vfModule =
2185                 getVfModuleObject(services.get(0), vnfId, vfModuleId);
2186         if(vfModule.isPresent()
2187                 && vfModule.get().getVfModuleData() != null
2188                 && vfModule.get().getVfModuleData().getVfModuleTopology() != null) {
2189             return new ResponseEntity<>(vfModule.get().getVfModuleData().getVfModuleTopology(), HttpStatus.OK);
2190         } else {
2191             log.info("No information found for {}", vfModuleId);
2192             throw new RestApplicationException("data-missing",
2193                     "Request could not be completed because the relevant data model content does not exist",
2194                     HttpStatus.NOT_FOUND.value());
2195         }
2196     }
2197
2198     @Override
2199     public ResponseEntity<Void> configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceDataVnfsVnfVnfIdVnfDataVfModulesVfModuleVfModuleIdDelete(
2200             String serviceInstanceId, String vnfId, String vfModuleId) throws RestProtocolException {
2201
2202         log.info("DELETE | vf-module Data for ({})", vfModuleId);
2203
2204         if (getObjectMapper().isPresent() && getAcceptHeader().isPresent()) {
2205             log.info("Something with header.");
2206         } else {
2207             log.warn("ObjectMapper or HttpServletRequest not configured in default ConfigApi interface so no example is generated");
2208         }
2209
2210         ConfigServices service;
2211         GenericResourceApiServicedataServiceData svcData;
2212         List<ConfigServices> services = configServicesRepository.findBySvcInstanceId(serviceInstanceId);
2213
2214         if((services == null) || (services.isEmpty())) {
2215             log.error("service-instance-id ({}) not found in SDN.", serviceInstanceId);
2216             return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
2217         } else {
2218             service = services.get(0);
2219         }
2220
2221         try {
2222             svcData = objectMapper.readValue(service.getSvcData(), GenericResourceApiServicedataServiceData.class);
2223         } catch(JsonProcessingException e) {
2224             log.error("Error", e);
2225             return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
2226         }
2227
2228         GenericResourceApiServicedataServicedataVnfs vnfs = svcData.getVnfs();
2229         Optional<GenericResourceApiServicedataServicedataVnfsVnf> vnf =
2230                 vnfs.getVnf()
2231                         .stream()
2232                         .filter(targetVnf -> targetVnf.getVnfId().equals(vnfId))
2233                         .findFirst();
2234
2235         if(! vnf.isPresent()) {
2236             log.error("vnf-id ({}) not found in SDN.", vnfId);
2237             return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
2238         }
2239
2240         GenericResourceApiServicedataServicedataVnfsVnfVnfData vnfData = vnf.get().getVnfData();
2241         GenericResourceApiServicedataServicedataVnfsVnfVnfdataVfModules existingVfModules = vnfData.getVfModules();
2242         GenericResourceApiServicedataServicedataVnfsVnfVnfdataVfModules newVfModules = new GenericResourceApiServicedataServicedataVnfsVnfVnfdataVfModules();
2243
2244         if (existingVfModules == null || existingVfModules.getVfModule().isEmpty()) {
2245             log.info("No existing vf-module found. Creating the first vf-module for vnf [{}]", vnfId);
2246             return new ResponseEntity<>(HttpStatus.OK);
2247         }
2248
2249         ArrayList<String> vfModuleIds = new ArrayList<>();
2250         for (GenericResourceApiServicedataServicedataVnfsVnfVnfdataVfmodulesVfModule vf : existingVfModules.getVfModule()) {
2251             vfModuleIds.add(vf.getVfModuleId());
2252         }
2253         log.info("[{}] vf-module(s) {} found in vnf [{}]", existingVfModules.getVfModule().size(), vfModuleIds, vnfId);
2254         if (!vfModuleIds.isEmpty() && vfModuleIds.contains(vfModuleId)) {
2255             log.info("Deleting vf-module [{}] from vnf [{}]",  vfModuleId, vnfId);
2256         } else {
2257             log.info("vf-module [{}] not found in vnf [{}]", vfModuleId, vnfId);
2258             return new ResponseEntity<>(HttpStatus.OK);
2259         }
2260         existingVfModules.getVfModule()
2261                 .stream()
2262                 .filter(vf -> ! vf.getVfModuleId().equals(vfModuleId))
2263                 .forEach(newVfModules::addVfModuleItem);
2264         vnfData.setVfModules(newVfModules);
2265         log.info("vf-module [{}] deleted from vnf [{}]", vfModuleId, vnfId);
2266
2267         // Map and save the new data
2268         try {
2269             service.setSvcData(objectMapper.writeValueAsString(svcData));
2270             configServicesRepository.save(service);
2271             return new ResponseEntity<>(HttpStatus.OK);
2272         } catch(JsonProcessingException e) {
2273             log.error("Error mapping object to JSON", e);
2274             // Should probably be a 500 INTERNAL_SERVICE_ERROR
2275             return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
2276         }
2277     }
2278 }