2 * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
8 package org.opendaylight.vpnservice.itm.confighelpers;
10 import java.math.BigInteger;
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.concurrent.Callable;
15 import com.google.common.util.concurrent.FutureCallback;
16 import com.google.common.util.concurrent.ListenableFuture;
18 import org.opendaylight.vpnservice.mdsalutil.MDSALUtil;
19 import org.opendaylight.vpnservice.itm.globals.ITMConstants;
20 import org.opendaylight.vpnservice.itm.impl.ItmUtils;
21 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
22 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.IdManagerService;
26 import org.opendaylight.vpnservice.mdsalutil.interfaces.IMdsalApiManager;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.TunnelTypeBase;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.TunnelTypeGre;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.TunnelTypeVxlan;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.DpnEndpoints;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.DpnEndpointsBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.ExternalTunnelList;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.TunnelList;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.dpn.endpoints.DPNTEPsInfo;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.dpn.endpoints.dpn.teps.info.TunnelEndPoints;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.external.tunnel.list.ExternalTunnel;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.external.tunnel.list.ExternalTunnelBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.external.tunnel.list.ExternalTunnelKey;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.tunnel.list.InternalTunnel;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.tunnel.list.InternalTunnelKey;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
46 public class ItmInternalTunnelAddWorker {
47 private static final Logger logger = LoggerFactory.getLogger(ItmInternalTunnelAddWorker.class) ;
48 private static final FutureCallback<Void> DEFAULT_CALLBACK =
49 new FutureCallback<Void>() {
50 public void onSuccess(Void result) {
51 logger.debug("Success in Datastore operation");
54 public void onFailure(Throwable error) {
55 logger.error("Error in Datastore operation", error);
60 public static List<ListenableFuture<Void>> build_all_tunnels(DataBroker dataBroker, IdManagerService idManagerService,IMdsalApiManager mdsalManager,
61 List<DPNTEPsInfo> cfgdDpnList, List<DPNTEPsInfo> meshedDpnList) {
62 logger.trace( "Building tunnels with DPN List {} " , cfgdDpnList );
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");
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<DPNTEPsInfo>() ;
76 meshedDpnList.add(dpn) ;
77 // Update the operational datastore -- FIXME -- Error Handling
78 updateOperationalDatastore(dataBroker, dpn, t, futures) ;
80 futures.add( t.submit()) ;
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<DPNTEPsInfo>() ;
89 DpnEndpoints tnlBuilder = new DpnEndpointsBuilder().setDPNTEPsInfo(dpnList).build() ;
90 t.merge(LogicalDatastoreType.CONFIGURATION, dep, tnlBuilder, true);
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 );
96 if( null == meshedDpnList || 0 == meshedDpnList.size()) {
97 logger.debug( "No DPN in the mesh ");
100 for( DPNTEPsInfo dstDpn: meshedDpnList) {
101 if ( ! srcDpn.equals(dstDpn) )
102 wireUpWithinTransportZone(srcDpn, dstDpn, dataBroker, idManagerService, mdsalManager, t, futures) ;
107 private static void wireUpWithinTransportZone( DPNTEPsInfo srcDpn, DPNTEPsInfo dstDpn, DataBroker dataBroker,
108 IdManagerService idManagerService, IMdsalApiManager mdsalManager,WriteTransaction t, List<ListenableFuture<Void>> futures) {
109 logger.trace( "Wiring up within Transport Zone for Dpns {}, {} " , srcDpn, dstDpn );
110 List<TunnelEndPoints> srcEndPts = srcDpn.getTunnelEndPoints();
111 List<TunnelEndPoints> dstEndPts = dstDpn.getTunnelEndPoints();
113 for( TunnelEndPoints srcte : srcEndPts) {
114 for( TunnelEndPoints dstte : dstEndPts ) {
115 // Compare the Transport zones
116 if (!srcDpn.getDPNID().equals(dstDpn.getDPNID())) {
117 if( (srcte.getTransportZone().equals(dstte.getTransportZone()))) {
119 wireUpBidirectionalTunnel( srcte, dstte, srcDpn.getDPNID(), dstDpn.getDPNID(), dataBroker, idManagerService, mdsalManager, t, futures );
120 // CHECK THIS -- Assumption -- One end point per Dpn per transport zone
128 private static void wireUpBidirectionalTunnel( TunnelEndPoints srcte, TunnelEndPoints dstte, BigInteger srcDpnId, BigInteger dstDpnId,
129 DataBroker dataBroker, IdManagerService idManagerService, IMdsalApiManager mdsalManager, WriteTransaction t, List<ListenableFuture<Void>> futures) {
130 // Setup the flow for LLDP monitoring -- PUNT TO CONTROLLER
131 ItmUtils.setUpOrRemoveTerminatingServiceTable(srcDpnId, mdsalManager, true);
132 ItmUtils.setUpOrRemoveTerminatingServiceTable(dstDpnId, mdsalManager, true);
134 // Create the forward direction tunnel
135 if(!wireUp( srcte, dstte, srcDpnId, dstDpnId, dataBroker, idManagerService, t, futures ))
136 logger.error("Could not build tunnel between end points {}, {} " , srcte, dstte );
138 // CHECK IF FORWARD IS NOT BUILT , REVERSE CAN BE BUILT
139 // Create the tunnel for the reverse direction
140 if(! wireUp( dstte, srcte, dstDpnId, srcDpnId, dataBroker, idManagerService, t, futures ))
141 logger.error("Could not build tunnel between end points {}, {} " , dstte, srcte);
144 private static boolean wireUp(TunnelEndPoints srcte, TunnelEndPoints dstte, BigInteger srcDpnId, BigInteger dstDpnId ,
145 DataBroker dataBroker, IdManagerService idManagerService, WriteTransaction t, List<ListenableFuture<Void>> futures) {
147 logger.trace( "Wiring between source tunnel end points {}, destination tunnel end points {} " , srcte, dstte );
148 String interfaceName = srcte.getInterfaceName() ;
149 Class<? extends TunnelTypeBase> tunType = srcte.getTunnelType();
150 String ifDescription = srcte.getTunnelType().getName();
151 // Form the trunk Interface Name
152 String trunkInterfaceName = ItmUtils.getTrunkInterfaceName(idManagerService, interfaceName,srcte.getIpAddress().getIpv4Address().getValue(), dstte.getIpAddress().getIpv4Address().getValue()) ;
153 IpAddress gwyIpAddress = ( srcte.getSubnetMask().equals(dstte.getSubnetMask()) ) ? null : srcte.getGwIpAddress() ;
154 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 ) ;
155 Interface iface = ItmUtils.buildTunnelInterface(srcDpnId, trunkInterfaceName, String.format( "%s %s",ifDescription, "Trunk Interface"), true, tunType, srcte.getIpAddress(), dstte.getIpAddress(), gwyIpAddress, true) ;
156 logger.debug( " Trunk Interface builder - {} ", iface ) ;
157 InstanceIdentifier<Interface> trunkIdentifier = ItmUtils.buildId(trunkInterfaceName);
158 logger.debug( " Trunk Interface Identifier - {} ", trunkIdentifier ) ;
159 logger.trace( " Writing Trunk Interface to Config DS {}, {} ", trunkIdentifier, iface ) ;
160 t.merge(LogicalDatastoreType.CONFIGURATION, trunkIdentifier, iface, true);
161 // also update itm-state ds?
162 InstanceIdentifier<InternalTunnel> path = InstanceIdentifier.create(
164 .child(InternalTunnel.class, new InternalTunnelKey( srcDpnId, dstDpnId));
165 InternalTunnel tnl = ItmUtils.buildInternalTunnel(srcDpnId, dstDpnId, trunkInterfaceName);
166 //ItmUtils.asyncUpdate(LogicalDatastoreType.CONFIGURATION, path, tnl, dataBroker, DEFAULT_CALLBACK);
167 t.merge(LogicalDatastoreType.CONFIGURATION,path, tnl, true) ;