Check for existing VNF in VNFM
[so.git] / adapters / mso-vnfm-adapter / mso-vnfm-etsi-adapter / src / main / java / org / onap / so / adapters / vnfmadapter / rest / VnfmAdapterController.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.adapters.vnfmadapter.rest;
22
23 import static org.onap.so.adapters.vnfmadapter.Constants.BASE_URL;
24 import java.util.UUID;
25 import javax.validation.Valid;
26 import javax.ws.rs.core.MediaType;
27 import org.onap.logging.ref.slf4j.ONAPLogConstants;
28 import org.onap.so.adapters.vnfmadapter.lifecycle.LifecycleManager;
29 import org.onap.vnfmadapter.v1.model.CreateVnfRequest;
30 import org.onap.vnfmadapter.v1.model.CreateVnfResponse;
31 import org.onap.vnfmadapter.v1.model.DeleteVnfResponse;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.slf4j.MDC;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.http.HttpStatus;
37 import org.springframework.http.ResponseEntity;
38 import org.springframework.stereotype.Controller;
39 import org.springframework.web.bind.annotation.DeleteMapping;
40 import org.springframework.web.bind.annotation.PathVariable;
41 import org.springframework.web.bind.annotation.PostMapping;
42 import org.springframework.web.bind.annotation.RequestBody;
43 import org.springframework.web.bind.annotation.RequestHeader;
44 import org.springframework.web.bind.annotation.RequestMapping;
45 import io.swagger.annotations.ApiParam;
46
47 /**
48  * Controller for handling requests to the VNFM (Virtual Network Function Manager) adapter REST API.
49  */
50 @Controller
51 @RequestMapping(value = BASE_URL, produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
52         consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
53 public class VnfmAdapterController {
54
55     private static final Logger logger = LoggerFactory.getLogger(VnfmAdapterController.class);
56     private final LifecycleManager lifecycleManager;
57
58     @Autowired
59     VnfmAdapterController(final LifecycleManager lifecycleManager) {
60         this.lifecycleManager = lifecycleManager;
61     }
62
63     @PostMapping(value = "/vnfs/{vnfId}")
64     public ResponseEntity<CreateVnfResponse> vnfCreate(
65             @ApiParam(value = "The identifier of the VNF. This must be the vnf-id of an existing generic-vnf in AAI.",
66                     required = true) @PathVariable("vnfId") final String vnfId,
67             @ApiParam(value = "VNF creation parameters",
68                     required = true) @Valid @RequestBody final CreateVnfRequest createVnfRequest,
69             @ApiParam(
70                     value = "Used to track REST requests for logging purposes. Identifies a single top level invocation of ONAP",
71                     required = false) @RequestHeader(value = ONAPLogConstants.Headers.REQUEST_ID,
72                             required = false) final String requestId,
73             @ApiParam(
74                     value = "Used to track REST requests for logging purposes. Identifies the client application user agent or user invoking the API",
75                     required = false) @RequestHeader(value = ONAPLogConstants.Headers.PARTNER_NAME,
76                             required = false) final String partnerName,
77             @ApiParam(
78                     value = "Used to track REST requests for logging purposes. Identifies a single invocation of a single component",
79                     required = false) @RequestHeader(value = ONAPLogConstants.Headers.INVOCATION_ID,
80                             required = false) final String invocationId) {
81
82         setLoggingMDCs(requestId, partnerName, invocationId);
83
84         logger.info("REST request vnfCreate with body: {}", createVnfRequest);
85
86         final CreateVnfResponse createVnfResponse = lifecycleManager.createVnf(vnfId, createVnfRequest);
87         clearLoggingMDCs();
88         return new ResponseEntity<>(createVnfResponse, HttpStatus.ACCEPTED);
89     }
90
91     @DeleteMapping(value = "/vnfs/{vnfId}")
92     public ResponseEntity<DeleteVnfResponse> vnfDelete(
93             @ApiParam(value = "The identifier of the VNF. This must be the vnf-id of an existing generic-vnf in AAI.",
94                     required = true) @PathVariable("vnfId") final String vnfId,
95             @ApiParam(
96                     value = "Used to track REST requests for logging purposes. Identifies a single top level invocation of ONAP",
97                     required = false) @RequestHeader(value = ONAPLogConstants.Headers.REQUEST_ID,
98                             required = false) final String requestId,
99             @ApiParam(
100                     value = "Used to track REST requests for logging purposes. Identifies the client application user agent or user invoking the API",
101                     required = false) @RequestHeader(value = ONAPLogConstants.Headers.PARTNER_NAME,
102                             required = false) final String partnerName,
103             @ApiParam(
104                     value = "Used to track REST requests for logging purposes. Identifies a single invocation of a single component",
105                     required = false) @RequestHeader(value = ONAPLogConstants.Headers.INVOCATION_ID,
106                             required = false) final String invocationId) {
107
108         setLoggingMDCs(requestId, partnerName, invocationId);
109
110         logger.info("REST request vnfDelete for VNF: {}", vnfId);
111
112         final DeleteVnfResponse response = new DeleteVnfResponse();
113         response.setJobId(UUID.randomUUID().toString());
114         clearLoggingMDCs();
115         return new ResponseEntity<>(response, HttpStatus.ACCEPTED);
116     }
117
118     private void setLoggingMDCs(final String requestId, final String partnerName, final String invocationId) {
119         MDC.put(ONAPLogConstants.MDCs.REQUEST_ID, requestId);
120         MDC.put(ONAPLogConstants.MDCs.PARTNER_NAME, partnerName);
121         MDC.put(ONAPLogConstants.MDCs.INVOCATION_ID, invocationId);
122     }
123
124     private void clearLoggingMDCs() {
125         MDC.clear();
126     }
127
128 }