Merge "Load config system files in etc/opendaylight/karaf"
[genius.git] / itm / itm-impl / src / main / java / org / opendaylight / genius / itm / confighelpers / ItmExternalTunnelAddWorker.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.base.Optional;
11 import com.google.common.util.concurrent.FutureCallback;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import org.apache.commons.net.util.SubnetUtils;
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.genius.itm.globals.ITMConstants;
18 import org.opendaylight.genius.itm.impl.ItmUtils;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
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.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelMonitoringTypeBase;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelTypeBase;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelTypeVxlan;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.ExternalTunnelList;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.endpoints.DPNTEPsInfo;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.endpoints.dpn.teps.info.TunnelEndPoints;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.external.tunnel.list.ExternalTunnel;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.external.tunnel.list.ExternalTunnelKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.TransportZones;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.TransportZone;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.TransportZoneKey;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.transport.zone.Subnets;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.transport.zone.subnets.DeviceVteps;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.transport.zone.subnets.Vteps;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 import java.math.BigInteger;
44 import java.util.ArrayList;
45 import java.util.List;
46
47 public class ItmExternalTunnelAddWorker {
48     private static final Logger logger = LoggerFactory.getLogger(ItmExternalTunnelAddWorker.class);
49
50     private static final FutureCallback<Void> DEFAULT_CALLBACK =
51             new FutureCallback<Void>() {
52                 public void onSuccess(Void result) {
53                     logger.debug("Success in Datastore operation");
54                 }
55
56                 public void onFailure(Throwable error) {
57                     logger.error("Error in Datastore operation", error);
58                 }
59
60                 ;
61             };
62
63     public static List<ListenableFuture<Void>> buildTunnelsToExternalEndPoint(DataBroker dataBroker, IdManagerService idManagerService,
64                                                                               List<DPNTEPsInfo> cfgDpnList, IpAddress extIp, Class<? extends TunnelTypeBase> tunType) {
65         List<ListenableFuture<Void>> futures = new ArrayList<>();
66         WriteTransaction t = dataBroker.newWriteOnlyTransaction();
67         if (null != cfgDpnList) {
68             for (DPNTEPsInfo teps : cfgDpnList) {
69                 // CHECK -- Assumption -- Only one End Point / Dpn for GRE/Vxlan Tunnels
70                 TunnelEndPoints firstEndPt = teps.getTunnelEndPoints().get(0);
71                 String interfaceName = firstEndPt.getInterfaceName();
72                 String tunTypeStr = tunType.getName();
73                 String trunkInterfaceName = ItmUtils.getTrunkInterfaceName(idManagerService, interfaceName, firstEndPt.getIpAddress().getIpv4Address().getValue(), extIp.getIpv4Address().getValue(), tunTypeStr);
74                 char[] subnetMaskArray = firstEndPt.getSubnetMask().getValue();
75                 String subnetMaskStr = String.valueOf(subnetMaskArray);
76                 SubnetUtils utils = new SubnetUtils(subnetMaskStr);
77                 String dcGwyIpStr = String.valueOf(extIp.getValue());
78                 IpAddress gatewayIpObj = new IpAddress("0.0.0.0".toCharArray());
79                 IpAddress gwyIpAddress = (utils.getInfo().isInRange(dcGwyIpStr)) ? gatewayIpObj : firstEndPt.getGwIpAddress();
80                 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);
81                 Interface iface = ItmUtils.buildTunnelInterface(teps.getDPNID(), trunkInterfaceName, String.format("%s %s", ItmUtils.convertTunnelTypetoString(tunType), "Trunk Interface"), true, tunType, firstEndPt.getIpAddress(), extIp, gwyIpAddress, firstEndPt.getVLANID(), false,false,ITMConstants.DEFAULT_MONITOR_PROTOCOL,null);
82                 logger.debug(" Trunk Interface builder - {} ", iface);
83                 InstanceIdentifier<Interface> trunkIdentifier = ItmUtils.buildId(trunkInterfaceName);
84                 logger.debug(" Trunk Interface Identifier - {} ", trunkIdentifier);
85                 logger.trace(" Writing Trunk Interface to Config DS {}, {} ", trunkIdentifier, iface);
86                 t.merge(LogicalDatastoreType.CONFIGURATION, trunkIdentifier, iface, true);
87                 // update external_tunnel_list ds
88                 InstanceIdentifier<ExternalTunnel> path = InstanceIdentifier.create(
89                         ExternalTunnelList.class)
90                         .child(ExternalTunnel.class, new ExternalTunnelKey(extIp.toString(), teps.getDPNID().toString(), tunType));
91                 ExternalTunnel tnl = ItmUtils.buildExternalTunnel(  teps.getDPNID().toString(), extIp.toString(),
92                         tunType, trunkInterfaceName);
93                 t.merge(LogicalDatastoreType.CONFIGURATION, path, tnl, true);
94             }
95             futures.add(t.submit());
96         }
97         return futures;
98     }
99
100     public static List<ListenableFuture<Void>> buildTunnelsFromDpnToExternalEndPoint(DataBroker dataBroker, IdManagerService idManagerService,
101                                                                                      List<BigInteger> dpnId, IpAddress extIp, Class<? extends TunnelTypeBase> tunType) {
102         List<ListenableFuture<Void>> futures = new ArrayList<>();
103         List<DPNTEPsInfo> cfgDpnList = (dpnId == null) ? ItmUtils.getTunnelMeshInfo(dataBroker) : ItmUtils.getDPNTEPListFromDPNId(dataBroker, dpnId);
104         futures = buildTunnelsToExternalEndPoint(dataBroker, idManagerService, cfgDpnList, extIp, tunType);
105         return futures;
106     }
107
108     public static List<ListenableFuture<Void>> buildHwVtepsTunnels(DataBroker dataBroker, IdManagerService idManagerService, List<DPNTEPsInfo> cfgdDpnList, List<HwVtep> cfgdHwVteps) {
109         List<ListenableFuture<Void>> futures = new ArrayList<>();
110         WriteTransaction t = dataBroker.newWriteOnlyTransaction();
111         if (null != cfgdDpnList && !cfgdDpnList.isEmpty()) {
112             logger.trace("calling tunnels from css {}",cfgdDpnList);
113             tunnelsFromCSS(cfgdDpnList, idManagerService , futures, t , dataBroker);
114
115         }
116         if (null != cfgdHwVteps && !cfgdHwVteps.isEmpty() ) {
117             logger.trace("calling tunnels from hwTep {}",cfgdHwVteps);
118             tunnelsFromhWVtep(cfgdHwVteps, idManagerService, futures, t, dataBroker);
119         }
120
121         if ((cfgdDpnList != null && !cfgdDpnList.isEmpty()) || (cfgdHwVteps != null && !cfgdHwVteps.isEmpty()))
122             futures.add(t.submit());
123         return futures;
124     }
125
126     private static void tunnelsFromCSS(List<DPNTEPsInfo> cfgdDpnList, IdManagerService idManagerService, List<ListenableFuture<Void>> futures, WriteTransaction t, DataBroker dataBroker) {
127         Boolean monitorEnabled = ItmUtils.readMonitoringStateFromDS(dataBroker);
128         Class<? extends TunnelMonitoringTypeBase> monitorProtocol = ITMConstants.DEFAULT_MONITOR_PROTOCOL;
129         for (DPNTEPsInfo dpn : cfgdDpnList) {
130             logger.trace("processing dpn {}" , dpn);
131             if (dpn.getTunnelEndPoints() != null && !dpn.getTunnelEndPoints().isEmpty())
132                 for (TunnelEndPoints tep : dpn.getTunnelEndPoints()) {
133                     InstanceIdentifier<TransportZone> tzonePath = InstanceIdentifier.builder(TransportZones.class).child(TransportZone.class, new TransportZoneKey((tep.getTransportZone()))).build();
134                     Optional<TransportZone> tZoneOptional = ItmUtils.read(LogicalDatastoreType.CONFIGURATION, tzonePath, dataBroker);
135                     if (tZoneOptional.isPresent()) {
136                         TransportZone tZone = tZoneOptional.get();
137                         //do we need to check tunnel type?
138                         if (tZone.getSubnets() != null && !tZone.getSubnets().isEmpty()) {
139                             for (Subnets sub : tZone.getSubnets()) {
140                                 if (sub.getDeviceVteps() != null && !sub.getDeviceVteps().isEmpty()) {
141                                     for (DeviceVteps hwVtepDS : sub.getDeviceVteps()) {
142                                         //dont mesh if hwVteps and CSS-tep have same ip-address
143                                         if(hwVtepDS.getIpAddress().equals(tep.getIpAddress()))
144                                             continue;
145                                         String cssID = dpn.getDPNID().toString();
146                                         String nodeId = hwVtepDS.getNodeId();
147                                         logger.trace("wire up {} and {}",tep, hwVtepDS);
148                                         if (!wireUp(dpn.getDPNID(), tep.getPortname(), sub.getVlanId(), tep.getIpAddress(), nodeId, hwVtepDS.getIpAddress(), tep.getSubnetMask(),
149                                                 sub.getGatewayIp(), sub.getPrefix(), tZone.getTunnelType(),monitorEnabled, monitorProtocol, ITMConstants.BFD_DEFAULT_MONITOR_INTERVAL, idManagerService, dataBroker, futures, t))
150                                             logger.error("Unable to build tunnel {} -- {}", tep.getIpAddress(), hwVtepDS.getIpAddress());
151                                         //TOR-CSS
152                                         logger.trace("wire up {} and {}", hwVtepDS,tep);
153                                         if (!wireUp(hwVtepDS.getTopologyId(), hwVtepDS.getNodeId(), hwVtepDS.getIpAddress(), cssID, tep.getIpAddress(), sub.getPrefix(),
154                                                 sub.getGatewayIp(), tep.getSubnetMask(), tZone.getTunnelType(), monitorEnabled, monitorProtocol, ITMConstants.BFD_DEFAULT_MONITOR_INTERVAL, idManagerService, dataBroker, futures, t))
155                                             logger.error("Unable to build tunnel {} -- {}", hwVtepDS.getIpAddress(), tep.getIpAddress());
156
157                                     }
158                                 }
159                             }
160                         }
161                     }
162                 }
163         }
164     }
165
166     private static void tunnelsFromhWVtep(List<HwVtep> cfgdHwVteps, IdManagerService idManagerService, List<ListenableFuture<Void>> futures, WriteTransaction t, DataBroker dataBroker) {
167         for (HwVtep hwTep : cfgdHwVteps) {
168             InstanceIdentifier<TransportZone> tzonePath = InstanceIdentifier.builder(TransportZones.class).child(TransportZone.class, new TransportZoneKey((hwTep.getTransportZone()))).build();
169             Optional<TransportZone> tZoneOptional = ItmUtils.read(LogicalDatastoreType.CONFIGURATION, tzonePath, dataBroker);
170             Class<? extends TunnelTypeBase> tunType = TunnelTypeVxlan.class;
171             Boolean monitorEnabled = ItmUtils.readMonitoringStateFromDS(dataBroker);
172             Class<? extends TunnelMonitoringTypeBase> monitorProtocol = ITMConstants.DEFAULT_MONITOR_PROTOCOL;
173             if (tZoneOptional.isPresent()) {
174                 TransportZone tZone = tZoneOptional.get();
175                 //do we need to check tunnel type?
176                 if (tZone.getSubnets() != null && !tZone.getSubnets().isEmpty()) {
177                     for (Subnets sub : tZone.getSubnets()) {
178                         if (sub.getDeviceVteps() != null && !sub.getDeviceVteps().isEmpty()) {
179                             for (DeviceVteps hwVtepDS : sub.getDeviceVteps()) {
180                                 if (hwVtepDS.getIpAddress().equals(hwTep.getHwIp()))
181                                     continue;//dont mesh with self
182                                 logger.trace("wire up {} and {}",hwTep, hwVtepDS);
183                                 if (!wireUp(hwTep.getTopo_id(), hwTep.getNode_id(), hwTep.getHwIp(), hwVtepDS.getNodeId(), hwVtepDS.getIpAddress(),
184                                         hwTep.getIpPrefix(), hwTep.getGatewayIP(), sub.getPrefix(), tunType,monitorEnabled,monitorProtocol,
185                                         ITMConstants.BFD_DEFAULT_MONITOR_INTERVAL, idManagerService, dataBroker, futures, t))
186                                     logger.error("Unable to build tunnel {} -- {}", hwTep.getHwIp(), hwVtepDS.getIpAddress());
187                                 //TOR2-TOR1
188                                 logger.trace("wire up {} and {}", hwVtepDS,hwTep);
189                                 if (!wireUp(hwTep.getTopo_id(), hwVtepDS.getNodeId(), hwVtepDS.getIpAddress(), hwTep.getNode_id(), hwTep.getHwIp(),
190                                         sub.getPrefix(), sub.getGatewayIp(), hwTep.getIpPrefix(), tunType, monitorEnabled,monitorProtocol, ITMConstants.BFD_DEFAULT_MONITOR_INTERVAL, idManagerService, dataBroker, futures, t))
191                                     logger.error("Unable to build tunnel {} -- {}", hwVtepDS.getIpAddress(), hwTep.getHwIp());
192                             }
193                         }
194                         if (sub.getVteps() != null && !sub.getVteps().isEmpty()) {
195                             for (Vteps vtep : sub.getVteps()) {
196                                 if(vtep.getIpAddress().equals(hwTep.getHwIp()))
197                                     continue;
198                                 //TOR-CSS
199                                 String cssID = vtep.getDpnId().toString();
200                                 logger.trace("wire up {} and {}",hwTep, vtep);
201                                 if(!wireUp(hwTep.getTopo_id(), hwTep.getNode_id(), hwTep.getHwIp(), cssID, vtep.getIpAddress(), hwTep.getIpPrefix(),
202                                         hwTep.getGatewayIP(), sub.getPrefix(), tunType,monitorEnabled, monitorProtocol, ITMConstants.BFD_DEFAULT_MONITOR_INTERVAL, idManagerService, dataBroker, futures, t ))
203                                     logger.error("Unable to build tunnel {} -- {}", hwTep.getHwIp(), vtep.getIpAddress());
204                                 //CSS-TOR
205                                 logger.trace("wire up {} and {}", vtep,hwTep);
206                                 if(!wireUp(vtep.getDpnId(), vtep.getPortname(), sub.getVlanId(), vtep.getIpAddress(),
207                                         hwTep.getNode_id(),hwTep.getHwIp(),sub.getPrefix(), sub.getGatewayIp(),hwTep.getIpPrefix(),
208                                         tunType,monitorEnabled,monitorProtocol, ITMConstants.BFD_DEFAULT_MONITOR_INTERVAL,idManagerService, dataBroker, futures, t ));
209
210                             }
211
212                         }
213                     }
214                 }
215             }
216         }
217     }
218
219     //for tunnels from TOR device
220     private static boolean wireUp(String topo_id, String srcNodeid, IpAddress srcIp, String dstNodeId, IpAddress dstIp, IpPrefix srcSubnet,
221                                   IpAddress gWIp, IpPrefix dstSubnet, Class<? extends TunnelTypeBase> tunType,Boolean monitorEnabled,  Class<? extends TunnelMonitoringTypeBase> monitorProtocol,
222                                   Integer monitorInterval,IdManagerService idManagerService, DataBroker dataBroker, List<ListenableFuture<Void>> futures, WriteTransaction t) {
223         IpAddress gatewayIpObj = new IpAddress("0.0.0.0".toCharArray());
224         IpAddress gwyIpAddress = (srcSubnet.equals(dstSubnet)) ? gatewayIpObj : gWIp;
225         String parentIf =  ItmUtils.getHwParentIf(topo_id, srcNodeid);
226         String tunTypeStr = tunType.getName();
227         String tunnelIfName = ItmUtils.getTrunkInterfaceName(idManagerService, parentIf,
228                 srcIp.getIpv4Address().getValue(), dstIp.getIpv4Address().getValue(), tunTypeStr);
229         logger.debug(" Creating ExternalTrunk Interface with parameters Name - {}, parent I/f name - {}, source IP - {}, destination IP - {} gateway IP - {}", tunnelIfName, parentIf, srcIp, dstIp, gwyIpAddress);
230         Interface hwTunnelIf = ItmUtils.buildHwTunnelInterface(tunnelIfName, String.format("%s %s", tunType.getName(), "Trunk Interface"),
231                 true, topo_id, srcNodeid, tunType, srcIp, dstIp, gwyIpAddress, monitorEnabled, monitorProtocol, monitorInterval);
232         InstanceIdentifier<Interface> ifIID = InstanceIdentifier.builder(Interfaces.class).child(Interface.class, new InterfaceKey(tunnelIfName)).build();
233         logger.trace(" Writing Trunk Interface to Config DS {}, {} ", ifIID, hwTunnelIf);
234         ItmUtils.itmCache.addInterface(hwTunnelIf);
235         t.merge(LogicalDatastoreType.CONFIGURATION, ifIID, hwTunnelIf, true);
236         // also update itm-state ds?
237         InstanceIdentifier<ExternalTunnel> path = InstanceIdentifier.create(
238                 ExternalTunnelList.class)
239                 .child(ExternalTunnel.class, new ExternalTunnelKey( getExternalTunnelKey(dstNodeId), getExternalTunnelKey(srcNodeid), tunType));
240         ExternalTunnel tnl = ItmUtils.buildExternalTunnel(  getExternalTunnelKey(srcNodeid),
241                 getExternalTunnelKey(dstNodeId),
242                 tunType, tunnelIfName);
243         t.merge(LogicalDatastoreType.CONFIGURATION, path, tnl, true);
244         ItmUtils.itmCache.addExternalTunnel(tnl);
245         return true;
246     }
247
248     //for tunnels from CSS
249     private static boolean wireUp(BigInteger dpnId, String portname, Integer vlanId, IpAddress srcIp, String dstNodeId, IpAddress dstIp, IpPrefix srcSubnet,
250                                   IpAddress gWIp, IpPrefix dstSubnet, Class<? extends TunnelTypeBase> tunType, Boolean monitorEnabled, Class<? extends TunnelMonitoringTypeBase> monitorProtocol, Integer monitorInterval,
251                                   IdManagerService idManagerService, DataBroker dataBroker, List<ListenableFuture<Void>> futures, WriteTransaction t) {
252         IpAddress gatewayIpObj = new IpAddress("0.0.0.0".toCharArray());
253         IpAddress gwyIpAddress = (srcSubnet.equals(dstSubnet)) ? gatewayIpObj : gWIp;
254         String parentIf = ItmUtils.getInterfaceName(dpnId, portname, vlanId);
255         String tunTypeStr = tunType.getName();
256         String tunnelIfName = ItmUtils.getTrunkInterfaceName(idManagerService, parentIf,
257                 srcIp.getIpv4Address().getValue(), dstIp.getIpv4Address().getValue(), tunTypeStr);
258         logger.debug(" Creating ExternalTrunk Interface with parameters Name - {}, parent I/f name - {}, source IP - {}, destination IP - {} gateway IP - {}", tunnelIfName, parentIf, srcIp, dstIp, gwyIpAddress);
259         Interface extTunnelIf = ItmUtils.buildTunnelInterface(dpnId, tunnelIfName, String.format("%s %s", tunType.getName(), "Trunk Interface"), true, tunType, srcIp, dstIp, gwyIpAddress, vlanId, false,monitorEnabled, monitorProtocol, monitorInterval);
260         InstanceIdentifier<Interface> ifIID = InstanceIdentifier.builder(Interfaces.class).child(Interface.class, new InterfaceKey(tunnelIfName)).build();
261         logger.trace(" Writing Trunk Interface to Config DS {}, {} ", ifIID, extTunnelIf);
262         t.merge(LogicalDatastoreType.CONFIGURATION, ifIID, extTunnelIf, true);
263         ItmUtils.itmCache.addInterface(extTunnelIf);
264         InstanceIdentifier<ExternalTunnel> path = InstanceIdentifier.create(
265                 ExternalTunnelList.class)
266                 .child(ExternalTunnel.class, new ExternalTunnelKey(getExternalTunnelKey(dstNodeId), dpnId.toString(), tunType));
267         ExternalTunnel tnl = ItmUtils.buildExternalTunnel(  dpnId.toString(),
268                 getExternalTunnelKey(dstNodeId),
269                 tunType, tunnelIfName);
270         t.merge(LogicalDatastoreType.CONFIGURATION, path, tnl, true);
271         ItmUtils.itmCache.addExternalTunnel(tnl);
272         return true;
273     }
274
275     static String getExternalTunnelKey(String nodeid) {
276         if (nodeid.indexOf("physicalswitch") > 0) {
277             nodeid = nodeid.substring(0, nodeid.indexOf("physicalswitch") - 1);
278         }
279         return nodeid;
280     }
281
282 }
283