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