2 * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
8 package org.opendaylight.vpnservice.interfacemgr.renderer.ovs.statehelpers;
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;
32 import java.util.ArrayList;
33 import java.util.List;
35 public class OvsInterfaceStateUpdateHelper {
36 private static final Logger LOG = LoggerFactory.getLogger(OvsInterfaceStateUpdateHelper.class);
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();
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();
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();
59 boolean opstateModified = false;
60 boolean adminStateModified = false;
61 boolean hardwareAddressModified = false;
62 if (!operStatusNew.equals(operStatusOld)) {
63 opstateModified = true;
65 if (!adminStatusNew.equals(adminStatusOld)) {
66 adminStateModified = true;
68 if (!macAddressNew.equals(macAddressOld)) {
69 hardwareAddressModified = true;
72 if (!opstateModified && !adminStateModified && !hardwareAddressModified) {
73 LOG.debug("If State entry for port: {} Not Modified.", portName);
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);
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;
91 ifaceBuilder.setOperStatus(operStatusNew);
95 if (adminStateModified) {
96 LOG.debug("Admin state Modified for Port: {}", portName);
97 ifaceBuilder.setAdminStatus(adminStatusNew);
101 if (hardwareAddressModified) {
102 LOG.debug("Hw-Address Modified for Port: {}", portName);
103 PhysAddress physAddress = new PhysAddress(macAddressNew.getValue());
104 ifaceBuilder.setPhysAddress(physAddress);
108 /* FIXME: Is there chance that lower layer node-connector info is updated.
109 Not Considering for now.
113 ifaceBuilder.setKey(IfmUtil.getStateInterfaceKeyFromName(portName));
114 t.merge(LogicalDatastoreType.OPERATIONAL, ifStateId, ifaceBuilder.build());
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);
125 AlivenessMonitorUtils.startLLDPMonitoring(alivenessMonitorService,dataBroker, iface);
129 List<InterfaceChildEntry> interfaceChildEntries = interfaceParentEntry.getInterfaceChildEntry();
130 if (interfaceChildEntries == null) {
131 futures.add(t.submit());
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());
144 futures.add(t.submit());