207f0ff38795669c2706afbfa231e79d0986835a
[so.git] / so-etsi-nfvo / so-etsi-nfvo-ns-lcm / so-etsi-nfvo-ns-lcm-service / src / main / java / org / onap / so / etsi / nfvo / ns / lcm / rest / NsLcmOperationOccurrencesController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 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 package org.onap.so.etsi.nfvo.ns.lcm.rest;
21
22 import static org.onap.so.etsi.nfvo.ns.lcm.Constants.NS_LIFE_CYCLE_MANAGEMENT_BASE_URL;
23 import static org.slf4j.LoggerFactory.getLogger;
24 import javax.ws.rs.core.MediaType;
25 import org.onap.so.etsi.nfvo.ns.lcm.lifecycle.NsLcmOperationOccurrenceManager;
26 import org.onap.so.etsi.nfvo.ns.lcm.model.InlineResponse400;
27 import org.onap.so.etsi.nfvo.ns.lcm.model.NsLcmOpOccsNsLcmOpOcc;
28 import org.slf4j.Logger;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.http.HttpStatus;
31 import org.springframework.http.ResponseEntity;
32 import org.springframework.stereotype.Controller;
33 import org.springframework.web.bind.annotation.GetMapping;
34 import org.springframework.web.bind.annotation.PathVariable;
35 import org.springframework.web.bind.annotation.RequestMapping;
36 import java.util.Optional;
37
38 /**
39  * Controller for handling NS lifecycle management operation occurrence requests see clause 6.4.9 and 6.4.10 in
40  * https://www.etsi.org/deliver/etsi_gs/NFV-SOL/001_099/005/02.07.01_60/gs_NFV-SOL005v020701p.pdf
41  * 
42  * @author Waqas Ikram (waqas.ikram@est.tech)
43  * @author Andrew Lamb (andrew.a.lamb@est.tech)
44  *
45  */
46 @Controller
47 @RequestMapping(value = NS_LIFE_CYCLE_MANAGEMENT_BASE_URL)
48 public class NsLcmOperationOccurrencesController {
49     private static final Logger logger = getLogger(NsLcmOperationOccurrencesController.class);
50
51     private final NsLcmOperationOccurrenceManager nsLcmOperationOccurrenceManager;
52
53     @Autowired
54     public NsLcmOperationOccurrencesController(final NsLcmOperationOccurrenceManager nsLcmOperationOccurrenceManager) {
55         this.nsLcmOperationOccurrenceManager = nsLcmOperationOccurrenceManager;
56     }
57
58     /**
59      * The GET method to retrieve status information about a NS lifecycle management operation occurrence by reading an
60      * individual "NS LCM operation occurrence" resource.
61      * 
62      * @param nsLcmOpOccId Identifier of a NS lifecycle management operation occurrence
63      * @return "200 OK" with {@link NsLcmOpOccsNsLcmOpOcc NsLcmOpOcc} Information about a NS LCM operation occurrence
64      *         was queried successfully. The response body shall contain status information about a NS lifecycle
65      *         management operation occurrence (see clause 6.5.2.3).
66      */
67     @GetMapping(value = "/ns_lcm_op_occs/{nsLcmOpOccId}",
68             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
69     public ResponseEntity<?> getOperationStatus(@PathVariable("nsLcmOpOccId") final String nsLcmOpOccId) {
70         logger.info("Received request to retrieve operation status for nsLcmOpOccId: {}", nsLcmOpOccId);
71         final Optional<NsLcmOpOccsNsLcmOpOcc> optionalNsLcmOpOccs =
72                 nsLcmOperationOccurrenceManager.getNsLcmOperationOccurrence(nsLcmOpOccId);
73
74         if (optionalNsLcmOpOccs.isPresent()) {
75             final NsLcmOpOccsNsLcmOpOcc nsLcmOpOcc = optionalNsLcmOpOccs.get();
76             logger.info("Sending back NsLcmOpOcc: {}", nsLcmOpOcc);
77             return ResponseEntity.ok().body(nsLcmOpOcc);
78         }
79
80         final String errorMessage = "Unable to retrieve operation occurrence status for nsLcmOpOccId: " + nsLcmOpOccId;
81         logger.error(errorMessage);
82         return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new InlineResponse400().detail(errorMessage));
83     }
84
85 }