aboutsummaryrefslogtreecommitdiffstats
path: root/so-etsi-sol003-adapter-lcm/so-etsi-sol003-adapter-lcm-service/src/main/java/org/onap/so/adapters/etsisol003adapter/lcm/rest/Sol003LcnContoller.java
blob: 784fb8efa68c2c7af580003c7ed3ab17f6b4dd71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2019 Nordix Foundation.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.so.adapters.etsisol003adapter.lcm.rest;

import static org.onap.so.adapters.etsi.sol003.adapter.common.CommonConstants.BASE_URL;
import static org.onap.so.adapters.etsisol003adapter.lcm.LifeCycleManagementConstants.OPERATION_NOTIFICATION_ENDPOINT;
import static org.slf4j.LoggerFactory.getLogger;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.ws.rs.core.MediaType;
import org.onap.aai.domain.yang.EsrVnfm;
import org.onap.aai.domain.yang.GenericVnf;
import org.onap.so.adapters.etsisol003adapter.lcm.extclients.aai.AaiHelper;
import org.onap.so.adapters.etsisol003adapter.lcm.extclients.aai.AaiServiceProvider;
import org.onap.so.adapters.etsisol003adapter.lcm.extclients.vnfm.VnfmServiceProvider;
import org.onap.so.adapters.etsisol003adapter.lcm.extclients.vnfm.model.InlineResponse201;
import org.onap.so.adapters.etsisol003adapter.lcm.jobmanagement.JobManager;
import org.onap.so.adapters.etsisol003adapter.lcm.lcn.model.VnfIdentifierCreationNotification;
import org.onap.so.adapters.etsisol003adapter.lcm.lcn.model.VnfIdentifierDeletionNotification;
import org.onap.so.adapters.etsisol003adapter.lcm.lcn.model.VnfLcmOperationOccurrenceNotification;
import org.onap.so.adapters.etsisol003adapter.lcm.lcn.model.VnfLcmOperationOccurrenceNotification.OperationEnum;
import org.onap.so.adapters.etsisol003adapter.lcm.lcn.model.VnfLcmOperationOccurrenceNotification.OperationStateEnum;
import org.onap.so.adapters.etsisol003adapter.lcm.notificationhandling.NotificationHandler;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Controller for handling notifications from the VNFM (Virtual Network Function Manager).
 */
@Controller
@RequestMapping(value = BASE_URL, produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
        consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public class Sol003LcnContoller {
    private static Logger logger = getLogger(Sol003LcnContoller.class);
    private static final String LOG_LCN_RECEIVED = "LCN received from VNFM: ";
    private final AaiServiceProvider aaiServiceProvider;
    private final AaiHelper aaiHelper;
    private final VnfmServiceProvider vnfmServiceProvider;
    private final JobManager jobManager;
    private final ExecutorService executor = Executors.newCachedThreadPool();

    @Autowired
    Sol003LcnContoller(final AaiServiceProvider aaiServiceProvider, final AaiHelper aaiHelper,
            final VnfmServiceProvider vnfmServiceProvider, final JobManager jobManager) {
        this.aaiServiceProvider = aaiServiceProvider;
        this.aaiHelper = aaiHelper;
        this.vnfmServiceProvider = vnfmServiceProvider;
        this.jobManager = jobManager;
    }

    @PostMapping(value = "/lcn/VnfIdentifierCreationNotification")
    public ResponseEntity<Void> lcnVnfIdentifierCreationNotificationPost(
            @RequestBody final VnfIdentifierCreationNotification vnfIdentifierCreationNotification) {
        logger.info(LOG_LCN_RECEIVED + vnfIdentifierCreationNotification);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @PostMapping(value = "/lcn/VnfIdentifierDeletionNotification")
    public ResponseEntity<Void> lcnVnfIdentifierDeletionNotificationPost(
            @RequestBody final VnfIdentifierDeletionNotification vnfIdentifierDeletionNotification) {
        logger.info(LOG_LCN_RECEIVED + vnfIdentifierDeletionNotification);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @PostMapping(value = OPERATION_NOTIFICATION_ENDPOINT)
    public ResponseEntity<Void> lcnVnfLcmOperationOccurrenceNotificationPost(
            @RequestBody final VnfLcmOperationOccurrenceNotification vnfLcmOperationOccurrenceNotification) {
        logger.info(LOG_LCN_RECEIVED + vnfLcmOperationOccurrenceNotification);

        if (isANotificationOfInterest(vnfLcmOperationOccurrenceNotification)) {
            final InlineResponse201 vnfInstance = getVnfInstance(vnfLcmOperationOccurrenceNotification);
            final NotificationHandler handler = new NotificationHandler(vnfLcmOperationOccurrenceNotification,
                    aaiHelper, aaiServiceProvider, vnfmServiceProvider, jobManager, vnfInstance);
            executor.execute(handler);
        }

        logger.info("Sending notification response");
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    private boolean isANotificationOfInterest(final VnfLcmOperationOccurrenceNotification notification) {
        return isInstanitiateCompleted(notification) || isTerminateTerminalState(notification);
    }

    private boolean isInstanitiateCompleted(final VnfLcmOperationOccurrenceNotification notification) {
        return notification.getOperation().equals(OperationEnum.INSTANTIATE)
                && notification.getOperationState().equals(OperationStateEnum.COMPLETED);
    }

    private boolean isTerminateTerminalState(final VnfLcmOperationOccurrenceNotification notification) {
        return notification.getOperation().equals(OperationEnum.TERMINATE)
                && (notification.getOperationState().equals(OperationStateEnum.COMPLETED)
                        || notification.getOperationState().equals(OperationStateEnum.FAILED)
                        || notification.getOperationState().equals(OperationStateEnum.ROLLED_BACK));
    }

    private InlineResponse201 getVnfInstance(
            final VnfLcmOperationOccurrenceNotification vnfLcmOperationOccurrenceNotification) {
        final GenericVnf vnfInAai = aaiServiceProvider
                .invokeQueryGenericVnf(vnfLcmOperationOccurrenceNotification.getLinks().getVnfInstance().getHref())
                .getGenericVnf().get(0);
        final EsrVnfm vnfm = aaiHelper.getAssignedVnfm(vnfInAai);
        return vnfmServiceProvider
                .getVnf(vnfm, vnfLcmOperationOccurrenceNotification.getLinks().getVnfInstance().getHref()).get();
    }

}