2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.sdnc.apps.ms.gra.controllers;
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;
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;
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);
59 private final ObjectMapper objectMapper;
61 private final HttpServletRequest request;
64 private ConfigPreloadDataRepository configPreloadDataRepository;
67 private ConfigServicesRepository configServicesRepository;
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;
78 public Optional<ObjectMapper> getObjectMapper() {
79 return Optional.ofNullable(objectMapper);
83 public Optional<HttpServletRequest> getRequest() {
84 return Optional.ofNullable(request);
88 public ResponseEntity<Void> configGENERICRESOURCEAPIpreloadInformationDelete() {
89 configPreloadDataRepository.deleteAll();
90 return (new ResponseEntity<>(HttpStatus.NO_CONTENT));
94 public ResponseEntity<GenericResourceApiPreloadModelInformation> configGENERICRESOURCEAPIpreloadInformationGet() throws RestApplicationException {
95 GenericResourceApiPreloadModelInformation genericResourceApiPreloadModelInformation = new GenericResourceApiPreloadModelInformation();
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());
101 for (ConfigPreloadData configPreloadData : configPreloadDataRepository.findAll()) {
102 GenericResourceApiPreloadmodelinformationPreloadList preloadListItem = new GenericResourceApiPreloadmodelinformationPreloadList();
104 preloadListItem.setPreloadId(configPreloadData.getPreloadId());
105 preloadListItem.setPreloadType(configPreloadData.getPreloadType());
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());
112 genericResourceApiPreloadModelInformation.addPreloadListItem(preloadListItem);
116 return new ResponseEntity<>(genericResourceApiPreloadModelInformation, HttpStatus.OK);
120 public ResponseEntity<Void> configGENERICRESOURCEAPIpreloadInformationPost(@Valid GenericResourceApiPreloadModelInformation graPreloadModelInfo) throws RestApplicationException, RestProtocolException {
122 List<GenericResourceApiPreloadmodelinformationPreloadList> preloadList = graPreloadModelInfo.getPreloadList();
123 List<ConfigPreloadData> newPreloadData = new LinkedList<>();
125 if (preloadList != null) {
126 // Verification pass - if any items already exist, return an error
127 for (GenericResourceApiPreloadmodelinformationPreloadList curItem : preloadList) {
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());
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());
145 for (ConfigPreloadData newDataItem : newPreloadData) {
146 log.info("Adding preload data for {}:{}", newDataItem.getPreloadId(), newDataItem.getPreloadType());
147 configPreloadDataRepository.save(newDataItem);
150 throw new RestProtocolException("data-missing", "No preload-list entries found to add", HttpStatus.CONFLICT.value());
153 return new ResponseEntity<>(HttpStatus.CREATED);
157 public ResponseEntity<Void> configGENERICRESOURCEAPIpreloadInformationPut(@Valid GenericResourceApiPreloadModelInformation graPreloadModelInfo) throws RestApplicationException {
159 boolean addedNew = false;
160 List<GenericResourceApiPreloadmodelinformationPreloadList> preloadList = graPreloadModelInfo.getPreloadList();
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()) {
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());
182 return new ResponseEntity<>(HttpStatus.CREATED);
184 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
190 public ResponseEntity<Void> configGENERICRESOURCEAPIpreloadInformationPreloadListPost(@Valid GenericResourceApiPreloadmodelinformationPreloadList preloadListItem) throws RestProtocolException {
192 throw new RestProtocolException("data-missing", "Missing key for list \"preload-list\"", HttpStatus.NOT_FOUND.value());
197 public ResponseEntity<Void> configGENERICRESOURCEAPIpreloadInformationPreloadListPreloadIdPreloadTypeDelete(String preloadId, String preloadType) {
198 configPreloadDataRepository.deleteByPreloadIdAndPreloadType(preloadId, preloadType);
199 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
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());
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());
217 return new ResponseEntity<>(preloadDataList, HttpStatus.OK);
220 return new ResponseEntity<>(HttpStatus.NOT_FOUND);
224 public ResponseEntity<Void> configGENERICRESOURCEAPIpreloadInformationPreloadListPreloadIdPreloadTypePost(String preloadId, String preloadType, @Valid GenericResourceApiPreloadmodelinformationPreloadList preloadListItem) throws RestApplicationException, RestProtocolException {
225 List<ConfigPreloadData> preloadDataItems = configPreloadDataRepository.findByPreloadIdAndPreloadType(preloadId, preloadType);
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());
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());
240 return new ResponseEntity<>(HttpStatus.CREATED);
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()) {
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());
260 log.info("Updating preload data for {}:{} -> {}", preloadId, preloadType, objectMapper.writeValueAsString(preloadListItem));
263 log.info("Adding preload data for {}:{}", preloadId, preloadType);
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());
274 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
276 return new ResponseEntity<>(HttpStatus.CREATED);
282 public ResponseEntity<Void> configGENERICRESOURCEAPIpreloadInformationPreloadListPreloadIdPreloadTypePreloadDataDelete(String preloadId, String preloadType) throws RestProtocolException {
283 List<ConfigPreloadData> preloadData = configPreloadDataRepository.findByPreloadIdAndPreloadType(preloadId, preloadType);
285 if ((preloadData == null) || preloadData.isEmpty()) {
286 throw new RestProtocolException("data-missing", "No preload entry found", HttpStatus.NOT_FOUND.value());
289 ConfigPreloadData preloadDataItem = preloadData.get(0);
291 if (preloadDataItem.getPreloadData() == null) {
292 throw new RestProtocolException("data-missing", "No preload-data found", HttpStatus.NOT_FOUND.value());
294 preloadDataItem.setPreloadData(null);
295 configPreloadDataRepository.save(preloadDataItem);
298 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
303 public ResponseEntity<GenericResourceApiPreloaddataPreloadData> configGENERICRESOURCEAPIpreloadInformationPreloadListPreloadIdPreloadTypePreloadDataGet(String preloadId, String preloadType) throws RestApplicationException, RestProtocolException {
304 List<ConfigPreloadData> preloadData = configPreloadDataRepository.findByPreloadIdAndPreloadType(preloadId, preloadType);
306 if ((preloadData == null) || preloadData.isEmpty()) {
307 throw new RestProtocolException("data-missing", "No preload entry found", HttpStatus.NOT_FOUND.value());
310 ConfigPreloadData preloadDataItem = preloadData.get(0);
312 if (preloadDataItem.getPreloadData() == null) {
313 throw new RestProtocolException("data-missing", "No preload-data found", HttpStatus.NOT_FOUND.value());
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());
324 public ResponseEntity<Void> configGENERICRESOURCEAPIpreloadInformationPreloadListPreloadIdPreloadTypePreloadDataPost(String preloadId, String preloadType, @Valid GenericResourceApiPreloaddataPreloadData preloadData) throws RestApplicationException, RestProtocolException {
325 List<ConfigPreloadData> preloadDataEntries = configPreloadDataRepository.findByPreloadIdAndPreloadType(preloadId, preloadType);
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());
332 if ((preloadData == null) ||
333 (preloadData.getPreloadNetworkTopologyInformation() == null)) {
334 throw new RestProtocolException("bad-attribute", "Invalid preloadData received", HttpStatus.BAD_REQUEST.value());
337 ConfigPreloadData preloadDataItem = preloadDataItems.get(0);
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());
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());
352 return new ResponseEntity<>(HttpStatus.CREATED);
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());
363 if ((preloadData == null) ||
364 (preloadData.getPreloadNetworkTopologyInformation() == null)) {
365 throw new RestProtocolException("bad-attribute", "Invalid preloadData received", HttpStatus.BAD_REQUEST.value());
368 ConfigPreloadData preloadDataItem = preloadDataItems.get(0);
370 if (preloadDataItem.getPreloadData() != null) {
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());
383 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
385 return new ResponseEntity<>(HttpStatus.CREATED);
390 public ResponseEntity<Void> configGENERICRESOURCEAPIservicesDelete() {
391 configServicesRepository.deleteAll();
392 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
396 public ResponseEntity<GenericResourceApiServiceModelInfrastructure> configGENERICRESOURCEAPIservicesGet() throws RestApplicationException {
397 GenericResourceApiServiceModelInfrastructure modelInfrastructure = new GenericResourceApiServiceModelInfrastructure();
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());
403 for (ConfigServices service : configServicesRepository.findAll()) {
404 GenericResourceApiServicemodelinfrastructureService serviceItem = new GenericResourceApiServicemodelinfrastructureService();
405 serviceItem.setServiceInstanceId(service.getSvcInstanceId());
406 if (service.getSvcData() != null) {
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());
415 serviceItem.setServiceStatus(service.getServiceStatus());
416 modelInfrastructure.addServiceItem(serviceItem);
420 return new ResponseEntity<>(modelInfrastructure, HttpStatus.OK);
425 public ResponseEntity<Void> configGENERICRESOURCEAPIservicesPost(@Valid GenericResourceApiServiceModelInfrastructure modelInfrastructure) throws RestApplicationException, RestProtocolException {
426 List<ConfigServices> newServices = new LinkedList<>();
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());
435 ConfigServices service = new ConfigServices();
436 service.setSvcInstanceId(svcInstanceId);
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());
444 service.setServiceStatus(serviceItem.getServiceStatus());
445 newServices.add(service);
448 for (ConfigServices service : newServices) {
449 configServicesRepository.save(service);
452 return new ResponseEntity<>(HttpStatus.CREATED);
457 public ResponseEntity<Void> configGENERICRESOURCEAPIservicesPut(@Valid GenericResourceApiServiceModelInfrastructure modelInfrastructure) throws RestApplicationException {
459 List<ConfigServices> newServices = new LinkedList<>();
460 boolean dataExists = false;
462 for (GenericResourceApiServicemodelinfrastructureService serviceItem : modelInfrastructure.getService()) {
463 String svcInstanceId = serviceItem.getServiceInstanceId();
464 List<ConfigServices> existingService = configServicesRepository.findBySvcInstanceId(svcInstanceId);
465 if ((existingService != null) && !existingService.isEmpty()) {
468 ConfigServices service = new ConfigServices();
469 service.setSvcInstanceId(svcInstanceId);
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());
477 service.setServiceStatus(serviceItem.getServiceStatus());
478 newServices.add(service);
481 for (ConfigServices service : newServices) {
482 configServicesRepository.save(service);
486 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
488 return new ResponseEntity<>(HttpStatus.CREATED);
494 public ResponseEntity<Void> configGENERICRESOURCEAPIservicesServicePost(@Valid GenericResourceApiServicemodelinfrastructureService servicesData) throws RestApplicationException {
495 String svcInstanceId = servicesData.getServiceInstanceId();
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());
506 return new ResponseEntity<>(HttpStatus.OK);
510 public ResponseEntity<Void> configGENERICRESOURCEAPIservicesServiceServiceInstanceIdDelete(String serviceInstanceId) {
511 configServicesRepository.deleteBySvcInstanceId(serviceInstanceId);
512 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
516 public ResponseEntity<GenericResourceApiServicemodelinfrastructureService> configGENERICRESOURCEAPIservicesServiceServiceInstanceIdGet(String serviceInstanceId) throws RestApplicationException {
517 GenericResourceApiServicemodelinfrastructureService retval = null;
519 List<ConfigServices> services = configServicesRepository.findBySvcInstanceId(serviceInstanceId);
521 if (services.isEmpty()) {
522 return new ResponseEntity<>(HttpStatus.NOT_FOUND);
524 ConfigServices service = services.get(0);
525 retval = new GenericResourceApiServicemodelinfrastructureService();
526 retval.setServiceInstanceId(serviceInstanceId);
527 retval.setServiceStatus(service.getServiceStatus());
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());
538 return new ResponseEntity<>(retval, HttpStatus.OK);
543 public ResponseEntity<Void> configGENERICRESOURCEAPIservicesServiceServiceInstanceIdPost(String svcInstanceId, @Valid GenericResourceApiServicemodelinfrastructureService newService) throws RestApplicationException, RestProtocolException {
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());
550 ConfigServices service = new ConfigServices();
551 service.setSvcInstanceId(svcInstanceId);
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());
559 service.setServiceStatus(newService.getServiceStatus());
560 configServicesRepository.save(service);
562 return new ResponseEntity<>(HttpStatus.CREATED);
566 public ResponseEntity<Void> configGENERICRESOURCEAPIservicesServiceServiceInstanceIdPut(String serviceInstanceId, @Valid GenericResourceApiServicemodelinfrastructureService newService) throws RestApplicationException {
568 boolean dataExists = false;
570 String svcInstanceId = newService.getServiceInstanceId();
572 ConfigServices service = null;
573 List<ConfigServices> existingService = configServicesRepository.findBySvcInstanceId(svcInstanceId);
574 if ((existingService != null) && !existingService.isEmpty()) {
576 service = existingService.get(0);
578 service = new ConfigServices();
579 service.setSvcInstanceId(svcInstanceId);
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());
589 service.setServiceStatus(newService.getServiceStatus());
590 configServicesRepository.save(service);
593 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
595 return new ResponseEntity<>(HttpStatus.CREATED);
601 public ResponseEntity<Void> configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceDataDelete(String serviceInstanceId) throws RestProtocolException {
602 List<ConfigServices> services = configServicesRepository.findBySvcInstanceId(serviceInstanceId);
604 if ((services == null) || (services.isEmpty())) {
605 throw new RestProtocolException("data-missing", "No service entry found", HttpStatus.NOT_FOUND.value());
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());
612 service.setSvcData(null);
613 configServicesRepository.save(service);
615 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
619 public ResponseEntity<GenericResourceApiServicedataServiceData> configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceDataGet(String serviceInstanceId) throws RestApplicationException, RestProtocolException {
620 GenericResourceApiServicedataServiceData serviceData = null;
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());
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());
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());
645 if ((serviceData == null) ||
646 (serviceData.getServiceInformation() == null)) {
647 throw new RestProtocolException("bad-attribute", "Invalid service-data received", HttpStatus.BAD_REQUEST.value());
650 service = services.get(0);
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());
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());
667 return new ResponseEntity<>(HttpStatus.CREATED);
672 public ResponseEntity<Void> configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceDataPut(String serviceInstanceId, @Valid GenericResourceApiServicedataServiceData serviceData) throws RestApplicationException, RestProtocolException {
673 ConfigServices service;
674 boolean dataExists = false;
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());
681 if ((serviceData == null) ||
682 (serviceData.getServiceInformation() == null)) {
683 throw new RestProtocolException("bad-attribute", "Invalid service-data received", HttpStatus.BAD_REQUEST.value());
686 service = services.get(0);
688 if ((service.getSvcData() != null) && (service.getSvcData().length() > 0)) {
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());
701 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
703 return new ResponseEntity<>(HttpStatus.CREATED);
708 public ResponseEntity<Void> configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceStatusDelete(String serviceInstanceId) throws RestProtocolException {
709 List<ConfigServices> services = configServicesRepository.findBySvcInstanceId(serviceInstanceId);
711 if ((services == null) || (services.isEmpty())) {
712 throw new RestProtocolException("data-missing", "No service entry found", HttpStatus.NOT_FOUND.value());
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());
719 service.setServiceStatus(null);
720 configServicesRepository.save(service);
722 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
727 public ResponseEntity<GenericResourceApiServicestatusServiceStatus> configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceStatusGet(String serviceInstanceId) throws RestApplicationException, RestProtocolException {
728 GenericResourceApiServicestatusServiceStatus serviceStatus = null;
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());
735 serviceStatus = services.get(0).getServiceStatus();
736 return new ResponseEntity<>(serviceStatus, HttpStatus.OK);
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());
747 if ((serviceStatus == null) ||
748 (serviceStatus.getAction() == null)) {
749 throw new RestProtocolException("bad-attribute", "Invalid service-status received", HttpStatus.BAD_REQUEST.value());
752 service = services.get(0);
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());
760 service.setServiceStatus(serviceStatus);
761 configServicesRepository.save(service);
764 return new ResponseEntity<>(HttpStatus.CREATED);
769 public ResponseEntity<Void> configGENERICRESOURCEAPIservicesServiceServiceInstanceIdServiceStatusPut(String serviceInstanceId, @Valid GenericResourceApiServicestatusServiceStatus serviceStatus) throws RestProtocolException {
770 ConfigServices service;
771 boolean dataExists = false;
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());
778 if ((serviceStatus == null) ||
779 (serviceStatus.getAction() == null)) {
780 throw new RestProtocolException("bad-attribute", "Invalid service-status received", HttpStatus.BAD_REQUEST.value());
783 service = services.get(0);
785 if (service.getServiceStatus() != null) {
790 service.setServiceStatus(serviceStatus);
791 configServicesRepository.save(service);
794 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
796 return new ResponseEntity<>(HttpStatus.CREATED);