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.confighelpers;
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.IfmUtil;
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.Interface;
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.AdminStatus;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceBuilder;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._interface.child.info.InterfaceParentEntry;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._interface.child.info.InterfaceParentEntryBuilder;
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.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._interface.child.info._interface.parent.entry.InterfaceChildEntryBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._interface.child.info._interface.parent.entry.InterfaceChildEntryKey;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfL2vlan;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.ParentRefs;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 import java.util.ArrayList;
37 import java.util.List;
39 public class OvsVlanMemberConfigAddHelper {
40 private static final Logger LOG = LoggerFactory.getLogger(OvsVlanMemberConfigAddHelper.class);
41 public static List<ListenableFuture<Void>> addConfiguration(DataBroker dataBroker, ParentRefs parentRefs,
42 Interface interfaceNew, IfL2vlan ifL2vlan,
43 IdManager idManager) {
44 List<ListenableFuture<Void>> futures = new ArrayList<>();
45 WriteTransaction t = dataBroker.newWriteOnlyTransaction();
47 InterfaceParentEntryKey interfaceParentEntryKey = new InterfaceParentEntryKey(parentRefs.getParentInterface());
48 createInterfaceParentEntryIfNotPresent(dataBroker, t, interfaceParentEntryKey, parentRefs.getParentInterface());
49 createInterfaceChildEntry(dataBroker, idManager, t, interfaceParentEntryKey, interfaceNew.getName());
51 InterfaceKey interfaceKey = new InterfaceKey(parentRefs.getParentInterface());
52 Interface ifaceParent = InterfaceManagerCommonUtils.getInterfaceFromConfigDS(interfaceKey, dataBroker);
53 if (ifaceParent == null) {
54 LOG.info("Parent Interface: {} not found when adding child interface: {}",
55 parentRefs.getParentInterface(), interfaceNew.getName());
56 futures.add(t.submit());
60 IfL2vlan parentIfL2Vlan = ifaceParent.getAugmentation(IfL2vlan.class);
61 if (parentIfL2Vlan == null || parentIfL2Vlan.getL2vlanMode() != IfL2vlan.L2vlanMode.Trunk) {
62 LOG.error("Parent Interface: {} not of trunk Type when adding trunk-member: {}", ifaceParent, interfaceNew);
63 futures.add(t.submit());
67 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState =
68 InterfaceManagerCommonUtils.getInterfaceStateFromOperDS(parentRefs.getParentInterface(), dataBroker);
69 if (ifState != null) {
70 OperStatus operStatus = ifState.getOperStatus();
71 AdminStatus adminStatus = ifState.getAdminStatus();
72 PhysAddress physAddress = ifState.getPhysAddress();
74 if (!interfaceNew.isEnabled()) {
75 operStatus = OperStatus.Down;
78 InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> ifStateId =
79 IfmUtil.buildStateInterfaceId(interfaceNew.getName());
80 List<String> lowerLayerIfList = new ArrayList<>();
81 lowerLayerIfList.add(parentRefs.getParentInterface());
82 InterfaceBuilder ifaceBuilder = new InterfaceBuilder().setAdminStatus(adminStatus).setOperStatus(operStatus)
83 .setPhysAddress(physAddress).setLowerLayerIf(lowerLayerIfList);
84 ifaceBuilder.setKey(IfmUtil.getStateInterfaceKeyFromName(interfaceNew.getName()));
85 t.put(LogicalDatastoreType.OPERATIONAL, ifStateId, ifaceBuilder.build(), true);
87 // FIXME: Maybe, add the new interface to the higher-layer if of the parent interface-state.
88 // That may not serve any purpose though for interface manager.... Unless some external parties are interested in it.
90 /* FIXME -- Below code is needed to add vlan-trunks to the of-port. Is this really needed.
91 String lowerLayerIf = ifState.getLowerLayerIf().get(0);
93 NodeConnectorId nodeConnectorId = new NodeConnectorId(lowerLayerIf);
94 BigInteger dpId = new BigInteger(IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId));
96 BridgeRefEntryKey BridgeRefEntryKey = new BridgeRefEntryKey(dpId);
97 InstanceIdentifier<BridgeRefEntry> dpnBridgeEntryIid =
98 InterfaceMetaUtils.getBridgeRefEntryIdentifier(BridgeRefEntryKey);
99 BridgeRefEntry bridgeRefEntry =
100 InterfaceMetaUtils.getBridgeRefEntryFromOperDS(dpnBridgeEntryIid, dataBroker);
101 if (bridgeRefEntry != null) {
102 InstanceIdentifier<OvsdbBridgeAugmentation> bridgeIid =
103 (InstanceIdentifier<OvsdbBridgeAugmentation>)bridgeRefEntry.getBridgeReference().getValue();
104 Optional<OvsdbBridgeAugmentation> bridgeNodeOptional =
105 IfmUtil.read(LogicalDatastoreType.OPERATIONAL, bridgeIid, dataBroker);
106 if (bridgeNodeOptional.isPresent()) {
107 OvsdbBridgeAugmentation ovsdbBridgeAugmentation = bridgeNodeOptional.get();
108 String bridgeName = ovsdbBridgeAugmentation.getBridgeName().getValue();
109 VlanTrunkSouthboundUtils.addVlanPortToBridge(bridgeIid, ifL2vlan,
110 ovsdbBridgeAugmentation, bridgeName, parentRefs.getParentInterface(), dataBroker, t);
113 // FIXME: Need to add the Group here with actions: Push-Vlan, output_port. May not be needed here...
116 futures.add(t.submit());
120 private static void createInterfaceParentEntryIfNotPresent(DataBroker dataBroker, WriteTransaction t,
121 InterfaceParentEntryKey interfaceParentEntryKey,
122 String parentInterface){
123 InstanceIdentifier<InterfaceParentEntry> interfaceParentEntryIdentifier =
124 InterfaceMetaUtils.getInterfaceParentEntryIdentifier(interfaceParentEntryKey);
125 InterfaceParentEntry interfaceParentEntry =
126 InterfaceMetaUtils.getInterfaceParentEntryFromConfigDS(interfaceParentEntryIdentifier, dataBroker);
128 if(interfaceParentEntry != null){
129 LOG.info("Not Found entry for Parent Interface: {} in Vlan Trunk-Member Interface Renderer ConfigDS. " +
130 "Creating...", parentInterface);
131 InterfaceParentEntryBuilder interfaceParentEntryBuilder = new InterfaceParentEntryBuilder()
132 .setKey(interfaceParentEntryKey).setParentInterface(parentInterface);
133 t.put(LogicalDatastoreType.CONFIGURATION, interfaceParentEntryIdentifier,
134 interfaceParentEntryBuilder.build(), true);
138 private static long createInterfaceChildEntry(DataBroker dataBroker, IdManager idManager, WriteTransaction t,
139 InterfaceParentEntryKey interfaceParentEntryKey, String childInterface){
140 long lportTag = InterfaceManagerCommonUtils.getUniqueId(idManager, childInterface);
141 InterfaceChildEntryKey interfaceChildEntryKey = new InterfaceChildEntryKey(childInterface);
142 InstanceIdentifier<InterfaceChildEntry> intfId =
143 InterfaceMetaUtils.getInterfaceChildEntryIdentifier(interfaceParentEntryKey, interfaceChildEntryKey);
144 InterfaceChildEntryBuilder entryBuilder = new InterfaceChildEntryBuilder().setKey(interfaceChildEntryKey)
145 .setChildInterface(childInterface);
146 t.put(LogicalDatastoreType.CONFIGURATION, intfId, entryBuilder.build(),true);