Update vnfm simulator - subscribe and notify
[integration/csit.git] / plans / so / integration-etsi-testing / so-simulators / vnfm-simulator / vnfm-service / src / main / java / org / onap / so / svnfm / simulator / controller / SvnfmController.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
21 package org.onap.so.svnfm.simulator.controller;
22
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;
47
48 /**
49  *
50  * @author Lathishbabu Ganesan (lathishbabu.ganesan@est.tech)
51  * @author Ronan Kenny (ronan.kenny@est.tech)
52  */
53 @RestController
54 @RequestMapping(path = Constant.BASE_URL, produces = MediaType.APPLICATION_JSON, consumes = MediaType.APPLICATION_JSON)
55 public class SvnfmController {
56
57     @Autowired
58     private SvnfmService svnfmService;
59
60     @Autowired
61     private VnfmCacheRepository vnfmCacheRepository;
62
63     private static final Logger LOGGER = LoggerFactory.getLogger(SvnfmController.class);
64
65     /**
66      * To create the Vnf and stores the response in cache
67      *
68      * @param createVNFRequest
69      * @return InlineResponse201
70      */
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;
81     }
82
83     /**
84      * Get the vnf by id from cache
85      *
86      * @param vnfId
87      * @return InlineResponse201
88      */
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);
94     }
95
96     /**
97      * To instantiate the vnf and returns the operation id
98      *
99      * @param vnfId
100      * @throws InterruptedException
101      */
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);
105
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);
110     }
111
112     /**
113      * To delete the vnf by id
114      *
115      * @param vnfId
116      * @return InlineResponse201
117      */
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);
126     }
127
128     /**
129      * To terminate the vnf by id
130      *
131      * @param vnfId
132      * @throws InterruptedException
133      */
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);
141     }
142
143
144     /**
145      * To get the status of the operation by id
146      *
147      * @param operationId
148      * @return response entity
149      * @throws InterruptedException
150      */
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);
158     }
159
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();
166
167         final HttpHeaders headers = new HttpHeaders();
168         headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
169         return new ResponseEntity<>(response, headers, HttpStatus.CREATED);
170     }
171 }