Merge "BUG:5116 add support for router-association" into stable/beryllium
[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 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;
38
39 import java.util.ArrayList;
40 import java.util.List;
41
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();
49
50         InterfaceParentEntryKey interfaceParentEntryKey = new InterfaceParentEntryKey(parentRefs.getParentInterface());
51         createInterfaceParentEntryIfNotPresent(dataBroker, t, interfaceParentEntryKey, parentRefs.getParentInterface());
52         createInterfaceChildEntry(dataBroker, idManager, t, interfaceParentEntryKey, interfaceNew.getName());
53
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());
60             return futures;
61         }
62
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());
67             return futures;
68         }
69
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();
76
77             if (!interfaceNew.isEnabled()) {
78                 operStatus = OperStatus.Down;
79             }
80
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(ifState.getLowerLayerIf().get(0));
85             lowerLayerIfList.add(parentRefs.getParentInterface());
86             Integer ifIndex = IfmUtil.allocateId(idManager, IfmConstants.IFM_IDPOOL_NAME, interfaceNew.getName());
87             InterfaceBuilder ifaceBuilder = new InterfaceBuilder().setAdminStatus(adminStatus).setOperStatus(operStatus)
88                     .setPhysAddress(physAddress).setLowerLayerIf(lowerLayerIfList).setIfIndex(ifIndex);
89             ifaceBuilder.setKey(IfmUtil.getStateInterfaceKeyFromName(interfaceNew.getName()));
90             t.put(LogicalDatastoreType.OPERATIONAL, ifStateId, ifaceBuilder.build(), true);
91
92             // create lportTag Interface Map
93             InterfaceMetaUtils.createLportTagInterfaceMap(t, interfaceNew.getName(), ifIndex);
94
95             // FIXME: Maybe, add the new interface to the higher-layer if of the parent interface-state.
96             // That may not serve any purpose though for interface manager.... Unless some external parties are interested in it.
97
98             /* FIXME -- Below code is needed to add vlan-trunks to the of-port. Is this really needed.
99             String lowerLayerIf = ifState.getLowerLayerIf().get(0);
100
101             NodeConnectorId nodeConnectorId = new NodeConnectorId(lowerLayerIf);
102             BigInteger dpId = new BigInteger(IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId));
103
104             BridgeRefEntryKey BridgeRefEntryKey = new BridgeRefEntryKey(dpId);
105             InstanceIdentifier<BridgeRefEntry> dpnBridgeEntryIid =
106                     InterfaceMetaUtils.getBridgeRefEntryIdentifier(BridgeRefEntryKey);
107             BridgeRefEntry bridgeRefEntry =
108                     InterfaceMetaUtils.getBridgeRefEntryFromOperDS(dpnBridgeEntryIid, dataBroker);
109             if (bridgeRefEntry != null) {
110                 InstanceIdentifier<OvsdbBridgeAugmentation> bridgeIid =
111                         (InstanceIdentifier<OvsdbBridgeAugmentation>)bridgeRefEntry.getBridgeReference().getValue();
112                 Optional<OvsdbBridgeAugmentation> bridgeNodeOptional =
113                         IfmUtil.read(LogicalDatastoreType.OPERATIONAL, bridgeIid, dataBroker);
114                 if (bridgeNodeOptional.isPresent()) {
115                     OvsdbBridgeAugmentation ovsdbBridgeAugmentation = bridgeNodeOptional.get();
116                     String bridgeName = ovsdbBridgeAugmentation.getBridgeName().getValue();
117                     VlanTrunkSouthboundUtils.addVlanPortToBridge(bridgeIid, ifL2vlan,
118                             ovsdbBridgeAugmentation, bridgeName, parentRefs.getParentInterface(), dataBroker, t);
119                 }
120             } */
121             // FIXME: Need to add the Group here with actions: Push-Vlan, output_port. May not be needed here...
122         }
123
124         futures.add(t.submit());
125         return futures;
126     }
127
128     private static void createInterfaceParentEntryIfNotPresent(DataBroker dataBroker, WriteTransaction t,
129                                                                InterfaceParentEntryKey interfaceParentEntryKey,
130                                                                String parentInterface){
131         InstanceIdentifier<InterfaceParentEntry> interfaceParentEntryIdentifier =
132                 InterfaceMetaUtils.getInterfaceParentEntryIdentifier(interfaceParentEntryKey);
133         InterfaceParentEntry interfaceParentEntry =
134                 InterfaceMetaUtils.getInterfaceParentEntryFromConfigDS(interfaceParentEntryIdentifier, dataBroker);
135
136         if(interfaceParentEntry != null){
137             LOG.info("Not Found entry for Parent Interface: {} in Vlan Trunk-Member Interface Renderer ConfigDS. " +
138                     "Creating...", parentInterface);
139             InterfaceParentEntryBuilder interfaceParentEntryBuilder = new InterfaceParentEntryBuilder()
140                     .setKey(interfaceParentEntryKey).setParentInterface(parentInterface);
141             t.put(LogicalDatastoreType.CONFIGURATION, interfaceParentEntryIdentifier,
142                     interfaceParentEntryBuilder.build(), true);
143         }
144     }
145
146     private static long createInterfaceChildEntry(DataBroker dataBroker, IdManagerService idManager, WriteTransaction t,
147                                                 InterfaceParentEntryKey interfaceParentEntryKey, String childInterface){
148
149         long lportTag = IfmUtil.allocateId(idManager, IfmConstants.IFM_IDPOOL_NAME, childInterface);
150         InterfaceChildEntryKey interfaceChildEntryKey = new InterfaceChildEntryKey(childInterface);
151         InstanceIdentifier<InterfaceChildEntry> intfId =
152                 InterfaceMetaUtils.getInterfaceChildEntryIdentifier(interfaceParentEntryKey, interfaceChildEntryKey);
153         InterfaceChildEntryBuilder entryBuilder = new InterfaceChildEntryBuilder().setKey(interfaceChildEntryKey)
154               .setChildInterface(childInterface);
155         t.put(LogicalDatastoreType.CONFIGURATION, intfId, entryBuilder.build(),true);
156         return lportTag;
157     }
158 }