5695cfcd85c9d26de88bd0306fae39426b78dfe7
[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.ObjectMapper;
26 import org.onap.ccsdk.apps.services.RestApplicationException;
27 import org.onap.ccsdk.apps.services.RestException;
28 import org.onap.ccsdk.apps.services.RestProtocolError;
29 import org.onap.ccsdk.apps.services.RestProtocolException;
30 import org.onap.sdnc.apps.ms.gra.data.ConfigPreloadData;
31 import org.onap.sdnc.apps.ms.gra.data.ConfigPreloadDataRepository;
32 import org.onap.sdnc.apps.ms.gra.data.ConfigServices;
33 import org.onap.sdnc.apps.ms.gra.data.ConfigServicesRepository;
34 import org.onap.sdnc.apps.ms.gra.swagger.ConfigApi;
35 import org.onap.sdnc.apps.ms.gra.swagger.model.*;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.boot.autoconfigure.domain.EntityScan;
40 import org.springframework.context.annotation.ComponentScan;
41 import org.springframework.http.HttpStatus;
42 import org.springframework.http.ResponseEntity;
43 import org.springframework.stereotype.Controller;
44
45 import javax.servlet.http.HttpServletRequest;
46 import javax.validation.Valid;
47 import java.util.Iterator;
48 import java.util.LinkedList;
49 import java.util.List;
50 import java.util.Optional;
51 import java.util.concurrent.atomic.AtomicBoolean;
52
53 @Controller
54 @ComponentScan(basePackages = {"org.onap.sdnc.apps.ms.gra.*"})
55 @EntityScan("org.onap.sdnc.apps.ms.gra.springboot.*")
56 public class ConfigApiController implements ConfigApi {
57     private static final Logger log = LoggerFactory.getLogger(ConfigApiController.class);
58
59     private final ObjectMapper objectMapper;
60
61     private final HttpServletRequest request;
62
63     @Autowired
64     private ConfigPreloadDataRepository configPreloadDataRepository;
65
66     @Autowired
67     private ConfigServicesRepository configServicesRepository;
68
69     @Autowired
70     public ConfigApiController(ObjectMapper objectMapper, HttpServletRequest request) {
71         objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
72         objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
73         this.objectMapper = objectMapper;
74         this.request = request;
75     }
76
77     @Override
78     public Optional<ObjectMapper> getObjectMapper() {
79         return Optional.ofNullable(objectMapper);
80     }
81
82     @Override
83     public Optional<HttpServletRequest> getRequest() {
84         return Optional.ofNullable(request);
85     }
86
87     @Override
88     public ResponseEntity<Void> configGENERICRESOURCEAPIpreloadInformationDelete() {
89         configPreloadDataRepository.deleteAll();
90         return (new ResponseEntity<>(HttpStatus.NO_CONTENT));
91     }
92
93     @Override
94     public ResponseEntity<GenericResourceApiPreloadModelInformation> configGENERICRESOURCEAPIpreloadInformationGet() throws RestApplicationException {
95         GenericResourceApiPreloadModelInformation genericResourceApiPreloadModelInformation = new GenericResourceApiPreloadModelInformation();
96
97         if (configPreloadDataRepository.count() == 0) {
98             throw new RestApplicationException("data-missing", "Request could not be completed because the relevant data model content does not exist", HttpStatus.NOT_FOUND.value());
99         }
100
101         for (ConfigPreloadData configPreloadData : configPreloadDataRepository.findAll()) {
102             GenericResourceApiPreloadmodelinformationPreloadList preloadListItem = new GenericResourceApiPreloadmodelinformationPreloadList();
103
104             preloadListItem.setPreloadId(configPreloadData.getPreloadId());
105             preloadListItem.setPreloadType(configPreloadData.getPreloadType());
106             try {
107                 preloadListItem.setPreloadData(objectMapper.readValue(configPreloadData.getPreloadData(), GenericResourceApiPreloaddataPreloadData.class));
108             } catch (JsonProcessingException e) {
109                 log.error("Could not convert preload data", e);
110                 throw new RestApplicationException("data-conversion", "Request could not be completed due to internal error", e, HttpStatus.INTERNAL_SERVER_ERROR.value());
111             }
112             genericResourceApiPreloadModelInformation.addPreloadListItem(preloadListItem);
113         }
114
115
116         return new ResponseEntity<>(genericResourceApiPreloadModelInformation, HttpStatus.OK);
117     }
118
119     @Override
120     public ResponseEntity<Void> configGENERICRESOURCEAPIpreloadInformationPost(@Valid GenericResourceApiPreloadModelInformation graPreloadModelInfo) throws RestApplicationException, RestProtocolException {
121
122         List<GenericResourceApiPreloadmodelinformationPreloadList> preloadList = graPreloadModelInfo.getPreloadList();
123         List<ConfigPreloadData> newPreloadData = new LinkedList<>();
124
125         if (preloadList != null) {
126             // Verification pass - if any items already exist, return an error
127             for (GenericResourceApiPreloadmodelinformationPreloadList curItem : preloadList) {
128
129                 List<ConfigPreloadData> curPreloadData = configPreloadDataRepository.findByPreloadIdAndPreloadType(curItem.getPreloadId(), curItem.getPreloadType());
130                 if ((curPreloadData != null) && (!curPreloadData.isEmpty())) {
131                     log.error("Preload data already exists for {}:{}", curItem.getPreloadId(), curItem.getPreloadType());
132                     throw new RestProtocolException("data-exists", "Data already exists for " + curItem.getPreloadId() + ":" + curItem.getPreloadType(), HttpStatus.CONFLICT.value());
133                 } else {
134                     try {
135                         newPreloadData.add(new ConfigPreloadData(curItem.getPreloadId(), curItem.getPreloadType(), objectMapper.writeValueAsString(curItem.getPreloadData())));
136                     } catch (JsonProcessingException e) {
137                         log.error("Cannot convert preload data");
138                         throw new RestApplicationException("data-conversion", "Request could not be completed due to internal error", e, HttpStatus.INTERNAL_SERVER_ERROR.value());
139
140                     }
141                 }
142             }
143
144             // Update pass
145             for (ConfigPreloadData newDataItem : newPreloadData) {
146                 log.info("Adding preload data for {}:{}", newDataItem.getPreloadId(), newDataItem.getPreloadType());
147                 configPreloadDataRepository.save(newDataItem);
148             }
149         } else {
150             throw new RestProtocolException("data-missing", "No preload-list entries found to add", HttpStatus.CONFLICT.value());
151         }
152
153         return new ResponseEntity<>(HttpStatus.CREATED);
154     }
155
156     @Override
157     public ResponseEntity<Void> configGENERICRESOURCEAPIpreloadInformationPut(@Valid GenericResourceApiPreloadModelInformation graPreloadModelInfo) throws RestApplicationException {
158
159         boolean addedNew = false;
160         List<GenericResourceApiPreloadmodelinformationPreloadList> preloadList = graPreloadModelInfo.getPreloadList();
161
162         if (preloadList != null) {
163             Iterator<GenericResourceApiPreloadmodelinformationPreloadList> iter = preloadList.iterator();
164             while (iter.hasNext()) {
165                 GenericResourceApiPreloadmodelinformationPreloadList curItem = iter.next();
166                 List<ConfigPreloadData> curPreloadData = configPreloadDataRepository.findByPreloadIdAndPreloadType(curItem.getPreloadId(), curItem.getPreloadType());
167                 if ((curPreloadData == null) || curPreloadData.isEmpty()) {
168                     addedNew = true;
169                 }
170
171                 try {
172                     configPreloadDataRepository.save(new ConfigPreloadData(curItem.getPreloadId(), curItem.getPreloadType(), objectMapper.writeValueAsString(curItem.getPreloadData())));
173                 } catch (JsonProcessingException e) {
174                     log.error("Cannot convert preload data", e);
175                     throw new RestApplicationException("data-conversion", "Request could not be completed due to internal error", e, HttpStatus.INTERNAL_SERVER_ERROR.value());
176
177                 }
178             }
179         }
180
181         if (addedNew) {
182             return new ResponseEntity<>(HttpStatus.CREATED);
183         } else {
184             return new ResponseEntity<>(HttpStatus.NO_CONTENT);
185         }
186
187     }
188
189     @Override
190     public ResponseEntity<Void> configGENERICRESOURCEAPIpreloadInformationPreloadListPost(@Valid GenericResourceApiPreloadmodelinformationPreloadList preloadListItem) throws RestProtocolException {
191
192         throw new RestProtocolException("data-missing", "Missing key for list \"preload-list\"", HttpStatus.NOT_FOUND.value());
193     }
194
195
196     @Override
197     public ResponseEntity<Void> configGENERICRESOURCEAPIpreloadInformationPreloadListPreloadIdPreloadTypeDelete(String preloadId, String preloadType) {
198         configPreloadDataRepository.deleteByPreloadIdAndPreloadType(preloadId, preloadType);
199         return new ResponseEntity<>(HttpStatus.NO_CONTENT);
200     }
201
202     @Override
203     public ResponseEntity<GenericResourceApiPreloadmodelinformationPreloadList> configGENERICRESOURCEAPIpreloadInformationPreloadListPreloadIdPreloadTypeGet(String preloadId, String preloadType) throws RestApplicationException {
204         List<ConfigPreloadData> preloadData = configPreloadDataRepository.findByPreloadIdAndPreloadType(preloadId, preloadType);
205         if (preloadData != null) {
206             if (!preloadData.isEmpty()) {
207                 ConfigPreloadData preloadDataItem = preloadData.get(0);
208                 GenericResourceApiPreloadmodelinformationPreloadList preloadDataList = new GenericResourceApiPreloadmodelinformationPreloadList();
209                 preloadDataList.setPreloadId(preloadDataItem.getPreloadId());
210                 preloadDataList.setPreloadType(preloadDataItem.getPreloadType());
211                 try {
212                     preloadDataList.setPreloadData(objectMapper.readValue(preloadDataItem.getPreloadData(), GenericResourceApiPreloaddataPreloadData.class));
213                 } catch (JsonProcessingException e) {
214                     log.error("Cannot convert preload data", e);
215                     throw new RestApplicationException("data-conversion", "Request could not be completed due to internal error", e, HttpStatus.INTERNAL_SERVER_ERROR.value());
216                 }
217                 return new ResponseEntity<>(preloadDataList, HttpStatus.OK);
218             }
219         }
220         return new ResponseEntity<>(HttpStatus.NOT_FOUND);
221     }
222
223     @Override
224     public ResponseEntity<Void> configGENERICRESOURCEAPIpreloadInformationPreloadListPreloadIdPreloadTypePost(String preloadId, String preloadType, @Valid GenericResourceApiPreloadmodelinformationPreloadList preloadListItem) throws RestApplicationException, RestProtocolException {
225         List<ConfigPreloadData> preloadDataItems = configPreloadDataRepository.findByPreloadIdAndPreloadType(preloadId, preloadType);
226
227         if ((preloadDataItems != null) && !preloadDataItems.isEmpty()) {
228             log.error("Preload data already exists for {}:{}", preloadId, preloadType);
229             throw new RestProtocolException("data-exists", "Data already exists for " + preloadId + ":" + preloadType, HttpStatus.CONFLICT.value());
230         }
231
232         try {
233             log.info("Adding preload data for {}:{}", preloadId, preloadType);
234             configPreloadDataRepository.save(new ConfigPreloadData(preloadId, preloadType, objectMapper.writeValueAsString(preloadListItem.getPreloadData())));
235         } catch (JsonProcessingException e) {
236             log.error("Cannot convert preload data", e);
237             throw new RestApplicationException("data-conversion", "Request could not be completed due to internal error", e, HttpStatus.INTERNAL_SERVER_ERROR.value());
238
239         }
240         return new ResponseEntity<>(HttpStatus.CREATED);
241     }
242
243     @Override
244     public ResponseEntity<Void> configGENERICRESOURCEAPIpreloadInformationPreloadListPreloadIdPreloadTypePut(String preloadId, String preloadType, @Valid GenericResourceApiPreloadmodelinformationPreloadList preloadListItem) throws RestApplicationException, RestProtocolException {
245         List<ConfigPreloadData> preloadDataItems = configPreloadDataRepository.findByPreloadIdAndPreloadType(preloadId, preloadType);
246         boolean dataExists = false;
247         if ((preloadDataItems != null) && !preloadDataItems.isEmpty()) {
248             dataExists = true;
249         }
250
251         if ((preloadListItem.getPreloadId() == null) ||
252                 (preloadListItem.getPreloadType() == null) ||
253                 (preloadListItem.getPreloadData() == null)) {
254             log.error("Invalid list item received: {}", preloadListItem);
255             throw new RestProtocolException("bad-attribute", "Invalid data received", HttpStatus.BAD_REQUEST.value());
256         }
257
258         try {
259             if (dataExists) {
260                 log.info("Updating preload data for {}:{} -> {}", preloadId, preloadType, objectMapper.writeValueAsString(preloadListItem));
261
262             } else {
263                 log.info("Adding preload data for {}:{}", preloadId, preloadType);
264             }
265
266             configPreloadDataRepository.save(new ConfigPreloadData(preloadId, preloadType, objectMapper.writeValueAsString(preloadListItem.getPreloadData())));
267         } catch (JsonProcessingException e) {
268             log.error("Cannot convert preload data", e);
269             throw new RestApplicationException("data-conversion", "Request could not be completed due to internal error", e, HttpStatus.INTERNAL_SERVER_ERROR.value());
270
271         }
272
273         if (dataExists) {
274             return new ResponseEntity<>(HttpStatus.NO_CONTENT);
275         } else {
276             return new ResponseEntity<>(HttpStatus.CREATED);
277         }
278     }
279
280
281     @Override
282     public ResponseEntity<Void> configGENERICRESOURCEAPIpreloadInformationPreloadListPreloadIdPreloadTypePreloadDataDelete(String preloadId, String preloadType) throws RestProtocolException {
283         List<ConfigPreloadData> preloadData = configPreloadDataRepository.findByPreloadIdAndPreloadType(preloadId, preloadType);
284
285         if ((preloadData == null) || preloadData.isEmpty()) {
286             throw new RestProtocolException("data-missing", "No preload entry found", HttpStatus.NOT_FOUND.value());
287         }
288
289         ConfigPreloadData preloadDataItem = preloadData.get(0);
290
291         if (preloadDataItem.getPreloadData() == null) {
292             throw new RestProtocolException("data-missing", "No preload-data found", HttpStatus.NOT_FOUND.value());
293         }
294         preloadDataItem.setPreloadData(null);
295         configPreloadDataRepository.save(preloadDataItem);
296
297
298         return new ResponseEntity<>(HttpStatus.NO_CONTENT);
299     }
300
301
302     @Override
303     public ResponseEntity<GenericResourceApiPreloaddataPreloadData> configGENERICRESOURCEAPIpreloadInformationPreloadListPreloadIdPreloadTypePreloadDataGet(String preloadId, String preloadType) throws RestApplicationException, RestProtocolException {
304         List<ConfigPreloadData> preloadData = configPreloadDataRepository.findByPreloadIdAndPreloadType(preloadId, preloadType);
305
306         if ((preloadData == null) || preloadData.isEmpty()) {
307             throw new RestProtocolException("data-missing", "No preload entry found", HttpStatus.NOT_FOUND.value());
308         }
309
310         ConfigPreloadData preloadDataItem = preloadData.get(0);
311
312         if (preloadDataItem.getPreloadData() == null) {
313             throw new RestProtocolException("data-missing", "No preload-data found", HttpStatus.NOT_FOUND.value());
314         }
315         try {
316             return new ResponseEntity<>(objectMapper.readValue(preloadDataItem.getPreloadData(), GenericResourceApiPreloaddataPreloadData.class), HttpStatus.OK);
317         } catch (JsonProcessingException e) {
318             log.error("Cannot convert preload data", e);
319             throw new RestApplicationException("data-conversion", "Request could not be completed due to internal error", e, HttpStatus.INTERNAL_SERVER_ERROR.value());
320         }
321     }
322
323     @Override
324     public ResponseEntity<Void> configGENERICRESOURCEAPIpreloadInformationPreloadListPreloadIdPreloadTypePreloadDataPost(String preloadId, String preloadType, @Valid GenericResourceApiPreloaddataPreloadData preloadData) throws RestApplicationException, RestProtocolException {
325         List<ConfigPreloadData> preloadDataEntries = configPreloadDataRepository.findByPreloadIdAndPreloadType(preloadId, preloadType);
326
327         List<ConfigPreloadData> preloadDataItems = configPreloadDataRepository.findByPreloadIdAndPreloadType(preloadId, preloadType);
328         if ((preloadDataItems == null) || (preloadDataItems.isEmpty())) {
329             throw new RestProtocolException("data-missing", "No preload entry found", HttpStatus.NOT_FOUND.value());
330         }
331
332         if ((preloadData == null) ||
333                 (preloadData.getPreloadNetworkTopologyInformation() == null)) {
334             throw new RestProtocolException("bad-attribute", "Invalid preloadData received", HttpStatus.BAD_REQUEST.value());
335         }
336
337         ConfigPreloadData preloadDataItem = preloadDataItems.get(0);
338
339         if (preloadDataItem.getPreloadData() != null) {
340             log.error("Preload data already exists for {}:{} ", preloadId, preloadType);
341             throw new RestProtocolException("data-exists", "Data already exists for " + preloadId + ":" + preloadType, HttpStatus.CONFLICT.value());
342         }
343
344         try {
345             preloadDataItem.setPreloadData(objectMapper.writeValueAsString(preloadData));
346             configPreloadDataRepository.save(preloadDataItem);
347         } catch (JsonProcessingException e) {
348             log.error("Cannot convert preload data", e);
349             throw new RestApplicationException("data-conversion", "Request could not be completed due to internal error", e, HttpStatus.INTERNAL_SERVER_ERROR.value());
350         }
351
352         return new ResponseEntity<>(HttpStatus.CREATED);
353     }
354
355     @Override
356     public ResponseEntity<Void> configGENERICRESOURCEAPIpreloadInformationPreloadListPreloadIdPreloadTypePreloadDataPut(String preloadId, String preloadType, @Valid GenericResourceApiPreloaddataPreloadData preloadData) throws RestApplicationException, RestProtocolException {
357         boolean dataExists = false;
358         List<ConfigPreloadData> preloadDataItems = configPreloadDataRepository.findByPreloadIdAndPreloadType(preloadId, preloadType);
359         if ((preloadDataItems == null) || (preloadDataItems.isEmpty())) {
360             throw new RestProtocolException("data-missing", "No preload entry found", HttpStatus.NOT_FOUND.value());
361         }
362
363         if ((preloadData == null) ||
364                 (preloadData.getPreloadNetworkTopologyInformation() == null)) {
365             throw new RestProtocolException("bad-attribute", "Invalid preloadData received", HttpStatus.BAD_REQUEST.value());
366         }
367
368         ConfigPreloadData preloadDataItem = preloadDataItems.get(0);
369
370         if (preloadDataItem.getPreloadData() != null) {
371             dataExists = true;
372         }
373
374         try {
375             preloadDataItem.setPreloadData(objectMapper.writeValueAsString(preloadData));
376             configPreloadDataRepository.save(preloadDataItem);
377         } catch (JsonProcessingException e) {
378             log.error("Cannot convert preload data", e);
379             throw new RestApplicationException("data-conversion", "Request could not be completed due to internal error", e, HttpStatus.INTERNAL_SERVER_ERROR.value());
380         }
381
382         if (dataExists) {
383             return new ResponseEntity<>(HttpStatus.NO_CONTENT);
384         } else {
385             return new ResponseEntity<>(HttpStatus.CREATED);
386         }
387     }
388
389     @Override
390     public ResponseEntity<Void> configGENERICRESOURCEAPIservicesDelete() {
391         configServicesRepository.deleteAll();
392         return new ResponseEntity<>(HttpStatus.NO_CONTENT);
393     }
394
395     @Override
396     public ResponseEntity<GenericResourceApiServiceModelInfrastructure> configGENERICRESOURCEAPIservicesGet() throws RestApplicationException {
397         GenericResourceApiServiceModelInfrastructure modelInfrastructure = new GenericResourceApiServiceModelInfrastructure();
398
399         if (configServicesRepository.count() == 0)  {
400             throw new RestApplicationException("data-missing", "Request could not be completed because the relevant data model content does not exist", HttpStatus.NOT_FOUND.value());
401         }
402
403         for (ConfigServices service : configServicesRepository.findAll()) {
404             GenericResourceApiServicemodelinfrastructureService serviceItem = new GenericResourceApiServicemodelinfrastructureService();
405             serviceItem.setServiceInstanceId(service.getSvcInstanceId());
406             if (service.getSvcData() != null) {
407             try {
408                 serviceItem.setServiceData(objectMapper.readValue(service.getSvcData(), GenericResourceApiServicedataServiceData.class));
409             } catch (JsonProcessingException e) {
410                 log.error("Could not deserialize service data for {}", service.getSvcInstanceId(), e);
411                 throw new RestApplicationException("data-conversion", "Request could not be completed due to internal error", e, HttpStatus.INTERNAL_SERVER_ERROR.value());
412
413             }
414         }
415             serviceItem.setServiceStatus(service.getServiceStatus());
416             modelInfrastructure.addServiceItem(serviceItem);
417         }
418
419
420         return new ResponseEntity<>(modelInfrastructure, HttpStatus.OK);
421
422     }
423
424     @Override
425     public ResponseEntity<Void> configGENERICRESOURCEAPIservicesPost(@Valid GenericResourceApiServiceModelInfrastructure modelInfrastructure) throws RestApplicationException, RestProtocolException {
426         List<ConfigServices> newServices = new LinkedList<>();
427
428         for (GenericResourceApiServicemodelinfrastructureService serviceItem : modelInfrastructure.getService()) {
429             String svcInstanceId = serviceItem.getServiceInstanceId();
430             List<ConfigServices> existingService = configServicesRepository.findBySvcInstanceId(svcInstanceId);
431             if ((existingService != null) && !existingService.isEmpty()) {
432                 log.error("Service data already exists for {}", svcInstanceId);
433                 throw new RestProtocolException("data-exists", "Data already exists for service-instance-id " + svcInstanceId, HttpStatus.CONFLICT.value());
434             }
435             ConfigServices service = new ConfigServices();
436             service.setSvcInstanceId(svcInstanceId);
437             try {
438                 service.setSvcData(objectMapper.writeValueAsString(serviceItem.getServiceData()));
439             } catch (JsonProcessingException e) {
440                 log.error("Could not serialize service data for {}", service.getSvcInstanceId(), e);
441                 throw new RestApplicationException("data-conversion", "Request could not be completed due to internal error", e, HttpStatus.INTERNAL_SERVER_ERROR.value());
442
443             }
444             service.setServiceStatus(serviceItem.getServiceStatus());
445             newServices.add(service);
446         }
447
448         for (ConfigServices service : newServices) {
449             configServicesRepository.save(service);
450         }
451
452         return new ResponseEntity<>(HttpStatus.CREATED);
453
454     }
455
456     @Override
457     public ResponseEntity<Void> configGENERICRESOURCEAPIservicesPut(@Valid GenericResourceApiServiceModelInfrastructure modelInfrastructure) throws RestApplicationException {
458
459         List<ConfigServices> newServices = new LinkedList<>();
460         boolean dataExists = false;
461
462         for (GenericResourceApiServicemodelinfrastructureService serviceItem : modelInfrastructure.getService()) {
463             String svcInstanceId = serviceItem.getServiceInstanceId();
464             List<ConfigServices> existingService = configServicesRepository.findBySvcInstanceId(svcInstanceId);
465             if ((existingService != null) && !existingService.isEmpty()) {
466                 dataExists = true;
467             }
468             ConfigServices service = new ConfigServices();
469             service.setSvcInstanceId(svcInstanceId);
470             try {
471                 service.setSvcData(objectMapper.writeValueAsString(serviceItem.getServiceData()));
472             } catch (JsonProcessingException e) {
473                 log.error("Could not serialize service data for {}", service.getSvcInstanceId(), e);
474                 throw new RestApplicationException("data-conversion", "Request could not be completed due to internal error", e, HttpStatus.INTERNAL_SERVER_ERROR.value());
475
476             }
477             service.setServiceStatus(serviceItem.getServiceStatus());
478             newServices.add(service);
479         }
480
481         for (ConfigServices service : newServices) {
482             configServicesRepository.save(service);
483         }
484
485         if (dataExists) {
486             return new ResponseEntity<>(HttpStatus.NO_CONTENT);
487         } else {
488             return new ResponseEntity<>(HttpStatus.CREATED);
489         }
490
491     }
492
493     @Override
494     public ResponseEntity<Void> configGENERICRESOURCEAPIservicesServicePost(@Valid GenericResourceApiServicemodelinfrastructureService servicesData) throws RestApplicationException {
495         String svcInstanceId = servicesData.getServiceInstanceId();
496         try {
497             String svcData = objectMapper.writeValueAsString(servicesData.getServiceData());
498             ConfigServices configService = new ConfigServices(svcInstanceId, svcData, servicesData.getServiceStatus());
499             configServicesRepository.deleteBySvcInstanceId(svcInstanceId);
500             configServicesRepository.save(configService);
501         } catch (JsonProcessingException e) {
502             log.error("Cannot convert service data", e);
503             throw new RestApplicationException("data-conversion", "Request could not be completed due to internal error", e, HttpStatus.INTERNAL_SERVER_ERROR.value());
504
505         }
506         return new ResponseEntity<>(HttpStatus.OK);
507     }
508
509     @Override
510     public ResponseEntity<Void> configGENERICRESOURCEAPIservicesServiceServiceInstanceIdDelete(String serviceInstanceId) {
511         configServicesRepository.deleteBySvcInstanceId(serviceInstanceId);
512         return new ResponseEntity<>(HttpStatus.NO_CONTENT);
513     }
514
515     @Override
516     public ResponseEntity<GenericResourceApiServicemodelinfrastructureService> configGENERICRESOURCEAPIservicesServiceServiceInstanceIdGet(String serviceInstanceId) throws RestApplicationException {
517         GenericResourceApiServicemodelinfrastructureService retval = null;
518
519         List<ConfigServices> services = configServicesRepository.findBySvcInstanceId(serviceInstanceId);
520
521         if (services.isEmpty()) {
522             return new ResponseEntity<>(HttpStatus.NOT_FOUND);
523         } else {
524             ConfigServices service = services.get(0);
525             retval = new GenericResourceApiServicemodelinfrastructureService();
526             retval.setServiceInstanceId(serviceInstanceId);
527             retval.setServiceStatus(service.getServiceStatus());
528             try {
529                 retval.setServiceData(objectMapper.readValue(service.getSvcData(), GenericResourceApiServicedataServiceData.class));
530             } catch (JsonProcessingException e) {
531                 log.error("Could not deserialize service data for service instance id {}", serviceInstanceId, e);
532                 throw new RestApplicationException("data-conversion", "Request could not be completed due to internal error", e, HttpStatus.INTERNAL_SERVER_ERROR.value());
533
534             }
535         }
536
537
538         return new ResponseEntity<>(retval, HttpStatus.OK);
539
540     }
541
542     @Override
543     public ResponseEntity<Void> configGENERICRESOURCEAPIservicesServiceServiceInstanceIdPost(String svcInstanceId, @Valid GenericResourceApiServicemodelinfrastructureService newService) throws RestApplicationException, RestProtocolException {
544
545         List<ConfigServices> existingService = configServicesRepository.findBySvcInstanceId(svcInstanceId);
546         if ((existingService != null) && !existingService.isEmpty()) {
547             log.error("Service data already exists for {}", svcInstanceId);
548             throw new RestProtocolException("data-exists", "Data already exists for service-instance-id " + svcInstanceId, HttpStatus.CONFLICT.value());
549         }
550         ConfigServices service = new ConfigServices();
551         service.setSvcInstanceId(svcInstanceId);
552         try {
553             service.setSvcData(objectMapper.writeValueAsString(newService.getServiceData()));
554         } catch (JsonProcessingException e) {
555             log.error("Could not serialize service data for {}", service.getSvcInstanceId(), e);
556             throw new RestApplicationException("data-conversion", "Request could not be completed due to internal error", e, HttpStatus.INTERNAL_SERVER_ERROR.value());
557
558         }
559         service.setServiceStatus(newService.getServiceStatus());
560         configServicesRepository.save(service);
561
562         return new ResponseEntity<>(HttpStatus.CREATED);
563     }
564
565     @Override
566     public ResponseEntity<Void> configGENERICRESOURCEAPIservicesServiceServiceInstanceIdPut(String serviceInstanceId, @Valid GenericResourceApiServicemodelinfrastructureService newService) throws RestApplicationException {
567
568         boolean dataExists = false;
569
570         String svcInstanceId = newService.getServiceInstanceId();
571
572         ConfigServices service = null;
573         List<ConfigServices> existingService = configServicesRepository.findBySvcInstanceId(svcInstanceId);
574         if ((existingService != null) && !existingService.isEmpty()) {
575             dataExists = true;
576             service = existingService.get(0);
577         } else {
578             service = new ConfigServices();
579             service.setSvcInstanceId(svcInstanceId);
580         }
581
582         try {
583             service.setSvcData(objectMapper.writeValueAsString(newService.getServiceData()));
584         } catch (JsonProcessingException e) {
585             log.error("Could not serialize service data for {}", service.getSvcInstanceId(), e);
586             throw new RestApplicationException("data-conversion", "Request could not be completed due to internal error", e, HttpStatus.INTERNAL_SERVER_ERROR.value());
587
588         }
589         service.setServiceStatus(newService.getServiceStatus());
590         configServicesRepository.save(service);
591
592         if (dataExists) {
593             return new ResponseEntity<>(HttpStatus.NO_CONTENT);
594         } else {
595             return new ResponseEntity<>(HttpStatus.CREATED);
596         }
597     }
598
599
600     @Override
601     public ResponseEntity<Void> configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceDataDelete(String serviceInstanceId) throws RestProtocolException {
602         List<ConfigServices> services = configServicesRepository.findBySvcInstanceId(serviceInstanceId);
603
604         if ((services == null) || (services.isEmpty())) {
605             throw new RestProtocolException("data-missing", "No service entry found", HttpStatus.NOT_FOUND.value());
606         }
607
608         ConfigServices service = services.get(0);
609         if (service.getSvcData() == null) {
610             throw new RestProtocolException("data-missing", "No service-data found", HttpStatus.NOT_FOUND.value());
611         }
612         service.setSvcData(null);
613         configServicesRepository.save(service);
614
615         return new ResponseEntity<>(HttpStatus.NO_CONTENT);
616     }
617
618     @Override
619     public ResponseEntity<GenericResourceApiServicedataServiceData> configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceDataGet(String serviceInstanceId) throws RestApplicationException, RestProtocolException {
620         GenericResourceApiServicedataServiceData serviceData = null;
621
622         List<ConfigServices> services = configServicesRepository.findBySvcInstanceId(serviceInstanceId);
623         if ((services == null) || (services.isEmpty())) {
624             throw new RestProtocolException("data-missing", "No service entry found", HttpStatus.NOT_FOUND.value());
625         }
626
627         try {
628             serviceData = objectMapper.readValue(services.get(0).getSvcData(), GenericResourceApiServicedataServiceData.class);
629             return new ResponseEntity<>(serviceData, HttpStatus.OK);
630         } catch (JsonProcessingException e) {
631             log.error("Could not parse service data", e);
632             throw new RestApplicationException("data-conversion", "Request could not be completed due to internal error", e, HttpStatus.INTERNAL_SERVER_ERROR.value());
633         }
634
635     }
636
637     @Override
638     public ResponseEntity<Void> configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceDataPost(String serviceInstanceId, @Valid GenericResourceApiServicedataServiceData serviceData) throws RestApplicationException, RestProtocolException {
639         ConfigServices service;
640         List<ConfigServices> services = configServicesRepository.findBySvcInstanceId(serviceInstanceId);
641         if ((services == null) || (services.isEmpty())) {
642             throw new RestProtocolException("data-missing", "No service entry found", HttpStatus.NOT_FOUND.value());
643         }
644
645         if ((serviceData == null) ||
646                 (serviceData.getServiceInformation() == null)) {
647             throw new RestProtocolException("bad-attribute", "Invalid service-data received", HttpStatus.BAD_REQUEST.value());
648
649         }
650         service = services.get(0);
651
652         if ((service.getSvcData() != null) && (service.getSvcData().length() > 0)){
653             log.error("service-data already exists for svcInstanceId {}", serviceInstanceId);
654             throw new RestProtocolException("data-exists", "Data already exists for " + serviceInstanceId, HttpStatus.CONFLICT.value());
655         }
656
657
658         try {
659             service.setSvcData(objectMapper.writeValueAsString(serviceData));
660             configServicesRepository.save(service);
661         } catch (JsonProcessingException e) {
662             log.error("Could not serialize service data for svc instance id {}", serviceInstanceId, e);
663             throw new RestApplicationException("data-conversion", "Request could not be completed due to internal error", e, HttpStatus.INTERNAL_SERVER_ERROR.value());
664         }
665
666
667         return new ResponseEntity<>(HttpStatus.CREATED);
668
669     }
670
671     @Override
672     public ResponseEntity<Void> configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceDataPut(String serviceInstanceId, @Valid GenericResourceApiServicedataServiceData serviceData) throws RestApplicationException, RestProtocolException {
673         ConfigServices service;
674         boolean dataExists = false;
675
676         List<ConfigServices> services = configServicesRepository.findBySvcInstanceId(serviceInstanceId);
677         if ((services == null) || (services.isEmpty())) {
678             throw new RestProtocolException("data-missing", "No service entry found", HttpStatus.NOT_FOUND.value());
679         }
680
681         if ((serviceData == null) ||
682                 (serviceData.getServiceInformation() == null)) {
683             throw new RestProtocolException("bad-attribute", "Invalid service-data received", HttpStatus.BAD_REQUEST.value());
684
685         }
686         service = services.get(0);
687
688         if ((service.getSvcData() != null) && (service.getSvcData().length() > 0)) {
689             dataExists = true;
690         }
691
692         try {
693             service.setSvcData(objectMapper.writeValueAsString(serviceData));
694             configServicesRepository.save(service);
695         } catch (JsonProcessingException e) {
696             log.error("Could not serialize service data for svc instance id {}", serviceInstanceId, e);
697             throw new RestApplicationException("data-conversion", "Request could not be completed due to internal error", e, HttpStatus.INTERNAL_SERVER_ERROR.value());
698         }
699
700         if (dataExists) {
701             return new ResponseEntity<>(HttpStatus.NO_CONTENT);
702         } else {
703             return new ResponseEntity<>(HttpStatus.CREATED);
704         }
705     }
706
707     @Override
708     public ResponseEntity<Void> configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceStatusDelete(String serviceInstanceId) throws RestProtocolException {
709         List<ConfigServices> services = configServicesRepository.findBySvcInstanceId(serviceInstanceId);
710
711         if ((services == null) || (services.isEmpty())) {
712             throw new RestProtocolException("data-missing", "No service entry found", HttpStatus.NOT_FOUND.value());
713         }
714
715         ConfigServices service = services.get(0);
716         if (service.getServiceStatus() == null) {
717             throw new RestProtocolException("data-missing", "No service-status found", HttpStatus.NOT_FOUND.value());
718         }
719         service.setServiceStatus(null);
720         configServicesRepository.save(service);
721
722         return new ResponseEntity<>(HttpStatus.NO_CONTENT);
723
724     }
725
726     @Override
727     public ResponseEntity<GenericResourceApiServicestatusServiceStatus> configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceStatusGet(String serviceInstanceId) throws RestApplicationException, RestProtocolException {
728         GenericResourceApiServicestatusServiceStatus serviceStatus = null;
729
730         List<ConfigServices> services = configServicesRepository.findBySvcInstanceId(serviceInstanceId);
731         if ((services == null) || (services.isEmpty())) {
732             throw new RestProtocolException("data-missing", "No service entry found", HttpStatus.NOT_FOUND.value());
733         }
734
735         serviceStatus = services.get(0).getServiceStatus();
736         return new ResponseEntity<>(serviceStatus, HttpStatus.OK);
737     }
738
739     @Override
740     public ResponseEntity<Void> configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceStatusPost(String serviceInstanceId, @Valid GenericResourceApiServicestatusServiceStatus serviceStatus) throws RestProtocolException {
741         ConfigServices service;
742         List<ConfigServices> services = configServicesRepository.findBySvcInstanceId(serviceInstanceId);
743         if ((services == null) || (services.isEmpty())) {
744             throw new RestProtocolException("data-missing", "No service entry found", HttpStatus.NOT_FOUND.value());
745         }
746
747         if ((serviceStatus == null) ||
748                 (serviceStatus.getAction() == null)) {
749             throw new RestProtocolException("bad-attribute", "Invalid service-status received", HttpStatus.BAD_REQUEST.value());
750
751         }
752         service = services.get(0);
753
754         if (service.getServiceStatus() != null) {
755             log.error("service-status already exists for svcInstanceId {}", serviceInstanceId);
756             throw new RestProtocolException("data-exists", "Data already exists for " + serviceInstanceId, HttpStatus.CONFLICT.value());
757         }
758
759
760         service.setServiceStatus(serviceStatus);
761         configServicesRepository.save(service);
762
763
764         return new ResponseEntity<>(HttpStatus.CREATED);
765
766     }
767
768     @Override
769     public ResponseEntity<Void> configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceStatusPut(String serviceInstanceId, @Valid GenericResourceApiServicestatusServiceStatus serviceStatus) throws RestProtocolException {
770         ConfigServices service;
771         boolean dataExists = false;
772
773         List<ConfigServices> services = configServicesRepository.findBySvcInstanceId(serviceInstanceId);
774         if ((services == null) || (services.isEmpty())) {
775             throw new RestProtocolException("data-missing", "No service entry found", HttpStatus.NOT_FOUND.value());
776         }
777
778         if ((serviceStatus == null) ||
779                 (serviceStatus.getAction() == null)) {
780             throw new RestProtocolException("bad-attribute", "Invalid service-status received", HttpStatus.BAD_REQUEST.value());
781
782         }
783         service = services.get(0);
784
785         if (service.getServiceStatus() != null) {
786             dataExists = true;
787         }
788
789
790         service.setServiceStatus(serviceStatus);
791         configServicesRepository.save(service);
792
793         if (dataExists) {
794             return new ResponseEntity<>(HttpStatus.NO_CONTENT);
795         } else {
796             return new ResponseEntity<>(HttpStatus.CREATED);
797         }
798     }
799
800 }