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