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