Fixing vulnerabilities and code smells
[so.git] / so-etsi-nfvo / so-etsi-nfvo-ns-lcm / so-etsi-nfvo-ns-lcm-service / src / main / java / org / onap / so / etsi / nfvo / ns / lcm / rest / NsLifecycleManagementController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 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 package org.onap.so.etsi.nfvo.ns.lcm.rest;
21
22 import static org.onap.so.etsi.nfvo.ns.lcm.Constants.HTTP_GLOBAL_CUSTOMER_ID_HTTP_HEADER_PARM_NAME;
23 import static org.onap.so.etsi.nfvo.ns.lcm.Constants.HTTP_SERVICETYPE_HEADER_DEFAULT_VALUE;
24 import static org.onap.so.etsi.nfvo.ns.lcm.Constants.HTTP_SERVICETYPE_HEADER_PARM_NAME;
25 import static org.onap.so.etsi.nfvo.ns.lcm.Constants.NS_LIFE_CYCLE_MANAGEMENT_BASE_URL;
26 import static org.slf4j.LoggerFactory.getLogger;
27 import java.net.URI;
28 import javax.ws.rs.core.MediaType;
29 import org.apache.commons.lang3.tuple.ImmutablePair;
30 import org.onap.so.etsi.nfvo.ns.lcm.lifecycle.NsLifeCycleManager;
31 import org.onap.so.etsi.nfvo.ns.lcm.model.Body;
32 import org.onap.so.etsi.nfvo.ns.lcm.model.CreateNsRequest;
33 import org.onap.so.etsi.nfvo.ns.lcm.model.InstantiateNsRequest;
34 import org.onap.so.etsi.nfvo.ns.lcm.model.NsInstancesNsInstance;
35 import org.onap.so.etsi.nfvo.ns.lcm.model.TerminateNsRequest;
36 import org.slf4j.Logger;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.http.ResponseEntity;
39 import org.springframework.stereotype.Controller;
40 import org.springframework.web.bind.annotation.DeleteMapping;
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.RequestHeader;
45 import org.springframework.web.bind.annotation.RequestMapping;
46
47 /**
48  * Controller for handling the NS Lifecycle Management. For further information please read:
49  * https://www.etsi.org/deliver/etsi_gs/NFV-SOL/001_099/005/02.07.01_60/gs_NFV-SOL005v020701p.pdf Use the section number
50  * above each endpoint to find the corresponding section in the above document.
51  * 
52  * @author Waqas Ikram (waqas.ikram@est.tech)
53  *
54  */
55 @Controller
56 @RequestMapping(value = NS_LIFE_CYCLE_MANAGEMENT_BASE_URL)
57 public class NsLifecycleManagementController {
58     private static final Logger logger = getLogger(NsLifecycleManagementController.class);
59
60     private final NsLifeCycleManager nsLifeCycleManager;
61
62     @Autowired
63     public NsLifecycleManagementController(final NsLifeCycleManager nsLifeCycleManager) {
64         this.nsLifeCycleManager = nsLifeCycleManager;
65     }
66
67     /**
68      * The POST method creates new {@link Body new NS instance resource} request. See Section Number: 6.3.1 for more
69      * detail
70      * 
71      * @param globalCustomerId The global customer ID
72      * @param serviceType The service type
73      * @param createNsRequest create network service request (see clause 6.5.2.9)
74      * @return "201 Created" response containing a representation of the NS instance resource
75      *         {@link NsInstancesNsInstance} just created by the NFVO, and provides the URI of the newly-created
76      *         resource in the "Location:" HTTP header
77      */
78     @PostMapping(value = "/ns_instances", produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
79             consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
80     public ResponseEntity<NsInstancesNsInstance> createNs(
81             @RequestHeader(value = HTTP_GLOBAL_CUSTOMER_ID_HTTP_HEADER_PARM_NAME,
82                     required = true) final String globalCustomerId,
83             @RequestHeader(value = HTTP_SERVICETYPE_HEADER_PARM_NAME, required = false,
84                     defaultValue = HTTP_SERVICETYPE_HEADER_DEFAULT_VALUE) final String serviceType,
85             @RequestBody final CreateNsRequest createNsRequest) {
86         logger.info("Received Create NS Request: {}\n with globalCustomerId: {}\n serviceType: {}\n", createNsRequest,
87                 globalCustomerId, serviceType);
88
89         final ImmutablePair<URI, NsInstancesNsInstance> nsInstance =
90                 nsLifeCycleManager.createNs(createNsRequest, globalCustomerId, serviceType);
91
92         final URI resourceUri = nsInstance.getLeft();
93         final NsInstancesNsInstance createdNsresponse = nsInstance.getRight();
94
95         logger.info("NS resource created successfully. Resource location: {}, response: {}", resourceUri,
96                 createdNsresponse);
97
98         return ResponseEntity.created(resourceUri).body(createdNsresponse);
99     }
100
101     /**
102      * The DELETE method delete NS instance
103      * 
104      * @param nsInstanceId Identifier of the NS instance to be deleted.
105      * @return "202 Accepted" response with an empty entity body
106      */
107     @DeleteMapping(value = "/ns_instances/{nsInstanceId}",
108             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
109     public ResponseEntity<Void> deleteNs(@PathVariable("nsInstanceId") final String nsInstanceId) {
110         logger.debug("Received delete NS request for nsInstanceId: {}", nsInstanceId);
111         nsLifeCycleManager.deleteNs(nsInstanceId);
112         logger.info("Successfully deleted NS for nsInstanceId: {}", nsInstanceId);
113         return ResponseEntity.noContent().build();
114     }
115
116     /**
117      * The POST method instantiate NS instance
118      * 
119      * @param nsInstanceId Identifier of the NS instance to be instantiated.
120      * @param instantiateNsRequest Instantiate network service request (see clause 6.5.2.11)
121      * @return "202 Accepted" response with an empty entity body and a "Location" HTTP header that points to the new "NS
122      *         Lifecycle Operation Occurrence" resource
123      */
124     @PostMapping(value = "/ns_instances/{nsInstanceId}/instantiate",
125             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
126             consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
127     public ResponseEntity<Void> instantiateNs(@PathVariable("nsInstanceId") final String nsInstanceId,
128             @RequestBody final InstantiateNsRequest instantiateNsRequest) {
129         logger.debug("Received instantiate NS request: {}\n with nsInstanceId: {}", instantiateNsRequest, nsInstanceId);
130         final URI resourceUri = nsLifeCycleManager.instantiateNs(nsInstanceId, instantiateNsRequest);
131         logger.info("{} Ns Instantiation started successfully. Resource Operation Occurrence uri: {}", nsInstanceId,
132                 resourceUri);
133         return ResponseEntity.accepted().location(resourceUri).build();
134     }
135
136     /**
137      * The POST method terminate NS instance
138      * 
139      * @param nsInstanceId Identifier of the NS instance to be terminated.
140      * @param terminateNsRequest The terminate NS request parameters (see clause 6.5.2.15)
141      * @return "202 Accepted" response with an empty entity body and a "Location" HTTP header that points to the new "NS
142      *         Lifecycle Operation Occurrence" resource
143      */
144     @PostMapping(value = "/ns_instances/{nsInstanceId}/terminate",
145             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
146             consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
147     public ResponseEntity<Void> terminateNs(@PathVariable("nsInstanceId") final String nsInstanceId,
148             @RequestBody final TerminateNsRequest terminateNsRequest) {
149         logger.debug("Received terminate NS request: {}\n with nsInstanceId: {}", terminateNsRequest, nsInstanceId);
150         final URI resourceUri = nsLifeCycleManager.terminateNs(nsInstanceId, terminateNsRequest);
151         logger.info("{} Ns Terminate started successfully. Resource Operation Occurrence uri: {}", nsInstanceId,
152                 resourceUri);
153         return ResponseEntity.accepted().location(resourceUri).build();
154     }
155
156 }