BUG:5042 LLDP Tunnel Monitoring should update the interface operational
[vpnservice.git] / interfacemgr / interfacemgr-impl / src / main / java / org / opendaylight / vpnservice / interfacemgr / renderer / ovs / statehelpers / OvsInterfaceStateUpdateHelper.java
1 /*
2  * Copyright (c) 2015 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.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.vpnservice.interfacemgr.IfmUtil;
15 import org.opendaylight.vpnservice.interfacemgr.commons.AlivenessMonitorUtils;
16 import org.opendaylight.vpnservice.interfacemgr.commons.InterfaceManagerCommonUtils;
17 import org.opendaylight.vpnservice.interfacemgr.commons.InterfaceMetaUtils;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceBuilder;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.alivenessmonitor.rev150629.AlivenessMonitorService;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._interface.child.info.InterfaceParentEntry;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._interface.child.info.InterfaceParentEntryKey;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._interface.child.info._interface.parent.entry.InterfaceChildEntry;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
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 OvsInterfaceStateUpdateHelper {
36     private static final Logger LOG = LoggerFactory.getLogger(OvsInterfaceStateUpdateHelper.class);
37
38     public static List<ListenableFuture<Void>> updateState(InstanceIdentifier<FlowCapableNodeConnector> key,
39                                                            AlivenessMonitorService alivenessMonitorService,
40                                                            DataBroker dataBroker, String portName,
41                                                            FlowCapableNodeConnector flowCapableNodeConnectorNew,
42                                                            FlowCapableNodeConnector flowCapableNodeConnectorOld) {
43         LOG.debug("Update of Interface State for port: {}", portName);
44         List<ListenableFuture<Void>> futures = new ArrayList<>();
45         WriteTransaction t = dataBroker.newWriteOnlyTransaction();
46
47         Interface.OperStatus operStatusNew =
48                 flowCapableNodeConnectorNew.getState().isLinkDown() ? Interface.OperStatus.Down : Interface.OperStatus.Up;
49         Interface.AdminStatus adminStatusNew =
50                 flowCapableNodeConnectorNew.getState().isBlocked() ? Interface.AdminStatus.Down : Interface.AdminStatus.Up;
51         MacAddress macAddressNew = flowCapableNodeConnectorNew.getHardwareAddress();
52
53         Interface.OperStatus operStatusOld =
54                 flowCapableNodeConnectorOld.getState().isLinkDown() ? Interface.OperStatus.Down : Interface.OperStatus.Up;
55         Interface.AdminStatus adminStatusOld =
56                 flowCapableNodeConnectorOld.getState().isBlocked() ? Interface.AdminStatus.Down : Interface.AdminStatus.Up;
57         MacAddress macAddressOld = flowCapableNodeConnectorOld.getHardwareAddress();
58
59         boolean opstateModified = false;
60         boolean adminStateModified = false;
61         boolean hardwareAddressModified = false;
62         if (!operStatusNew.equals(operStatusOld)) {
63             opstateModified = true;
64         }
65         if (!adminStatusNew.equals(adminStatusOld)) {
66             adminStateModified = true;
67         }
68         if (!macAddressNew.equals(macAddressOld)) {
69             hardwareAddressModified = true;
70         }
71
72         if (!opstateModified && !adminStateModified && !hardwareAddressModified) {
73             LOG.debug("If State entry for port: {} Not Modified.", portName);
74             return futures;
75         }
76
77         InstanceIdentifier<Interface> ifStateId = IfmUtil.buildStateInterfaceId(portName);
78         InterfaceBuilder ifaceBuilder = new InterfaceBuilder();
79         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface iface = null;
80         boolean modified = false;
81         if (opstateModified) {
82             LOG.debug("Opstate Modified for Port: {}", portName);
83             InterfaceKey interfaceKey = new InterfaceKey(portName);
84              iface = InterfaceManagerCommonUtils.getInterfaceFromConfigDS(interfaceKey, dataBroker);
85
86             // If interface config admin state is disabled, set operstate of the Interface State entity to Down.
87             if (iface != null && !iface.isEnabled()) {
88                 operStatusNew = Interface.OperStatus.Down;
89             }
90
91             ifaceBuilder.setOperStatus(operStatusNew);
92             modified = true;
93         }
94
95         if (adminStateModified) {
96             LOG.debug("Admin state Modified for Port: {}", portName);
97             ifaceBuilder.setAdminStatus(adminStatusNew);
98             modified = true;
99         }
100
101         if (hardwareAddressModified) {
102             LOG.debug("Hw-Address Modified for Port: {}", portName);
103             PhysAddress physAddress = new PhysAddress(macAddressNew.getValue());
104             ifaceBuilder.setPhysAddress(physAddress);
105             modified = true;
106         }
107
108         /* FIXME: Is there chance that lower layer node-connector info is updated.
109                   Not Considering for now.
110          */
111
112         if (modified) {
113             ifaceBuilder.setKey(IfmUtil.getStateInterfaceKeyFromName(portName));
114             t.merge(LogicalDatastoreType.OPERATIONAL, ifStateId, ifaceBuilder.build());
115
116             InterfaceParentEntryKey interfaceParentEntryKey = new InterfaceParentEntryKey(portName);
117             InterfaceParentEntry interfaceParentEntry =
118                     InterfaceMetaUtils.getInterfaceParentEntryFromConfigDS(interfaceParentEntryKey, dataBroker);
119             if (interfaceParentEntry == null) {
120                 futures.add(t.submit());
121                 // start/stop monitoring based on opState
122                 if(operStatusNew == Interface.OperStatus.Down )
123                     AlivenessMonitorUtils.stopLLDPMonitoring(alivenessMonitorService, dataBroker, iface);
124                 else
125                     AlivenessMonitorUtils.startLLDPMonitoring(alivenessMonitorService,dataBroker, iface);
126                 return futures;
127             }
128
129             List<InterfaceChildEntry> interfaceChildEntries = interfaceParentEntry.getInterfaceChildEntry();
130             if (interfaceChildEntries == null) {
131                 futures.add(t.submit());
132                 return futures;
133             }
134
135             LOG.debug("Updating if-state entries for Vlan-Trunk Members for port: {}", portName);
136             //FIXME: If the no. of child entries exceeds 100, perform txn updates in batches of 100.
137             for (InterfaceChildEntry interfaceChildEntry : interfaceChildEntries) {
138                 InstanceIdentifier<Interface> ifChildStateId =
139                         IfmUtil.buildStateInterfaceId(interfaceChildEntry.getChildInterface());
140                 t.merge(LogicalDatastoreType.OPERATIONAL, ifChildStateId, ifaceBuilder.build());
141             }
142         }
143
144         futures.add(t.submit());
145         return futures;
146     }
147 }