Fix some security vulnerabilities
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / main / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / onap / direct / notification / AAINotificationProcessor.java
1 /*
2  * Copyright 2016-2017, Nokia Corporation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct.notification;
17
18 import com.nokia.cbam.lcm.v32.model.AffectedVirtualLink;
19 import com.nokia.cbam.lcm.v32.model.OperationExecution;
20 import com.nokia.cbam.lcm.v32.model.VnfLifecycleChangeNotification;
21 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.api.INotificationSender;
22 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.spring.Conditions;
23 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.notification.ReportedAffectedConnectionPoints;
24 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.notification.ReportedAffectedCp;
25 import org.slf4j.Logger;
26 import org.springframework.beans.factory.annotation.Autowired;
27 import org.springframework.context.annotation.Conditional;
28 import org.springframework.stereotype.Component;
29
30 import java.util.Collection;
31 import java.util.HashSet;
32 import java.util.Optional;
33 import java.util.Set;
34
35 import static com.google.common.collect.Iterables.filter;
36 import static com.google.common.collect.Iterables.tryFind;
37 import static com.nokia.cbam.lcm.v32.model.ChangeType.*;
38 import static com.nokia.cbam.lcm.v32.model.OperationStatus.STARTED;
39 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct.notification.LInterfaceManager.buildUrl;
40 import static org.slf4j.LoggerFactory.getLogger;
41 import static org.springframework.util.StringUtils.isEmpty;
42
43 /**
44  * Responsible for providing information related to the VNFM from VF-C source
45  */
46 @Component
47 @Conditional(value = Conditions.UseForDirect.class)
48 public class AAINotificationProcessor implements INotificationSender {
49     private static Logger logger = getLogger(AAINotificationProcessor.class);
50     private final GenericVnfManager genericVnfManager;
51     private final L3NetworkManager l3NetworkManager;
52     private final LInterfaceManager lInterfaceManager;
53     private final VnfcManager vnfcManager;
54     private final VserverManager vserverManager;
55
56     @Autowired
57     AAINotificationProcessor(GenericVnfManager genericVnfManager, L3NetworkManager l3NetworkManager, LInterfaceManager lInterfaceManager, VnfcManager vnfcManager, VserverManager vserverManager) {
58         this.genericVnfManager = genericVnfManager;
59         this.l3NetworkManager = l3NetworkManager;
60         this.lInterfaceManager = lInterfaceManager;
61         this.vnfcManager = vnfcManager;
62         this.vserverManager = vserverManager;
63     }
64
65     @Override
66     public void processNotification(VnfLifecycleChangeNotification receivedNotification, OperationExecution operationExecution, Optional<ReportedAffectedConnectionPoints> affectedConnectionPoints, String vimId) {
67         boolean inMaintenance = STARTED.equals(receivedNotification.getStatus());
68         genericVnfManager.createOrUpdate(receivedNotification.getVnfInstanceId(), inMaintenance);
69         addOrUpdateVls(receivedNotification, vimId);
70         addOrUpdateVnfcs(receivedNotification, vimId, inMaintenance);
71         processCps(receivedNotification, affectedConnectionPoints, vimId, inMaintenance);
72         removeVnfcs(receivedNotification, vimId);
73         removeVls(receivedNotification);
74         logger.info("Notification processed successfully");
75     }
76
77     private void removeVls(VnfLifecycleChangeNotification receivedNotification) {
78         for (AffectedVirtualLink removedVl : filter(receivedNotification.getAffectedVirtualLinks(), affectedVirtualLink -> affectedVirtualLink.getChangeType().equals(REMOVED))) {
79             l3NetworkManager.delete(receivedNotification.getVnfInstanceId(), removedVl);
80         }
81     }
82
83     private void removeVnfcs(VnfLifecycleChangeNotification receivedNotification, String vimId) {
84         for (com.nokia.cbam.lcm.v32.model.AffectedVnfc removedVnfc : filter(receivedNotification.getAffectedVnfcs(), vnfc -> REMOVED.equals(vnfc.getChangeType()))) {
85             vnfcManager.delete(receivedNotification.getVnfInstanceId(), removedVnfc);
86             vserverManager.delete(vimId, removedVnfc);
87         }
88     }
89
90     private void processCps(VnfLifecycleChangeNotification receivedNotification, Optional<ReportedAffectedConnectionPoints> affectedConnectionPoints, String vimId, boolean inMaintenance) {
91         if (affectedConnectionPoints.isPresent()) {
92             for (ReportedAffectedCp removedCp : collectCpsToBeDeleted(vimId, affectedConnectionPoints.get())) {
93                 lInterfaceManager.delete(vimId, removedCp);
94             }
95             //these can only be added or modified because if something is in the post CPS it can not be removed
96             //since it is present after the operation
97             for (ReportedAffectedCp affectedCp : affectedConnectionPoints.get().getPost()) {
98                 if (!isEmpty(affectedCp.getServerProviderId())) {
99                     lInterfaceManager.update(receivedNotification.getVnfInstanceId(), vimId, affectedCp, inMaintenance);
100                 } else {
101                     logger.warn("The changed {} connection point is not linked to any server", affectedCp.getCpId());
102                 }
103             }
104         } else {
105             logger.warn("The changed connection points are not present in VNF with {} identifier", receivedNotification.getVnfInstanceId());
106         }
107     }
108
109     private void addOrUpdateVnfcs(VnfLifecycleChangeNotification receivedNotification, String vimId, boolean inMaintenance) {
110         for (com.nokia.cbam.lcm.v32.model.AffectedVnfc affectedVnfc : receivedNotification.getAffectedVnfcs()) {
111             if (affectedVnfc.getChangeType() == MODIFIED || affectedVnfc.getChangeType() == ADDED) {
112                 vserverManager.update(vimId, receivedNotification.getVnfInstanceId(), affectedVnfc, receivedNotification.getAffectedVirtualStorages(), inMaintenance);
113                 vnfcManager.update(vimId, VserverManager.getTenantId(affectedVnfc), receivedNotification.getVnfInstanceId(), affectedVnfc, inMaintenance);
114             }
115         }
116     }
117
118     private void addOrUpdateVls(VnfLifecycleChangeNotification receivedNotification, String vimId) {
119         for (AffectedVirtualLink affectedVirtualLink : receivedNotification.getAffectedVirtualLinks()) {
120             if ((affectedVirtualLink.getChangeType() == MODIFIED) || (affectedVirtualLink.getChangeType() == ADDED)) {
121                 l3NetworkManager.update(vimId, receivedNotification.getVnfInstanceId(), affectedVirtualLink);
122             }
123         }
124     }
125
126     /**
127      * The ports that are present in the pre, but not present in the post are
128      * removed regardless of the "removed" flag being present in the pre, because
129      * that only signals the remove intention, but does not actually mean that
130      * the resource have been removed
131      */
132     private Collection<ReportedAffectedCp> collectCpsToBeDeleted(String vimId, ReportedAffectedConnectionPoints cps) {
133         Set<ReportedAffectedCp> cpsToRemove = new HashSet<>();
134         for (ReportedAffectedCp cpBeforeOperation : cps.getPre()) {
135             if (!isEmpty(cpBeforeOperation.getServerProviderId())) {
136                 String originalResource = buildUrl(vimId, cpBeforeOperation);
137                 if (!tryFind(cps.getPost(), cpAfterOperation -> originalResource.equals(buildUrl(vimId, cpAfterOperation))).isPresent()) {
138                     cpsToRemove.add(cpBeforeOperation);
139                 }
140             }
141         }
142         return cpsToRemove;
143     }
144 }