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