f0958cbe7daf1223a2023342e6e723c17ba68ea2
[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 static org.slf4j.LoggerFactory.getLogger;
25 import java.util.concurrent.ExecutorService;
26 import java.util.concurrent.Executors;
27 import javax.ws.rs.core.MediaType;
28 import org.onap.so.adapters.vnfmadapter.extclients.aai.AaiHelper;
29 import org.onap.so.adapters.vnfmadapter.extclients.aai.AaiServiceProvider;
30 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.VnfmServiceProvider;
31 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.VnfIdentifierCreationNotification;
32 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.VnfIdentifierDeletionNotification;
33 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.VnfLcmOperationOccurrenceNotification;
34 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.VnfLcmOperationOccurrenceNotification.OperationEnum;
35 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.VnfLcmOperationOccurrenceNotification.OperationStateEnum;
36 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201;
37 import org.onap.so.adapters.vnfmadapter.jobmanagement.JobManager;
38 import org.onap.so.adapters.vnfmadapter.notificationhandling.NotificationHandler;
39 import org.slf4j.Logger;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.http.HttpStatus;
42 import org.springframework.http.ResponseEntity;
43 import org.springframework.stereotype.Controller;
44 import org.springframework.web.bind.annotation.PostMapping;
45 import org.springframework.web.bind.annotation.RequestBody;
46 import org.springframework.web.bind.annotation.RequestMapping;
47
48 /**
49  * Controller for handling notifications from the VNFM (Virtual Network Function Manager).
50  */
51 @Controller
52 @RequestMapping(value = BASE_URL, produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
53         consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
54 public class Sol003LcnContoller {
55     private static Logger logger = getLogger(Sol003LcnContoller.class);
56     private static final String LOG_LCN_RECEIVED = "LCN received from VNFM: ";
57     private final AaiServiceProvider aaiServiceProvider;
58     private final AaiHelper aaiHelper;
59     private final VnfmServiceProvider vnfmServiceProvider;
60     private final JobManager jobManager;
61     private final ExecutorService executor = Executors.newCachedThreadPool();
62
63     @Autowired
64     Sol003LcnContoller(final AaiServiceProvider aaiServiceProvider, final AaiHelper aaiHelper,
65             final VnfmServiceProvider vnfmServiceProvider, final JobManager jobManager) {
66         this.aaiServiceProvider = aaiServiceProvider;
67         this.aaiHelper = aaiHelper;
68         this.vnfmServiceProvider = vnfmServiceProvider;
69         this.jobManager = jobManager;
70     }
71
72     @PostMapping(value = "/lcn/VnfIdentifierCreationNotification")
73     public ResponseEntity<Void> lcnVnfIdentifierCreationNotificationPost(
74             @RequestBody final VnfIdentifierCreationNotification vnfIdentifierCreationNotification) {
75         logger.info(LOG_LCN_RECEIVED + vnfIdentifierCreationNotification);
76         return new ResponseEntity<>(HttpStatus.NO_CONTENT);
77     }
78
79     @PostMapping(value = "/lcn/VnfIdentifierDeletionNotification")
80     public ResponseEntity<Void> lcnVnfIdentifierDeletionNotificationPost(
81             @RequestBody final VnfIdentifierDeletionNotification vnfIdentifierDeletionNotification) {
82         logger.info(LOG_LCN_RECEIVED + vnfIdentifierDeletionNotification);
83         return new ResponseEntity<>(HttpStatus.NO_CONTENT);
84     }
85
86     @PostMapping(value = "/lcn/VnfLcmOperationOccurrenceNotification")
87     public ResponseEntity<Void> lcnVnfLcmOperationOccurrenceNotificationPost(
88             @RequestBody final VnfLcmOperationOccurrenceNotification vnfLcmOperationOccurrenceNotification) {
89         logger.info(LOG_LCN_RECEIVED + vnfLcmOperationOccurrenceNotification);
90
91         if (isANotificationOfInterest(vnfLcmOperationOccurrenceNotification)) {
92             final InlineResponse201 vnfInstance = getVnfInstance(vnfLcmOperationOccurrenceNotification);
93             final NotificationHandler handler = new NotificationHandler(vnfLcmOperationOccurrenceNotification,
94                     aaiHelper, aaiServiceProvider, vnfmServiceProvider, jobManager, vnfInstance);
95             executor.execute(handler);
96         }
97
98         logger.info("Sending notification response");
99         return new ResponseEntity<>(HttpStatus.NO_CONTENT);
100     }
101
102     private boolean isANotificationOfInterest(final VnfLcmOperationOccurrenceNotification notification) {
103         return isInstanitiateCompleted(notification) || isTerminateTerminalState(notification);
104     }
105
106     private boolean isInstanitiateCompleted(final VnfLcmOperationOccurrenceNotification notification) {
107         return notification.getOperation().equals(OperationEnum.INSTANTIATE)
108                 && notification.getOperationState().equals(OperationStateEnum.COMPLETED);
109     }
110
111     private boolean isTerminateTerminalState(final VnfLcmOperationOccurrenceNotification notification) {
112         return notification.getOperation().equals(OperationEnum.TERMINATE)
113                 && (notification.getOperationState().equals(OperationStateEnum.COMPLETED)
114                         || notification.getOperationState().equals(OperationStateEnum.FAILED)
115                         || notification.getOperationState().equals(OperationStateEnum.ROLLED_BACK));
116     }
117
118     private InlineResponse201 getVnfInstance(
119             final VnfLcmOperationOccurrenceNotification vnfLcmOperationOccurrenceNotification) {
120         return vnfmServiceProvider.getVnf(vnfLcmOperationOccurrenceNotification.getLinks().getVnfInstance().getHref())
121                 .get();
122     }
123
124 }