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.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.InternalTunnel;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.tunnel.list.InternalTunnelKey;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 public class ItmInternalTunnelAddWorker {
44 private static final Logger logger = LoggerFactory.getLogger(ItmInternalTunnelAddWorker.class) ;
45 private static final FutureCallback<Void> DEFAULT_CALLBACK =
46 new FutureCallback<Void>() {
47 public void onSuccess(Void result) {
48 logger.debug("Success in Datastore operation");
51 public void onFailure(Throwable error) {
52 logger.error("Error in Datastore operation", error);
57 public static List<ListenableFuture<Void>> build_all_tunnels(DataBroker dataBroker, IdManagerService idManagerService,
58 List<DPNTEPsInfo> cfgdDpnList, List<DPNTEPsInfo> meshedDpnList) {
59 logger.trace( "Building tunnels with DPN List {} " , cfgdDpnList );
60 List<ListenableFuture<Void>> futures = new ArrayList<>();
61 WriteTransaction t = dataBroker.newWriteOnlyTransaction();
62 if( null == cfgdDpnList || cfgdDpnList.isEmpty()) {
63 logger.error(" Build Tunnels was invoked with empty list");
67 for( DPNTEPsInfo dpn : cfgdDpnList) {
68 //#####if dpn is not in meshedDpnList
69 build_tunnel_from(dpn, meshedDpnList, dataBroker, idManagerService, t, futures);
70 if(null == meshedDpnList) {
71 meshedDpnList = new ArrayList<DPNTEPsInfo>() ;
73 meshedDpnList.add(dpn) ;
74 // Update the operational datastore -- FIXME -- Error Handling
75 updateOperationalDatastore(dataBroker, dpn, t, futures) ;
77 futures.add( t.submit()) ;
81 private static void updateOperationalDatastore(DataBroker dataBroker, DPNTEPsInfo dpn, WriteTransaction t, List<ListenableFuture<Void>> futures) {
82 logger.debug("Updating CONFIGURATION datastore with DPN {} ", dpn);
83 InstanceIdentifier<DpnEndpoints> dep = InstanceIdentifier.builder( DpnEndpoints.class).build() ;
84 List<DPNTEPsInfo> dpnList = new ArrayList<DPNTEPsInfo>() ;
86 DpnEndpoints tnlBuilder = new DpnEndpointsBuilder().setDPNTEPsInfo(dpnList).build() ;
87 t.merge(LogicalDatastoreType.CONFIGURATION, dep, tnlBuilder, true);
90 private static void build_tunnel_from( DPNTEPsInfo srcDpn,List<DPNTEPsInfo> meshedDpnList, DataBroker dataBroker, IdManagerService idManagerService, WriteTransaction t, List<ListenableFuture<Void>> futures) {
91 logger.trace( "Building tunnels from DPN {} " , srcDpn );
93 if( null == meshedDpnList || 0 == meshedDpnList.size()) {
94 logger.debug( "No DPN in the mesh ");
97 for( DPNTEPsInfo dstDpn: meshedDpnList) {
98 if ( ! srcDpn.equals(dstDpn) )
99 wireUpWithinTransportZone(srcDpn, dstDpn, dataBroker, idManagerService, t, futures) ;
104 private static void wireUpWithinTransportZone( DPNTEPsInfo srcDpn, DPNTEPsInfo dstDpn, DataBroker dataBroker,
105 IdManagerService idManagerService, WriteTransaction t, List<ListenableFuture<Void>> futures) {
106 logger.trace( "Wiring up within Transport Zone for Dpns {}, {} " , srcDpn, dstDpn );
107 List<TunnelEndPoints> srcEndPts = srcDpn.getTunnelEndPoints();
108 List<TunnelEndPoints> dstEndPts = dstDpn.getTunnelEndPoints();
110 for( TunnelEndPoints srcte : srcEndPts) {
111 for( TunnelEndPoints dstte : dstEndPts ) {
112 // Compare the Transport zones
113 if (!srcDpn.getDPNID().equals(dstDpn.getDPNID())) {
114 if( (srcte.getTransportZone().equals(dstte.getTransportZone()))) {
116 wireUpBidirectionalTunnel( srcte, dstte, srcDpn.getDPNID(), dstDpn.getDPNID(), dataBroker, idManagerService, t, futures );
117 // CHECK THIS -- Assumption -- One end point per Dpn per transport zone
125 private static void wireUpBidirectionalTunnel( TunnelEndPoints srcte, TunnelEndPoints dstte, BigInteger srcDpnId, BigInteger dstDpnId,
126 DataBroker dataBroker, IdManagerService idManagerService, WriteTransaction t, List<ListenableFuture<Void>> futures) {
127 // Setup the flow for LLDP monitoring -- PUNT TO CONTROLLER
128 // setUpOrRemoveTerminatingServiceTable(srcDpnId, true);
129 // setUpOrRemoveTerminatingServiceTable(dstDpnId, true);
131 // Create the forward direction tunnel
132 if(!wireUp( srcte, dstte, srcDpnId, dstDpnId, dataBroker, idManagerService, t, futures ))
133 logger.error("Could not build tunnel between end points {}, {} " , srcte, dstte );
135 // CHECK IF FORWARD IS NOT BUILT , REVERSE CAN BE BUILT
136 // Create the tunnel for the reverse direction
137 if(! wireUp( dstte, srcte, dstDpnId, srcDpnId, dataBroker, idManagerService, t, futures ))
138 logger.error("Could not build tunnel between end points {}, {} " , dstte, srcte);
141 private static boolean wireUp(TunnelEndPoints srcte, TunnelEndPoints dstte, BigInteger srcDpnId, BigInteger dstDpnId ,
142 DataBroker dataBroker, IdManagerService idManagerService, WriteTransaction t, List<ListenableFuture<Void>> futures) {
144 logger.trace( "Wiring between source tunnel end points {}, destination tunnel end points {} " , srcte, dstte );
145 String interfaceName = srcte.getInterfaceName() ;
146 Class<? extends TunnelTypeBase> tunType = srcte.getTunnelType();
147 String ifDescription = srcte.getTunnelType().getName();
148 // Form the trunk Interface Name
149 String trunkInterfaceName = ItmUtils.getTrunkInterfaceName(idManagerService, interfaceName,srcte.getIpAddress().getIpv4Address().getValue(), dstte.getIpAddress().getIpv4Address().getValue()) ;
150 IpAddress gwyIpAddress = ( srcte.getSubnetMask().equals(dstte.getSubnetMask()) ) ? null : srcte.getGwIpAddress() ;
151 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 ) ;
152 Interface iface = ItmUtils.buildTunnelInterface(srcDpnId, trunkInterfaceName, String.format( "%s %s",ifDescription, "Trunk Interface"), true, tunType, srcte.getIpAddress(), dstte.getIpAddress(), gwyIpAddress, true) ;
153 logger.debug( " Trunk Interface builder - {} ", iface ) ;
154 InstanceIdentifier<Interface> trunkIdentifier = ItmUtils.buildId(trunkInterfaceName);
155 logger.debug( " Trunk Interface Identifier - {} ", trunkIdentifier ) ;
156 logger.trace( " Writing Trunk Interface to Config DS {}, {} ", trunkIdentifier, iface ) ;
157 t.merge(LogicalDatastoreType.CONFIGURATION, trunkIdentifier, iface, true);
158 // also update itm-state ds?
159 InstanceIdentifier<InternalTunnel> path = InstanceIdentifier.create(
161 .child(InternalTunnel.class, new InternalTunnelKey( srcDpnId, dstDpnId));
162 InternalTunnel tnl = ItmUtils.buildInternalTunnel(srcDpnId, dstDpnId, trunkInterfaceName);
163 //ItmUtils.asyncUpdate(LogicalDatastoreType.CONFIGURATION, path, tnl, dataBroker, DEFAULT_CALLBACK);
164 t.merge(LogicalDatastoreType.CONFIGURATION,path, tnl, true) ;