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.core.JsonProcessingException;
24 import com.fasterxml.jackson.databind.ObjectMapper;
25 import org.onap.sdnc.apps.ms.gra.data.ConfigPreloadData;
26 import org.onap.sdnc.apps.ms.gra.data.ConfigPreloadDataRepository;
27 import org.onap.sdnc.apps.ms.gra.data.ConfigServices;
28 import org.onap.sdnc.apps.ms.gra.data.ConfigServicesRepository;
29 import org.onap.sdnc.apps.ms.gra.swagger.ConfigApi;
30 import org.onap.sdnc.apps.ms.gra.swagger.model.*;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.boot.autoconfigure.domain.EntityScan;
35 import org.springframework.context.annotation.ComponentScan;
36 import org.springframework.http.HttpStatus;
37 import org.springframework.http.ResponseEntity;
38 import org.springframework.stereotype.Controller;
40 import javax.servlet.http.HttpServletRequest;
41 import javax.validation.Valid;
42 import java.util.Iterator;
43 import java.util.List;
44 import java.util.Optional;
47 @ComponentScan(basePackages = {"org.onap.sdnc.apps.ms.gra.*"})
48 @EntityScan("org.onap.sdnc.apps.ms.gra.springboot.*")
49 public class ConfigApiController implements ConfigApi {
50 private static final Logger log = LoggerFactory.getLogger(ConfigApiController.class);
52 private final ObjectMapper objectMapper;
54 private final HttpServletRequest request;
57 private ConfigPreloadDataRepository configPreloadDataRepository;
60 private ConfigServicesRepository configServicesRepository;
63 public ConfigApiController(ObjectMapper objectMapper, HttpServletRequest request) {
64 this.objectMapper = objectMapper;
65 this.request = request;
69 public Optional<ObjectMapper> getObjectMapper() {
70 return Optional.ofNullable(objectMapper);
74 public Optional<HttpServletRequest> getRequest() {
75 return Optional.ofNullable(request);
79 public ResponseEntity<Void> configGENERICRESOURCEAPIpreloadInformationDelete() {
80 configPreloadDataRepository.deleteAll();
81 return (new ResponseEntity<>(HttpStatus.OK));
85 public ResponseEntity<GenericResourceApiPreloadModelInformation> configGENERICRESOURCEAPIpreloadInformationGet() {
86 GenericResourceApiPreloadModelInformation genericResourceApiPreloadModelInformation = new GenericResourceApiPreloadModelInformation();
88 configPreloadDataRepository.findAll().forEach(configPreloadData -> {
89 GenericResourceApiPreloadmodelinformationPreloadList preloadListItem = new GenericResourceApiPreloadmodelinformationPreloadList();
91 preloadListItem.setPreloadId(configPreloadData.getPreloadId());
92 preloadListItem.setPreloadType(configPreloadData.getPreloadType());
94 preloadListItem.setPreloadData(objectMapper.readValue(configPreloadData.getPreloadData(), GenericResourceApiPreloaddataPreloadData.class));
95 } catch (JsonProcessingException e) {
96 log.error("Could not convert preload data", e);
98 genericResourceApiPreloadModelInformation.addPreloadListItem(preloadListItem);
102 return new ResponseEntity<>(genericResourceApiPreloadModelInformation, HttpStatus.OK);
106 public ResponseEntity<Void> configGENERICRESOURCEAPIpreloadInformationPost(@Valid GenericResourceApiPreloadModelInformation graPreloadModelInfo) {
108 List<GenericResourceApiPreloadmodelinformationPreloadList> preloadList = graPreloadModelInfo.getPreloadList();
110 if (preloadList != null) {
111 Iterator<GenericResourceApiPreloadmodelinformationPreloadList> iter = preloadList.iterator();
112 while (iter.hasNext()) {
113 GenericResourceApiPreloadmodelinformationPreloadList curItem = iter.next();
115 // Remove any entries already existing for this preloadId/preloadType
116 configPreloadDataRepository.deleteByPreloadIdAndPreloadType(curItem.getPreloadId(), curItem.getPreloadType());
119 configPreloadDataRepository.save(new ConfigPreloadData(curItem.getPreloadId(), curItem.getPreloadType(), objectMapper.writeValueAsString(curItem.getPreloadData())));
120 } catch (JsonProcessingException e) {
121 log.error("Cannot convert preload data", e);
126 return new ResponseEntity<>(HttpStatus.OK);
130 public ResponseEntity<Void> configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPost(@Valid GenericResourceApiPreloadmodelinformationPreloadList preloadListItem) {
132 // Remove any entries already existing for this preloadId/preloadType
133 configPreloadDataRepository.deleteByPreloadIdAndPreloadType(preloadListItem.getPreloadId(), preloadListItem.getPreloadType());
136 configPreloadDataRepository.save(new ConfigPreloadData(preloadListItem.getPreloadId(), preloadListItem.getPreloadType(), objectMapper.writeValueAsString(preloadListItem.getPreloadData())));
137 } catch (JsonProcessingException e) {
138 log.error("Cannot convert preload data", e);
140 return new ResponseEntity<>(HttpStatus.OK);
144 public ResponseEntity<Void> configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypeDelete(String preloadId, String preloadType) {
145 configPreloadDataRepository.deleteByPreloadIdAndPreloadType(preloadId, preloadType);
146 return new ResponseEntity<>(HttpStatus.OK);
150 public ResponseEntity<GenericResourceApiPreloadmodelinformationPreloadList> configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypeGet(String preloadId, String preloadType) {
151 List<ConfigPreloadData> preloadData = configPreloadDataRepository.findByPreloadIdAndPreloadType(preloadId, preloadType);
152 if (preloadData != null) {
153 if (!preloadData.isEmpty()) {
154 ConfigPreloadData preloadDataItem = preloadData.get(0);
155 GenericResourceApiPreloadmodelinformationPreloadList preloadDataList = new GenericResourceApiPreloadmodelinformationPreloadList();
156 preloadDataList.setPreloadId(preloadDataItem.getPreloadId());
157 preloadDataList.setPreloadType(preloadDataItem.getPreloadType());
159 preloadDataList.setPreloadData(objectMapper.readValue(preloadDataItem.getPreloadData(), GenericResourceApiPreloaddataPreloadData.class));
160 } catch (JsonProcessingException e) {
161 log.error("Cannot convert preload data", e);
163 return new ResponseEntity<>(preloadDataList, HttpStatus.OK);
166 return new ResponseEntity<>(HttpStatus.NOT_FOUND);
170 public ResponseEntity<Void> configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypePost(String preloadId, String preloadType, @Valid GenericResourceApiPreloadmodelinformationPreloadList preloadListItem) {
171 configPreloadDataRepository.deleteByPreloadIdAndPreloadType(preloadId, preloadType);
173 configPreloadDataRepository.save(new ConfigPreloadData(preloadId, preloadType, objectMapper.writeValueAsString(preloadListItem.getPreloadData())));
174 } catch (JsonProcessingException e) {
175 log.error("Cannot convert preload data", e);
177 return new ResponseEntity<>(HttpStatus.OK);
182 public ResponseEntity<Void> configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypeGENERICRESOURCEAPIpreloadDataDelete(String preloadId, String preloadType) {
183 List<ConfigPreloadData> preloadData = configPreloadDataRepository.findByPreloadIdAndPreloadType(preloadId, preloadType);
185 if (preloadData != null) {
186 Iterator<ConfigPreloadData> iter = preloadData.iterator();
188 while (iter.hasNext()) {
189 configPreloadDataRepository.delete(iter.next());
192 return new ResponseEntity<>(HttpStatus.OK);
196 public ResponseEntity<Void> configGENERICRESOURCEAPIpreloadInformationPut(@Valid GenericResourceApiPreloadModelInformation genericResourceApiPreloadModelInformationBodyParam) {
201 public ResponseEntity<GenericResourceApiPreloaddataPreloadData> configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypeGENERICRESOURCEAPIpreloadDataGet(String preloadId, String preloadType) {
202 List<ConfigPreloadData> preloadData = configPreloadDataRepository.findByPreloadIdAndPreloadType(preloadId, preloadType);
203 if (preloadData != null) {
204 if (!preloadData.isEmpty()) {
205 ConfigPreloadData preloadDataItem = preloadData.get(0);
207 return new ResponseEntity<>(objectMapper.readValue(preloadDataItem.getPreloadData(), GenericResourceApiPreloaddataPreloadData.class), HttpStatus.OK);
208 } catch (JsonProcessingException e) {
209 log.error("Cannot convert preload data", e);
213 return new ResponseEntity<>(HttpStatus.NOT_FOUND);
217 public ResponseEntity<Void> configGENERICRESOURCEAPIpreloadInformationGENERICRESOURCEAPIpreloadListPreloadIdPreloadTypeGENERICRESOURCEAPIpreloadDataPost(String preloadId, String preloadType, @Valid GenericResourceApiPreloaddataPreloadData preloadData) {
218 configPreloadDataRepository.deleteByPreloadIdAndPreloadType(preloadId, preloadType);
220 configPreloadDataRepository.save(new ConfigPreloadData(preloadId, preloadType, objectMapper.writeValueAsString(preloadData)));
221 } catch (JsonProcessingException e) {
222 log.error("Cannot convert preload data", e);
224 return new ResponseEntity<>(HttpStatus.OK);
228 public ResponseEntity<Void> configGENERICRESOURCEAPIservicesDelete() {
229 configServicesRepository.deleteAll();
230 return new ResponseEntity<>(HttpStatus.OK);
234 public ResponseEntity<Void> configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIservicePost(@Valid GenericResourceApiServicemodelinfrastructureService servicesData) {
235 String svcInstanceId = servicesData.getServiceInstanceId();
237 String svcData = objectMapper.writeValueAsString(servicesData.getServiceData());
238 ConfigServices configService = new ConfigServices(svcInstanceId, svcData, servicesData.getServiceStatus());
239 configServicesRepository.deleteBySvcInstanceId(svcInstanceId);
240 configServicesRepository.save(configService);
241 } catch (JsonProcessingException e) {
242 log.error("Cannot convert service data", e);
244 return new ResponseEntity<>(HttpStatus.OK);
248 public ResponseEntity<Void> configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdDelete(String serviceInstanceId) {
249 configServicesRepository.deleteBySvcInstanceId(serviceInstanceId);
250 return new ResponseEntity<>(HttpStatus.OK);
254 public ResponseEntity<Void> configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceDataDelete(String serviceInstanceId) {
259 public ResponseEntity<GenericResourceApiServicedataServiceData> configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceDataGet(String serviceInstanceId) {
264 public ResponseEntity<Void> configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceDataPost(String serviceInstanceId, @Valid GenericResourceApiServicedataServiceData genericResourceApiServicedataServiceDataBodyParam) {
269 public ResponseEntity<Void> configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceDataPut(String serviceInstanceId, @Valid GenericResourceApiServicedataServiceData genericResourceApiServicedataServiceDataBodyParam) {
274 public ResponseEntity<Void> configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceStatusDelete(String serviceInstanceId) {
279 public ResponseEntity<GenericResourceApiServicestatusServiceStatus> configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceStatusGet(String serviceInstanceId) {
284 public ResponseEntity<Void> configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceStatusPost(String serviceInstanceId, @Valid GenericResourceApiServicestatusServiceStatus genericResourceApiServicestatusServiceStatusBodyParam) {
289 public ResponseEntity<Void> configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGENERICRESOURCEAPIserviceStatusPut(String serviceInstanceId, @Valid GenericResourceApiServicestatusServiceStatus genericResourceApiServicestatusServiceStatusBodyParam) {
294 public ResponseEntity<GenericResourceApiServicemodelinfrastructureService> configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdGet(String serviceInstanceId) {
299 public ResponseEntity<Void> configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdPost(String serviceInstanceId, @Valid GenericResourceApiServicemodelinfrastructureService genericResourceApiServicemodelinfrastructureServiceBodyParam) {
304 public ResponseEntity<Void> configGENERICRESOURCEAPIservicesGENERICRESOURCEAPIserviceServiceInstanceIdPut(String serviceInstanceId, @Valid GenericResourceApiServicemodelinfrastructureService genericResourceApiServicemodelinfrastructureServiceBodyParam) {
309 public ResponseEntity<GenericResourceApiServiceModelInfrastructure> configGENERICRESOURCEAPIservicesGet() {
314 public ResponseEntity<Void> configGENERICRESOURCEAPIservicesPost(@Valid GenericResourceApiServiceModelInfrastructure genericResourceApiServiceModelInfrastructureBodyParam) {
319 public ResponseEntity<Void> configGENERICRESOURCEAPIservicesPut(@Valid GenericResourceApiServiceModelInfrastructure genericResourceApiServiceModelInfrastructureBodyParam) {