Working with OVS
[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.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 import java.math.BigInteger;
38 import java.util.ArrayList;
39 import java.util.List;
40
41 public class OvsInterfaceTopologyStateAddHelper {
42     private static final Logger LOG = LoggerFactory.getLogger(OvsInterfaceTopologyStateAddHelper.class);
43     public static List<ListenableFuture<Void>> addPortToBridge(InstanceIdentifier<OvsdbBridgeAugmentation> bridgeIid,
44                                                                OvsdbBridgeAugmentation bridgeNew, DataBroker dataBroker) {
45         List<ListenableFuture<Void>> futures = new ArrayList<>();
46         WriteTransaction t = dataBroker.newWriteOnlyTransaction();
47
48         if (bridgeNew.getDatapathId() == null) {
49             LOG.warn("DataPathId found as null for Bridge Augmentation: {}... retrying...", bridgeNew);
50             Optional<OvsdbBridgeAugmentation> bridgeNodeOptional = IfmUtil.read(LogicalDatastoreType.OPERATIONAL, bridgeIid, dataBroker);
51             if (bridgeNodeOptional.isPresent()) {
52                 bridgeNew = bridgeNodeOptional.get();
53             }
54             if (bridgeNew.getDatapathId() == null) {
55                 LOG.warn("DataPathId found as null again for Bridge Augmentation: {}. Bailing out.", bridgeNew);
56                 return futures;
57             }
58         }
59
60         String dpId = bridgeNew.getDatapathId().getValue();
61         String bridgeName = bridgeNew.getBridgeName().getValue();
62
63         if (dpId == null) {
64             LOG.error("Optained null dpid for bridge: {}", bridgeNew);
65             return futures;
66         }
67
68         dpId = dpId.replaceAll("[^\\d.]", "");
69         BigInteger ovsdbDpId = new BigInteger(dpId, 16);
70         BridgeRefEntryKey bridgeRefEntryKey = new BridgeRefEntryKey(ovsdbDpId);
71         InstanceIdentifier<BridgeRefEntry> bridgeEntryId =
72                 InterfaceMetaUtils.getBridgeRefEntryIdentifier(bridgeRefEntryKey);
73         BridgeRefEntryBuilder tunnelDpnBridgeEntryBuilder =
74                 new BridgeRefEntryBuilder().setKey(bridgeRefEntryKey).setDpid(ovsdbDpId)
75                         .setBridgeReference(new OvsdbBridgeRef(bridgeIid));
76         t.put(LogicalDatastoreType.OPERATIONAL, bridgeEntryId, tunnelDpnBridgeEntryBuilder.build(), true);
77
78         BridgeEntryKey bridgeEntryKey = new BridgeEntryKey(ovsdbDpId);
79         InstanceIdentifier<BridgeEntry> bridgeEntryInstanceIdentifier =
80                 InterfaceMetaUtils.getBridgeEntryIdentifier(bridgeEntryKey);
81         BridgeEntry bridgeEntry =
82                 InterfaceMetaUtils.getBridgeEntryFromConfigDS(bridgeEntryInstanceIdentifier,
83                         dataBroker);
84         if (bridgeEntry == null) {
85             futures.add(t.submit());
86             return futures;
87         }
88
89         List<BridgeInterfaceEntry> bridgeInterfaceEntries = bridgeEntry.getBridgeInterfaceEntry();
90         for (BridgeInterfaceEntry bridgeInterfaceEntry : bridgeInterfaceEntries) {
91             String portName = bridgeInterfaceEntry.getInterfaceName();
92             InterfaceKey interfaceKey = new InterfaceKey(portName);
93             Interface iface = InterfaceManagerCommonUtils.getInterfaceFromConfigDS(interfaceKey, dataBroker);
94             if (iface.getAugmentation(IfTunnel.class) != null) {
95                 SouthboundUtils.addPortToBridge(bridgeIid, iface, bridgeNew, bridgeName, portName, dataBroker, t);
96                  InstanceIdentifier<TerminationPoint> tpIid = SouthboundUtils.createTerminationPointInstanceIdentifier(
97                         InstanceIdentifier.keyOf(bridgeIid.firstIdentifierOf(Node.class)), portName);
98                 InterfaceMetaUtils.createBridgeInterfaceEntryInConfigDS(bridgeEntryKey,
99                         new BridgeInterfaceEntryKey(portName), portName, tpIid, t);
100             }
101         }
102
103         futures.add(t.submit());
104         return futures;
105     }
106 }