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