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