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