2f24ef694ee2d6159c0ea1f1b532d6f9da1909a8
[integration/csit.git] / plans / so / integration-etsi-testing / so-simulators / sdnc-simulator / src / main / java / org / onap / so / sdncsimulator / controller / OperationsController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.so.sdncsimulator.controller;
21
22 import static org.onap.sdnc.northbound.client.model.GenericResourceApiRequestActionEnumeration.DELETESERVICEINSTANCE;
23 import static org.onap.sdnc.northbound.client.model.GenericResourceApiRequestActionEnumeration.DELETEVNFINSTANCE;
24 import static org.onap.sdnc.northbound.client.model.GenericResourceApiSvcActionEnumeration.DELETE;
25 import static org.onap.so.sdncsimulator.utils.Constants.OPERATIONS_URL;
26 import javax.servlet.http.HttpServletRequest;
27 import javax.ws.rs.core.MediaType;
28 import org.onap.sdnc.northbound.client.model.GenericResourceApiRequestActionEnumeration;
29 import org.onap.sdnc.northbound.client.model.GenericResourceApiRequestinformationRequestInformation;
30 import org.onap.sdnc.northbound.client.model.GenericResourceApiSdncrequestheaderSdncRequestHeader;
31 import org.onap.sdnc.northbound.client.model.GenericResourceApiServiceOperationInformation;
32 import org.onap.sdnc.northbound.client.model.GenericResourceApiSvcActionEnumeration;
33 import org.onap.sdnc.northbound.client.model.GenericResourceApiVnfOperationInformation;
34 import org.onap.so.sdncsimulator.models.InputRequest;
35 import org.onap.so.sdncsimulator.models.Output;
36 import org.onap.so.sdncsimulator.models.OutputRequest;
37 import org.onap.so.sdncsimulator.providers.ServiceOperationsCacheServiceProvider;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.http.HttpStatus;
42 import org.springframework.http.ResponseEntity;
43 import org.springframework.stereotype.Controller;
44 import org.springframework.web.bind.annotation.PostMapping;
45 import org.springframework.web.bind.annotation.RequestBody;
46 import org.springframework.web.bind.annotation.RequestMapping;
47
48 /**
49  * @author Waqas Ikram (waqas.ikram@est.tech)
50  *
51  */
52 @Controller
53 @RequestMapping(path = OPERATIONS_URL)
54 public class OperationsController {
55     private static final String HTTP_STATUS_OK = HttpStatus.OK.value() + "";
56
57     private static final Logger LOGGER = LoggerFactory.getLogger(OperationsController.class);
58
59     private final ServiceOperationsCacheServiceProvider cacheServiceProvider;
60
61     @Autowired
62     public OperationsController(final ServiceOperationsCacheServiceProvider cacheServiceProvider) {
63         this.cacheServiceProvider = cacheServiceProvider;
64     }
65
66     @PostMapping(value = "/GENERIC-RESOURCE-API:service-topology-operation/",
67             consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
68             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
69     public ResponseEntity<?> postServiceOperationInformation(
70             @RequestBody final InputRequest<GenericResourceApiServiceOperationInformation> inputRequest,
71             final HttpServletRequest request) {
72         LOGGER.info("Request Received: {}  ...", inputRequest);
73
74         final GenericResourceApiServiceOperationInformation apiServiceOperationInformation = inputRequest.getInput();
75
76         if (apiServiceOperationInformation == null) {
77             LOGGER.error("Invalid input request: {}", inputRequest);
78             return ResponseEntity.badRequest().build();
79         }
80
81         final Output output = getOutput(apiServiceOperationInformation);
82         final OutputRequest outputRequest = new OutputRequest(output);
83
84         if (output.getResponseCode().equals(HTTP_STATUS_OK)) {
85             LOGGER.info("Sucessfully executed service request sending response: {}", outputRequest);
86             return ResponseEntity.ok(outputRequest);
87         }
88         LOGGER.error("Unable to execute input request: {}, will send OutputRequest: {}", inputRequest, outputRequest);
89         return ResponseEntity.badRequest().body(outputRequest);
90
91     }
92
93     @PostMapping(value = "/GENERIC-RESOURCE-API:vnf-topology-operation/",
94             consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
95             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
96     public ResponseEntity<?> postVnfOperationInformation(
97             @RequestBody final InputRequest<GenericResourceApiVnfOperationInformation> inputRequest,
98             final HttpServletRequest request) {
99         LOGGER.info("Request Received: {}  ...", inputRequest);
100
101         final GenericResourceApiVnfOperationInformation apiVnfOperationInformation = inputRequest.getInput();
102         if (apiVnfOperationInformation == null) {
103             LOGGER.error("Invalid input request: {}", inputRequest);
104             return ResponseEntity.badRequest().build();
105         }
106
107         final Output output = getOutput(apiVnfOperationInformation);
108         final OutputRequest outputRequest = new OutputRequest(output);
109
110         if (output.getResponseCode().equals(HTTP_STATUS_OK)) {
111             LOGGER.info("Sucessfully executed request vnf sending response: {}", outputRequest);
112             return ResponseEntity.ok(outputRequest);
113         }
114
115         LOGGER.error("Unable to execute input request: {}, will send OutputRequest: {}", inputRequest, outputRequest);
116         return ResponseEntity.badRequest().body(outputRequest);
117
118     }
119
120     private Output getOutput(final GenericResourceApiServiceOperationInformation serviceOperationInformation) {
121         final GenericResourceApiRequestinformationRequestInformation requestInformation =
122                 serviceOperationInformation.getRequestInformation();
123         final GenericResourceApiSdncrequestheaderSdncRequestHeader sdncRequestHeader =
124                 serviceOperationInformation.getSdncRequestHeader();
125         if (requestInformation != null && sdncRequestHeader != null) {
126             final GenericResourceApiRequestActionEnumeration requestAction = requestInformation.getRequestAction();
127             final GenericResourceApiSvcActionEnumeration svcAction = sdncRequestHeader.getSvcAction();
128             if (DELETESERVICEINSTANCE.equals(requestAction) && DELETE.equals(svcAction)) {
129                 LOGGER.info("RequestAction: {} and SvcAction: {} will delete service instance from cache ...",
130                         requestAction, svcAction);
131                 return cacheServiceProvider.deleteServiceOperationInformation(serviceOperationInformation);
132             }
133         }
134         return cacheServiceProvider.putServiceOperationInformation(serviceOperationInformation);
135     }
136
137     private Output getOutput(final GenericResourceApiVnfOperationInformation apiVnfOperationInformation) {
138         final GenericResourceApiRequestinformationRequestInformation requestInformation =
139                 apiVnfOperationInformation.getRequestInformation();
140         if (requestInformation != null) {
141             final GenericResourceApiRequestActionEnumeration requestAction = requestInformation.getRequestAction();
142             if (DELETEVNFINSTANCE.equals(requestAction)) {
143                 LOGGER.info("RequestAction: {} will delete vnf instance from cache ...", requestAction);
144                 return cacheServiceProvider.deleteVnfOperationInformation(apiVnfOperationInformation);
145             }
146         }
147         return cacheServiceProvider.putVnfOperationInformation(apiVnfOperationInformation);
148     }
149
150 }