L2 Gw create changes related to ITM Tunnels creation in neutronvpn module
[vpnservice.git] / neutronvpn / neutronvpn-impl / src / main / java / org / opendaylight / vpnservice / neutronvpn / l2gw / L2GatewayUtils.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
9 package org.opendaylight.vpnservice.neutronvpn.l2gw;
10
11 import java.util.concurrent.ExecutionException;
12 import java.util.concurrent.Future;
13
14 import org.opendaylight.vpnservice.neutronvpn.api.l2gw.L2GatewayDevice;
15 import org.opendaylight.vpnservice.utils.hwvtep.HwvtepSouthboundConstants;
16 import org.opendaylight.vpnservice.utils.hwvtep.HwvtepSouthboundUtils;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.rpcs.rev151217.AddL2GwDeviceInputBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.rpcs.rev151217.DeleteL2GwDeviceInputBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.rpcs.rev151217.ItmRpcService;
21 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
22 import org.opendaylight.yangtools.yang.common.RpcResult;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 public class L2GatewayUtils {
27     private static final Logger LOG = LoggerFactory.getLogger(L2GatewayUtils.class);
28
29     protected static boolean isGatewayAssociatedToL2Device(L2GatewayDevice l2GwDevice) {
30         return (l2GwDevice.getL2GatewayIds().size() > 0);
31     }
32
33     protected static boolean isLastL2GatewayBeingDeleted(L2GatewayDevice l2GwDevice) {
34         return (l2GwDevice.getL2GatewayIds().size() == 1);
35     }
36
37     protected static boolean isL2GwDeviceConnected(L2GatewayDevice l2GwDevice) {
38         return (l2GwDevice.getHwvtepNodeId() != null);
39     }
40
41     protected static boolean isItmTunnelsCreatedForL2Device(L2GatewayDevice l2GwDevice) {
42         return (l2GwDevice.getHwvtepNodeId() != null && l2GwDevice.getL2GatewayIds().size() > 0);
43     }
44
45     protected static void createItmTunnels(ItmRpcService itmRpcService, String hwvtepId, String psName,
46             IpAddress tunnelIp) {
47         AddL2GwDeviceInputBuilder builder = new AddL2GwDeviceInputBuilder();
48         builder.setTopologyId(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID.getValue());
49         builder.setNodeId(HwvtepSouthboundUtils.createManagedNodeId(new NodeId(hwvtepId), psName).getValue());
50         builder.setIpAddress(tunnelIp);
51         try {
52             Future<RpcResult<Void>> result = itmRpcService.addL2GwDevice(builder.build());
53             RpcResult<Void> rpcResult = result.get();
54             if (rpcResult.isSuccessful()) {
55                 LOG.info("Created ITM tunnels for {}", hwvtepId);
56             } else {
57                 LOG.error("Failed to create ITM Tunnels: ", rpcResult.getErrors());
58             }
59         } catch (InterruptedException | ExecutionException e) {
60             LOG.error("RPC to create ITM tunnels failed", e);
61         }
62     }
63
64     protected static void deleteItmTunnels(ItmRpcService itmRpcService, String hwvtepId, String psName,
65             IpAddress tunnelIp) {
66         DeleteL2GwDeviceInputBuilder builder = new DeleteL2GwDeviceInputBuilder();
67         builder.setTopologyId(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID.getValue());
68         builder.setNodeId(HwvtepSouthboundUtils.createManagedNodeId(new NodeId(hwvtepId), psName).getValue());
69         builder.setIpAddress(tunnelIp);
70         try {
71             Future<RpcResult<Void>> result = itmRpcService.deleteL2GwDevice(builder.build());
72             RpcResult<Void> rpcResult = result.get();
73             if (rpcResult.isSuccessful()) {
74                 LOG.info("Deleted ITM tunnels for {}", hwvtepId);
75             } else {
76                 LOG.error("Failed to delete ITM Tunnels: ", rpcResult.getErrors());
77             }
78         } catch (InterruptedException | ExecutionException e) {
79             LOG.error("RPC to delete ITM tunnels failed", e);
80         }
81     }
82 }