e3d084ee7d9468366856be084bf753bf4b32a581
[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> configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPost(@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> configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypeDelete(String preloadId, String preloadType) {
198         configPreloadDataRepository.deleteByPreloadIdAndPreloadType(preloadId, preloadType);
199         return new ResponseEntity<>(HttpStatus.NO_CONTENT);
200     }
201
202     @Override
203     public ResponseEntity<GenericResourceApiPreloadmodelinformationPreloadList> configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypeGet(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> configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypePost(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> configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypePut(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> configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypeGENERICRESOURCEAPIpreloadDataDelete(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> configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypeGENERICRESOURCEAPIpreloadDataGet(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> configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypeGENERICRESOURCEAPIpreloadDataPost(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> configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypeGENERICRESOURCEAPIpreloadDataPut(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             try {
407                 serviceItem.setServiceData(objectMapper.readValue(service.getSvcData(), GenericResourceApiServicedataServiceData.class));
408             } catch (JsonProcessingException e) {
409                 log.error("Could not deserialize service data for {}", service.getSvcInstanceId(), e);
410                 throw new RestApplicationException("data-conversion", "Request could not be completed due to internal error", e, HttpStatus.INTERNAL_SERVER_ERROR.value());
411
412             }
413             serviceItem.setServiceStatus(service.getServiceStatus());
414             modelInfrastructure.addServiceItem(serviceItem);
415         }
416
417
418         return new ResponseEntity<>(modelInfrastructure, HttpStatus.OK);
419
420     }
421
422     @Override
423     public ResponseEntity<Void> configGENERICRESOURCEAPIservicesPost(@Valid GenericResourceApiServiceModelInfrastructure modelInfrastructure) throws RestApplicationException, RestProtocolException {
424         List<ConfigServices> newServices = new LinkedList<>();
425
426         for (GenericResourceApiServicemodelinfrastructureService serviceItem : modelInfrastructure.getService()) {
427             String svcInstanceId = serviceItem.getServiceInstanceId();
428             List<ConfigServices> existingService = configServicesRepository.findBySvcInstanceId(svcInstanceId);
429             if ((existingService != null) && !existingService.isEmpty()) {
430                 log.error("Service data already exists for {}", svcInstanceId);
431                 throw new RestProtocolException("data-exists", "Data already exists for service-instance-id " + svcInstanceId, HttpStatus.CONFLICT.value());
432             }
433             ConfigServices service = new ConfigServices();
434             service.setSvcInstanceId(svcInstanceId);
435             try {
436                 service.setSvcData(objectMapper.writeValueAsString(serviceItem.getServiceData()));
437             } catch (JsonProcessingException e) {
438                 log.error("Could not serialize service data for {}", service.getSvcInstanceId(), e);
439                 throw new RestApplicationException("data-conversion", "Request could not be completed due to internal error", e, HttpStatus.INTERNAL_SERVER_ERROR.value());
440
441             }
442             service.setServiceStatus(serviceItem.getServiceStatus());
443             newServices.add(service);
444         }
445
446         for (ConfigServices service : newServices) {
447             configServicesRepository.save(service);
448         }
449
450         return new ResponseEntity<>(HttpStatus.CREATED);
451
452     }
453
454     @Override
455     public ResponseEntity<Void> configGENERICRESOURCEAPIservicesPut(@Valid GenericResourceApiServiceModelInfrastructure modelInfrastructure) throws RestApplicationException {
456
457         List<ConfigServices> newServices = new LinkedList<>();
458         boolean dataExists = false;
459
460         for (GenericResourceApiServicemodelinfrastructureService serviceItem : modelInfrastructure.getService()) {
461             String svcInstanceId = serviceItem.getServiceInstanceId();
462             List<ConfigServices> existingService = configServicesRepository.findBySvcInstanceId(svcInstanceId);
463             if ((existingService != null) && !existingService.isEmpty()) {
464                 dataExists = true;
465             }
466             ConfigServices service = new ConfigServices();
467             service.setSvcInstanceId(svcInstanceId);
468             try {
469                 service.setSvcData(objectMapper.writeValueAsString(serviceItem.getServiceData()));
470             } catch (JsonProcessingException e) {
471                 log.error("Could not serialize service data for {}", service.getSvcInstanceId(), e);
472                 throw new RestApplicationException("data-conversion", "Request could not be completed due to internal error", e, HttpStatus.INTERNAL_SERVER_ERROR.value());
473
474             }
475             service.setServiceStatus(serviceItem.getServiceStatus());
476             newServices.add(service);
477         }
478
479         for (ConfigServices service : newServices) {
480             configServicesRepository.save(service);
481         }
482
483         if (dataExists) {
484             return new ResponseEntity<>(HttpStatus.NO_CONTENT);
485         } else {
486             return new ResponseEntity<>(HttpStatus.CREATED);
487         }
488
489     }
490
491     @Override
492     public ResponseEntity<Void> configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIservicePost(@Valid GenericResourceApiServicemodelinfrastructureService servicesData) throws RestApplicationException {
493         String svcInstanceId = servicesData.getServiceInstanceId();
494         try {
495             String svcData = objectMapper.writeValueAsString(servicesData.getServiceData());
496             ConfigServices configService = new ConfigServices(svcInstanceId, svcData, servicesData.getServiceStatus());
497             configServicesRepository.deleteBySvcInstanceId(svcInstanceId);
498             configServicesRepository.save(configService);
499         } catch (JsonProcessingException e) {
500             log.error("Cannot convert service data", e);
501             throw new RestApplicationException("data-conversion", "Request could not be completed due to internal error", e, HttpStatus.INTERNAL_SERVER_ERROR.value());
502
503         }
504         return new ResponseEntity<>(HttpStatus.OK);
505     }
506
507     @Override
508     public ResponseEntity<Void> configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdDelete(String serviceInstanceId) {
509         configServicesRepository.deleteBySvcInstanceId(serviceInstanceId);
510         return new ResponseEntity<>(HttpStatus.NO_CONTENT);
511     }
512
513     @Override
514     public ResponseEntity<GenericResourceApiServicemodelinfrastructureService> configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGet(String serviceInstanceId) throws RestApplicationException {
515         GenericResourceApiServicemodelinfrastructureService retval = null;
516
517         List<ConfigServices> services = configServicesRepository.findBySvcInstanceId(serviceInstanceId);
518
519         if (services.isEmpty()) {
520             return new ResponseEntity<>(HttpStatus.NOT_FOUND);
521         } else {
522             ConfigServices service = services.get(0);
523             retval = new GenericResourceApiServicemodelinfrastructureService();
524             retval.setServiceInstanceId(serviceInstanceId);
525             retval.setServiceStatus(service.getServiceStatus());
526             try {
527                 retval.setServiceData(objectMapper.readValue(service.getSvcData(), GenericResourceApiServicedataServiceData.class));
528             } catch (JsonProcessingException e) {
529                 log.error("Could not deserialize service data for service instance id {}", serviceInstanceId, e);
530                 throw new RestApplicationException("data-conversion", "Request could not be completed due to internal error", e, HttpStatus.INTERNAL_SERVER_ERROR.value());
531
532             }
533         }
534
535
536         return new ResponseEntity<>(retval, HttpStatus.OK);
537
538     }
539
540     @Override
541     public ResponseEntity<Void> configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdPost(String svcInstanceId, @Valid GenericResourceApiServicemodelinfrastructureService newService) throws RestApplicationException, RestProtocolException {
542
543         List<ConfigServices> existingService = configServicesRepository.findBySvcInstanceId(svcInstanceId);
544         if ((existingService != null) && !existingService.isEmpty()) {
545             log.error("Service data already exists for {}", svcInstanceId);
546             throw new RestProtocolException("data-exists", "Data already exists for service-instance-id " + svcInstanceId, HttpStatus.CONFLICT.value());
547         }
548         ConfigServices service = new ConfigServices();
549         service.setSvcInstanceId(svcInstanceId);
550         try {
551             service.setSvcData(objectMapper.writeValueAsString(newService.getServiceData()));
552         } catch (JsonProcessingException e) {
553             log.error("Could not serialize service data for {}", service.getSvcInstanceId(), e);
554             throw new RestApplicationException("data-conversion", "Request could not be completed due to internal error", e, HttpStatus.INTERNAL_SERVER_ERROR.value());
555
556         }
557         service.setServiceStatus(newService.getServiceStatus());
558         configServicesRepository.save(service);
559
560         return new ResponseEntity<>(HttpStatus.CREATED);
561     }
562
563     @Override
564     public ResponseEntity<Void> configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdPut(String serviceInstanceId, @Valid GenericResourceApiServicemodelinfrastructureService newService) throws RestApplicationException {
565
566         boolean dataExists = false;
567
568         String svcInstanceId = newService.getServiceInstanceId();
569
570         ConfigServices service = null;
571         List<ConfigServices> existingService = configServicesRepository.findBySvcInstanceId(svcInstanceId);
572         if ((existingService != null) && !existingService.isEmpty()) {
573             dataExists = true;
574             service = existingService.get(0);
575         } else {
576             service = new ConfigServices();
577             service.setSvcInstanceId(svcInstanceId);
578         }
579
580         try {
581             service.setSvcData(objectMapper.writeValueAsString(newService.getServiceData()));
582         } catch (JsonProcessingException e) {
583             log.error("Could not serialize service data for {}", service.getSvcInstanceId(), e);
584             throw new RestApplicationException("data-conversion", "Request could not be completed due to internal error", e, HttpStatus.INTERNAL_SERVER_ERROR.value());
585
586         }
587         service.setServiceStatus(newService.getServiceStatus());
588         configServicesRepository.save(service);
589
590         if (dataExists) {
591             return new ResponseEntity<>(HttpStatus.NO_CONTENT);
592         } else {
593             return new ResponseEntity<>(HttpStatus.CREATED);
594         }
595     }
596
597
598     @Override
599     public ResponseEntity<Void> configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceDataDelete(String serviceInstanceId) throws RestProtocolException {
600         List<ConfigServices> services = configServicesRepository.findBySvcInstanceId(serviceInstanceId);
601
602         if ((services == null) || (services.isEmpty())) {
603             throw new RestProtocolException("data-missing", "No service entry found", HttpStatus.NOT_FOUND.value());
604         }
605
606         ConfigServices service = services.get(0);
607         if (service.getSvcData() == null) {
608             throw new RestProtocolException("data-missing", "No service-data found", HttpStatus.NOT_FOUND.value());
609         }
610         service.setSvcData(null);
611         configServicesRepository.save(service);
612
613         return new ResponseEntity<>(HttpStatus.NO_CONTENT);
614     }
615
616     @Override
617     public ResponseEntity<GenericResourceApiServicedataServiceData> configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceDataGet(String serviceInstanceId) throws RestApplicationException, RestProtocolException {
618         GenericResourceApiServicedataServiceData serviceData = null;
619
620         List<ConfigServices> services = configServicesRepository.findBySvcInstanceId(serviceInstanceId);
621         if ((services == null) || (services.isEmpty())) {
622             throw new RestProtocolException("data-missing", "No service entry found", HttpStatus.NOT_FOUND.value());
623         }
624
625         try {
626             serviceData = objectMapper.readValue(services.get(0).getSvcData(), GenericResourceApiServicedataServiceData.class);
627             return new ResponseEntity<>(serviceData, HttpStatus.OK);
628         } catch (JsonProcessingException e) {
629             log.error("Could not parse service data", e);
630             throw new RestApplicationException("data-conversion", "Request could not be completed due to internal error", e, HttpStatus.INTERNAL_SERVER_ERROR.value());
631         }
632
633     }
634
635     @Override
636     public ResponseEntity<Void> configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceDataPost(String serviceInstanceId, @Valid GenericResourceApiServicedataServiceData serviceData) throws RestApplicationException, RestProtocolException {
637         ConfigServices service;
638         List<ConfigServices> services = configServicesRepository.findBySvcInstanceId(serviceInstanceId);
639         if ((services == null) || (services.isEmpty())) {
640             throw new RestProtocolException("data-missing", "No service entry found", HttpStatus.NOT_FOUND.value());
641         }
642
643         if ((serviceData == null) ||
644                 (serviceData.getServiceInformation() == null)) {
645             throw new RestProtocolException("bad-attribute", "Invalid service-data received", HttpStatus.BAD_REQUEST.value());
646
647         }
648         service = services.get(0);
649
650         if ((service.getSvcData() != null) && (service.getSvcData().length() > 0)){
651             log.error("service-data already exists for svcInstanceId {}", serviceInstanceId);
652             throw new RestProtocolException("data-exists", "Data already exists for " + serviceInstanceId, HttpStatus.CONFLICT.value());
653         }
654
655
656         try {
657             service.setSvcData(objectMapper.writeValueAsString(serviceData));
658             configServicesRepository.save(service);
659         } catch (JsonProcessingException e) {
660             log.error("Could not serialize service data for svc instance id {}", serviceInstanceId, e);
661             throw new RestApplicationException("data-conversion", "Request could not be completed due to internal error", e, HttpStatus.INTERNAL_SERVER_ERROR.value());
662         }
663
664
665         return new ResponseEntity<>(HttpStatus.CREATED);
666
667     }
668
669     @Override
670     public ResponseEntity<Void> configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceDataPut(String serviceInstanceId, @Valid GenericResourceApiServicedataServiceData serviceData) throws RestApplicationException, RestProtocolException {
671         ConfigServices service;
672         boolean dataExists = false;
673
674         List<ConfigServices> services = configServicesRepository.findBySvcInstanceId(serviceInstanceId);
675         if ((services == null) || (services.isEmpty())) {
676             throw new RestProtocolException("data-missing", "No service entry found", HttpStatus.NOT_FOUND.value());
677         }
678
679         if ((serviceData == null) ||
680                 (serviceData.getServiceInformation() == null)) {
681             throw new RestProtocolException("bad-attribute", "Invalid service-data received", HttpStatus.BAD_REQUEST.value());
682
683         }
684         service = services.get(0);
685
686         if ((service.getSvcData() != null) && (service.getSvcData().length() > 0)) {
687             dataExists = true;
688         }
689
690         try {
691             service.setSvcData(objectMapper.writeValueAsString(serviceData));
692             configServicesRepository.save(service);
693         } catch (JsonProcessingException e) {
694             log.error("Could not serialize service data for svc instance id {}", serviceInstanceId, e);
695             throw new RestApplicationException("data-conversion", "Request could not be completed due to internal error", e, HttpStatus.INTERNAL_SERVER_ERROR.value());
696         }
697
698         if (dataExists) {
699             return new ResponseEntity<>(HttpStatus.NO_CONTENT);
700         } else {
701             return new ResponseEntity<>(HttpStatus.CREATED);
702         }
703     }
704
705     @Override
706     public ResponseEntity<Void> configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceStatusDelete(String serviceInstanceId) throws RestProtocolException {
707         List<ConfigServices> services = configServicesRepository.findBySvcInstanceId(serviceInstanceId);
708
709         if ((services == null) || (services.isEmpty())) {
710             throw new RestProtocolException("data-missing", "No service entry found", HttpStatus.NOT_FOUND.value());
711         }
712
713         ConfigServices service = services.get(0);
714         if (service.getServiceStatus() == null) {
715             throw new RestProtocolException("data-missing", "No service-status found", HttpStatus.NOT_FOUND.value());
716         }
717         service.setServiceStatus(null);
718         configServicesRepository.save(service);
719
720         return new ResponseEntity<>(HttpStatus.NO_CONTENT);
721
722     }
723
724     @Override
725     public ResponseEntity<GenericResourceApiServicestatusServiceStatus> configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceStatusGet(String serviceInstanceId) throws RestApplicationException, RestProtocolException {
726         GenericResourceApiServicestatusServiceStatus serviceStatus = null;
727
728         List<ConfigServices> services = configServicesRepository.findBySvcInstanceId(serviceInstanceId);
729         if ((services == null) || (services.isEmpty())) {
730             throw new RestProtocolException("data-missing", "No service entry found", HttpStatus.NOT_FOUND.value());
731         }
732
733         serviceStatus = services.get(0).getServiceStatus();
734         return new ResponseEntity<>(serviceStatus, HttpStatus.OK);
735     }
736
737     @Override
738     public ResponseEntity<Void> configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceStatusPost(String serviceInstanceId, @Valid GenericResourceApiServicestatusServiceStatus serviceStatus) throws RestProtocolException {
739         ConfigServices service;
740         List<ConfigServices> services = configServicesRepository.findBySvcInstanceId(serviceInstanceId);
741         if ((services == null) || (services.isEmpty())) {
742             throw new RestProtocolException("data-missing", "No service entry found", HttpStatus.NOT_FOUND.value());
743         }
744
745         if ((serviceStatus == null) ||
746                 (serviceStatus.getAction() == null)) {
747             throw new RestProtocolException("bad-attribute", "Invalid service-status received", HttpStatus.BAD_REQUEST.value());
748
749         }
750         service = services.get(0);
751
752         if (service.getServiceStatus() != null) {
753             log.error("service-status already exists for svcInstanceId {}", serviceInstanceId);
754             throw new RestProtocolException("data-exists", "Data already exists for " + serviceInstanceId, HttpStatus.CONFLICT.value());
755         }
756
757
758         service.setServiceStatus(serviceStatus);
759         configServicesRepository.save(service);
760
761
762         return new ResponseEntity<>(HttpStatus.CREATED);
763
764     }
765
766     @Override
767     public ResponseEntity<Void> configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceStatusPut(String serviceInstanceId, @Valid GenericResourceApiServicestatusServiceStatus serviceStatus) throws RestProtocolException {
768         ConfigServices service;
769         boolean dataExists = false;
770
771         List<ConfigServices> services = configServicesRepository.findBySvcInstanceId(serviceInstanceId);
772         if ((services == null) || (services.isEmpty())) {
773             throw new RestProtocolException("data-missing", "No service entry found", HttpStatus.NOT_FOUND.value());
774         }
775
776         if ((serviceStatus == null) ||
777                 (serviceStatus.getAction() == null)) {
778             throw new RestProtocolException("bad-attribute", "Invalid service-status received", HttpStatus.BAD_REQUEST.value());
779
780         }
781         service = services.get(0);
782
783         if (service.getServiceStatus() != null) {
784             dataExists = true;
785         }
786
787
788         service.setServiceStatus(serviceStatus);
789         configServicesRepository.save(service);
790
791         if (dataExists) {
792             return new ResponseEntity<>(HttpStatus.NO_CONTENT);
793         } else {
794             return new ResponseEntity<>(HttpStatus.CREATED);
795         }
796     }
797
798 }