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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
20 package org.onap.so.sdncsimulator.controller;
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;
29 import java.util.ArrayList;
30 import java.util.List;
32 import javax.servlet.http.HttpServletRequest;
33 import javax.ws.rs.core.MediaType;
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;
61 * @author Waqas Ikram (waqas.ikram@est.tech)
65 @RequestMapping(path = BASE_URL)
66 public class OperationsController {
67 private static final String HTTP_STATUS_OK = HttpStatus.OK.value() + "";
69 private static final Logger LOGGER = LoggerFactory.getLogger(OperationsController.class);
71 private final ServiceOperationsCacheServiceProvider cacheServiceProvider;
74 public OperationsController(final ServiceOperationsCacheServiceProvider cacheServiceProvider) {
75 this.cacheServiceProvider = cacheServiceProvider;
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);
86 final GenericResourceApiServiceOperationInformation apiServiceOperationInformation = inputRequest.getInput();
88 if (apiServiceOperationInformation == null) {
89 LOGGER.error("Invalid input request: {}", inputRequest);
90 return ResponseEntity.badRequest().build();
93 final Output output = getOutput(apiServiceOperationInformation);
94 final OutputRequest outputRequest = new OutputRequest(output);
96 if (output.getResponseCode().equals(HTTP_STATUS_OK)) {
97 LOGGER.info("Sucessfully executed service request sending response: {}", outputRequest);
98 return ResponseEntity.ok(outputRequest);
100 LOGGER.error("Unable to execute input request: {}, will send OutputRequest: {}", inputRequest, outputRequest);
101 return ResponseEntity.badRequest().body(outputRequest);
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);
113 final GenericResourceApiVnfOperationInformation apiVnfOperationInformation = inputRequest.getInput();
114 if (apiVnfOperationInformation == null) {
115 LOGGER.error("Invalid input request: {}", inputRequest);
116 return ResponseEntity.badRequest().build();
119 final Output output = getOutput(apiVnfOperationInformation);
120 final OutputRequest outputRequest = new OutputRequest(output);
122 if (output.getResponseCode().equals(HTTP_STATUS_OK)) {
123 LOGGER.info("Sucessfully executed request vnf sending response: {}", outputRequest);
124 return ResponseEntity.ok(outputRequest);
127 LOGGER.error("Unable to execute input request: {}, will send OutputRequest: {}", inputRequest, outputRequest);
128 return ResponseEntity.badRequest().body(outputRequest);
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);
146 return cacheServiceProvider.putServiceOperationInformation(serviceOperationInformation);
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);
159 return cacheServiceProvider.putVnfOperationInformation(apiVnfOperationInformation);
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);
170 final GenericResourceApiVfModuleOperationInformation apiVfModuleperationInformation = inputRequest.getInput();
171 if (apiVfModuleperationInformation == null) {
172 LOGGER.error("Invalid input request: {}", inputRequest);
173 return ResponseEntity.badRequest().build();
176 final Output output = getOutput(apiVfModuleperationInformation);
177 final OutputRequest outputRequest = new OutputRequest(output);
179 if (output.getResponseCode().equals(HTTP_STATUS_OK)) {
180 LOGGER.info("Sucessfully executed request vnf sending response: {}", outputRequest);
181 return ResponseEntity.ok(outputRequest);
184 LOGGER.error("Unable to execute input request: {}, will send OutputRequest: {}", inputRequest, outputRequest);
185 return ResponseEntity.badRequest().body(outputRequest);
189 private Output getOutput(final GenericResourceApiVfModuleOperationInformation apiVfModuleOperationInformation) {
191 return cacheServiceProvider.putVfModuleOperationInformation(apiVfModuleOperationInformation);
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) {
199 LOGGER.info("Get vnf-topology with serviceId {} and vnfId {}",serviceId, vnfId);
200 GenericResourceApiVnfTopology genericResourceApiVnfTopology = new GenericResourceApiVnfTopology();
202 genericResourceApiVnfTopology = cacheServiceProvider.getGenericResourceApiVnfTopology();
203 return ResponseEntity.ok(genericResourceApiVnfTopology);
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);
212 GenericResourceApiVfModuleTopology genericResourceApiVfModuleTopology = new GenericResourceApiVfModuleTopology();
214 genericResourceApiVfModuleTopology = cacheServiceProvider.getGenericResourceApiVfModuleTopology();
215 return ResponseEntity.ok(genericResourceApiVfModuleTopology);