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=========================================================
21 package org.onap.so.svnfm.simulator.controller;
23 import java.util.UUID;
24 import javax.ws.rs.core.MediaType;
25 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.CreateVnfRequest;
26 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200;
27 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse2001;
28 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201;
29 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.LccnSubscriptionRequest;
30 import org.onap.so.svnfm.simulator.constants.Constant;
31 import org.onap.so.svnfm.simulator.repository.VnfmCacheRepository;
32 import org.onap.so.svnfm.simulator.services.SvnfmService;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.http.HttpHeaders;
37 import org.springframework.http.HttpStatus;
38 import org.springframework.http.ResponseEntity;
39 import org.springframework.web.bind.annotation.DeleteMapping;
40 import org.springframework.web.bind.annotation.GetMapping;
41 import org.springframework.web.bind.annotation.PathVariable;
42 import org.springframework.web.bind.annotation.PostMapping;
43 import org.springframework.web.bind.annotation.RequestBody;
44 import org.springframework.web.bind.annotation.RequestMapping;
45 import org.springframework.web.bind.annotation.ResponseStatus;
46 import org.springframework.web.bind.annotation.RestController;
50 * @author Lathishbabu Ganesan (lathishbabu.ganesan@est.tech)
51 * @author Ronan Kenny (ronan.kenny@est.tech)
54 @RequestMapping(path = Constant.BASE_URL, produces = MediaType.APPLICATION_JSON, consumes = MediaType.APPLICATION_JSON)
55 public class SvnfmController {
58 private SvnfmService svnfmService;
61 private VnfmCacheRepository vnfmCacheRepository;
63 private static final Logger LOGGER = LoggerFactory.getLogger(SvnfmController.class);
66 * To create the Vnf and stores the response in cache
68 * @param CreateVnfRequest
69 * @return InlineResponse201
71 @PostMapping(value = "/vnf_instances")
72 public ResponseEntity<InlineResponse201> createVnf(@RequestBody final CreateVnfRequest createVNFRequest) {
73 LOGGER.info("Start createVnf {}", createVNFRequest);
74 final String id = UUID.randomUUID().toString();
75 final HttpHeaders headers = new HttpHeaders();
76 headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
77 final ResponseEntity<InlineResponse201> responseEntity =
78 new ResponseEntity<>(vnfmCacheRepository.createVnf(createVNFRequest, id), headers, HttpStatus.CREATED);
79 LOGGER.info("Finished create {}", responseEntity);
80 return responseEntity;
84 * Get the vnf by id from cache
87 * @return InlineResponse201
89 @GetMapping(value = "/vnf_instances/{vnfInstanceId}")
90 @ResponseStatus(code = HttpStatus.OK)
91 public InlineResponse201 getVnf(@PathVariable("vnfInstanceId") final String vnfId) {
92 LOGGER.info("Start getVnf------");
93 return vnfmCacheRepository.getVnf(vnfId);
97 * To instantiate the vnf and returns the operation id
100 * @throws InterruptedException
102 @PostMapping(value = "/vnf_instances/{vnfInstanceId}/instantiate")
103 public ResponseEntity<Void> instantiateVnf(@PathVariable("vnfInstanceId") final String vnfId) {
104 LOGGER.info("Start instantiateVNFRequest for vnf id {} ", vnfId);
106 final HttpHeaders headers = new HttpHeaders();
107 headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
108 headers.add(HttpHeaders.LOCATION, svnfmService.instantiateVnf(vnfId));
109 return new ResponseEntity<>(headers, HttpStatus.ACCEPTED);
113 * To delete the vnf by id
116 * @return InlineResponse201
118 @DeleteMapping(value = "/vnf_instances/{vnfInstanceId}")
119 @ResponseStatus(code = HttpStatus.OK)
120 public ResponseEntity<Void> deleteVnf(@PathVariable("vnfInstanceId") final String vnfId) {
121 LOGGER.info("Start deleting Vnf------");
122 vnfmCacheRepository.deleteVnf(vnfId);
123 final HttpHeaders headers = new HttpHeaders();
124 headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
125 return new ResponseEntity<>(headers, HttpStatus.NO_CONTENT);
129 * To terminate the vnf by id
132 * @throws InterruptedException
134 @PostMapping(value = "/vnf_instances/{vnfInstanceId}/terminate")
135 public ResponseEntity<Object> terminateVnf(@PathVariable("vnfInstanceId") final String vnfId) {
136 LOGGER.info("Start terminateVNFRequest {}", vnfId);
137 final HttpHeaders headers = new HttpHeaders();
138 headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
139 headers.add(HttpHeaders.LOCATION, svnfmService.terminateVnf(vnfId));
140 return new ResponseEntity<>(headers, HttpStatus.ACCEPTED);
145 * To get the status of the operation by id
148 * @return response entity
149 * @throws InterruptedException
151 @GetMapping(value = "/vnf_lcm_op_occs/{vnfLcmOpOccId}")
152 public ResponseEntity<InlineResponse200> getOperationStatus(
153 @PathVariable("vnfLcmOpOccId") final String operationId) {
154 LOGGER.info("Start getOperationStatus");
155 final HttpHeaders headers = new HttpHeaders();
156 headers.add("Content-Type", MediaType.APPLICATION_JSON);
157 return new ResponseEntity<>(svnfmService.getOperationStatus(operationId), headers, HttpStatus.OK);
160 @PostMapping(value = "/subscriptions")
161 public ResponseEntity<InlineResponse2001> subscribeForNotifications(
162 @RequestBody final LccnSubscriptionRequest lccnSubscriptionRequest) {
163 LOGGER.info("Subscription request received: {}", lccnSubscriptionRequest);
164 svnfmService.registerSubscription(lccnSubscriptionRequest);
165 final InlineResponse2001 response = new InlineResponse2001();
167 final HttpHeaders headers = new HttpHeaders();
168 headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
169 return new ResponseEntity<>(response, headers, HttpStatus.CREATED);