Bug 5182 - Customized name support for VLAN trunk should be supported
[vpnservice.git] / interfacemgr / interfacemgr-impl / src / main / java / org / opendaylight / vpnservice / interfacemgr / renderer / ovs / confighelpers / OvsVlanMemberConfigAddHelper.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.confighelpers;
9
10 import com.google.common.util.concurrent.ListenableFuture;
11
12 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
13 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
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.vpnservice.interfacemgr.servicebindings.flowbased.utilities.FlowBasedServicesUtils;
20 import org.opendaylight.vpnservice.mdsalutil.MatchInfo;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.AdminStatus;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceBuilder;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.IdManagerService;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfL2vlan;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.ParentRefs;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 import java.math.BigInteger;
36 import java.util.ArrayList;
37 import java.util.List;
38
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                                                                 IdManagerService idManager) {
44         LOG.debug("add vlan member configuration {}",interfaceNew.getName());
45         List<ListenableFuture<Void>> futures = new ArrayList<>();
46         WriteTransaction t = dataBroker.newWriteOnlyTransaction();
47
48         InterfaceManagerCommonUtils.createInterfaceParentEntryIfNotPresent(dataBroker, t, parentRefs.getParentInterface());
49         InterfaceManagerCommonUtils.createInterfaceChildEntry(t, parentRefs.getParentInterface(), interfaceNew.getName());
50
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());
57             return futures;
58         }
59
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());
64             return futures;
65         }
66
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             LOG.debug("add interface state info for vlan member {}",interfaceNew.getName());
71             OperStatus operStatus = ifState.getOperStatus();
72             AdminStatus adminStatus = ifState.getAdminStatus();
73             PhysAddress physAddress = ifState.getPhysAddress();
74
75             if (!interfaceNew.isEnabled()) {
76                 operStatus = OperStatus.Down;
77             }
78
79             InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> ifStateId =
80                     IfmUtil.buildStateInterfaceId(interfaceNew.getName());
81             List<String> lowerLayerIfList = new ArrayList<>();
82             lowerLayerIfList.add(ifState.getLowerLayerIf().get(0));
83             //lowerLayerIfList.add(parentRefs.getParentInterface());
84             Integer ifIndex = IfmUtil.allocateId(idManager, IfmConstants.IFM_IDPOOL_NAME, interfaceNew.getName());
85             InterfaceBuilder ifaceBuilder = new InterfaceBuilder().setAdminStatus(adminStatus).setOperStatus(operStatus)
86                     .setPhysAddress(physAddress).setLowerLayerIf(lowerLayerIfList).setIfIndex(ifIndex);
87             ifaceBuilder.setKey(IfmUtil.getStateInterfaceKeyFromName(interfaceNew.getName()));
88             t.put(LogicalDatastoreType.OPERATIONAL, ifStateId, ifaceBuilder.build(), true);
89
90             // create lportTag Interface Map
91             InterfaceMetaUtils.createLportTagInterfaceMap(t, interfaceNew.getName(), ifIndex);
92             //Installing vlan flow for vlan member
93             NodeConnectorId nodeConnectorId = new NodeConnectorId(ifState.getLowerLayerIf().get(0));
94             BigInteger dpId = new BigInteger(IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId));
95             long portNo = Long.valueOf(IfmUtil.getPortNoFromNodeConnectorId(nodeConnectorId));
96             if (operStatus == OperStatus.Up) {
97                 List<MatchInfo> matches = FlowBasedServicesUtils.getMatchInfoForVlanPortAtIngressTable(dpId, portNo, interfaceNew);
98                 FlowBasedServicesUtils.installVlanFlow(dpId, portNo, interfaceNew, t, matches, ifIndex);
99             }
100
101             // FIXME: Maybe, add the new interface to the higher-layer if of the parent interface-state.
102             // That may not serve any purpose though for interface manager.... Unless some external parties are interested in it.
103
104             /* FIXME -- Below code is needed to add vlan-trunks to the of-port. Is this really needed.
105             String lowerLayerIf = ifState.getLowerLayerIf().get(0);
106
107             NodeConnectorId nodeConnectorId = new NodeConnectorId(lowerLayerIf);
108             BigInteger dpId = new BigInteger(IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId));
109
110             BridgeRefEntryKey BridgeRefEntryKey = new BridgeRefEntryKey(dpId);
111             InstanceIdentifier<BridgeRefEntry> dpnBridgeEntryIid =
112                     InterfaceMetaUtils.getBridgeRefEntryIdentifier(BridgeRefEntryKey);
113             BridgeRefEntry bridgeRefEntry =
114                     InterfaceMetaUtils.getBridgeRefEntryFromOperDS(dpnBridgeEntryIid, dataBroker);
115             if (bridgeRefEntry != null) {
116                 InstanceIdentifier<OvsdbBridgeAugmentation> bridgeIid =
117                         (InstanceIdentifier<OvsdbBridgeAugmentation>)bridgeRefEntry.getBridgeReference().getValue();
118                 Optional<OvsdbBridgeAugmentation> bridgeNodeOptional =
119                         IfmUtil.read(LogicalDatastoreType.OPERATIONAL, bridgeIid, dataBroker);
120                 if (bridgeNodeOptional.isPresent()) {
121                     OvsdbBridgeAugmentation ovsdbBridgeAugmentation = bridgeNodeOptional.get();
122                     String bridgeName = ovsdbBridgeAugmentation.getBridgeName().getValue();
123                     VlanTrunkSouthboundUtils.addVlanPortToBridge(bridgeIid, ifL2vlan,
124                             ovsdbBridgeAugmentation, bridgeName, parentRefs.getParentInterface(), dataBroker, t);
125                 }
126             } */
127             // FIXME: Need to add the Group here with actions: Push-Vlan, output_port. May not be needed here...
128         }
129
130         futures.add(t.submit());
131         return futures;
132     }
133 }