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