2dcbfcc266d2783ff6cac497d2ee5c05e235d408
[vpnservice.git] / itm / itm-impl / src / main / java / org / opendaylight / vpnservice / itm / confighelpers / ItmExternalTunnelAddWorker.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.itm.confighelpers;
9
10 import java.math.BigInteger;
11 import java.util.ArrayList;
12 import java.util.List;
13
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.vpnservice.itm.impl.ItmUtils;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
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.opendaylight.vpnservice.interfacemgr.rev150331.TunnelTypeBase;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.TunnelTypeGre;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.TunnelTypeVxlan;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.ExternalTunnelList;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.dpn.endpoints.DPNTEPsInfo;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.dpn.endpoints.dpn.teps.info.TunnelEndPoints;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.external.tunnel.list.ExternalTunnel;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.external.tunnel.list.ExternalTunnelBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.external.tunnel.list.ExternalTunnelKey;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.apache.commons.net.util.SubnetUtils;
33
34 import com.google.common.util.concurrent.FutureCallback;
35 import com.google.common.util.concurrent.ListenableFuture;
36
37 public class ItmExternalTunnelAddWorker {
38     private static final Logger logger = LoggerFactory.getLogger(ItmExternalTunnelAddWorker.class ) ;
39
40     private static final FutureCallback<Void> DEFAULT_CALLBACK =
41             new FutureCallback<Void>() {
42                 public void onSuccess(Void result) {
43                     logger.debug("Success in Datastore operation");
44                 }
45
46                 public void onFailure(Throwable error) {
47                     logger.error("Error in Datastore operation", error);
48                 };
49             };
50
51     public static List<ListenableFuture<Void>> buildTunnelsToExternalEndPoint(DataBroker dataBroker,List<DPNTEPsInfo> cfgDpnList, IpAddress extIp, Class<? extends TunnelTypeBase> tunType) {
52         List<ListenableFuture<Void>> futures = new ArrayList<>();
53         WriteTransaction t = dataBroker.newWriteOnlyTransaction();
54        if( null != cfgDpnList) {
55           for( DPNTEPsInfo teps : cfgDpnList ) {
56              // CHECK -- Assumption -- Only one End Point / Dpn for GRE/Vxlan Tunnels
57               TunnelEndPoints firstEndPt = teps.getTunnelEndPoints().get(0) ;
58               String interfaceName = firstEndPt.getInterfaceName() ;
59               String trunkInterfaceName = ItmUtils.getTrunkInterfaceName(interfaceName, firstEndPt.getIpAddress().getIpv4Address().getValue(), extIp.getIpv4Address().getValue()) ;
60               char[] subnetMaskArray = firstEndPt.getSubnetMask().getValue() ;
61               String subnetMaskStr = String.valueOf(subnetMaskArray) ;
62               SubnetUtils utils = new SubnetUtils(subnetMaskStr);
63               String dcGwyIpStr = String.valueOf(extIp.getValue());
64               IpAddress gwyIpAddress = (utils.getInfo().isInRange(dcGwyIpStr) ) ? null : firstEndPt.getGwIpAddress() ;
65               String ifDescription = tunType.getName();
66               logger.debug(  " Creating Trunk Interface with parameters trunk I/f Name - {}, parent I/f name - {}, source IP - {}, DC Gateway IP - {} gateway IP - {}",trunkInterfaceName, interfaceName, firstEndPt.getIpAddress(), extIp, gwyIpAddress ) ;
67               Interface iface = ItmUtils.buildTunnelInterface(teps.getDPNID(), trunkInterfaceName, String.format( "%s %s",ifDescription, "Trunk Interface"), true, tunType, firstEndPt.getIpAddress(), extIp, gwyIpAddress) ;
68               logger.debug(  " Trunk Interface builder - {} ", iface ) ;
69               InstanceIdentifier<Interface> trunkIdentifier = ItmUtils.buildId(trunkInterfaceName);
70               logger.debug(  " Trunk Interface Identifier - {} ", trunkIdentifier ) ;
71               logger.trace(  " Writing Trunk Interface to Config DS {}, {} ", trunkIdentifier, iface ) ;
72               //ItmUtils.asyncUpdate(LogicalDatastoreType.CONFIGURATION,trunkIdentifier, iface , dataBroker, ItmUtils.DEFAULT_CALLBACK);
73               t.merge(LogicalDatastoreType.CONFIGURATION, trunkIdentifier, iface, true);
74        //       update_external_tunnels_ds(teps.getDPNID(), extIp, trunkInterfaceName, tunType);
75               InstanceIdentifier<ExternalTunnel> path = InstanceIdentifier.create(
76                       ExternalTunnelList.class)
77                           .child(ExternalTunnel.class, new ExternalTunnelKey(extIp, teps.getDPNID()));   
78               ExternalTunnel tnl = new ExternalTunnelBuilder().setKey(new ExternalTunnelKey(extIp, teps.getDPNID()))
79                                              .setDestinationIP(extIp)
80                                              .setSourceDPN(teps.getDPNID())
81                                              .setTunnelInterfaceName(trunkInterfaceName).build();
82               ItmUtils.asyncUpdate(LogicalDatastoreType.CONFIGURATION, path, tnl, dataBroker, DEFAULT_CALLBACK);
83           }
84           futures.add( t.submit()) ;
85        }
86         return futures ;
87     }
88
89     public static List<ListenableFuture<Void>> buildTunnelsFromDpnToExternalEndPoint(DataBroker dataBroker, List<BigInteger> dpnId, IpAddress extIp, Class<? extends TunnelTypeBase> tunType) {
90         List<ListenableFuture<Void>> futures = new ArrayList<>();
91         List<DPNTEPsInfo> cfgDpnList = new ArrayList<DPNTEPsInfo>() ;
92         List<DPNTEPsInfo> meshedDpnList = ItmUtils.getTunnelMeshInfo(dataBroker) ;
93         if( null != meshedDpnList) {
94            for (BigInteger dpn : dpnId){
95                   for( DPNTEPsInfo teps : meshedDpnList ) {
96                       if( teps.getDPNID().equals(dpn)) {
97                          cfgDpnList.add(teps) ;
98                       }
99                    }
100            }
101           futures = buildTunnelsToExternalEndPoint( dataBroker, cfgDpnList, extIp, tunType) ;
102        }
103         return futures ;
104     }
105 }