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