6077bddde33b6863f9144702482a0fa327454961
[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 static org.onap.so.sdncsimulator.utils.Constants.BASE_URL;
27 import static org.onap.so.sdncsimulator.utils.Constants.RESTCONF_CONFIG_END_POINT;
28
29 import java.util.ArrayList;
30 import java.util.List;
31
32 import javax.servlet.http.HttpServletRequest;
33 import javax.ws.rs.core.MediaType;
34
35 import org.onap.sdnc.northbound.client.model.GenericResourceApiRequestActionEnumeration;
36 import org.onap.sdnc.northbound.client.model.GenericResourceApiRequestinformationRequestInformation;
37 import org.onap.sdnc.northbound.client.model.GenericResourceApiSdncrequestheaderSdncRequestHeader;
38 import org.onap.sdnc.northbound.client.model.GenericResourceApiServiceOperationInformation;
39 import org.onap.sdnc.northbound.client.model.GenericResourceApiSvcActionEnumeration;
40 import org.onap.sdnc.northbound.client.model.GenericResourceApiVnfOperationInformation;
41 import org.onap.sdnc.northbound.client.model.GenericResourceApiVfModuleOperationInformation;
42 import org.onap.sdnc.northbound.client.model.GenericResourceApiVnfTopology;
43 import org.onap.sdnc.northbound.client.model.GenericResourceApiVfModuleTopology;
44 import org.onap.so.sdncsimulator.models.InputRequest;
45 import org.onap.so.sdncsimulator.models.Output;
46 import org.onap.so.sdncsimulator.models.OutputRequest;
47 import org.onap.so.sdncsimulator.providers.ServiceOperationsCacheServiceProvider;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50 import org.springframework.beans.factory.annotation.Autowired;
51 import org.springframework.http.HttpStatus;
52 import org.springframework.http.ResponseEntity;
53 import org.springframework.stereotype.Controller;
54 import org.springframework.web.bind.annotation.GetMapping;
55 import org.springframework.web.bind.annotation.PathVariable;
56 import org.springframework.web.bind.annotation.PostMapping;
57 import org.springframework.web.bind.annotation.RequestBody;
58 import org.springframework.web.bind.annotation.RequestMapping;
59
60 /**
61  * @author Waqas Ikram (waqas.ikram@est.tech)
62  *
63  */
64 @Controller
65 @RequestMapping(path = BASE_URL)
66 public class OperationsController {
67     private static final String HTTP_STATUS_OK = HttpStatus.OK.value() + "";
68
69     private static final Logger LOGGER = LoggerFactory.getLogger(OperationsController.class);
70
71     private final ServiceOperationsCacheServiceProvider cacheServiceProvider;
72
73     @Autowired
74     public OperationsController(final ServiceOperationsCacheServiceProvider cacheServiceProvider) {
75         this.cacheServiceProvider = cacheServiceProvider;
76     }
77
78     @PostMapping(value = "/operations/GENERIC-RESOURCE-API:service-topology-operation/",
79             consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
80             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
81     public ResponseEntity<?> postServiceOperationInformation(
82             @RequestBody final InputRequest<GenericResourceApiServiceOperationInformation> inputRequest,
83             final HttpServletRequest request) {
84         LOGGER.info("Request Received: {}  ...", inputRequest);
85
86         final GenericResourceApiServiceOperationInformation apiServiceOperationInformation = inputRequest.getInput();
87
88         if (apiServiceOperationInformation == null) {
89             LOGGER.error("Invalid input request: {}", inputRequest);
90             return ResponseEntity.badRequest().build();
91         }
92
93         final Output output = getOutput(apiServiceOperationInformation);
94         final OutputRequest outputRequest = new OutputRequest(output);
95
96         if (output.getResponseCode().equals(HTTP_STATUS_OK)) {
97             LOGGER.info("Sucessfully executed service request sending response: {}", outputRequest);
98             return ResponseEntity.ok(outputRequest);
99         }
100         LOGGER.error("Unable to execute input request: {}, will send OutputRequest: {}", inputRequest, outputRequest);
101         return ResponseEntity.badRequest().body(outputRequest);
102
103     }
104
105     @PostMapping(value = "/operations/GENERIC-RESOURCE-API:vnf-topology-operation/",
106             consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
107             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
108     public ResponseEntity<?> postVnfOperationInformation(
109             @RequestBody final InputRequest<GenericResourceApiVnfOperationInformation> inputRequest,
110             final HttpServletRequest request) {
111         LOGGER.info("Request Received: {}  ...", inputRequest);
112
113         final GenericResourceApiVnfOperationInformation apiVnfOperationInformation = inputRequest.getInput();
114         if (apiVnfOperationInformation == null) {
115             LOGGER.error("Invalid input request: {}", inputRequest);
116             return ResponseEntity.badRequest().build();
117         }
118
119         final Output output = getOutput(apiVnfOperationInformation);
120         final OutputRequest outputRequest = new OutputRequest(output);
121
122         if (output.getResponseCode().equals(HTTP_STATUS_OK)) {
123             LOGGER.info("Sucessfully executed request vnf sending response: {}", outputRequest);
124             return ResponseEntity.ok(outputRequest);
125         }
126
127         LOGGER.error("Unable to execute input request: {}, will send OutputRequest: {}", inputRequest, outputRequest);
128         return ResponseEntity.badRequest().body(outputRequest);
129
130     }
131
132     private Output getOutput(final GenericResourceApiServiceOperationInformation serviceOperationInformation) {
133         final GenericResourceApiRequestinformationRequestInformation requestInformation =
134                 serviceOperationInformation.getRequestInformation();
135         final GenericResourceApiSdncrequestheaderSdncRequestHeader sdncRequestHeader =
136                 serviceOperationInformation.getSdncRequestHeader();
137         if (requestInformation != null && sdncRequestHeader != null) {
138             final GenericResourceApiRequestActionEnumeration requestAction = requestInformation.getRequestAction();
139             final GenericResourceApiSvcActionEnumeration svcAction = sdncRequestHeader.getSvcAction();
140             if (DELETESERVICEINSTANCE.equals(requestAction) && DELETE.equals(svcAction)) {
141                 LOGGER.info("RequestAction: {} and SvcAction: {} will delete service instance from cache ...",
142                         requestAction, svcAction);
143                 return cacheServiceProvider.deleteServiceOperationInformation(serviceOperationInformation);
144             }
145         }
146         return cacheServiceProvider.putServiceOperationInformation(serviceOperationInformation);
147     }
148
149     private Output getOutput(final GenericResourceApiVnfOperationInformation apiVnfOperationInformation) {
150         final GenericResourceApiRequestinformationRequestInformation requestInformation =
151                 apiVnfOperationInformation.getRequestInformation();
152         if (requestInformation != null) {
153             final GenericResourceApiRequestActionEnumeration requestAction = requestInformation.getRequestAction();
154             if (DELETEVNFINSTANCE.equals(requestAction)) {
155                 LOGGER.info("RequestAction: {} will delete vnf instance from cache ...", requestAction);
156                 return cacheServiceProvider.deleteVnfOperationInformation(apiVnfOperationInformation);
157             }
158         }
159         return cacheServiceProvider.putVnfOperationInformation(apiVnfOperationInformation);
160     }
161
162     @PostMapping(value = "/operations/GENERIC-RESOURCE-API:vf-module-topology-operation/",
163             consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
164             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
165     public ResponseEntity<?> postVfModuleOperationInformation(
166             @RequestBody final InputRequest<GenericResourceApiVfModuleOperationInformation> inputRequest,
167             final HttpServletRequest request) {
168         LOGGER.info("Request Received for VfModule : {}  ...", inputRequest);
169
170         final GenericResourceApiVfModuleOperationInformation apiVfModuleperationInformation = inputRequest.getInput();
171         if (apiVfModuleperationInformation == null) {
172             LOGGER.error("Invalid input request: {}", inputRequest);
173             return ResponseEntity.badRequest().build();
174         }
175
176         final Output output = getOutput(apiVfModuleperationInformation);
177         final OutputRequest outputRequest = new OutputRequest(output);
178
179         if (output.getResponseCode().equals(HTTP_STATUS_OK)) {
180             LOGGER.info("Sucessfully executed request vnf sending response: {}", outputRequest);
181             return ResponseEntity.ok(outputRequest);
182         }
183
184         LOGGER.error("Unable to execute input request: {}, will send OutputRequest: {}", inputRequest, outputRequest);
185         return ResponseEntity.badRequest().body(outputRequest);
186
187     }
188
189     private Output getOutput(final GenericResourceApiVfModuleOperationInformation apiVfModuleOperationInformation) {
190
191         return cacheServiceProvider.putVfModuleOperationInformation(apiVfModuleOperationInformation);
192     }
193
194     
195     @GetMapping(value = "/config/GENERIC-RESOURCE-API:services/service/{service-id}/service-data/vnfs/vnf/{vnf-id}/vnf-data/vnf-topology/")
196         public ResponseEntity<?> getVNf(@PathVariable("service-id") String serviceId,
197                         @PathVariable("vnf-id") String vnfId) {
198
199         LOGGER.info("Get vnf-topology with serviceId {} and vnfId {}",serviceId, vnfId);
200                 GenericResourceApiVnfTopology genericResourceApiVnfTopology = new GenericResourceApiVnfTopology();
201                 
202                 genericResourceApiVnfTopology = cacheServiceProvider.getGenericResourceApiVnfTopology();
203                 return ResponseEntity.ok(genericResourceApiVnfTopology);
204         }
205
206         @GetMapping(value = "/config/GENERIC-RESOURCE-API:services/service/{service-id}/service-data/vnfs/vnf/{vnf-id}/vnf-data/vf-modules/vf-module/{vf-module-id}/vf-module-data/vf-module-topology/", produces = {
207                         MediaType.APPLICATION_JSON })
208         public ResponseEntity<?> getVFmodule(@PathVariable("service-id") String serviceId,
209                         @PathVariable("vnf-id") String vnfId, @PathVariable("vf-module-id") String vfModuleId) {
210                 LOGGER.info("Get vfModule-topology with serviceId {}, vnfId {} and vfModuleId {}",serviceId, vnfId,vfModuleId);
211
212                 GenericResourceApiVfModuleTopology genericResourceApiVfModuleTopology = new GenericResourceApiVfModuleTopology();
213
214                 genericResourceApiVfModuleTopology = cacheServiceProvider.getGenericResourceApiVfModuleTopology();
215                 return ResponseEntity.ok(genericResourceApiVfModuleTopology);
216
217         }
218 }