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