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