Fixing SO build
[so.git] / adapters / etsi-sol003-adapter / etsi-sol003-lcm / etsi-sol003-lcm-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.common.CommonConstants.BASE_URL;
24 import javax.validation.Valid;
25 import javax.ws.rs.core.MediaType;
26 import org.onap.logging.ref.slf4j.ONAPLogConstants;
27 import org.onap.so.adapters.vnfmadapter.common.CommonConstants;
28 import org.onap.so.adapters.vnfmadapter.jobmanagement.JobManager;
29 import org.onap.so.adapters.vnfmadapter.lifecycle.LifecycleManager;
30 import org.onap.vnfmadapter.v1.model.CreateVnfRequest;
31 import org.onap.vnfmadapter.v1.model.CreateVnfResponse;
32 import org.onap.vnfmadapter.v1.model.DeleteVnfResponse;
33 import org.onap.vnfmadapter.v1.model.QueryJobResponse;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.slf4j.MDC;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.http.HttpStatus;
39 import org.springframework.http.ResponseEntity;
40 import org.springframework.stereotype.Controller;
41 import org.springframework.web.bind.annotation.DeleteMapping;
42 import org.springframework.web.bind.annotation.GetMapping;
43 import org.springframework.web.bind.annotation.PathVariable;
44 import org.springframework.web.bind.annotation.PostMapping;
45 import org.springframework.web.bind.annotation.RequestBody;
46 import org.springframework.web.bind.annotation.RequestHeader;
47 import org.springframework.web.bind.annotation.RequestMapping;
48 import io.swagger.annotations.ApiParam;
49
50 /**
51  * Controller for handling requests to the VNFM (Virtual Network Function Manager) adapter REST API.
52  */
53 @Controller
54 @RequestMapping(value = BASE_URL, produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
55         consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
56 public class VnfmAdapterController {
57
58     private static final Logger logger = LoggerFactory.getLogger(VnfmAdapterController.class);
59     private final LifecycleManager lifecycleManager;
60     private final JobManager jobManager;
61
62     @Autowired
63     VnfmAdapterController(final LifecycleManager lifecycleManager, final JobManager jobManager) {
64         this.lifecycleManager = lifecycleManager;
65         this.jobManager = jobManager;
66     }
67
68     @PostMapping(value = "/vnfs/{vnfId}")
69     public ResponseEntity<CreateVnfResponse> vnfCreate(
70             @ApiParam(value = "The identifier of the VNF. This must be the vnf-id of an existing generic-vnf in AAI.",
71                     required = true) @PathVariable("vnfId") final String vnfId,
72             @ApiParam(value = "VNF creation parameters",
73                     required = true) @Valid @RequestBody final CreateVnfRequest createVnfRequest,
74             @ApiParam(
75                     value = "Used to track REST requests for logging purposes. Identifies a single top level invocation of ONAP",
76                     required = false) @RequestHeader(value = ONAPLogConstants.Headers.REQUEST_ID,
77                             required = false) final String requestId,
78             @ApiParam(
79                     value = "Used to track REST requests for logging purposes. Identifies the client application user agent or user invoking the API",
80                     required = false) @RequestHeader(value = ONAPLogConstants.Headers.PARTNER_NAME,
81                             required = false) final String partnerName,
82             @ApiParam(
83                     value = "Used to track REST requests for logging purposes. Identifies a single invocation of a single component",
84                     required = false) @RequestHeader(value = ONAPLogConstants.Headers.INVOCATION_ID,
85                             required = false) final String invocationId) {
86
87         setLoggingMDCs(requestId, partnerName, invocationId);
88
89         logger.info("REST request vnfCreate with body: {}", createVnfRequest);
90
91         try {
92             final CreateVnfResponse createVnfResponse = lifecycleManager.createVnf(vnfId, createVnfRequest);
93             return new ResponseEntity<>(createVnfResponse, HttpStatus.ACCEPTED);
94         } finally {
95             clearLoggingMDCs();
96         }
97     }
98
99     @DeleteMapping(value = "/vnfs/{vnfId}")
100     public ResponseEntity<DeleteVnfResponse> vnfDelete(
101             @ApiParam(value = "The identifier of the VNF. This must be the vnf-id of an existing generic-vnf in AAI.",
102                     required = true) @PathVariable("vnfId") final String vnfId,
103             @ApiParam(
104                     value = "Used to track REST requests for logging purposes. Identifies a single top level invocation of ONAP",
105                     required = false) @RequestHeader(value = ONAPLogConstants.Headers.REQUEST_ID,
106                             required = false) final String requestId,
107             @ApiParam(
108                     value = "Used to track REST requests for logging purposes. Identifies the client application user agent or user invoking the API",
109                     required = false) @RequestHeader(value = ONAPLogConstants.Headers.PARTNER_NAME,
110                             required = false) final String partnerName,
111             @ApiParam(
112                     value = "Used to track REST requests for logging purposes. Identifies a single invocation of a single component",
113                     required = false) @RequestHeader(value = ONAPLogConstants.Headers.INVOCATION_ID,
114                             required = false) final String invocationId) {
115
116         setLoggingMDCs(requestId, partnerName, invocationId);
117
118         logger.info("REST request vnfDelete for VNF: {}", vnfId);
119
120         try {
121             final DeleteVnfResponse response = lifecycleManager.deleteVnf(vnfId);
122             return new ResponseEntity<>(response, HttpStatus.ACCEPTED);
123         } finally {
124             clearLoggingMDCs();
125         }
126     }
127
128     @GetMapping(value = "/jobs/{jobId}")
129     public ResponseEntity<QueryJobResponse> jobQuery(
130             @ApiParam(value = "The identifier of the Job.", required = true) @PathVariable("jobId") final String jobId,
131             @ApiParam(
132                     value = "Used to track REST requests for logging purposes. Identifies a single top level invocation of ONAP",
133                     required = false) @RequestHeader(value = ONAPLogConstants.Headers.REQUEST_ID,
134                             required = false) final String requestId,
135             @ApiParam(
136                     value = "Used to track REST requests for logging purposes. Identifies the client application user agent or user invoking the API",
137                     required = false) @RequestHeader(value = ONAPLogConstants.Headers.PARTNER_NAME,
138                             required = false) final String partnerName,
139             @ApiParam(
140                     value = "Used to track REST requests for logging purposes. Identifies a single invocation of a single component",
141                     required = false) @RequestHeader(value = ONAPLogConstants.Headers.INVOCATION_ID,
142                             required = false) final String invocationId) {
143
144         setLoggingMDCs(requestId, partnerName, invocationId);
145
146         try {
147             final QueryJobResponse response = jobManager.getVnfmOperation(jobId);
148             return new ResponseEntity<>(response, HttpStatus.OK);
149         } finally {
150             clearLoggingMDCs();
151         }
152     }
153
154     private void setLoggingMDCs(final String requestId, final String partnerName, final String invocationId) {
155         MDC.put(ONAPLogConstants.MDCs.REQUEST_ID, requestId);
156         MDC.put(ONAPLogConstants.MDCs.PARTNER_NAME, partnerName);
157         MDC.put(ONAPLogConstants.MDCs.INVOCATION_ID, invocationId);
158     }
159
160     private void clearLoggingMDCs() {
161         MDC.clear();
162     }
163
164 }