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