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