L2 Gw create changes related to ITM Tunnels creation in neutronvpn module
[vpnservice.git] / interfacemgr / interfacemgr-impl / src / main / java / org / opendaylight / vpnservice / interfacemgr / renderer / ovs / statehelpers / OvsInterfaceTopologyStateAddHelper.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.statehelpers;
9
10 import com.google.common.base.Optional;
11 import com.google.common.util.concurrent.ListenableFuture;
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.IfmUtil;
16 import org.opendaylight.vpnservice.interfacemgr.commons.InterfaceManagerCommonUtils;
17 import org.opendaylight.vpnservice.interfacemgr.commons.InterfaceMetaUtils;
18 import org.opendaylight.vpnservice.interfacemgr.renderer.ovs.utilities.SouthboundUtils;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeRef;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.bridge._interface.info.BridgeEntry;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.bridge._interface.info.BridgeEntryKey;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.bridge._interface.info.bridge.entry.BridgeInterfaceEntry;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.bridge._interface.info.bridge.entry.BridgeInterfaceEntryKey;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.bridge.ref.info.BridgeRefEntry;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.bridge.ref.info.BridgeRefEntryBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.bridge.ref.info.BridgeRefEntryKey;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfTunnel;
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 OvsInterfaceTopologyStateAddHelper {
40     private static final Logger LOG = LoggerFactory.getLogger(OvsInterfaceTopologyStateAddHelper.class);
41     public static List<ListenableFuture<Void>> addPortToBridge(InstanceIdentifier<OvsdbBridgeAugmentation> bridgeIid,
42                                                                OvsdbBridgeAugmentation bridgeNew, DataBroker dataBroker) {
43         List<ListenableFuture<Void>> futures = new ArrayList<>();
44         WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
45
46         if (bridgeNew.getDatapathId() == null) {
47             LOG.warn("DataPathId found as null for Bridge Augmentation: {}... retrying...", bridgeNew);
48             Optional<OvsdbBridgeAugmentation> bridgeNodeOptional = IfmUtil.read(LogicalDatastoreType.OPERATIONAL, bridgeIid, dataBroker);
49             if (bridgeNodeOptional.isPresent()) {
50                 bridgeNew = bridgeNodeOptional.get();
51             }
52             if (bridgeNew.getDatapathId() == null) {
53                 LOG.warn("DataPathId found as null again for Bridge Augmentation: {}. Bailing out.", bridgeNew);
54                 return futures;
55             }
56         }
57         String bridgeName = bridgeNew.getBridgeName().getValue();
58         BigInteger dpnId = IfmUtil.getDpnId(bridgeNew.getDatapathId());
59
60         if (dpnId == null) {
61             LOG.warn("Got Null DPID for Bridge: {}", bridgeNew);
62             return futures;
63         }
64
65         // create bridge reference entry in interface meta operational DS
66         InterfaceMetaUtils.createBridgeRefEntry(dpnId, bridgeIid, writeTransaction);
67
68         // FIX for OVSDB Bug - manually copying the bridge info from topology operational DS to config DS
69         SouthboundUtils.addBridge(bridgeIid, bridgeNew, dataBroker, futures);
70
71         BridgeEntryKey bridgeEntryKey = new BridgeEntryKey(dpnId);
72         InstanceIdentifier<BridgeEntry> bridgeEntryInstanceIdentifier =
73                 InterfaceMetaUtils.getBridgeEntryIdentifier(bridgeEntryKey);
74         BridgeEntry bridgeEntry =
75                 InterfaceMetaUtils.getBridgeEntryFromConfigDS(bridgeEntryInstanceIdentifier,
76                         dataBroker);
77         if (bridgeEntry == null) {
78             futures.add(writeTransaction.submit());
79             return futures;
80         }
81
82         List<BridgeInterfaceEntry> bridgeInterfaceEntries = bridgeEntry.getBridgeInterfaceEntry();
83         for (BridgeInterfaceEntry bridgeInterfaceEntry : bridgeInterfaceEntries) {
84             String portName = bridgeInterfaceEntry.getInterfaceName();
85             InterfaceKey interfaceKey = new InterfaceKey(portName);
86             Interface iface = InterfaceManagerCommonUtils.getInterfaceFromConfigDS(interfaceKey, dataBroker);
87             if (iface.getAugmentation(IfTunnel.class) != null) {
88                 SouthboundUtils.addPortToBridge(bridgeIid, iface, bridgeNew, bridgeName, portName, dataBroker, futures);
89             }
90         }
91
92         futures.add(writeTransaction.submit());
93         return futures;
94     }
95 }