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