Added CSIT for Macroflow with HEAT
[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.sdnc.northbound.client.model.GenericResourceApiVfModuleOperationInformation;
35 import org.onap.so.sdncsimulator.models.InputRequest;
36 import org.onap.so.sdncsimulator.models.Output;
37 import org.onap.so.sdncsimulator.models.OutputRequest;
38 import org.onap.so.sdncsimulator.providers.ServiceOperationsCacheServiceProvider;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.http.HttpStatus;
43 import org.springframework.http.ResponseEntity;
44 import org.springframework.stereotype.Controller;
45 import org.springframework.web.bind.annotation.PostMapping;
46 import org.springframework.web.bind.annotation.RequestBody;
47 import org.springframework.web.bind.annotation.RequestMapping;
48
49 /**
50  * @author Waqas Ikram (waqas.ikram@est.tech)
51  *
52  */
53 @Controller
54 @RequestMapping(path = OPERATIONS_URL)
55 public class OperationsController {
56     private static final String HTTP_STATUS_OK = HttpStatus.OK.value() + "";
57
58     private static final Logger LOGGER = LoggerFactory.getLogger(OperationsController.class);
59
60     private final ServiceOperationsCacheServiceProvider cacheServiceProvider;
61
62     @Autowired
63     public OperationsController(final ServiceOperationsCacheServiceProvider cacheServiceProvider) {
64         this.cacheServiceProvider = cacheServiceProvider;
65     }
66
67     @PostMapping(value = "/GENERIC-RESOURCE-API:service-topology-operation/",
68             consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
69             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
70     public ResponseEntity<?> postServiceOperationInformation(
71             @RequestBody final InputRequest<GenericResourceApiServiceOperationInformation> inputRequest,
72             final HttpServletRequest request) {
73         LOGGER.info("Request Received: {}  ...", inputRequest);
74
75         final GenericResourceApiServiceOperationInformation apiServiceOperationInformation = inputRequest.getInput();
76
77         if (apiServiceOperationInformation == null) {
78             LOGGER.error("Invalid input request: {}", inputRequest);
79             return ResponseEntity.badRequest().build();
80         }
81
82         final Output output = getOutput(apiServiceOperationInformation);
83         final OutputRequest outputRequest = new OutputRequest(output);
84
85         if (output.getResponseCode().equals(HTTP_STATUS_OK)) {
86             LOGGER.info("Sucessfully executed service request sending response: {}", outputRequest);
87             return ResponseEntity.ok(outputRequest);
88         }
89         LOGGER.error("Unable to execute input request: {}, will send OutputRequest: {}", inputRequest, outputRequest);
90         return ResponseEntity.badRequest().body(outputRequest);
91
92     }
93
94     @PostMapping(value = "/GENERIC-RESOURCE-API:vnf-topology-operation/",
95             consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
96             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
97     public ResponseEntity<?> postVnfOperationInformation(
98             @RequestBody final InputRequest<GenericResourceApiVnfOperationInformation> inputRequest,
99             final HttpServletRequest request) {
100         LOGGER.info("Request Received: {}  ...", inputRequest);
101
102         final GenericResourceApiVnfOperationInformation apiVnfOperationInformation = inputRequest.getInput();
103         if (apiVnfOperationInformation == null) {
104             LOGGER.error("Invalid input request: {}", inputRequest);
105             return ResponseEntity.badRequest().build();
106         }
107
108         final Output output = getOutput(apiVnfOperationInformation);
109         final OutputRequest outputRequest = new OutputRequest(output);
110
111         if (output.getResponseCode().equals(HTTP_STATUS_OK)) {
112             LOGGER.info("Sucessfully executed request vnf sending response: {}", outputRequest);
113             return ResponseEntity.ok(outputRequest);
114         }
115
116         LOGGER.error("Unable to execute input request: {}, will send OutputRequest: {}", inputRequest, outputRequest);
117         return ResponseEntity.badRequest().body(outputRequest);
118
119     }
120
121     private Output getOutput(final GenericResourceApiServiceOperationInformation serviceOperationInformation) {
122         final GenericResourceApiRequestinformationRequestInformation requestInformation =
123                 serviceOperationInformation.getRequestInformation();
124         final GenericResourceApiSdncrequestheaderSdncRequestHeader sdncRequestHeader =
125                 serviceOperationInformation.getSdncRequestHeader();
126         if (requestInformation != null && sdncRequestHeader != null) {
127             final GenericResourceApiRequestActionEnumeration requestAction = requestInformation.getRequestAction();
128             final GenericResourceApiSvcActionEnumeration svcAction = sdncRequestHeader.getSvcAction();
129             if (DELETESERVICEINSTANCE.equals(requestAction) && DELETE.equals(svcAction)) {
130                 LOGGER.info("RequestAction: {} and SvcAction: {} will delete service instance from cache ...",
131                         requestAction, svcAction);
132                 return cacheServiceProvider.deleteServiceOperationInformation(serviceOperationInformation);
133             }
134         }
135         return cacheServiceProvider.putServiceOperationInformation(serviceOperationInformation);
136     }
137
138     private Output getOutput(final GenericResourceApiVnfOperationInformation apiVnfOperationInformation) {
139         final GenericResourceApiRequestinformationRequestInformation requestInformation =
140                 apiVnfOperationInformation.getRequestInformation();
141         if (requestInformation != null) {
142             final GenericResourceApiRequestActionEnumeration requestAction = requestInformation.getRequestAction();
143             if (DELETEVNFINSTANCE.equals(requestAction)) {
144                 LOGGER.info("RequestAction: {} will delete vnf instance from cache ...", requestAction);
145                 return cacheServiceProvider.deleteVnfOperationInformation(apiVnfOperationInformation);
146             }
147         }
148         return cacheServiceProvider.putVnfOperationInformation(apiVnfOperationInformation);
149     }
150
151     @PostMapping(value = "/GENERIC-RESOURCE-API:vf-module-topology-operation/",
152             consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
153             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
154     public ResponseEntity<?> postVfModuleOperationInformation(
155             @RequestBody final InputRequest<GenericResourceApiVfModuleOperationInformation> inputRequest,
156             final HttpServletRequest request) {
157         LOGGER.info("Request Received for VfModule : {}  ...", inputRequest);
158
159         final GenericResourceApiVfModuleOperationInformation apiVfModuleOperationInformation = inputRequest.getInput();
160         if (apiVfModuleOperationInformation == null) {
161             LOGGER.error("Invalid input request: {}", inputRequest);
162             return ResponseEntity.badRequest().build();
163         }
164
165         final Output output = getOutput(apiVfModuleOperationInformation);
166         final OutputRequest outputRequest = new OutputRequest(output);
167
168         if (output.getResponseCode().equals(HTTP_STATUS_OK)) {
169             LOGGER.info("Sucessfully executed request vnf sending response: {}", outputRequest);
170             return ResponseEntity.ok(outputRequest);
171         }
172
173         LOGGER.error("Unable to execute input request: {}, will send OutputRequest: {}", inputRequest, outputRequest);
174         return ResponseEntity.badRequest().body(outputRequest);
175
176     }
177
178     private Output getOutput(final GenericResourceApiVfModuleOperationInformation apiVfModuleOperationInformation) {
179
180         return cacheServiceProvider.putVfModuleOperationInformation(apiVfModuleOperationInformation);
181     }
182
183 }