Merge "BUG 7494 : Idmanager returns the same Id from the same pool for different...
[genius.git] / interfacemanager / interfacemanager-impl / src / main / java / org / opendaylight / genius / interfacemanager / renderer / ovs / confighelpers / OvsInterfaceConfigUpdateHelper.java
1 /*
2  * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.genius.interfacemanager.renderer.ovs.confighelpers;
9
10 import com.google.common.util.concurrent.ListenableFuture;
11 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
12 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
13 import org.opendaylight.genius.datastoreutils.DataStoreJobCoordinator;
14 import org.opendaylight.genius.interfacemanager.IfmConstants;
15 import org.opendaylight.genius.interfacemanager.commons.AlivenessMonitorUtils;
16 import org.opendaylight.genius.interfacemanager.commons.InterfaceManagerCommonUtils;
17 import org.opendaylight.genius.interfacemanager.commons.InterfaceMetaUtils;
18 import org.opendaylight.genius.interfacemanager.renderer.ovs.utilities.SouthboundUtils;
19 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.AlivenessMonitorService;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406._interface.child.info.InterfaceParentEntry;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406._interface.child.info.InterfaceParentEntryKey;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406._interface.child.info._interface.parent.entry.InterfaceChildEntry;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406.bridge.ref.info.BridgeRefEntry;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.IfL2vlan;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.IfTunnel;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.ParentRefs;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 import java.util.ArrayList;
35 import java.util.List;
36 import java.util.concurrent.Callable;
37
38 public class OvsInterfaceConfigUpdateHelper{
39     private static final Logger LOG = LoggerFactory.getLogger(OvsInterfaceConfigUpdateHelper.class);
40
41     public static List<ListenableFuture<Void>> updateConfiguration(DataBroker dataBroker,  AlivenessMonitorService alivenessMonitorService,
42                                                                    IdManagerService idManager, IMdsalApiManager mdsalApiManager,
43                                                                    Interface interfaceNew, Interface interfaceOld) {
44         List<ListenableFuture<Void>> futures = new ArrayList<>();
45
46         // If any of the port attributes are modified, treat it as a delete and recreate scenario
47         if(portAttributesModified(interfaceOld, interfaceNew)) {
48             futures.addAll(OvsInterfaceConfigRemoveHelper.removeConfiguration(dataBroker, alivenessMonitorService, interfaceOld, idManager,
49                     mdsalApiManager, interfaceOld.getAugmentation(ParentRefs.class)));
50             futures.addAll(OvsInterfaceConfigAddHelper.addConfiguration(dataBroker,
51                     interfaceNew.getAugmentation(ParentRefs.class), interfaceNew, idManager,alivenessMonitorService,mdsalApiManager));
52             return futures;
53         }
54
55         // If there is no operational state entry for the interface, treat it as create
56         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState =
57                 InterfaceManagerCommonUtils.getInterfaceStateFromOperDS(interfaceNew.getName(), dataBroker);
58         if (ifState == null) {
59             futures.addAll(OvsInterfaceConfigAddHelper.addConfiguration(dataBroker,
60                     interfaceNew.getAugmentation(ParentRefs.class), interfaceNew, idManager, alivenessMonitorService, mdsalApiManager));
61             return futures;
62         }
63
64         WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
65         if(tunnelMonitoringAttributesModified(interfaceOld, interfaceNew)){
66             handleTunnelMonitorUpdates(futures, transaction, alivenessMonitorService, interfaceNew,
67                     interfaceOld, dataBroker);
68             return futures;
69         }
70
71         if (interfaceNew.isEnabled() != interfaceOld.isEnabled()) {
72             handleInterfaceAdminStateUpdates(futures, transaction, interfaceNew, dataBroker, ifState);
73         }
74
75         futures.add(transaction.submit());
76         return futures;
77     }
78
79     private static boolean portAttributesModified(Interface interfaceOld, Interface interfaceNew) {
80         ParentRefs parentRefsOld = interfaceOld.getAugmentation(ParentRefs.class);
81         ParentRefs parentRefsNew = interfaceNew.getAugmentation(ParentRefs.class);
82         if (checkAugmentations(parentRefsOld, parentRefsNew)) {
83             return true;
84         }
85
86         IfL2vlan ifL2vlanOld = interfaceOld.getAugmentation(IfL2vlan.class);
87         IfL2vlan ifL2vlanNew = interfaceNew.getAugmentation(IfL2vlan.class);
88         if (checkAugmentations(ifL2vlanOld, ifL2vlanNew)) {
89             return true;
90         }
91
92         IfTunnel ifTunnelOld = interfaceOld.getAugmentation(IfTunnel.class);
93         IfTunnel ifTunnelNew = interfaceNew.getAugmentation(IfTunnel.class);
94         if (checkAugmentations(ifTunnelOld,ifTunnelNew)) {
95             if(!ifTunnelNew.getTunnelDestination().equals(ifTunnelOld.getTunnelDestination()) ||
96                     !ifTunnelNew.getTunnelSource().equals(ifTunnelOld.getTunnelSource()) ||
97                     ifTunnelNew.getTunnelGateway() !=null && ifTunnelOld.getTunnelGateway() !=null &&
98                             !ifTunnelNew.getTunnelGateway().equals(ifTunnelOld.getTunnelGateway())) {
99                 return true;
100             }
101         }
102
103         return false;
104     }
105
106     private static boolean tunnelMonitoringAttributesModified(Interface interfaceOld, Interface interfaceNew) {
107         IfTunnel ifTunnelOld = interfaceOld.getAugmentation(IfTunnel.class);
108         IfTunnel ifTunnelNew = interfaceNew.getAugmentation(IfTunnel.class);
109         return checkAugmentations(ifTunnelOld, ifTunnelNew);
110     }
111
112     /*
113      * if the tunnel monitoring attributes have changed, handle it based on the tunnel type.
114      * As of now internal vxlan tunnels use LLDP monitoring and external tunnels use BFD monitoring.
115      */
116     private static void handleTunnelMonitorUpdates(List<ListenableFuture<Void>> futures, WriteTransaction transaction,
117             AlivenessMonitorService alivenessMonitorService, Interface interfaceNew, Interface interfaceOld,
118             DataBroker dataBroker) {
119         LOG.debug("tunnel monitoring attributes modified for interface {}", interfaceNew.getName());
120         // update termination point on switch, if switch is connected
121         BridgeRefEntry bridgeRefEntry =
122                 InterfaceMetaUtils.getBridgeReferenceForInterface(interfaceNew, dataBroker);
123         IfTunnel ifTunnel = interfaceNew.getAugmentation(IfTunnel.class);
124         if(SouthboundUtils.isMonitorProtocolBfd(ifTunnel) && InterfaceMetaUtils.bridgeExists(bridgeRefEntry, dataBroker)) {
125             SouthboundUtils.updateBfdParamtersForTerminationPoint(bridgeRefEntry.getBridgeReference().getValue(),
126                     interfaceNew.getAugmentation(IfTunnel.class),
127                     interfaceNew.getName(), transaction);
128         }else {
129             // update lldp tunnel monitoring attributes for an internal vxlan tunnel interface
130             AlivenessMonitorUtils.handleTunnelMonitorUpdates(alivenessMonitorService, dataBroker, interfaceOld, interfaceNew);
131         }
132         futures.add(transaction.submit());
133     }
134
135     private static void handleInterfaceAdminStateUpdates(List<ListenableFuture<Void>> futures, WriteTransaction transaction,
136                                                          Interface interfaceNew, DataBroker dataBroker,
137                                                          org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState){
138         OperStatus operStatus = InterfaceManagerCommonUtils.updateStateEntry(interfaceNew, dataBroker, transaction, ifState);
139
140         IfL2vlan ifL2vlan = interfaceNew.getAugmentation(IfL2vlan.class);
141         if (ifL2vlan == null || IfL2vlan.L2vlanMode.Trunk != ifL2vlan.getL2vlanMode() && IfL2vlan.L2vlanMode.Transparent != ifL2vlan.getL2vlanMode()) {
142             return;
143         }
144
145         InterfaceParentEntryKey interfaceParentEntryKey = new InterfaceParentEntryKey(interfaceNew.getName());
146         InterfaceParentEntry interfaceParentEntry =
147                 InterfaceMetaUtils.getInterfaceParentEntryFromConfigDS(interfaceParentEntryKey, dataBroker);
148         if (interfaceParentEntry == null || interfaceParentEntry.getInterfaceChildEntry() == null) {
149             return;
150         }
151
152         DataStoreJobCoordinator coordinator = DataStoreJobCoordinator.getInstance();
153         VlanMemberStateUpdateWorker vlanMemberStateUpdateWorker = new VlanMemberStateUpdateWorker(dataBroker,
154                 operStatus, interfaceParentEntry.getInterfaceChildEntry());
155         coordinator.enqueueJob(interfaceNew.getName(), vlanMemberStateUpdateWorker, IfmConstants.JOB_MAX_RETRIES);
156     }
157
158     private static <T> boolean checkAugmentations(T oldAug, T newAug) {
159         if (oldAug != null && newAug == null
160                 || oldAug == null && newAug != null) {
161             return true;
162         }
163
164         return newAug != null && !newAug.equals(oldAug);
165     }
166
167     private static class VlanMemberStateUpdateWorker implements Callable<List<ListenableFuture<Void>>> {
168
169         private final DataBroker dataBroker;
170         private final OperStatus operStatus;
171         private final List<InterfaceChildEntry> interfaceChildEntries;
172
173         VlanMemberStateUpdateWorker(DataBroker dataBroker, OperStatus operStatus,
174                 List<InterfaceChildEntry> interfaceChildEntries) {
175             this.dataBroker = dataBroker;
176             this.operStatus = operStatus;
177             this.interfaceChildEntries = interfaceChildEntries;
178         }
179
180         @Override
181         public List<ListenableFuture<Void>> call() throws Exception {
182             List<ListenableFuture<Void>> futures = new ArrayList<>();
183             WriteTransaction operShardTransaction = dataBroker.newWriteOnlyTransaction();
184             for (InterfaceChildEntry interfaceChildEntry : interfaceChildEntries) {
185                 InterfaceManagerCommonUtils.updateOperStatus(interfaceChildEntry.getChildInterface(), operStatus,
186                         operShardTransaction);
187             }
188
189             futures.add(operShardTransaction.submit());
190             return futures;
191         }
192
193         @Override
194         public String toString() {
195             return "VlanMemberStateUpdateWorker [operStatus=" + operStatus + ", interfaceChildEntries="
196                     + interfaceChildEntries + "]";
197         }
198     }
199 }
200