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.sdnc.apps.ms.gra.data.ConfigPreloadData;
27 import org.onap.sdnc.apps.ms.gra.data.ConfigPreloadDataRepository;
28 import org.onap.sdnc.apps.ms.gra.data.ConfigServices;
29 import org.onap.sdnc.apps.ms.gra.data.ConfigServicesRepository;
30 import org.onap.sdnc.apps.ms.gra.swagger.ConfigApi;
31 import org.onap.sdnc.apps.ms.gra.swagger.model.*;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.boot.autoconfigure.domain.EntityScan;
36 import org.springframework.context.annotation.ComponentScan;
37 import org.springframework.http.HttpStatus;
38 import org.springframework.http.ResponseEntity;
39 import org.springframework.stereotype.Controller;
41 import javax.servlet.http.HttpServletRequest;
42 import javax.validation.Valid;
43 import java.util.Iterator;
44 import java.util.List;
45 import java.util.Optional;
48 @ComponentScan(basePackages = {"org.onap.sdnc.apps.ms.gra.*"})
49 @EntityScan("org.onap.sdnc.apps.ms.gra.springboot.*")
50 public class ConfigApiController implements ConfigApi {
51 private static final Logger log = LoggerFactory.getLogger(ConfigApiController.class);
53 private final ObjectMapper objectMapper;
55 private final HttpServletRequest request;
58 private ConfigPreloadDataRepository configPreloadDataRepository;
61 private ConfigServicesRepository configServicesRepository;
64 public ConfigApiController(ObjectMapper objectMapper, HttpServletRequest request) {
65 objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
66 objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
67 this.objectMapper = objectMapper;
68 this.request = request;
72 public Optional<ObjectMapper> getObjectMapper() {
73 return Optional.ofNullable(objectMapper);
77 public Optional<HttpServletRequest> getRequest() {
78 return Optional.ofNullable(request);
82 public ResponseEntity<Void> configGENERICRESOURCEAPIpreloadInformationDelete() {
83 configPreloadDataRepository.deleteAll();
84 return (new ResponseEntity<>(HttpStatus.OK));
88 public ResponseEntity<GenericResourceApiPreloadModelInformation> configGENERICRESOURCEAPIpreloadInformationGet() {
89 GenericResourceApiPreloadModelInformation genericResourceApiPreloadModelInformation = new GenericResourceApiPreloadModelInformation();
91 configPreloadDataRepository.findAll().forEach(configPreloadData -> {
92 GenericResourceApiPreloadmodelinformationPreloadList preloadListItem = new GenericResourceApiPreloadmodelinformationPreloadList();
94 preloadListItem.setPreloadId(configPreloadData.getPreloadId());
95 preloadListItem.setPreloadType(configPreloadData.getPreloadType());
97 preloadListItem.setPreloadData(objectMapper.readValue(configPreloadData.getPreloadData(), GenericResourceApiPreloaddataPreloadData.class));
98 } catch (JsonProcessingException e) {
99 log.error("Could not convert preload data", e);
101 genericResourceApiPreloadModelInformation.addPreloadListItem(preloadListItem);
105 return new ResponseEntity<>(genericResourceApiPreloadModelInformation, HttpStatus.OK);
109 public ResponseEntity<Void> configGENERICRESOURCEAPIpreloadInformationPost(@Valid GenericResourceApiPreloadModelInformation graPreloadModelInfo) {
111 List<GenericResourceApiPreloadmodelinformationPreloadList> preloadList = graPreloadModelInfo.getPreloadList();
113 if (preloadList != null) {
114 Iterator<GenericResourceApiPreloadmodelinformationPreloadList> iter = preloadList.iterator();
115 while (iter.hasNext()) {
116 GenericResourceApiPreloadmodelinformationPreloadList curItem = iter.next();
118 // Remove any entries already existing for this preloadId/preloadType
119 configPreloadDataRepository.deleteByPreloadIdAndPreloadType(curItem.getPreloadId(), curItem.getPreloadType());
122 configPreloadDataRepository.save(new ConfigPreloadData(curItem.getPreloadId(), curItem.getPreloadType(), objectMapper.writeValueAsString(curItem.getPreloadData())));
123 } catch (JsonProcessingException e) {
124 log.error("Cannot convert preload data", e);
129 return new ResponseEntity<>(HttpStatus.OK);
133 public ResponseEntity<Void> configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPost(@Valid GenericResourceApiPreloadmodelinformationPreloadList preloadListItem) {
135 // Remove any entries already existing for this preloadId/preloadType
136 configPreloadDataRepository.deleteByPreloadIdAndPreloadType(preloadListItem.getPreloadId(), preloadListItem.getPreloadType());
139 configPreloadDataRepository.save(new ConfigPreloadData(preloadListItem.getPreloadId(), preloadListItem.getPreloadType(), objectMapper.writeValueAsString(preloadListItem.getPreloadData())));
140 } catch (JsonProcessingException e) {
141 log.error("Cannot convert preload data", e);
143 return new ResponseEntity<>(HttpStatus.OK);
147 public ResponseEntity<Void> configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypeDelete(String preloadId, String preloadType) {
148 configPreloadDataRepository.deleteByPreloadIdAndPreloadType(preloadId, preloadType);
149 return new ResponseEntity<>(HttpStatus.OK);
153 public ResponseEntity<GenericResourceApiPreloadmodelinformationPreloadList> configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypeGet(String preloadId, String preloadType) {
154 List<ConfigPreloadData> preloadData = configPreloadDataRepository.findByPreloadIdAndPreloadType(preloadId, preloadType);
155 if (preloadData != null) {
156 if (!preloadData.isEmpty()) {
157 ConfigPreloadData preloadDataItem = preloadData.get(0);
158 GenericResourceApiPreloadmodelinformationPreloadList preloadDataList = new GenericResourceApiPreloadmodelinformationPreloadList();
159 preloadDataList.setPreloadId(preloadDataItem.getPreloadId());
160 preloadDataList.setPreloadType(preloadDataItem.getPreloadType());
162 preloadDataList.setPreloadData(objectMapper.readValue(preloadDataItem.getPreloadData(), GenericResourceApiPreloaddataPreloadData.class));
163 } catch (JsonProcessingException e) {
164 log.error("Cannot convert preload data", e);
166 return new ResponseEntity<>(preloadDataList, HttpStatus.OK);
169 return new ResponseEntity<>(HttpStatus.NOT_FOUND);
173 public ResponseEntity<Void> configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypePost(String preloadId, String preloadType, @Valid GenericResourceApiPreloadmodelinformationPreloadList preloadListItem) {
174 configPreloadDataRepository.deleteByPreloadIdAndPreloadType(preloadId, preloadType);
176 configPreloadDataRepository.save(new ConfigPreloadData(preloadId, preloadType, objectMapper.writeValueAsString(preloadListItem.getPreloadData())));
177 } catch (JsonProcessingException e) {
178 log.error("Cannot convert preload data", e);
180 return new ResponseEntity<>(HttpStatus.OK);
185 public ResponseEntity<Void> configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypeGENERICRESOURCEAPIpreloadDataDelete(String preloadId, String preloadType) {
186 List<ConfigPreloadData> preloadData = configPreloadDataRepository.findByPreloadIdAndPreloadType(preloadId, preloadType);
188 if (preloadData != null) {
189 Iterator<ConfigPreloadData> iter = preloadData.iterator();
191 while (iter.hasNext()) {
192 configPreloadDataRepository.delete(iter.next());
195 return new ResponseEntity<>(HttpStatus.OK);
199 public ResponseEntity<Void> configGENERICRESOURCEAPIpreloadInformationPut(@Valid GenericResourceApiPreloadModelInformation genericResourceApiPreloadModelInformationBodyParam) {
204 public ResponseEntity<GenericResourceApiPreloaddataPreloadData> configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypeGENERICRESOURCEAPIpreloadDataGet(String preloadId, String preloadType) {
205 List<ConfigPreloadData> preloadData = configPreloadDataRepository.findByPreloadIdAndPreloadType(preloadId, preloadType);
206 if (preloadData != null) {
207 if (!preloadData.isEmpty()) {
208 ConfigPreloadData preloadDataItem = preloadData.get(0);
210 return new ResponseEntity<>(objectMapper.readValue(preloadDataItem.getPreloadData(), GenericResourceApiPreloaddataPreloadData.class), HttpStatus.OK);
211 } catch (JsonProcessingException e) {
212 log.error("Cannot convert preload data", e);
216 return new ResponseEntity<>(HttpStatus.NOT_FOUND);
220 public ResponseEntity<Void> configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypeGENERICRESOURCEAPIpreloadDataPost(String preloadId, String preloadType, @Valid GenericResourceApiPreloaddataPreloadData preloadData) {
221 configPreloadDataRepository.deleteByPreloadIdAndPreloadType(preloadId, preloadType);
223 configPreloadDataRepository.save(new ConfigPreloadData(preloadId, preloadType, objectMapper.writeValueAsString(preloadData)));
224 } catch (JsonProcessingException e) {
225 log.error("Cannot convert preload data", e);
227 return new ResponseEntity<>(HttpStatus.OK);
231 public ResponseEntity<Void> configGENERICRESOURCEAPIservicesDelete() {
232 configServicesRepository.deleteAll();
233 return new ResponseEntity<>(HttpStatus.OK);
237 public ResponseEntity<Void> configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIservicePost(@Valid GenericResourceApiServicemodelinfrastructureService servicesData) {
238 String svcInstanceId = servicesData.getServiceInstanceId();
240 String svcData = objectMapper.writeValueAsString(servicesData.getServiceData());
241 ConfigServices configService = new ConfigServices(svcInstanceId, svcData, servicesData.getServiceStatus());
242 configServicesRepository.deleteBySvcInstanceId(svcInstanceId);
243 configServicesRepository.save(configService);
244 } catch (JsonProcessingException e) {
245 log.error("Cannot convert service data", e);
247 return new ResponseEntity<>(HttpStatus.OK);
251 public ResponseEntity<Void> configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdDelete(String serviceInstanceId) {
252 configServicesRepository.deleteBySvcInstanceId(serviceInstanceId);
253 return new ResponseEntity<>(HttpStatus.OK);
257 public ResponseEntity<Void> configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceDataDelete(String serviceInstanceId) {
262 public ResponseEntity<GenericResourceApiServicedataServiceData> configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceDataGet(String serviceInstanceId) {
267 public ResponseEntity<Void> configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceDataPost(String serviceInstanceId, @Valid GenericResourceApiServicedataServiceData genericResourceApiServicedataServiceDataBodyParam) {
272 public ResponseEntity<Void> configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceDataPut(String serviceInstanceId, @Valid GenericResourceApiServicedataServiceData genericResourceApiServicedataServiceDataBodyParam) {
277 public ResponseEntity<Void> configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceStatusDelete(String serviceInstanceId) {
282 public ResponseEntity<GenericResourceApiServicestatusServiceStatus> configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceStatusGet(String serviceInstanceId) {
287 public ResponseEntity<Void> configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceStatusPost(String serviceInstanceId, @Valid GenericResourceApiServicestatusServiceStatus genericResourceApiServicestatusServiceStatusBodyParam) {
292 public ResponseEntity<Void> configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceStatusPut(String serviceInstanceId, @Valid GenericResourceApiServicestatusServiceStatus genericResourceApiServicestatusServiceStatusBodyParam) {
297 public ResponseEntity<GenericResourceApiServicemodelinfrastructureService> configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGet(String serviceInstanceId) {
302 public ResponseEntity<Void> configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdPost(String serviceInstanceId, @Valid GenericResourceApiServicemodelinfrastructureService genericResourceApiServicemodelinfrastructureServiceBodyParam) {
307 public ResponseEntity<Void> configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdPut(String serviceInstanceId, @Valid GenericResourceApiServicemodelinfrastructureService genericResourceApiServicemodelinfrastructureServiceBodyParam) {
312 public ResponseEntity<GenericResourceApiServiceModelInfrastructure> configGENERICRESOURCEAPIservicesGet() {
317 public ResponseEntity<Void> configGENERICRESOURCEAPIservicesPost(@Valid GenericResourceApiServiceModelInfrastructure genericResourceApiServiceModelInfrastructureBodyParam) {
322 public ResponseEntity<Void> configGENERICRESOURCEAPIservicesPut(@Valid GenericResourceApiServiceModelInfrastructure genericResourceApiServiceModelInfrastructureBodyParam) {