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.idmanager.IdManager;
15 import org.opendaylight.vpnservice.interfacemgr.IfmConstants;
16 import org.opendaylight.vpnservice.interfacemgr.IfmUtil;
17 import org.opendaylight.vpnservice.interfacemgr.commons.InterfaceManagerCommonUtils;
18 import org.opendaylight.vpnservice.interfacemgr.commons.InterfaceMetaUtils;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceBuilder;
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.inventory.rev130819.NodeConnectorId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.IdManagerService;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._interface.child.info.InterfaceParentEntry;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._interface.child.info.InterfaceParentEntryKey;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._interface.child.info._interface.parent.entry.InterfaceChildEntry;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfL2vlan;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 import java.util.ArrayList;
35 import java.util.List;
38 * This worker is responsible for adding the openflow-interfaces/of-port-info container
39 * in odl-interface-openflow yang.
41 * Create the entries in Interface-State OperDS.
42 * Create the entries in Inventory OperDS.
45 public class OvsInterfaceStateAddHelper {
46 private static final Logger LOG = LoggerFactory.getLogger(OvsInterfaceStateAddHelper.class);
48 public static List<ListenableFuture<Void>> addState(DataBroker dataBroker, IdManagerService idManager, NodeConnectorId nodeConnectorId,
49 String portName, FlowCapableNodeConnector fcNodeConnectorNew) {
50 LOG.debug("Adding Interface State to Oper DS for port: {}", portName);
51 List<ListenableFuture<Void>> futures = new ArrayList<>();
52 WriteTransaction t = dataBroker.newWriteOnlyTransaction();
54 //Retrieve PbyAddress & OperState from the DataObject
55 PhysAddress physAddress = new PhysAddress(fcNodeConnectorNew.getHardwareAddress().getValue());
57 State state = fcNodeConnectorNew.getState();
58 Interface.OperStatus operStatus =
59 fcNodeConnectorNew == null ? Interface.OperStatus.Down : Interface.OperStatus.Up;
60 Interface.AdminStatus adminStatus = state.isBlocked() ? Interface.AdminStatus.Down : Interface.AdminStatus.Up;
62 Interface.OperStatus operStatus = Interface.OperStatus.Up;
63 Interface.AdminStatus adminStatus = Interface.AdminStatus.Up;
64 InterfaceKey interfaceKey = new InterfaceKey(portName);
65 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface iface =
66 InterfaceManagerCommonUtils.getInterfaceFromConfigDS(interfaceKey, dataBroker);
68 if (iface != null && !iface.isEnabled()) {
69 operStatus = Interface.OperStatus.Down;
72 List<String> lowerLayerIfList = new ArrayList<>();
73 lowerLayerIfList.add(nodeConnectorId.getValue());
75 Integer ifIndex = IfmUtil.allocateId(idManager, IfmConstants.IFM_IDPOOL_NAME, portName);
76 InstanceIdentifier<Interface> ifStateId = IfmUtil.buildStateInterfaceId(portName);
77 InterfaceBuilder ifaceBuilder = new InterfaceBuilder().setOperStatus(operStatus)
78 .setAdminStatus(adminStatus).setPhysAddress(physAddress).setIfIndex(ifIndex).setLowerLayerIf(lowerLayerIfList)
79 .setKey(IfmUtil.getStateInterfaceKeyFromName(portName));
80 t.put(LogicalDatastoreType.OPERATIONAL, ifStateId, ifaceBuilder.build(), true);
82 // allocate lport tag and set in if-index
83 InterfaceMetaUtils.createLportTagInterfaceMap(t, portName, ifIndex);
85 futures.add(t.submit());
89 // If this interface maps to a Vlan trunk entity, operational states of all the vlan-trunk-members
90 // should also be created here.
91 IfL2vlan ifL2vlan = iface.getAugmentation(IfL2vlan.class);
92 if (ifL2vlan == null || ifL2vlan.getL2vlanMode() != IfL2vlan.L2vlanMode.Trunk) {
93 futures.add(t.submit());
97 InterfaceParentEntryKey interfaceParentEntryKey = new InterfaceParentEntryKey(iface.getName());
98 InterfaceParentEntry interfaceParentEntry =
99 InterfaceMetaUtils.getInterfaceParentEntryFromConfigDS(interfaceParentEntryKey, dataBroker);
100 if (interfaceParentEntry == null) {
101 futures.add(t.submit());
105 List<InterfaceChildEntry> interfaceChildEntries = interfaceParentEntry.getInterfaceChildEntry();
106 if (interfaceChildEntries == null) {
107 futures.add(t.submit());
111 //FIXME: If the no. of child entries exceeds 100, perform txn updates in batches of 100.
112 //List<Trunks> trunks = new ArrayList<>();
113 for (InterfaceChildEntry interfaceChildEntry : interfaceChildEntries) {
114 InterfaceKey childIfKey = new InterfaceKey(interfaceChildEntry.getChildInterface());
115 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface ifaceChild =
116 InterfaceManagerCommonUtils.getInterfaceFromConfigDS(childIfKey, dataBroker);
118 // IfL2vlan ifL2vlanChild = iface.getAugmentation(IfL2vlan.class);
119 // trunks.add(new TrunksBuilder().setTrunk(ifL2vlanChild.getVlanId()).build());
121 if (!ifaceChild.isEnabled()) {
122 operStatus = Interface.OperStatus.Down;
125 InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> ifChildStateId =
126 IfmUtil.buildStateInterfaceId(ifaceChild.getName());
127 List<String> childLowerLayerIfList = new ArrayList<>();
128 childLowerLayerIfList.add(0, nodeConnectorId.getValue());
129 childLowerLayerIfList.add(1, iface.getName());
130 InterfaceBuilder childIfaceBuilder = new InterfaceBuilder().setAdminStatus(adminStatus).setOperStatus(operStatus)
131 .setPhysAddress(physAddress).setLowerLayerIf(childLowerLayerIfList);
132 childIfaceBuilder.setKey(IfmUtil.getStateInterfaceKeyFromName(ifaceChild.getName()));
133 t.put(LogicalDatastoreType.OPERATIONAL, ifChildStateId, childIfaceBuilder.build(), true);
136 /** Below code will be needed if we want to update the vlan-trunks on the of-port
137 if (trunks.isEmpty()) {
138 futures.add(t.submit());
142 BigInteger dpId = new BigInteger(IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId));
144 BridgeRefEntryKey BridgeRefEntryKey = new BridgeRefEntryKey(dpId);
145 InstanceIdentifier<BridgeRefEntry> dpnBridgeEntryIid =
146 InterfaceMetaUtils.getBridgeRefEntryIdentifier(BridgeRefEntryKey);
147 BridgeRefEntry bridgeRefEntry =
148 InterfaceMetaUtils.getBridgeRefEntryFromOperDS(dpnBridgeEntryIid, dataBroker);
149 if (bridgeRefEntry == null) {
150 futures.add(t.submit());
154 InstanceIdentifier<OvsdbBridgeAugmentation> bridgeIid =
155 (InstanceIdentifier<OvsdbBridgeAugmentation>)bridgeRefEntry.getBridgeReference().getValue();
156 VlanTrunkSouthboundUtils.addTerminationPointWithTrunks(bridgeIid, trunks, iface.getName(), t);
159 futures.add(t.submit());