Merge "Enforce checkstyle validations"
[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 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,  AlivenessMonitorService alivenessMonitorService,
41                                                                    IdManagerService idManager, IMdsalApiManager mdsalApiManager,
42                                                                    Interface interfaceNew, Interface interfaceOld) {
43         List<ListenableFuture<Void>> futures = new ArrayList<>();
44
45         // If any of the port attributes are modified, treat it as a delete and recreate scenario
46         if(portAttributesModified(interfaceOld, interfaceNew)) {
47             futures.addAll(OvsInterfaceConfigRemoveHelper.removeConfiguration(dataBroker, alivenessMonitorService, interfaceOld, idManager,
48                     mdsalApiManager, interfaceOld.getAugmentation(ParentRefs.class)));
49             futures.addAll(OvsInterfaceConfigAddHelper.addConfiguration(dataBroker,
50                     interfaceNew.getAugmentation(ParentRefs.class), interfaceNew, idManager,alivenessMonitorService,mdsalApiManager));
51             return futures;
52         }
53
54         // If there is no operational state entry for the interface, treat it as create
55         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState =
56                 InterfaceManagerCommonUtils.getInterfaceStateFromOperDS(interfaceNew.getName(), dataBroker);
57         if (ifState == null) {
58             futures.addAll(OvsInterfaceConfigAddHelper.addConfiguration(dataBroker,
59                     interfaceNew.getAugmentation(ParentRefs.class), interfaceNew, idManager, alivenessMonitorService, mdsalApiManager));
60             return futures;
61         }
62
63         WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
64         if(tunnelMonitoringAttributesModified(interfaceOld, interfaceNew)){
65             handleTunnelMonitorUpdates(futures, transaction, alivenessMonitorService, interfaceNew,
66                     interfaceOld, dataBroker);
67             return futures;
68         }
69
70         if (interfaceNew.isEnabled() != interfaceOld.isEnabled()) {
71             handleInterfaceAdminStateUpdates(futures, transaction, interfaceNew, dataBroker, ifState);
72         }
73
74         futures.add(transaction.submit());
75         return futures;
76     }
77
78     private static boolean portAttributesModified(Interface interfaceOld, Interface interfaceNew) {
79         ParentRefs parentRefsOld = interfaceOld.getAugmentation(ParentRefs.class);
80         ParentRefs parentRefsNew = interfaceNew.getAugmentation(ParentRefs.class);
81         if (checkAugmentations(parentRefsOld, parentRefsNew)) {
82             return true;
83         }
84
85         IfL2vlan ifL2vlanOld = interfaceOld.getAugmentation(IfL2vlan.class);
86         IfL2vlan ifL2vlanNew = interfaceNew.getAugmentation(IfL2vlan.class);
87         if (checkAugmentations(ifL2vlanOld, ifL2vlanNew)) {
88             return true;
89         }
90
91         IfTunnel ifTunnelOld = interfaceOld.getAugmentation(IfTunnel.class);
92         IfTunnel ifTunnelNew = interfaceNew.getAugmentation(IfTunnel.class);
93         if (checkAugmentations(ifTunnelOld,ifTunnelNew)) {
94             if(!ifTunnelNew.getTunnelDestination().equals(ifTunnelOld.getTunnelDestination()) ||
95                     !ifTunnelNew.getTunnelSource().equals(ifTunnelOld.getTunnelSource()) ||
96                     ifTunnelNew.getTunnelGateway() !=null && ifTunnelOld.getTunnelGateway() !=null &&
97                             !ifTunnelNew.getTunnelGateway().equals(ifTunnelOld.getTunnelGateway())) {
98                 return true;
99             }
100         }
101
102         return false;
103     }
104
105     private static boolean tunnelMonitoringAttributesModified(Interface interfaceOld, Interface interfaceNew) {
106         IfTunnel ifTunnelOld = interfaceOld.getAugmentation(IfTunnel.class);
107         IfTunnel ifTunnelNew = interfaceNew.getAugmentation(IfTunnel.class);
108         return checkAugmentations(ifTunnelOld, ifTunnelNew);
109     }
110
111     /*
112      * if the tunnel monitoring attributes have changed, handle it based on the tunnel type.
113      * As of now internal vxlan tunnels use LLDP monitoring and external tunnels use BFD monitoring.
114      */
115     private static void handleTunnelMonitorUpdates(List<ListenableFuture<Void>> futures, WriteTransaction transaction,
116             AlivenessMonitorService alivenessMonitorService, Interface interfaceNew, Interface interfaceOld,
117             DataBroker dataBroker) {
118         LOG.debug("tunnel monitoring attributes modified for interface {}", interfaceNew.getName());
119         // update termination point on switch, if switch is connected
120         BridgeRefEntry bridgeRefEntry =
121                 InterfaceMetaUtils.getBridgeReferenceForInterface(interfaceNew, dataBroker);
122         IfTunnel ifTunnel = interfaceNew.getAugmentation(IfTunnel.class);
123         if(SouthboundUtils.isMonitorProtocolBfd(ifTunnel) && InterfaceMetaUtils.bridgeExists(bridgeRefEntry, dataBroker)) {
124             SouthboundUtils.updateBfdParamtersForTerminationPoint(bridgeRefEntry.getBridgeReference().getValue(),
125                     interfaceNew.getAugmentation(IfTunnel.class),
126                     interfaceNew.getName(), transaction);
127         }else {
128             // update lldp tunnel monitoring attributes for an internal vxlan tunnel interface
129             AlivenessMonitorUtils.handleTunnelMonitorUpdates(alivenessMonitorService, dataBroker, interfaceOld, interfaceNew);
130         }
131         futures.add(transaction.submit());
132     }
133
134     private static void handleInterfaceAdminStateUpdates(List<ListenableFuture<Void>> futures, WriteTransaction transaction,
135                                                          Interface interfaceNew, DataBroker dataBroker,
136                                                          org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState){
137         OperStatus operStatus = InterfaceManagerCommonUtils.updateStateEntry(interfaceNew, dataBroker, transaction, ifState);
138
139         IfL2vlan ifL2vlan = interfaceNew.getAugmentation(IfL2vlan.class);
140         if (ifL2vlan == null || IfL2vlan.L2vlanMode.Trunk != ifL2vlan.getL2vlanMode() && IfL2vlan.L2vlanMode.Transparent != ifL2vlan.getL2vlanMode()) {
141             return;
142         }
143
144         InterfaceParentEntryKey interfaceParentEntryKey = new InterfaceParentEntryKey(interfaceNew.getName());
145         InterfaceParentEntry interfaceParentEntry =
146                 InterfaceMetaUtils.getInterfaceParentEntryFromConfigDS(interfaceParentEntryKey, dataBroker);
147         if (interfaceParentEntry == null || interfaceParentEntry.getInterfaceChildEntry() == null) {
148             return;
149         }
150
151         DataStoreJobCoordinator coordinator = DataStoreJobCoordinator.getInstance();
152         VlanMemberStateUpdateWorker vlanMemberStateUpdateWorker = new VlanMemberStateUpdateWorker(dataBroker,
153                 operStatus, interfaceParentEntry.getInterfaceChildEntry());
154         coordinator.enqueueJob(interfaceNew.getName(), vlanMemberStateUpdateWorker, IfmConstants.JOB_MAX_RETRIES);
155     }
156
157     private static <T> boolean checkAugmentations(T oldAug, T newAug) {
158         if (oldAug != null && newAug == null
159                 || oldAug == null && newAug != null) {
160             return true;
161         }
162
163         return newAug != null && !newAug.equals(oldAug);
164     }
165
166     private static class VlanMemberStateUpdateWorker implements Callable<List<ListenableFuture<Void>>> {
167
168         private final DataBroker dataBroker;
169         private final OperStatus operStatus;
170         private final List<InterfaceChildEntry> interfaceChildEntries;
171
172         VlanMemberStateUpdateWorker(DataBroker dataBroker, OperStatus operStatus,
173                 List<InterfaceChildEntry> interfaceChildEntries) {
174             this.dataBroker = dataBroker;
175             this.operStatus = operStatus;
176             this.interfaceChildEntries = interfaceChildEntries;
177         }
178
179         @Override
180         public List<ListenableFuture<Void>> call() throws Exception {
181             List<ListenableFuture<Void>> futures = new ArrayList<>();
182             WriteTransaction operShardTransaction = dataBroker.newWriteOnlyTransaction();
183             for (InterfaceChildEntry interfaceChildEntry : interfaceChildEntries) {
184                 InterfaceManagerCommonUtils.updateOperStatus(interfaceChildEntry.getChildInterface(), operStatus,
185                         operShardTransaction);
186             }
187
188             futures.add(operShardTransaction.submit());
189             return futures;
190         }
191
192         @Override
193         public String toString() {
194             return "VlanMemberStateUpdateWorker [operStatus=" + operStatus + ", interfaceChildEntries="
195                     + interfaceChildEntries + "]";
196         }
197     }
198 }
199