Merge "Using latest RPC from interface mgr"
[vpnservice.git] / itm / itm-impl / src / main / java / org / opendaylight / vpnservice / itm / confighelpers / ItmInternalTunnelAddWorker.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 import java.util.concurrent.Callable;
14
15 import com.google.common.util.concurrent.FutureCallback;
16 import com.google.common.util.concurrent.ListenableFuture;
17
18 import org.opendaylight.vpnservice.itm.impl.ItmUtils;
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.TunnelTypeBase;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.TunnelTypeGre;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.TunnelTypeVxlan;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.DpnEndpoints;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.DpnEndpointsBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.ExternalTunnelList;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.TunnelList;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.dpn.endpoints.DPNTEPsInfo;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.dpn.endpoints.dpn.teps.info.TunnelEndPoints;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.external.tunnel.list.ExternalTunnel;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.external.tunnel.list.ExternalTunnelBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.external.tunnel.list.ExternalTunnelKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.tunnel.list.Tunnel;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.tunnel.list.TunnelBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.tunnel.list.TunnelKey;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 public class ItmInternalTunnelAddWorker {
44      private static final Logger logger = LoggerFactory.getLogger(ItmInternalTunnelAddWorker.class) ;
45      private static final FutureCallback<Void> DEFAULT_CALLBACK =
46              new FutureCallback<Void>() {
47                  public void onSuccess(Void result) {
48                      logger.debug("Success in Datastore operation");
49                  }
50
51                  public void onFailure(Throwable error) {
52                      logger.error("Error in Datastore operation", error);
53                  };
54              };
55
56
57     public static List<ListenableFuture<Void>> build_all_tunnels(DataBroker dataBroker, List<DPNTEPsInfo> cfgdDpnList, List<DPNTEPsInfo> meshedDpnList) {
58         logger.trace( "Building tunnels with DPN List {} " , cfgdDpnList );
59         List<ListenableFuture<Void>> futures = new ArrayList<>();
60         WriteTransaction t = dataBroker.newWriteOnlyTransaction();
61         if( null == cfgdDpnList || cfgdDpnList.isEmpty()) {
62             logger.error(" Build Tunnels was invoked with empty list");
63             return null;
64          }
65
66         for( DPNTEPsInfo dpn : cfgdDpnList) {
67             //#####if dpn is not in meshedDpnList
68             build_tunnel_from(dpn, meshedDpnList, dataBroker, t, futures);
69             if(null == meshedDpnList) {
70                 meshedDpnList = new ArrayList<DPNTEPsInfo>() ;
71             }
72             meshedDpnList.add(dpn) ;
73             // Update the operational datastore -- FIXME -- Error Handling
74             updateOperationalDatastore(dataBroker, dpn, t, futures) ;
75         }
76         futures.add( t.submit()) ;
77         return futures ;
78     }
79
80     private static void updateOperationalDatastore(DataBroker dataBroker, DPNTEPsInfo dpn, WriteTransaction t, List<ListenableFuture<Void>> futures) {
81         logger.debug("Updating CONFIGURATION datastore with DPN {} ", dpn);
82         InstanceIdentifier<DpnEndpoints> dep = InstanceIdentifier.builder( DpnEndpoints.class).build() ;
83         List<DPNTEPsInfo> dpnList = new ArrayList<DPNTEPsInfo>() ;
84         dpnList.add(dpn) ;
85         DpnEndpoints tnlBuilder = new DpnEndpointsBuilder().setDPNTEPsInfo(dpnList).build() ;
86         t.merge(LogicalDatastoreType.CONFIGURATION, dep, tnlBuilder, true);
87     }
88
89     private static void build_tunnel_from( DPNTEPsInfo srcDpn,List<DPNTEPsInfo> meshedDpnList, DataBroker dataBroker,  WriteTransaction t, List<ListenableFuture<Void>> futures) {
90         logger.trace( "Building tunnels from DPN {} " , srcDpn );
91
92         if( null == meshedDpnList || 0 == meshedDpnList.size()) {
93             logger.debug( "No DPN in the mesh ");
94             return ;
95         }
96         for( DPNTEPsInfo dstDpn: meshedDpnList) {
97             if ( ! srcDpn.equals(dstDpn) )
98                     wireUpWithinTransportZone(srcDpn, dstDpn, dataBroker, t, futures) ;
99         }
100
101    }
102
103     private static void wireUpWithinTransportZone( DPNTEPsInfo srcDpn, DPNTEPsInfo dstDpn, DataBroker dataBroker,  WriteTransaction t, List<ListenableFuture<Void>> futures) {
104         logger.trace( "Wiring up within Transport Zone for Dpns {}, {} " , srcDpn, dstDpn );
105         List<TunnelEndPoints> srcEndPts = srcDpn.getTunnelEndPoints();
106         List<TunnelEndPoints> dstEndPts = dstDpn.getTunnelEndPoints();
107
108         for( TunnelEndPoints srcte : srcEndPts) {
109             for( TunnelEndPoints dstte : dstEndPts ) {
110                 // Compare the Transport zones
111               if (!srcDpn.getDPNID().equals(dstDpn.getDPNID())) {
112                 if( (srcte.getTransportZone().equals(dstte.getTransportZone()))) {
113                      // wire them up
114                      wireUpBidirectionalTunnel( srcte, dstte, srcDpn.getDPNID(), dstDpn.getDPNID(), dataBroker, t, futures );
115                      // CHECK THIS -- Assumption -- One end point per Dpn per transport zone
116                      break ;
117                 }
118               }
119             }
120          }
121     }
122
123     private static void wireUpBidirectionalTunnel( TunnelEndPoints srcte, TunnelEndPoints dstte, BigInteger srcDpnId, BigInteger dstDpnId,
124         DataBroker dataBroker,  WriteTransaction t, List<ListenableFuture<Void>> futures) {
125        // Setup the flow for LLDP monitoring -- PUNT TO CONTROLLER
126        //  setUpOrRemoveTerminatingServiceTable(srcDpnId, true);
127        //  setUpOrRemoveTerminatingServiceTable(dstDpnId, true);
128
129         // Create the forward direction tunnel
130         if(!wireUp( srcte, dstte, srcDpnId, dstDpnId, dataBroker, t, futures ))
131            logger.error("Could not build tunnel between end points {}, {} " , srcte, dstte );
132
133         // CHECK IF FORWARD IS NOT BUILT , REVERSE CAN BE BUILT
134       // Create the tunnel for the reverse direction
135        if(! wireUp( dstte, srcte, dstDpnId, srcDpnId, dataBroker, t, futures ))
136           logger.error("Could not build tunnel between end points {}, {} " , dstte, srcte);
137     }
138
139     private static boolean wireUp( TunnelEndPoints srcte, TunnelEndPoints dstte, BigInteger srcDpnId, BigInteger dstDpnId ,
140           DataBroker dataBroker,  WriteTransaction t, List<ListenableFuture<Void>> futures) {
141         // Wire Up logic
142         logger.trace( "Wiring between source tunnel end points {}, destination tunnel end points {} " , srcte, dstte );
143         String interfaceName = srcte.getInterfaceName() ;
144         Class<? extends TunnelTypeBase> tunType = srcte.getTunnelType();
145         String ifDescription = srcte.getTunnelType().getName();
146         // Form the trunk Interface Name
147         String trunkInterfaceName = ItmUtils.getTrunkInterfaceName(interfaceName,srcte.getIpAddress().getIpv4Address().getValue(), dstte.getIpAddress().getIpv4Address().getValue()) ;
148         IpAddress gwyIpAddress = ( srcte.getSubnetMask().equals(dstte.getSubnetMask()) ) ? null : srcte.getGwIpAddress() ;
149         logger.debug(  " Creating Trunk Interface with parameters trunk I/f Name - {}, parent I/f name - {}, source IP - {}, destination IP - {} gateway IP - {}",trunkInterfaceName, interfaceName, srcte.getIpAddress(), dstte.getIpAddress(), gwyIpAddress ) ;
150         Interface iface = ItmUtils.buildTunnelInterface(srcDpnId, trunkInterfaceName, String.format( "%s %s",ifDescription, "Trunk Interface"), true, tunType, srcte.getIpAddress(), dstte.getIpAddress(), gwyIpAddress) ;
151         logger.debug(  " Trunk Interface builder - {} ", iface ) ;
152         InstanceIdentifier<Interface> trunkIdentifier = ItmUtils.buildId(trunkInterfaceName);
153         logger.debug(  " Trunk Interface Identifier - {} ", trunkIdentifier ) ;
154         logger.trace(  " Writing Trunk Interface to Config DS {}, {} ", trunkIdentifier, iface ) ;
155         t.merge(LogicalDatastoreType.CONFIGURATION, trunkIdentifier, iface, true);
156         // also update itm-state ds?
157         InstanceIdentifier<Tunnel> path = InstanceIdentifier.create(
158                 TunnelList.class)
159                     .child(Tunnel.class, new TunnelKey(dstDpnId, srcDpnId));   
160         Tunnel tnl = new TunnelBuilder().setKey(new TunnelKey(dstDpnId, srcDpnId))
161                                        .setDestinationDPN(dstDpnId)
162                                        .setSourceDPN(srcDpnId)
163                                        .setTunnelInterfaceName(trunkInterfaceName).build();
164         ItmUtils.asyncUpdate(LogicalDatastoreType.CONFIGURATION, path, tnl, dataBroker, DEFAULT_CALLBACK);
165         return true;
166     }
167
168 }