Merge "ITM: Fix for tunnel ports not getting deleted."
[genius.git] / interfacemanager / interfacemanager-impl / src / main / java / org / opendaylight / genius / interfacemanager / renderer / ovs / statehelpers / OvsInterfaceStateUpdateHelper.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.statehelpers;
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.controller.md.sal.common.api.data.LogicalDatastoreType;
14 import org.opendaylight.genius.interfacemanager.IfmUtil;
15 import org.opendaylight.genius.interfacemanager.commons.AlivenessMonitorUtils;
16 import org.opendaylight.genius.interfacemanager.commons.InterfaceManagerCommonUtils;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceBuilder;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.AlivenessMonitorService;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.IfTunnel;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 import java.util.ArrayList;
30 import java.util.List;
31
32 public class OvsInterfaceStateUpdateHelper {
33     private static final Logger LOG = LoggerFactory.getLogger(OvsInterfaceStateUpdateHelper.class);
34
35     public static List<ListenableFuture<Void>> updateState(InstanceIdentifier<FlowCapableNodeConnector> key,
36                                                            AlivenessMonitorService alivenessMonitorService,
37                                                            DataBroker dataBroker, String interfaceName,
38                                                            FlowCapableNodeConnector flowCapableNodeConnectorNew,
39                                                            FlowCapableNodeConnector flowCapableNodeConnectorOld) {
40         LOG.debug("Update of Interface State for port: {}", interfaceName);
41         List<ListenableFuture<Void>> futures = new ArrayList<>();
42         WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
43
44         Interface.OperStatus operStatusNew = getOpState(flowCapableNodeConnectorNew);
45         MacAddress macAddressNew = flowCapableNodeConnectorNew.getHardwareAddress();
46
47         Interface.OperStatus operStatusOld = getOpState(flowCapableNodeConnectorOld);
48         MacAddress macAddressOld = flowCapableNodeConnectorOld.getHardwareAddress();
49
50         boolean opstateModified = false;
51         boolean hardwareAddressModified = false;
52         if (!operStatusNew.equals(operStatusOld)) {
53             opstateModified = true;
54         }
55         if (!macAddressNew.equals(macAddressOld)) {
56             hardwareAddressModified = true;
57         }
58
59         if (!opstateModified && !hardwareAddressModified) {
60             LOG.debug("If State entry for port: {} Not Modified.", interfaceName);
61             return futures;
62         }
63
64         InterfaceBuilder ifaceBuilder = new InterfaceBuilder();
65         if (hardwareAddressModified) {
66             LOG.debug("Hw-Address Modified for Port: {}", interfaceName);
67             PhysAddress physAddress = new PhysAddress(macAddressNew.getValue());
68             ifaceBuilder.setPhysAddress(physAddress);
69         }
70
71         // modify the attributes in interface operational DS
72         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface iface =
73                 handleInterfaceStateUpdates(interfaceName, transaction, dataBroker,
74                         ifaceBuilder, opstateModified, flowCapableNodeConnectorNew.getName(), operStatusNew);
75
76         // start/stop monitoring based on opState
77         if(modifyTunnel(iface, opstateModified)){
78             handleTunnelMonitoringUpdates(alivenessMonitorService, dataBroker, iface.getAugmentation(IfTunnel.class),
79                     iface.getName(), operStatusNew);
80         }
81
82         futures.add(transaction.submit());
83         return futures;
84     }
85
86     public static void updateInterfaceStateOnNodeRemove(String interfaceName, FlowCapableNodeConnector flowCapableNodeConnector,
87                                                         DataBroker dataBroker, AlivenessMonitorService alivenessMonitorService,
88                                                         WriteTransaction transaction){
89         LOG.debug("Updating interface oper-status to UNKNOWN for : {}", interfaceName);
90
91         InterfaceBuilder ifaceBuilder = new InterfaceBuilder();
92         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface iface =
93                 handleInterfaceStateUpdates(interfaceName,transaction, dataBroker,
94                         ifaceBuilder, true, flowCapableNodeConnector.getName(),
95                         Interface.OperStatus.Unknown);
96         if (InterfaceManagerCommonUtils.isTunnelInterface(iface)){
97             handleTunnelMonitoringUpdates(alivenessMonitorService, dataBroker, iface.getAugmentation(IfTunnel.class),
98                     interfaceName, Interface.OperStatus.Unknown);
99         }
100     }
101
102     public static Interface.OperStatus getOpState(FlowCapableNodeConnector flowCapableNodeConnector){
103         Interface.OperStatus operStatus =
104                 (flowCapableNodeConnector.getState().isLive() &&
105                         !flowCapableNodeConnector.getConfiguration().isPORTDOWN())
106                         ? Interface.OperStatus.Up: Interface.OperStatus.Down;
107         return operStatus;
108     }
109
110     public static org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface
111     handleInterfaceStateUpdates(String interfaceName, WriteTransaction transaction,
112                                 DataBroker dataBroker, InterfaceBuilder ifaceBuilder, boolean opStateModified,
113                                 String portName,
114                                 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus opState){
115         LOG.debug("updating interface state entry for {}", interfaceName);
116         InstanceIdentifier<Interface> ifStateId = IfmUtil.buildStateInterfaceId(interfaceName);
117         ifaceBuilder.setKey(new InterfaceKey(interfaceName));
118         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface iface =
119                 InterfaceManagerCommonUtils.getInterfaceFromConfigDS(interfaceName, dataBroker);
120         // if interface config DS is null, do the update only for the lower-layer-interfaces
121         // which have no corresponding config entries
122         if(iface == null && interfaceName != portName){
123             return null;
124         }
125         if (modifyOpState(iface, opStateModified)) {
126             LOG.debug("updating interface oper status as {} for {}", opState.name(), interfaceName);
127             ifaceBuilder.setOperStatus(opState);
128         }
129         transaction.merge(LogicalDatastoreType.OPERATIONAL, ifStateId, ifaceBuilder.build(), false);
130
131         return iface;
132     }
133
134     public static void handleTunnelMonitoringUpdates(AlivenessMonitorService alivenessMonitorService, DataBroker dataBroker,
135                                                      IfTunnel ifTunnel, String interfaceName,
136                                                      Interface.OperStatus operStatus){
137
138         LOG.debug("handling tunnel monitoring updates for {} due to opstate modification", interfaceName);
139         if (operStatus == Interface.OperStatus.Down || operStatus == Interface.OperStatus.Unknown)
140             AlivenessMonitorUtils.stopLLDPMonitoring(alivenessMonitorService, dataBroker, ifTunnel, interfaceName);
141         else
142             AlivenessMonitorUtils.startLLDPMonitoring(alivenessMonitorService, dataBroker, ifTunnel, interfaceName);
143     }
144
145     public static boolean modifyOpState(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface iface,
146                                         boolean opStateModified){
147         return (opStateModified && (iface == null || iface != null && iface.isEnabled()));
148     }
149
150     public static boolean modifyTunnel(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface iface,
151                                        boolean opStateModified){
152         return modifyOpState(iface, opStateModified) && iface != null && iface.getAugmentation(IfTunnel.class) != null;
153     }
154 }