Bulk merge of l2gw changes
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / l2gw / utils / L2GatewayUtils.java
1 /*
2  * Copyright (c) 2016 ,2017  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.netvirt.elan.l2gw.utils;
9
10 import java.util.concurrent.ExecutionException;
11 import org.opendaylight.genius.mdsalutil.MDSALUtil;
12 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundConstants;
13 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundUtils;
14 import org.opendaylight.mdsal.binding.api.DataBroker;
15 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
16 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.AddL2GwDeviceInputBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.AddL2GwDeviceOutput;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.DeleteL2GwDeviceInputBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.DeleteL2GwDeviceOutput;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.ItmRpcService;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
24 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
26 import org.opendaylight.yangtools.yang.common.RpcResult;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 public final class L2GatewayUtils {
31
32     private static final Logger LOG = LoggerFactory.getLogger(L2GatewayUtils.class);
33
34     private L2GatewayUtils() {
35     }
36
37     public static boolean isGatewayAssociatedToL2Device(L2GatewayDevice l2GwDevice) {
38         return (l2GwDevice.getL2GatewayIds().size() > 0);
39     }
40
41     public static boolean isLastL2GatewayBeingDeleted(L2GatewayDevice l2GwDevice) {
42         return (l2GwDevice.getL2GatewayIds().size() == 1);
43     }
44
45     public static boolean isItmTunnelsCreatedForL2Device(L2GatewayDevice l2GwDevice) {
46         return (l2GwDevice.getHwvtepNodeId() != null && l2GwDevice.getL2GatewayIds().size() > 0);
47     }
48
49     public static void deleteItmTunnels(ItmRpcService itmRpcService, String hwvtepId, String psName,
50                                         IpAddress tunnelIp) {
51         DeleteL2GwDeviceInputBuilder builder = new DeleteL2GwDeviceInputBuilder();
52         builder.setTopologyId(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID.getValue());
53         builder.setNodeId(HwvtepSouthboundUtils.createManagedNodeId(new NodeId(hwvtepId), psName).getValue());
54         builder.setIpAddress(tunnelIp);
55         try {
56             RpcResult<DeleteL2GwDeviceOutput> rpcResult = itmRpcService.deleteL2GwDevice(builder.build()).get();
57             if (rpcResult.isSuccessful()) {
58                 LOG.info("Deleted ITM tunnels for {}", hwvtepId);
59             } else {
60                 LOG.error("Failed to delete ITM Tunnels: {}", rpcResult.getErrors());
61             }
62         } catch (InterruptedException | ExecutionException e) {
63             LOG.error("RPC to delete ITM tunnels failed", e);
64         }
65     }
66
67     public static void createItmTunnels(ItmRpcService itmRpcService, String hwvtepId, String psName,
68                                         IpAddress tunnelIp) {
69         AddL2GwDeviceInputBuilder builder = new AddL2GwDeviceInputBuilder();
70         builder.setTopologyId(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID.getValue());
71         builder.setNodeId(HwvtepSouthboundUtils.createManagedNodeId(new NodeId(hwvtepId), psName).getValue());
72         builder.setIpAddress(tunnelIp);
73         try {
74             RpcResult<AddL2GwDeviceOutput> rpcResult = itmRpcService.addL2GwDevice(builder.build()).get();
75             if (rpcResult.isSuccessful()) {
76                 LOG.info("Created ITM tunnels for {}", hwvtepId);
77             } else {
78                 LOG.error("Failed to create ITM Tunnels: {}", rpcResult.getErrors());
79             }
80         } catch (InterruptedException | ExecutionException e) {
81             LOG.error("RPC to create ITM tunnels failed", e);
82         }
83     }
84
85     //TODO Remove the method from HwvtepUtils.getDbVersion() from genius
86     public static String getConfigDbVersion(DataBroker broker, NodeId nodeId) {
87         Node hwvtepNode = null;
88         try {
89             hwvtepNode = getHwVtepNode(broker, LogicalDatastoreType.CONFIGURATION, nodeId);
90         } catch (ExecutionException | InterruptedException e) {
91             LOG.error("Failed to created Node {} for retriving configDbVersion", nodeId, e);
92         }
93         String dbVersion = "";
94         if (hwvtepNode != null) {
95             HwvtepGlobalAugmentation globalAugmentation = hwvtepNode.augmentation(HwvtepGlobalAugmentation.class);
96             if (globalAugmentation != null) {
97                 dbVersion = globalAugmentation.getDbVersion();
98             }
99         } else {
100             LOG.warn("HWVTEP Node missing in config topo for {}", nodeId.getValue());
101         }
102
103         return dbVersion;
104     }
105
106     public static Node getHwVtepNode(DataBroker dataBroker, LogicalDatastoreType datastoreType,
107         NodeId nodeId) throws ExecutionException, InterruptedException {
108         return (Node) MDSALUtil.read(dataBroker, datastoreType, HwvtepSouthboundUtils.createInstanceIdentifier(nodeId))
109                 .orElse(null);
110
111     }
112 }