Adding ITM internal/external tunnel build logic
[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.params.xml.ns.yang.itm.op.rev150701.tunnels.DPNTEPsInfo;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.itm.op.rev150701.tunnels.dpn.teps.info.TunnelEndPoints;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.TunnelTypeBase;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.TunnelTypeGre;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.TunnelTypeVxlan;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.apache.commons.net.util.SubnetUtils;
29
30 import com.google.common.util.concurrent.ListenableFuture;
31
32 public class ItmExternalTunnelAddWorker {
33     private static final Logger logger = LoggerFactory.getLogger(ItmExternalTunnelAddWorker.class ) ;
34
35     public static List<ListenableFuture<Void>> buildTunnelsToExternalEndPoint(DataBroker dataBroker,List<DPNTEPsInfo> meshedDpnList, IpAddress extIp) {
36         List<ListenableFuture<Void>> futures = new ArrayList<>();
37         WriteTransaction t = dataBroker.newWriteOnlyTransaction();
38         if( null == meshedDpnList)
39             ItmUtils.getTunnelMeshInfo(dataBroker) ;
40        if( null != meshedDpnList) {
41           for( DPNTEPsInfo teps : meshedDpnList ) {
42              // CHECK -- Assumption -- Only one End Point / Dpn for GRE/Vxlan Tunnels
43               TunnelEndPoints firstEndPt = teps.getTunnelEndPoints().get(0) ;
44               String interfaceName = firstEndPt.getInterfaceName() ;
45               String trunkInterfaceName = ItmUtils.getTrunkInterfaceName(interfaceName, firstEndPt.getIpAddress().getIpv4Address().getValue(), extIp.getIpv4Address().getValue()) ;
46               char[] subnetMaskArray = firstEndPt.getSubnetMask().getValue() ;
47               String subnetMaskStr = String.valueOf(subnetMaskArray) ;
48               SubnetUtils utils = new SubnetUtils(subnetMaskStr);
49               String dcGwyIpStr = String.valueOf(extIp.getValue());
50               IpAddress gwyIpAddress = (utils.getInfo().isInRange(dcGwyIpStr) ) ? null : firstEndPt.getGwIpAddress() ;
51               Class<? extends TunnelTypeBase> tunType = (teps.getTunnelEndPoints().get(0).getTunnelType().equals("GRE") ) ? TunnelTypeGre.class :TunnelTypeVxlan.class ;
52               String ifDescription = (tunType.equals("GRE") ) ? "GRE" : "VxLan" ;
53               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 ) ;
54               Interface iface = ItmUtils.buildTunnelInterface(teps.getDPNID(), trunkInterfaceName, String.format( "%s %s",ifDescription, "Trunk Interface"), true, tunType, firstEndPt.getIpAddress(), extIp, gwyIpAddress) ;
55               logger.debug(  " Trunk Interface builder - {} ", iface ) ;
56               InstanceIdentifier<Interface> trunkIdentifier = ItmUtils.buildId(trunkInterfaceName);
57               logger.debug(  " Trunk Interface Identifier - {} ", trunkIdentifier ) ;
58               logger.trace(  " Writing Trunk Interface to Config DS {}, {} ", trunkIdentifier, iface ) ;
59               //ItmUtils.asyncUpdate(LogicalDatastoreType.CONFIGURATION,trunkIdentifier, iface , dataBroker, ItmUtils.DEFAULT_CALLBACK);
60               t.merge(LogicalDatastoreType.CONFIGURATION, trunkIdentifier, iface, true);
61           }
62           futures.add( t.submit()) ;
63        }
64         return futures ;
65     }
66
67     public static List<ListenableFuture<Void>> buildTunnelsFromDpnToExternalEndPoint(DataBroker dataBroker,BigInteger dpnId,List<DPNTEPsInfo> meshedDpnList, IpAddress extIp) {
68         List<ListenableFuture<Void>> futures = new ArrayList<>();
69         List<DPNTEPsInfo> cfgDpnList = new ArrayList<DPNTEPsInfo>() ;
70        if( null != meshedDpnList) {
71           for( DPNTEPsInfo teps : meshedDpnList ) {
72              if( teps.getDPNID().equals(dpnId)) {
73                 cfgDpnList.add(teps) ;
74              }
75           }
76           futures = buildTunnelsToExternalEndPoint( dataBroker, cfgDpnList, extIp) ;
77        }
78         return futures ;
79     }
80 }