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;
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.vpnservice.itm.impl.ItmUtils;
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.vpnservice.idmanager.rev150403.IdManagerService;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.TunnelTypeBase;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.TunnelTypeGre;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.TunnelTypeVxlan;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.ExternalTunnelList;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.dpn.endpoints.DPNTEPsInfo;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.dpn.endpoints.dpn.teps.info.TunnelEndPoints;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.external.tunnel.list.ExternalTunnel;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.external.tunnel.list.ExternalTunnelBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.op.rev150701.external.tunnel.list.ExternalTunnelKey;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.apache.commons.net.util.SubnetUtils;
35 import com.google.common.util.concurrent.FutureCallback;
36 import com.google.common.util.concurrent.ListenableFuture;
38 public class ItmExternalTunnelAddWorker {
39 private static final Logger logger = LoggerFactory.getLogger(ItmExternalTunnelAddWorker.class ) ;
41 private static final FutureCallback<Void> DEFAULT_CALLBACK =
42 new FutureCallback<Void>() {
43 public void onSuccess(Void result) {
44 logger.debug("Success in Datastore operation");
47 public void onFailure(Throwable error) {
48 logger.error("Error in Datastore operation", error);
52 public static List<ListenableFuture<Void>> buildTunnelsToExternalEndPoint(DataBroker dataBroker, IdManagerService idManagerService,
53 List<DPNTEPsInfo> cfgDpnList, IpAddress extIp, Class<? extends TunnelTypeBase> tunType) {
54 List<ListenableFuture<Void>> futures = new ArrayList<>();
55 WriteTransaction t = dataBroker.newWriteOnlyTransaction();
56 if( null != cfgDpnList) {
57 for( DPNTEPsInfo teps : cfgDpnList ) {
58 // CHECK -- Assumption -- Only one End Point / Dpn for GRE/Vxlan Tunnels
59 TunnelEndPoints firstEndPt = teps.getTunnelEndPoints().get(0) ;
60 String interfaceName = firstEndPt.getInterfaceName() ;
61 String trunkInterfaceName = ItmUtils.getTrunkInterfaceName(idManagerService, interfaceName, firstEndPt.getIpAddress().getIpv4Address().getValue(), extIp.getIpv4Address().getValue()) ;
62 char[] subnetMaskArray = firstEndPt.getSubnetMask().getValue() ;
63 String subnetMaskStr = String.valueOf(subnetMaskArray) ;
64 SubnetUtils utils = new SubnetUtils(subnetMaskStr);
65 String dcGwyIpStr = String.valueOf(extIp.getValue());
66 IpAddress gwyIpAddress = (utils.getInfo().isInRange(dcGwyIpStr) ) ? null : firstEndPt.getGwIpAddress() ;
67 String ifDescription = tunType.getName();
68 logger.debug( " Creating Trunk Interface with parameters trunk I/f Name - {}, parent I/f name - {}, source IP - {}, DC Gateway IP - {} gateway IP - {}",trunkInterfaceName, interfaceName, firstEndPt.getIpAddress(), extIp, gwyIpAddress ) ;
69 Interface iface = ItmUtils.buildTunnelInterface(teps.getDPNID(), trunkInterfaceName, String.format( "%s %s",ifDescription, "Trunk Interface"), true, tunType, firstEndPt.getIpAddress(), extIp, gwyIpAddress, false) ;
70 logger.debug( " Trunk Interface builder - {} ", iface ) ;
71 InstanceIdentifier<Interface> trunkIdentifier = ItmUtils.buildId(trunkInterfaceName);
72 logger.debug( " Trunk Interface Identifier - {} ", trunkIdentifier ) ;
73 logger.trace( " Writing Trunk Interface to Config DS {}, {} ", trunkIdentifier, iface ) ;
74 t.merge(LogicalDatastoreType.CONFIGURATION, trunkIdentifier, iface, true);
75 // update external_tunnel_list ds
76 InstanceIdentifier<ExternalTunnel> path = InstanceIdentifier.create(
77 ExternalTunnelList.class)
78 .child(ExternalTunnel.class, new ExternalTunnelKey(extIp, teps.getDPNID()));
79 ExternalTunnel tnl = ItmUtils.buildExternalTunnel(teps.getDPNID(), extIp, trunkInterfaceName);
80 t.merge(LogicalDatastoreType.CONFIGURATION,path, tnl, true) ;
82 futures.add( t.submit()) ;
87 public static List<ListenableFuture<Void>> buildTunnelsFromDpnToExternalEndPoint(DataBroker dataBroker, IdManagerService idManagerService,
88 List<BigInteger> dpnId, IpAddress extIp, Class<? extends TunnelTypeBase> tunType) {
89 List<ListenableFuture<Void>> futures = new ArrayList<>();
90 List<DPNTEPsInfo> cfgDpnList =( dpnId == null ) ? ItmUtils.getTunnelMeshInfo(dataBroker) :ItmUtils.getDPNTEPListFromDPNId(dataBroker, dpnId) ;
91 futures = buildTunnelsToExternalEndPoint( dataBroker, idManagerService, cfgDpnList, extIp, tunType) ;