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