11099a24fc1df7ec8ba9d79ffff98671adeed794
[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.svnfm.simulator.repository.VnfmCacheRepository;
26 import org.onap.svnfm.simulator.services.SvnfmService;
27 import org.onap.vnfm.v1.model.CreateVnfRequest;
28 import org.onap.vnfm.v1.model.InlineResponse201;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.http.HttpHeaders;
33 import org.springframework.http.HttpStatus;
34 import org.springframework.http.ResponseEntity;
35 import org.springframework.web.bind.annotation.PathVariable;
36 import org.springframework.web.bind.annotation.RequestBody;
37 import org.springframework.web.bind.annotation.RequestMapping;
38 import org.springframework.web.bind.annotation.RequestMethod;
39 import org.springframework.web.bind.annotation.ResponseStatus;
40 import org.springframework.web.bind.annotation.RestController;
41
42 /**
43  * 
44  * @author Lathishbabu Ganesan (lathishbabu.ganesan@est.tech)
45  * @author Ronan Kenny (ronan.kenny@est.tech)
46  */
47 @RestController
48 @RequestMapping("/svnfm")
49 public class SvnfmController {
50
51     @Autowired
52     private SvnfmService svnfmService;
53
54     @Autowired
55     private VnfmCacheRepository vnfmCacheRepository;
56
57     private static final Logger LOGGER = LoggerFactory.getLogger(SvnfmController.class);
58
59     /**
60      * 
61      * @param createVNFRequest
62      * @return
63      */
64     @RequestMapping(method = RequestMethod.POST, value = "/vnf_instances")
65     public ResponseEntity<InlineResponse201> createVnf(@RequestBody final CreateVnfRequest createVNFRequest) {
66         LOGGER.info("Start createVnf------");
67         final HttpHeaders headers = new HttpHeaders();
68         headers.add("Content-Type", MediaType.APPLICATION_JSON);
69         return new ResponseEntity<>(vnfmCacheRepository.createVnf(createVNFRequest), headers, HttpStatus.CREATED);
70     }
71
72     /**
73      * 
74      * @param vnfId
75      * @return vnfm cache repository
76      */
77     @RequestMapping(method = RequestMethod.GET, value = "/vnf_instances/{vnfInstanceId}",
78             produces = MediaType.APPLICATION_JSON)
79     @ResponseStatus(code = HttpStatus.OK)
80     public InlineResponse201 getVnf(@PathVariable("vnfInstanceId") final String vnfId) {
81         LOGGER.info("Start getVnf------");
82         return vnfmCacheRepository.getVnf(vnfId);
83     }
84
85     /**
86      * 
87      * @param vnfId
88      * @return response entity
89      * @throws InterruptedException
90      */
91     @RequestMapping(method = RequestMethod.POST, value = "/vnf_instances/{vnfInstanceId}/instantiate")
92     public ResponseEntity<Object> instantiateVnf(@PathVariable("vnfInstanceId") final String vnfId)
93             throws InterruptedException {
94         LOGGER.info("Start instantiateVNFRequest");
95         final String instantiateJobId = UUID.randomUUID().toString();
96         final HttpHeaders headers = new HttpHeaders();
97         headers.add("Content-Type", MediaType.APPLICATION_JSON);
98         headers.add("Location", instantiateJobId);
99         return new ResponseEntity<>(svnfmService.instatiateVnf(vnfId, instantiateJobId), headers, HttpStatus.ACCEPTED);
100     }
101
102     /**
103      * 
104      * @param jobId
105      * @return response entity
106      * @throws InterruptedException
107      */
108     public ResponseEntity<Object> getJobStatus(@PathVariable("jobId") final String jobId) throws InterruptedException {
109         LOGGER.info("Start getJobStatus");
110         final HttpHeaders headers = new HttpHeaders();
111         headers.add("Content-Type", MediaType.APPLICATION_JSON);
112         return new ResponseEntity<>(svnfmService.getJobStatus(jobId), headers, HttpStatus.ACCEPTED);
113     }
114
115     /**
116      * 
117      * @param vnfId
118      * @return delete VNF
119      */
120     @RequestMapping(method = RequestMethod.DELETE, value = "/vnf_instances/{vnfInstanceId}",
121             produces = MediaType.APPLICATION_JSON)
122     @ResponseStatus(code = HttpStatus.OK)
123     public InlineResponse201 deleteVnf(@PathVariable("vnfInstanceId") final String vnfId) {
124         LOGGER.info("Start deleting Vnf------");
125         return vnfmCacheRepository.deleteVnf(vnfId);
126     }
127
128     /**
129      * 
130      * @param vnfId
131      * @return response entity
132      * @throws InterruptedException
133      */
134     @RequestMapping(method = RequestMethod.POST, value = "/vnf_instances/{vnfInstanceId}/terminate")
135     public ResponseEntity<Object> terminateVnf(@PathVariable("vnfInstanceId") final String vnfId)
136             throws InterruptedException {
137         LOGGER.info("Start terminateVNFRequest");
138         final HttpHeaders headers = new HttpHeaders();
139         headers.add("Content-Type", MediaType.APPLICATION_JSON);
140         return new ResponseEntity<>(svnfmService.terminateVnf(vnfId), headers, HttpStatus.ACCEPTED);
141     }
142 }