Bump versions by 0.1.0 for next dev cycle
[vpnservice.git] / interfacemgr / interfacemgr-impl / src / main / java / org / opendaylight / vpnservice / interfacemgr / renderer / ovs / confighelpers / OvsInterfaceConfigUpdateHelper.java
1 /*
2  * Copyright (c) 2015 - 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.vpnservice.interfacemgr.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.vpnservice.interfacemgr.commons.AlivenessMonitorUtils;
14 import org.opendaylight.vpnservice.interfacemgr.commons.InterfaceManagerCommonUtils;
15 import org.opendaylight.vpnservice.interfacemgr.commons.InterfaceMetaUtils;
16 import org.opendaylight.vpnservice.interfacemgr.renderer.ovs.utilities.SouthboundUtils;
17 import org.opendaylight.vpnservice.mdsalutil.interfaces.IMdsalApiManager;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.alivenessmonitor.rev150629.AlivenessMonitorService;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.IdManagerService;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._interface.child.info.InterfaceParentEntry;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._interface.child.info.InterfaceParentEntryKey;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._interface.child.info._interface.parent.entry.InterfaceChildEntry;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.bridge.ref.info.BridgeRefEntry;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfL2vlan;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfTunnel;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.ParentRefs;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 import java.util.ArrayList;
33 import java.util.List;
34
35 public class OvsInterfaceConfigUpdateHelper{
36     private static final Logger LOG = LoggerFactory.getLogger(OvsInterfaceConfigUpdateHelper.class);
37
38     public static List<ListenableFuture<Void>> updateConfiguration(DataBroker dataBroker,  AlivenessMonitorService alivenessMonitorService,
39                                                                    IdManagerService idManager, IMdsalApiManager mdsalApiManager,
40                                                                    Interface interfaceNew, Interface interfaceOld) {
41         List<ListenableFuture<Void>> futures = new ArrayList<>();
42
43         // If any of the port attributes are modified, treat it as a delete and recreate scenario
44         if(portAttributesModified(interfaceOld, interfaceNew)) {
45             futures.addAll(OvsInterfaceConfigRemoveHelper.removeConfiguration(dataBroker, alivenessMonitorService, interfaceOld, idManager,
46                     mdsalApiManager, interfaceOld.getAugmentation(ParentRefs.class)));
47             futures.addAll(OvsInterfaceConfigAddHelper.addConfiguration(dataBroker,
48                     interfaceNew.getAugmentation(ParentRefs.class), interfaceNew, idManager,alivenessMonitorService,mdsalApiManager));
49             return futures;
50         }
51
52         // If there is no operational state entry for the interface, treat it as create
53         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState =
54                 InterfaceManagerCommonUtils.getInterfaceStateFromOperDS(interfaceNew.getName(), dataBroker);
55         if (ifState == null) {
56             futures.addAll(OvsInterfaceConfigAddHelper.addConfiguration(dataBroker,
57                     interfaceNew.getAugmentation(ParentRefs.class), interfaceNew, idManager, alivenessMonitorService, mdsalApiManager));
58             return futures;
59         }
60
61         WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
62         if(TunnelMonitoringAttributesModified(interfaceOld, interfaceNew)){
63             handleTunnelMonitorUpdates(futures, transaction, alivenessMonitorService, interfaceNew,
64                     interfaceOld, dataBroker);
65             return futures;
66         }
67
68         if (interfaceNew.isEnabled() != interfaceOld.isEnabled()) {
69             handleInterfaceAdminStateUpdates(futures, transaction, interfaceNew, dataBroker, ifState);
70         }
71
72         futures.add(transaction.submit());
73         return futures;
74     }
75
76     private static boolean portAttributesModified(Interface interfaceOld, Interface interfaceNew) {
77         ParentRefs parentRefsOld = interfaceOld.getAugmentation(ParentRefs.class);
78         ParentRefs parentRefsNew = interfaceNew.getAugmentation(ParentRefs.class);
79         if (checkAugmentations(parentRefsOld, parentRefsNew)) {
80             return true;
81         }
82
83         IfL2vlan ifL2vlanOld = interfaceOld.getAugmentation(IfL2vlan.class);
84         IfL2vlan ifL2vlanNew = interfaceNew.getAugmentation(IfL2vlan.class);
85         if (checkAugmentations(ifL2vlanOld, ifL2vlanNew)) {
86             return true;
87         }
88
89         IfTunnel ifTunnelOld = interfaceOld.getAugmentation(IfTunnel.class);
90         IfTunnel ifTunnelNew = interfaceNew.getAugmentation(IfTunnel.class);
91         if (checkAugmentations(ifTunnelOld, ifTunnelNew)) {
92             if(!ifTunnelNew.getTunnelDestination().equals(ifTunnelOld.getTunnelDestination()) ||
93                     !ifTunnelNew.getTunnelSource().equals(ifTunnelOld.getTunnelSource()) ||
94                     !ifTunnelNew.getTunnelGateway().equals(ifTunnelOld.getTunnelGateway())) {
95                 return true;
96             }
97         }
98
99         return false;
100     }
101
102     private static boolean TunnelMonitoringAttributesModified(Interface interfaceOld, Interface interfaceNew) {
103         IfTunnel ifTunnelOld = interfaceOld.getAugmentation(IfTunnel.class);
104         IfTunnel ifTunnelNew = interfaceNew.getAugmentation(IfTunnel.class);
105         if (checkAugmentations(ifTunnelOld, ifTunnelNew)) {
106             return true;
107         }
108         return false;
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,
117                                                    Interface interfaceNew, Interface interfaceOld, 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         if(InterfaceMetaUtils.bridgeExists(bridgeRefEntry, dataBroker)) {
123             SouthboundUtils.updateBfdParamtersForTerminationPoint(bridgeRefEntry.getBridgeReference().getValue(),
124                     interfaceNew.getAugmentation(IfTunnel.class),
125                     interfaceNew.getName(), transaction);
126         }
127
128         // stop tunnel monitoring if admin state is disabled for an internal vxlan trunk interface
129         if(interfaceOld.isEnabled() && !interfaceNew.isEnabled()) {
130             AlivenessMonitorUtils.stopLLDPMonitoring(alivenessMonitorService, dataBroker, interfaceNew);
131         }else{
132             AlivenessMonitorUtils.handleTunnelMonitorUpdates(alivenessMonitorService, dataBroker, interfaceOld, interfaceNew);
133         }
134         futures.add(transaction.submit());
135     }
136
137     private static void handleInterfaceAdminStateUpdates(List<ListenableFuture<Void>> futures, WriteTransaction transaction,
138                                                          Interface interfaceNew, DataBroker dataBroker,
139                                                          org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState){
140         OperStatus operStatus = InterfaceManagerCommonUtils.updateStateEntry(interfaceNew, dataBroker, transaction, ifState);
141
142         IfL2vlan ifL2vlan = interfaceNew.getAugmentation(IfL2vlan.class);
143         if (ifL2vlan == null || (IfL2vlan.L2vlanMode.Trunk != ifL2vlan.getL2vlanMode() && IfL2vlan.L2vlanMode.Transparent != ifL2vlan.getL2vlanMode())) {
144             futures.add(transaction.submit());
145             return;
146         }
147
148         InterfaceParentEntryKey interfaceParentEntryKey = new InterfaceParentEntryKey(interfaceNew.getName());
149         InterfaceParentEntry interfaceParentEntry =
150                 InterfaceMetaUtils.getInterfaceParentEntryFromConfigDS(interfaceParentEntryKey, dataBroker);
151         if (interfaceParentEntry == null || interfaceParentEntry.getInterfaceChildEntry() == null) {
152             futures.add(transaction.submit());
153             return;
154         }
155
156         for (InterfaceChildEntry interfaceChildEntry : interfaceParentEntry.getInterfaceChildEntry()) {
157             InterfaceManagerCommonUtils.updateOperStatus(interfaceChildEntry.getChildInterface(), operStatus, transaction);
158         }
159     }
160
161     private static<T> boolean checkAugmentations(T oldAug, T newAug) {
162         if ((oldAug != null && newAug == null) ||
163                 (oldAug == null && newAug != null)) {
164             return true;
165         }
166
167         if (newAug != null && oldAug != null && !newAug.equals(oldAug)) {
168             return true;
169         }
170
171         return false;
172     }
173
174 }