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