VNFM simulator implementation for instantiate flow
[so.git] / vnfm-simulator / vnfm-service / src / main / java / org / onap / 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.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.InstantiateVnfRequest;
30 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.LccnSubscriptionRequest;
31 import org.onap.svnfm.simulator.constants.Constant;
32 import org.onap.svnfm.simulator.repository.VnfmCacheRepository;
33 import org.onap.svnfm.simulator.services.SvnfmService;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.http.HttpHeaders;
38 import org.springframework.http.HttpStatus;
39 import org.springframework.http.ResponseEntity;
40 import org.springframework.web.bind.annotation.DeleteMapping;
41 import org.springframework.web.bind.annotation.GetMapping;
42 import org.springframework.web.bind.annotation.PathVariable;
43 import org.springframework.web.bind.annotation.PostMapping;
44 import org.springframework.web.bind.annotation.RequestBody;
45 import org.springframework.web.bind.annotation.RequestMapping;
46 import org.springframework.web.bind.annotation.ResponseStatus;
47 import org.springframework.web.bind.annotation.RestController;
48
49 /**
50  *
51  * @author Lathishbabu Ganesan (lathishbabu.ganesan@est.tech)
52  * @author Ronan Kenny (ronan.kenny@est.tech)
53  */
54 @RestController
55 @RequestMapping(path = Constant.BASE_URL, produces = MediaType.APPLICATION_JSON, consumes = MediaType.APPLICATION_JSON)
56 public class SvnfmController {
57
58     @Autowired
59     private SvnfmService svnfmService;
60
61     @Autowired
62     private VnfmCacheRepository vnfmCacheRepository;
63
64     private static final Logger LOGGER = LoggerFactory.getLogger(SvnfmController.class);
65
66     /**
67      * To create the Vnf and stores the response in cache
68      *
69      * @param CreateVnfRequest
70      * @return InlineResponse201
71      */
72     @PostMapping(value = "/vnf_instances")
73     public ResponseEntity<InlineResponse201> createVnf(@RequestBody final CreateVnfRequest createVNFRequest) {
74         LOGGER.info("Start createVnf {}", createVNFRequest);
75         final String id = UUID.randomUUID().toString();
76         final HttpHeaders headers = new HttpHeaders();
77         headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
78         final ResponseEntity<InlineResponse201> responseEntity =
79                 new ResponseEntity<>(vnfmCacheRepository.createVnf(createVNFRequest, id), headers, HttpStatus.CREATED);
80         LOGGER.info("Finished create {}", responseEntity);
81         return responseEntity;
82     }
83
84     /**
85      * Get the vnf by id from cache
86      *
87      * @param vnfId
88      * @return InlineResponse201
89      */
90     @GetMapping(value = "/vnf_instances/{vnfInstanceId}")
91     @ResponseStatus(code = HttpStatus.OK)
92     public InlineResponse201 getVnf(@PathVariable("vnfInstanceId") final String vnfId) {
93         LOGGER.info("Start getVnf------");
94         return vnfmCacheRepository.getVnf(vnfId);
95     }
96
97     /**
98      * To instantiate the vnf and returns the operation id
99      *
100      * @param vnfId
101      * @throws InterruptedException
102      */
103     @PostMapping(value = "/vnf_instances/{vnfInstanceId}/instantiate")
104     public ResponseEntity<Void> instantiateVnf(@PathVariable("vnfInstanceId") final String vnfId,
105             @RequestBody final InstantiateVnfRequest instantiateVNFRequest) {
106         LOGGER.info("Start instantiateVNFRequest {} ", instantiateVNFRequest);
107
108         final HttpHeaders headers = new HttpHeaders();
109         headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
110         headers.add(HttpHeaders.LOCATION, svnfmService.instantiateVnf(vnfId, instantiateVNFRequest));
111         return new ResponseEntity<>(headers, HttpStatus.ACCEPTED);
112     }
113
114     /**
115      * To delete the vnf by id
116      *
117      * @param vnfId
118      * @return InlineResponse201
119      */
120     @DeleteMapping(value = "/vnf_instances/{vnfInstanceId}")
121     @ResponseStatus(code = HttpStatus.OK)
122     public InlineResponse201 deleteVnf(@PathVariable("vnfInstanceId") final String vnfId) {
123         LOGGER.info("Start deleting Vnf------");
124         return vnfmCacheRepository.deleteVnf(vnfId);
125     }
126
127     /**
128      * To terminate the vnf by id
129      *
130      * @param vnfId
131      * @throws InterruptedException
132      */
133     @PostMapping(value = "/vnf_instances/{vnfInstanceId}/terminate")
134     public ResponseEntity<Object> terminateVnf(@PathVariable("vnfInstanceId") final String vnfId) {
135         LOGGER.info("Start terminateVNFRequest");
136         final HttpHeaders headers = new HttpHeaders();
137         headers.add("Content-Type", MediaType.APPLICATION_JSON);
138         return new ResponseEntity<>(svnfmService.terminateVnf(vnfId), headers, HttpStatus.ACCEPTED);
139     }
140
141
142     /**
143      * To get the status of the operation by id
144      *
145      * @param operationId
146      * @return response entity
147      * @throws InterruptedException
148      */
149     @GetMapping(value = "/vnf_lcm_op_occs/{vnfLcmOpOccId}")
150     public ResponseEntity<InlineResponse200> getOperationStatus(
151             @PathVariable("vnfLcmOpOccId") final String operationId) {
152         LOGGER.info("Start getOperationStatus");
153         final HttpHeaders headers = new HttpHeaders();
154         headers.add("Content-Type", MediaType.APPLICATION_JSON);
155         return new ResponseEntity<>(svnfmService.getOperationStatus(operationId), headers, HttpStatus.OK);
156     }
157
158     @PostMapping(value = "/subscriptions")
159     public ResponseEntity<InlineResponse2001> subscribeForNotifications(
160             @RequestBody final LccnSubscriptionRequest lccnSubscriptionRequest) {
161         LOGGER.info("Subscription request received: {}", lccnSubscriptionRequest);
162         svnfmService.registerSubscription(lccnSubscriptionRequest);
163         final InlineResponse2001 response = new InlineResponse2001();
164
165         final HttpHeaders headers = new HttpHeaders();
166         headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
167         return new ResponseEntity<>(response, headers, HttpStatus.OK);
168     }
169 }