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.JsonMappingException;
26 import com.fasterxml.jackson.databind.ObjectMapper;
27 import com.google.gson.JsonParser;
28 import org.onap.ccsdk.apps.services.SvcLogicFactory;
29 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
30 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
31 import org.onap.ccsdk.sli.core.sli.provider.base.SvcLogicServiceBase;
32 import org.onap.sdnc.apps.ms.gra.data.ConfigPreloadData;
33 import org.onap.sdnc.apps.ms.gra.data.ConfigPreloadDataRepository;
34 import org.onap.sdnc.apps.ms.gra.data.OperationalPreloadData;
35 import org.onap.sdnc.apps.ms.gra.data.OperationalPreloadDataRepository;
36 import org.onap.sdnc.apps.ms.gra.swagger.OperationsApi;
37 import org.onap.sdnc.apps.ms.gra.swagger.model.*;
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.context.annotation.Import;
42 import org.springframework.http.HttpStatus;
43 import org.springframework.http.ResponseEntity;
44 import org.springframework.stereotype.Controller;
46 import javax.servlet.http.HttpServletRequest;
47 import javax.validation.Valid;
49 import java.util.concurrent.atomic.AtomicBoolean;
53 @ComponentScan(basePackages = {"org.onap.sdnc.apps.ms.gra.*"})
54 @EntityScan("org.onap.sdnc.apps.ms.gra.*")
55 @Import(value = SvcLogicFactory.class)
56 public class OperationsApiController implements OperationsApi {
58 private static final String CALLED_STR = "{} called.";
59 private static final String MODULE_NAME = "GENERIC-RESOURCE-API";
61 private final ObjectMapper objectMapper;
63 private final HttpServletRequest request;
66 protected SvcLogicServiceBase svc;
69 private ConfigPreloadDataRepository configPreloadDataRepository;
72 private OperationalPreloadDataRepository operationalPreloadDataRepository;
75 @org.springframework.beans.factory.annotation.Autowired
76 public OperationsApiController(ObjectMapper objectMapper, HttpServletRequest request) {
77 objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
78 objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
79 this.objectMapper = objectMapper;
80 this.request = request;
84 public Optional<ObjectMapper> getObjectMapper() {
85 return Optional.ofNullable(objectMapper);
89 public Optional<HttpServletRequest> getRequest() {
90 return Optional.ofNullable(request);
94 public ResponseEntity<GenericResourceApiPreloadNetworkTopologyOperation> operationsGENERICRESOURCEAPIpreloadNetworkTopologyOperationPost(@Valid GenericResourceApiPreloadnetworktopologyoperationInputBodyparam graInput) {
95 final String svcOperation = "preload-network-topology-operation";
96 GenericResourceApiPreloadNetworkTopologyOperation retval = new GenericResourceApiPreloadNetworkTopologyOperation();
97 GenericResourceApiPreloadTopologyResponseBody resp = new GenericResourceApiPreloadTopologyResponseBody();
99 log.info(CALLED_STR, svcOperation);
100 if (hasInvalidPreloadNetwork(graInput)) {
101 log.debug("exiting {} because of null or empty preload-network-topology-information", svcOperation);
104 resp.setResponseCode("403");
105 resp.setResponseMessage("invalid input, null or empty preload-network-topology-information");
106 resp.setAckFinalIndicator("Y");
109 retval.setOutput(resp);
111 return new ResponseEntity<>(retval, HttpStatus.FORBIDDEN);
114 String preloadId = graInput.getInput().getPreloadNetworkTopologyInformation().getNetworkTopologyIdentifierStructure().getNetworkId();
115 String preloadType = "network";
117 resp.setSvcRequestId(graInput.getInput().getSdncRequestHeader().getSvcRequestId());
119 SvcLogicContext ctxIn = new SvcLogicContext();
121 GenericResourceApiPreloaddataPreloadData preloadData = null;
124 // Add input to SvcLogicContext
126 ctxIn.mergeJson(svcOperation+"-input", objectMapper.writeValueAsString(graInput.getInput()));
127 } catch (JsonProcessingException e) {
128 log.error("exiting {} due to parse error on input preload data", svcOperation);
129 resp.setResponseCode("500");
130 resp.setResponseMessage("internal error");
131 resp.setAckFinalIndicator("Y");
132 retval.setOutput(resp);
133 return new ResponseEntity<>(retval, HttpStatus.INTERNAL_SERVER_ERROR);
137 // Add config tree data to SvcLogicContext
139 preloadData = getConfigPreloadData(preloadId, preloadType);
140 ctxIn.mergeJson("preload-data", objectMapper.writeValueAsString(preloadData));
141 } catch (JsonProcessingException e) {
142 log.error("exiting {} due to parse error on saved config preload data", svcOperation);
143 resp.setResponseCode("500");
144 resp.setResponseMessage("internal error");
145 resp.setAckFinalIndicator("Y");
146 retval.setOutput(resp);
147 return new ResponseEntity<>(retval, HttpStatus.INTERNAL_SERVER_ERROR);
151 // Add operational tree data to SvcLogicContext
153 preloadData = getOperationalPreloadData(preloadId, preloadType);
154 ctxIn.mergeJson("operational-data", objectMapper.writeValueAsString(preloadData));
155 } catch (JsonProcessingException e) {
156 log.error("exiting {} due to parse error on saved operational preload data", svcOperation);
157 resp.setResponseCode("500");
158 resp.setResponseMessage("internal error");
159 resp.setAckFinalIndicator("Y");
160 retval.setOutput(resp);
161 return new ResponseEntity<>(retval, HttpStatus.INTERNAL_SERVER_ERROR);
167 // Any of these can throw a nullpointer exception
168 // execute should only throw a SvcLogicException
169 SvcLogicContext ctxOut = svc.execute(MODULE_NAME, svcOperation, null, "sync", ctxIn);
170 Properties respProps = ctxOut.toProperties();
172 resp.setAckFinalIndicator(respProps.getProperty("ack-final-indicator", "Y"));
173 resp.setResponseCode(respProps.getProperty("error-code", "200"));
174 resp.setResponseMessage(respProps.getProperty("error-message", "SUCCESS"));
176 if ("200".equals(resp.getResponseCode())) {
177 // If DG returns success, update database
178 String ctxJson = ctxOut.toJsonString("preload-data");
179 log.info("DG preload-data is {}", ctxJson);
180 GenericResourceApiPreloaddataPreloadData preloadToLoad = objectMapper.readValue(ctxJson, GenericResourceApiPreloaddataPreloadData.class);
181 saveConfigPreloadData(preloadId, preloadType, preloadToLoad);
182 saveOperationalPreloadData(preloadId, preloadType, preloadToLoad);
185 } catch (NullPointerException npe) {
186 log.error("Caught NPE", npe);
187 resp.setAckFinalIndicator("true");
188 resp.setResponseCode("500");
189 resp.setResponseMessage("Check that you populated module, rpc and or mode correctly.");
190 } catch (SvcLogicException e) {
191 log.error("Caught SvcLogicException", e);
192 resp.setAckFinalIndicator("true");
193 resp.setResponseCode("500");
194 resp.setResponseMessage(e.getMessage());
195 } catch (JsonMappingException e) {
196 log.error("Caught JsonMappingException", e);
197 resp.setAckFinalIndicator("true");
198 resp.setResponseCode("500");
199 resp.setResponseMessage(e.getMessage());
200 } catch (JsonProcessingException e) {
201 log.error("Caught JsonProcessingException", e);
202 resp.setAckFinalIndicator("true");
203 resp.setResponseCode("500");
204 resp.setResponseMessage(e.getMessage());
208 retval.setOutput(resp);
209 return (new ResponseEntity<>(retval, HttpStatus.valueOf(Integer.parseInt(resp.getResponseCode()))));
213 public ResponseEntity<GenericResourceApiPreloadVfModuleTopologyOperation> operationsGENERICRESOURCEAPIpreloadVfModuleTopologyOperationPost(@Valid GenericResourceApiPreloadvfmoduletopologyoperationInputBodyparam graInput) {
214 final String svcOperation = "preload-vf-module-topology-operation";
215 GenericResourceApiPreloadVfModuleTopologyOperation retval = new GenericResourceApiPreloadVfModuleTopologyOperation();
216 GenericResourceApiPreloadTopologyResponseBody resp = new GenericResourceApiPreloadTopologyResponseBody();
218 log.info(CALLED_STR, svcOperation);
219 if (hasInvalidPreloadNetwork(graInput)) {
220 log.debug("exiting {} because of null or empty preload-network-topology-information", svcOperation);
223 resp.setResponseCode("403");
224 resp.setResponseMessage("invalid input, null or empty preload-network-topology-information");
225 resp.setAckFinalIndicator("Y");
228 retval.setOutput(resp);
230 return new ResponseEntity<>(retval, HttpStatus.FORBIDDEN);
233 String preloadId = graInput.getInput().getPreloadVfModuleTopologyInformation().getVfModuleTopology().getVfModuleTopologyIdentifier().getVfModuleName();
234 String preloadType = "vf-module";
236 resp.setSvcRequestId(graInput.getInput().getSdncRequestHeader().getSvcRequestId());
238 SvcLogicContext ctxIn = new SvcLogicContext();
240 GenericResourceApiPreloaddataPreloadData preloadData = null;
243 // Add input to SvcLogicContext
245 ctxIn.mergeJson(svcOperation+"-input", objectMapper.writeValueAsString(graInput.getInput()));
246 } catch (JsonProcessingException e) {
247 log.error("exiting {} due to parse error on input preload data", svcOperation);
248 resp.setResponseCode("500");
249 resp.setResponseMessage("internal error");
250 resp.setAckFinalIndicator("Y");
251 retval.setOutput(resp);
252 return new ResponseEntity<>(retval, HttpStatus.INTERNAL_SERVER_ERROR);
256 // Add config tree data to SvcLogicContext
258 preloadData = getConfigPreloadData(preloadId, preloadType);
259 ctxIn.mergeJson("preload-data", objectMapper.writeValueAsString(preloadData));
260 } catch (JsonProcessingException e) {
261 log.error("exiting {} due to parse error on saved config preload data", svcOperation);
262 resp.setResponseCode("500");
263 resp.setResponseMessage("internal error");
264 resp.setAckFinalIndicator("Y");
265 retval.setOutput(resp);
266 return new ResponseEntity<>(retval, HttpStatus.INTERNAL_SERVER_ERROR);
270 // Add operational tree data to SvcLogicContext
272 preloadData = getOperationalPreloadData(preloadId, preloadType);
273 ctxIn.mergeJson("operational-data", objectMapper.writeValueAsString(preloadData));
274 } catch (JsonProcessingException e) {
275 log.error("exiting {} due to parse error on saved operational preload data", svcOperation);
276 resp.setResponseCode("500");
277 resp.setResponseMessage("internal error");
278 resp.setAckFinalIndicator("Y");
279 retval.setOutput(resp);
280 return new ResponseEntity<>(retval, HttpStatus.INTERNAL_SERVER_ERROR);
286 // Any of these can throw a nullpointer exception
287 // execute should only throw a SvcLogicException
288 SvcLogicContext ctxOut = svc.execute(MODULE_NAME, svcOperation, null, "sync", ctxIn);
289 Properties respProps = ctxOut.toProperties();
291 resp.setAckFinalIndicator(respProps.getProperty("ack-final-indicator", "Y"));
292 resp.setResponseCode(respProps.getProperty("error-code", "200"));
293 resp.setResponseMessage(respProps.getProperty("error-message", "SUCCESS"));
295 if ("200".equals(resp.getResponseCode())) {
296 // If DG returns success, update database
297 String ctxJson = ctxOut.toJsonString("preload-data");
298 GenericResourceApiPreloaddataPreloadData preloadToLoad = objectMapper.readValue(ctxJson, GenericResourceApiPreloaddataPreloadData.class);
299 saveConfigPreloadData(preloadId, preloadType, preloadToLoad);
300 saveOperationalPreloadData(preloadId, preloadType, preloadToLoad);
303 } catch (NullPointerException npe) {
304 resp.setAckFinalIndicator("true");
305 resp.setResponseCode("500");
306 resp.setResponseMessage("Check that you populated module, rpc and or mode correctly.");
307 } catch (SvcLogicException e) {
308 resp.setAckFinalIndicator("true");
309 resp.setResponseCode("500");
310 resp.setResponseMessage(e.getMessage());
311 } catch (JsonMappingException e) {
312 resp.setAckFinalIndicator("true");
313 resp.setResponseCode("500");
314 resp.setResponseMessage(e.getMessage());
315 } catch (JsonProcessingException e) {
316 resp.setAckFinalIndicator("true");
317 resp.setResponseCode("500");
318 resp.setResponseMessage(e.getMessage());
322 retval.setOutput(resp);
323 return (new ResponseEntity<>(retval, HttpStatus.valueOf(Integer.parseInt(resp.getResponseCode()))));
326 private boolean hasInvalidPreloadNetwork(GenericResourceApiPreloadnetworktopologyoperationInputBodyparam preloadData) {
327 return ((preloadData == null) ||
328 (preloadData.getInput() == null) ||
329 (preloadData.getInput().getPreloadNetworkTopologyInformation() == null));
331 private boolean hasInvalidPreloadNetwork(GenericResourceApiPreloadvfmoduletopologyoperationInputBodyparam preloadData) {
332 return ((preloadData == null) ||
333 (preloadData.getInput() == null) ||
334 (preloadData.getInput().getPreloadVfModuleTopologyInformation() == null));
337 private GenericResourceApiPreloaddataPreloadData getConfigPreloadData(String preloadId, String preloadType) throws JsonProcessingException {
339 List<ConfigPreloadData> configPreloadData = configPreloadDataRepository.findByPreloadIdAndPreloadType(preloadId, preloadType);
341 if (configPreloadData.isEmpty()) {
344 return(objectMapper.readValue(configPreloadData.get(0).getPreloadData(), GenericResourceApiPreloaddataPreloadData.class));
348 private GenericResourceApiPreloaddataPreloadData getOperationalPreloadData(String preloadId, String preloadType) throws JsonProcessingException {
350 List<OperationalPreloadData> configPreloadData = operationalPreloadDataRepository.findByPreloadIdAndPreloadType(preloadId, preloadType);
352 if (configPreloadData.isEmpty()) {
355 return(objectMapper.readValue(configPreloadData.get(0).getPreloadData(), GenericResourceApiPreloaddataPreloadData.class));
359 private void saveConfigPreloadData(String preloadId, String preloadType, GenericResourceApiPreloaddataPreloadData preloadData) throws JsonProcessingException {
361 configPreloadDataRepository.deleteByPreloadIdAndPreloadType(preloadId, preloadType);
362 configPreloadDataRepository.save(new ConfigPreloadData(preloadId, preloadType, objectMapper.writeValueAsString(preloadData)));
366 private void saveOperationalPreloadData(String preloadId, String preloadType, GenericResourceApiPreloaddataPreloadData preloadData) throws JsonProcessingException {
368 operationalPreloadDataRepository.deleteByPreloadIdAndPreloadType(preloadId, preloadType);
369 operationalPreloadDataRepository.save(new OperationalPreloadData(preloadId, preloadType, objectMapper.writeValueAsString(preloadData)));