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