Merge "Create CSIT test for VNF delete"
[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.so.sdncsimulator.utils.Constants.OPERATIONS_URL;
25 import javax.servlet.http.HttpServletRequest;
26 import javax.ws.rs.core.MediaType;
27 import org.onap.sdnc.northbound.client.model.GenericResourceApiRequestActionEnumeration;
28 import org.onap.sdnc.northbound.client.model.GenericResourceApiRequestinformationRequestInformation;
29 import org.onap.sdnc.northbound.client.model.GenericResourceApiServiceOperationInformation;
30 import org.onap.sdnc.northbound.client.model.GenericResourceApiVnfOperationInformation;
31 import org.onap.so.sdncsimulator.models.InputRequest;
32 import org.onap.so.sdncsimulator.models.Output;
33 import org.onap.so.sdncsimulator.models.OutputRequest;
34 import org.onap.so.sdncsimulator.providers.ServiceOperationsCacheServiceProvider;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.http.HttpStatus;
39 import org.springframework.http.ResponseEntity;
40 import org.springframework.stereotype.Controller;
41 import org.springframework.web.bind.annotation.PostMapping;
42 import org.springframework.web.bind.annotation.RequestBody;
43 import org.springframework.web.bind.annotation.RequestMapping;
44
45 /**
46  * @author Waqas Ikram (waqas.ikram@est.tech)
47  *
48  */
49 @Controller
50 @RequestMapping(path = OPERATIONS_URL)
51 public class OperationsController {
52
53     private static final Logger LOGGER = LoggerFactory.getLogger(OperationsController.class);
54
55     private final ServiceOperationsCacheServiceProvider cacheServiceProvider;
56
57     @Autowired
58     public OperationsController(final ServiceOperationsCacheServiceProvider cacheServiceProvider) {
59         this.cacheServiceProvider = cacheServiceProvider;
60     }
61
62     @PostMapping(value = "/GENERIC-RESOURCE-API:service-topology-operation/",
63             consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
64             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
65     public ResponseEntity<?> postServiceOperationInformation(
66             @RequestBody final InputRequest<GenericResourceApiServiceOperationInformation> inputRequest,
67             final HttpServletRequest request) {
68         LOGGER.info("Request Received: {}  ...", inputRequest);
69
70         final GenericResourceApiServiceOperationInformation apiServiceOperationInformation = inputRequest.getInput();
71
72         if (apiServiceOperationInformation == null) {
73             LOGGER.error("Invalid input request: {}", inputRequest);
74             return ResponseEntity.badRequest().build();
75         }
76
77         final Output output = getOutput(apiServiceOperationInformation);
78         final OutputRequest outputRequest = new OutputRequest(output);
79
80         if (output.getResponseCode().equals(HttpStatus.OK.toString())) {
81             LOGGER.info("Sucessfully executed service request sending response: {}", outputRequest);
82             return ResponseEntity.ok(outputRequest);
83         }
84         LOGGER.error("Unable to execute input request: {}, will send OutputRequest: {}", inputRequest, outputRequest);
85         return ResponseEntity.badRequest().body(outputRequest);
86
87     }
88
89     @PostMapping(value = "/GENERIC-RESOURCE-API:vnf-topology-operation/",
90             consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
91             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
92     public ResponseEntity<?> postVnfOperationInformation(
93             @RequestBody final InputRequest<GenericResourceApiVnfOperationInformation> inputRequest,
94             final HttpServletRequest request) {
95         LOGGER.info("Request Received: {}  ...", inputRequest);
96
97         final GenericResourceApiVnfOperationInformation apiVnfOperationInformation = inputRequest.getInput();
98         if (apiVnfOperationInformation == null) {
99             LOGGER.error("Invalid input request: {}", inputRequest);
100             return ResponseEntity.badRequest().build();
101         }
102
103         final Output output = getOutput(apiVnfOperationInformation);
104         final OutputRequest outputRequest = new OutputRequest(output);
105
106         if (output.getResponseCode().equals(HttpStatus.OK.toString())) {
107             LOGGER.info("Sucessfully executed request vnf sending response: {}", outputRequest);
108             return ResponseEntity.ok(outputRequest);
109         }
110
111         LOGGER.error("Unable to execute input request: {}, will send OutputRequest: {}", inputRequest, outputRequest);
112         return ResponseEntity.badRequest().body(outputRequest);
113
114     }
115
116     private Output getOutput(final GenericResourceApiServiceOperationInformation serviceOperationInformation) {
117         final GenericResourceApiRequestinformationRequestInformation requestInformation =
118                 serviceOperationInformation.getRequestInformation();
119         if (requestInformation != null) {
120             final GenericResourceApiRequestActionEnumeration requestAction = requestInformation.getRequestAction();
121             if (DELETESERVICEINSTANCE.equals(requestAction)) {
122                 LOGGER.info("RequestAction: {} will delete service instance from cache ...", requestAction);
123                 return cacheServiceProvider.deleteServiceOperationInformation(serviceOperationInformation);
124             }
125         }
126         return cacheServiceProvider.putServiceOperationInformation(serviceOperationInformation);
127     }
128
129     private Output getOutput(final GenericResourceApiVnfOperationInformation apiVnfOperationInformation) {
130         final GenericResourceApiRequestinformationRequestInformation requestInformation =
131                 apiVnfOperationInformation.getRequestInformation();
132         if (requestInformation != null) {
133             final GenericResourceApiRequestActionEnumeration requestAction = requestInformation.getRequestAction();
134             if (DELETEVNFINSTANCE.equals(requestAction)) {
135                 LOGGER.info("RequestAction: {} will delete vnf instance from cache ...", requestAction);
136                 return cacheServiceProvider.deleteVnfOperationInformation(apiVnfOperationInformation);
137             }
138         }
139         return cacheServiceProvider.putVnfOperationInformation(apiVnfOperationInformation);
140     }
141
142 }