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