2 * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
8 package org.opendaylight.vpnservice.interfacemgr.renderer.ovs.statehelpers;
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;
37 import java.math.BigInteger;
38 import java.util.ArrayList;
39 import java.util.List;
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();
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();
54 if (bridgeNew.getDatapathId() == null) {
55 LOG.warn("DataPathId found as null again for Bridge Augmentation: {}. Bailing out.", bridgeNew);
60 String dpId = bridgeNew.getDatapathId().getValue();
61 String bridgeName = bridgeNew.getBridgeName().getValue();
62 LOG.info("adding dpId {} to bridge reference {}", dpId, bridgeNew);
65 LOG.error("Optained null dpid for bridge: {}", bridgeNew);
69 String datapathId = dpId.replaceAll("[^\\d.]", "");
70 BigInteger ovsdbDpId = new BigInteger(datapathId, 16);
72 LOG.info("adding dpId {} to bridge reference {}", datapathId, bridgeName);
73 BridgeRefEntryKey bridgeRefEntryKey = new BridgeRefEntryKey(ovsdbDpId);
74 InstanceIdentifier<BridgeRefEntry> bridgeEntryId =
75 InterfaceMetaUtils.getBridgeRefEntryIdentifier(bridgeRefEntryKey);
76 BridgeRefEntryBuilder tunnelDpnBridgeEntryBuilder =
77 new BridgeRefEntryBuilder().setKey(bridgeRefEntryKey).setDpid(ovsdbDpId)
78 .setBridgeReference(new OvsdbBridgeRef(bridgeIid));
79 t.put(LogicalDatastoreType.OPERATIONAL, bridgeEntryId, tunnelDpnBridgeEntryBuilder.build(), true);
81 BridgeEntryKey bridgeEntryKey = new BridgeEntryKey(ovsdbDpId);
82 InstanceIdentifier<BridgeEntry> bridgeEntryInstanceIdentifier =
83 InterfaceMetaUtils.getBridgeEntryIdentifier(bridgeEntryKey);
84 BridgeEntry bridgeEntry =
85 InterfaceMetaUtils.getBridgeEntryFromConfigDS(bridgeEntryInstanceIdentifier,
87 if (bridgeEntry == null) {
88 futures.add(t.submit());
92 List<BridgeInterfaceEntry> bridgeInterfaceEntries = bridgeEntry.getBridgeInterfaceEntry();
93 for (BridgeInterfaceEntry bridgeInterfaceEntry : bridgeInterfaceEntries) {
94 String portName = bridgeInterfaceEntry.getInterfaceName();
95 InterfaceKey interfaceKey = new InterfaceKey(portName);
96 Interface iface = InterfaceManagerCommonUtils.getInterfaceFromConfigDS(interfaceKey, dataBroker);
97 if (iface.getAugmentation(IfTunnel.class) != null) {
98 SouthboundUtils.addPortToBridge(bridgeIid, iface, bridgeNew, bridgeName, portName, dataBroker, futures);
99 InterfaceMetaUtils.createBridgeInterfaceEntryInConfigDS(bridgeEntryKey,
100 new BridgeInterfaceEntryKey(portName), portName, t);
104 futures.add(t.submit());