Fixing service delete in sdnc simulator
[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
56     private static final Logger LOGGER = LoggerFactory.getLogger(OperationsController.class);
57
58     private final ServiceOperationsCacheServiceProvider cacheServiceProvider;
59
60     @Autowired
61     public OperationsController(final ServiceOperationsCacheServiceProvider cacheServiceProvider) {
62         this.cacheServiceProvider = cacheServiceProvider;
63     }
64
65     @PostMapping(value = "/GENERIC-RESOURCE-API:service-topology-operation/",
66             consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
67             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
68     public ResponseEntity<?> postServiceOperationInformation(
69             @RequestBody final InputRequest<GenericResourceApiServiceOperationInformation> inputRequest,
70             final HttpServletRequest request) {
71         LOGGER.info("Request Received: {}  ...", inputRequest);
72
73         final GenericResourceApiServiceOperationInformation apiServiceOperationInformation = inputRequest.getInput();
74
75         if (apiServiceOperationInformation == null) {
76             LOGGER.error("Invalid input request: {}", inputRequest);
77             return ResponseEntity.badRequest().build();
78         }
79
80         final Output output = getOutput(apiServiceOperationInformation);
81         final OutputRequest outputRequest = new OutputRequest(output);
82
83         if (output.getResponseCode().equals(HttpStatus.OK.toString())) {
84             LOGGER.info("Sucessfully executed service request sending response: {}", outputRequest);
85             return ResponseEntity.ok(outputRequest);
86         }
87         LOGGER.error("Unable to execute input request: {}, will send OutputRequest: {}", inputRequest, outputRequest);
88         return ResponseEntity.badRequest().body(outputRequest);
89
90     }
91
92     @PostMapping(value = "/GENERIC-RESOURCE-API:vnf-topology-operation/",
93             consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
94             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
95     public ResponseEntity<?> postVnfOperationInformation(
96             @RequestBody final InputRequest<GenericResourceApiVnfOperationInformation> inputRequest,
97             final HttpServletRequest request) {
98         LOGGER.info("Request Received: {}  ...", inputRequest);
99
100         final GenericResourceApiVnfOperationInformation apiVnfOperationInformation = inputRequest.getInput();
101         if (apiVnfOperationInformation == null) {
102             LOGGER.error("Invalid input request: {}", inputRequest);
103             return ResponseEntity.badRequest().build();
104         }
105
106         final Output output = getOutput(apiVnfOperationInformation);
107         final OutputRequest outputRequest = new OutputRequest(output);
108
109         if (output.getResponseCode().equals(HttpStatus.OK.toString())) {
110             LOGGER.info("Sucessfully executed request vnf sending response: {}", outputRequest);
111             return ResponseEntity.ok(outputRequest);
112         }
113
114         LOGGER.error("Unable to execute input request: {}, will send OutputRequest: {}", inputRequest, outputRequest);
115         return ResponseEntity.badRequest().body(outputRequest);
116
117     }
118
119     private Output getOutput(final GenericResourceApiServiceOperationInformation serviceOperationInformation) {
120         final GenericResourceApiRequestinformationRequestInformation requestInformation =
121                 serviceOperationInformation.getRequestInformation();
122         final GenericResourceApiSdncrequestheaderSdncRequestHeader sdncRequestHeader =
123                 serviceOperationInformation.getSdncRequestHeader();
124         if (requestInformation != null && sdncRequestHeader != null) {
125             final GenericResourceApiRequestActionEnumeration requestAction = requestInformation.getRequestAction();
126             final GenericResourceApiSvcActionEnumeration svcAction = sdncRequestHeader.getSvcAction();
127             if (DELETESERVICEINSTANCE.equals(requestAction) && DELETE.equals(svcAction)) {
128                 LOGGER.info("RequestAction: {} and SvcAction: {} will delete service instance from cache ...",
129                         requestAction, svcAction);
130                 return cacheServiceProvider.deleteServiceOperationInformation(serviceOperationInformation);
131             }
132         }
133         return cacheServiceProvider.putServiceOperationInformation(serviceOperationInformation);
134     }
135
136     private Output getOutput(final GenericResourceApiVnfOperationInformation apiVnfOperationInformation) {
137         final GenericResourceApiRequestinformationRequestInformation requestInformation =
138                 apiVnfOperationInformation.getRequestInformation();
139         if (requestInformation != null) {
140             final GenericResourceApiRequestActionEnumeration requestAction = requestInformation.getRequestAction();
141             if (DELETEVNFINSTANCE.equals(requestAction)) {
142                 LOGGER.info("RequestAction: {} will delete vnf instance from cache ...", requestAction);
143                 return cacheServiceProvider.deleteVnfOperationInformation(apiVnfOperationInformation);
144             }
145         }
146         return cacheServiceProvider.putVnfOperationInformation(apiVnfOperationInformation);
147     }
148
149 }