Bug 5463 : Fixing datapath-id changing behaviour seen while testing with mininet
[vpnservice.git] / interfacemgr / interfacemgr-impl / src / main / java / org / opendaylight / vpnservice / interfacemgr / renderer / ovs / statehelpers / OvsInterfaceTopologyStateUpdateHelper.java
1 /*
2  * Copyright (c) 2016 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.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.vpnservice.interfacemgr.IfmUtil;
14 import org.opendaylight.vpnservice.interfacemgr.commons.InterfaceManagerCommonUtils;
15 import org.opendaylight.vpnservice.interfacemgr.commons.InterfaceMetaUtils;
16 import org.opendaylight.vpnservice.interfacemgr.renderer.ovs.utilities.SouthboundUtils;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.bridge._interface.info.BridgeEntry;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.bridge._interface.info.BridgeEntryKey;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.bridge._interface.info.bridge.entry.BridgeInterfaceEntry;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfTunnel;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 import java.math.BigInteger;
29 import java.util.ArrayList;
30 import java.util.List;
31
32 public class OvsInterfaceTopologyStateUpdateHelper {
33     private static final Logger LOG = LoggerFactory.getLogger(OvsInterfaceTopologyStateUpdateHelper.class);
34     /*
35      *  This code is used to handle only a dpnId change scenario for a particular change,
36      * which is not expected to happen in usual cases.
37      */
38     public static List<ListenableFuture<Void>> updateBridgeRefEntry(InstanceIdentifier<OvsdbBridgeAugmentation> bridgeIid,
39                                                                     OvsdbBridgeAugmentation bridgeNew,
40                                                                     OvsdbBridgeAugmentation bridgeOld,
41                                                                     DataBroker dataBroker) {
42         List<ListenableFuture<Void>> futures = new ArrayList<>();
43         WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
44
45         BigInteger dpnIdNew = IfmUtil.getDpnId(bridgeNew.getDatapathId());
46         BigInteger dpnIdOld = IfmUtil.getDpnId(bridgeOld.getDatapathId());
47
48         //delete bridge reference entry for the old dpn in interface meta operational DS
49         InterfaceMetaUtils.deleteBridgeRefEntry(dpnIdOld, writeTransaction);
50
51         // create bridge reference entry in interface meta operational DS
52         InterfaceMetaUtils.createBridgeRefEntry(dpnIdNew, bridgeIid, writeTransaction);
53
54         // handle pre-provisioning of tunnels for the newly connected dpn
55         BridgeEntry bridgeEntry = InterfaceMetaUtils.getBridgeEntryFromConfigDS(dpnIdNew, dataBroker);
56         if (bridgeEntry == null) {
57             futures.add(writeTransaction.submit());
58             return futures;
59         }
60         SouthboundUtils.addAllPortsToBridge(bridgeEntry, dataBroker, bridgeIid, bridgeNew, writeTransaction);
61
62         futures.add(writeTransaction.submit());
63         return futures;
64     }
65 }