2 * Copyright (c) 2016 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.genius.itm.cli;
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import org.apache.commons.net.util.SubnetUtils;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
16 import org.opendaylight.genius.itm.globals.ITMConstants;
17 import org.opendaylight.genius.itm.impl.ItmUtils;
18 import org.opendaylight.genius.mdsalutil.MDSALDataStoreUtils;
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.Interface;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.AdminStatus;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.*;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.config.rev160406.TunnelMonitorInterval;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.config.rev160406.TunnelMonitorIntervalBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.config.rev160406.TunnelMonitorParams;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.config.rev160406.TunnelMonitorParamsBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.TunnelList;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.tunnel.list.InternalTunnel;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.TransportZones;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.TransportZonesBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.TransportZone;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.TransportZoneBuilder;
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.SubnetsBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.transport.zone.SubnetsKey;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.transport.zone.subnets.Vteps;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.transport.zone.subnets.VtepsBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.transport.zone.subnets.VtepsKey;
42 import org.opendaylight.yangtools.yang.binding.DataObject;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
47 import java.math.BigInteger;
48 import java.util.ArrayList;
49 import java.util.HashMap;
50 import java.util.List;
53 //import org.opendaylight.genius.interfacemgr.util.OperationalIfmUtil;
56 public class TepCommandHelper {
58 private static final Logger LOG = LoggerFactory.getLogger(TepCommandHelper.class);
59 private DataBroker dataBroker;
61 static short flag = 0;
63 * boolean flag add_or_delete --- can be set to true if the last called tep
64 * command is Tep-add else set to false when Tep-delete is called
65 * tepCommandHelper object is created only once in session initiated
67 final Map<String, Map<SubnetObject, List<Vteps>>> tZones = new HashMap<String, Map<SubnetObject, List<Vteps>>>();
68 private List<Subnets> subnetList = new ArrayList<Subnets>();
69 private List<TransportZone> tZoneList = new ArrayList<TransportZone>();
70 private List<Vteps> vtepDelCommitList = new ArrayList<Vteps>();
71 private IInterfaceManager interfaceManager;
73 // private List<InstanceIdentifier<? extends DataObject>> vtepPaths = new
77 public TepCommandHelper(final DataBroker broker) {
78 this.dataBroker = broker;
82 public void setInterfaceManager(IInterfaceManager interfaceManager) {
83 this.interfaceManager = interfaceManager;
86 public void createLocalCache(BigInteger dpnId, String portName, Integer vlanId, String ipAddress,
87 String subnetMask, String gatewayIp, String transportZone) {
90 IpAddress ipAddressObj = null;
91 IpAddress gatewayIpObj = null;
92 IpPrefix subnetMaskObj = null;
93 VtepsKey vtepkey = new VtepsKey(dpnId, portName);
95 ipAddressObj = new IpAddress(ipAddress.toCharArray());
96 gatewayIpObj = new IpAddress("0.0.0.0".toCharArray());
97 if (gatewayIp != null) {
98 gatewayIpObj = new IpAddress(gatewayIp.toCharArray());
100 LOG.debug("gateway is null");
102 } catch (Exception e) {
103 System.out.println("Invalid IpAddress. Expected: 1.0.0.0 to 254.255.255.255");
107 subnetMaskObj = new IpPrefix(subnetMask.toCharArray());
108 } catch (Exception e) {
109 System.out.println("Invalid Subnet Mask. Expected: 0.0.0.0/0 to 255.255.255.255/32");
113 if (!validateIPs(ipAddress, subnetMask, gatewayIp)) {
114 System.out.println("IpAddress and gateWayIp should belong to the subnet provided");
118 if (checkTepPerTzPerDpn(transportZone, dpnId)) {
119 System.out.println("Only one end point per transport Zone per Dpn is allowed");
122 Vteps vtepCli = new VtepsBuilder().setDpnId(dpnId).setIpAddress(ipAddressObj).setKey(vtepkey)
123 .setPortname(portName).build();
124 validateForDuplicates(vtepCli, transportZone);
126 SubnetsKey subnetsKey = new SubnetsKey(subnetMaskObj);
127 SubnetObject subObCli = new SubnetObject(gatewayIpObj, subnetsKey, subnetMaskObj, vlanId);
128 if (tZones.containsKey(transportZone)) {
129 Map<SubnetObject, List<Vteps>> subVtepMapTemp = (Map<SubnetObject, List<Vteps>>) tZones.get(transportZone);
130 if (subVtepMapTemp.containsKey(subObCli)) { // if Subnet exists
131 List<Vteps> vtepListTemp = (List<Vteps>) subVtepMapTemp.get(subObCli);
132 if (vtepListTemp.contains(vtepCli)) {
135 vtepListTemp.add(vtepCli);
137 } else { // subnet doesnt exist
138 if (checkExistingSubnet(subVtepMapTemp, subObCli)) {
139 System.out.println("subnet with subnet mask " + subObCli.get_key() + "already exists");
142 List<Vteps> vtepListTemp = new ArrayList<Vteps>();
143 vtepListTemp.add(vtepCli);
144 subVtepMapTemp.put(subObCli, vtepListTemp);
147 List<Vteps> vtepListTemp = new ArrayList<Vteps>();
148 vtepListTemp.add(vtepCli);
149 Map<SubnetObject, List<Vteps>> subVtepMapTemp = new HashMap<SubnetObject, List<Vteps>>();
150 subVtepMapTemp.put(subObCli, vtepListTemp);
151 tZones.put(transportZone, subVtepMapTemp);
155 private boolean validateIPs(String ipAddress, String subnetMask, String gatewayIp) {
156 SubnetUtils utils = new SubnetUtils(subnetMask);
157 if ((utils.getInfo().isInRange(ipAddress)) && ((gatewayIp == null) || (utils.getInfo().isInRange(gatewayIp)))) {
160 LOG.trace("InValid IP");
166 * Validate for duplicates.
170 * @param transportZone
173 public void validateForDuplicates(Vteps inputVtep, String transportZone) {
174 Map<String, TransportZone> tZoneMap = getAllTransportZonesAsMap();
176 boolean isConfiguredTepGreType = isGreTunnelType(transportZone, tZoneMap);
177 // Checking for duplicates in local cache
178 for (String tZ : tZones.keySet()) {
179 boolean isGreType = isGreTunnelType(tZ, tZoneMap);
180 Map<SubnetObject, List<Vteps>> subVtepMapTemp = (Map<SubnetObject, List<Vteps>>) tZones.get(tZ);
181 for (SubnetObject subOb : subVtepMapTemp.keySet()) {
182 List<Vteps> vtepList = subVtepMapTemp.get(subOb);
183 validateForDuplicateAndSingleGreTep(inputVtep, isConfiguredTepGreType, isGreType, vtepList);
186 // Checking for duplicates in config DS
187 for (TransportZone tZ : tZoneMap.values()) {
188 boolean isGreType = false;
189 if (tZ.getTunnelType().equals(TunnelTypeGre.class)) {
192 for (Subnets sub : ItmUtils.emptyIfNull(tZ.getSubnets())) {
193 List<Vteps> vtepList = sub.getVteps();
194 validateForDuplicateAndSingleGreTep(inputVtep, isConfiguredTepGreType, isGreType, vtepList);
199 private void validateForDuplicateAndSingleGreTep(Vteps inputVtep, boolean isConfiguredTepGreType, boolean isGreType,
200 List<Vteps> vtepList) {
201 if (ItmUtils.isEmpty(vtepList)) {
204 if (vtepList.contains(inputVtep)) {
205 Preconditions.checkArgument(false, "VTEP already exists");
207 BigInteger dpnId = inputVtep.getDpnId();
208 if (isConfiguredTepGreType && isGreType) {
209 for (Vteps vtep : vtepList) {
210 if (vtep.getDpnId().equals(dpnId)) {
211 String errMsg = new StringBuilder("DPN [").append(dpnId)
212 .append("] already configured with GRE TEP. Mutiple GRE TEP's on a single DPN are not allowed.")
214 Preconditions.checkArgument(false, errMsg);
221 * Gets all transport zones as map.
223 * @return all transport zones as map
225 private Map<String, TransportZone> getAllTransportZonesAsMap() {
226 TransportZones tZones = getAllTransportZones();
227 Map<String, TransportZone> tZoneMap = new HashMap<>();
228 if( null != tZones) {
229 for (TransportZone tzone : ItmUtils.emptyIfNull(tZones.getTransportZone())) {
230 tZoneMap.put(tzone.getZoneName(), tzone);
237 * Checks if is gre tunnel type.
243 * @return true, if is gre tunnel type
245 private boolean isGreTunnelType(String tZoneName, Map<String, TransportZone> tZoneMap) {
246 TransportZone tzone = tZoneMap.get(tZoneName);
248 if (tzone != null && StringUtils.equalsIgnoreCase(ITMConstants.TUNNEL_TYPE_GRE, tzone.getTunnelType())) {
252 if( (tzone != null) && (tzone.getTunnelType()).equals(TunnelTypeGre.class) ) {
259 * Gets the transport zone.
263 * @return the transport zone
265 public TransportZone getTransportZone(String tzone) {
266 InstanceIdentifier<TransportZone> tzonePath = InstanceIdentifier.builder(TransportZones.class)
267 .child(TransportZone.class, new TransportZoneKey(tzone)).build();
268 Optional<TransportZone> tZoneOptional = ItmUtils.read(LogicalDatastoreType.CONFIGURATION, tzonePath,
270 if (tZoneOptional.isPresent()) {
271 return tZoneOptional.get();
277 * Gets the transport zone from config ds.
281 * @return the transport zone
283 public TransportZone getTransportZoneFromConfigDS(String tzone) {
284 InstanceIdentifier<TransportZone> tzonePath = InstanceIdentifier.builder(TransportZones.class)
285 .child(TransportZone.class, new TransportZoneKey(tzone)).build();
286 Optional<TransportZone> tZoneOptional = ItmUtils.read(LogicalDatastoreType.CONFIGURATION, tzonePath,
288 if (tZoneOptional.isPresent()) {
289 return tZoneOptional.get();
295 * Gets all transport zones.
297 * @return all transport zones
299 public TransportZones getAllTransportZones() {
300 InstanceIdentifier<TransportZones> path = InstanceIdentifier.builder(TransportZones.class).build();
301 Optional<TransportZones> tZonesOptional = ItmUtils.read(LogicalDatastoreType.CONFIGURATION, path, dataBroker);
302 if (tZonesOptional.isPresent()) {
303 return tZonesOptional.get();
308 public boolean checkExistingSubnet(Map<SubnetObject, List<Vteps>> subVtepMapTemp, SubnetObject subObCli) {
309 for (SubnetObject subOb : subVtepMapTemp.keySet()) {
310 if (subOb.get_key().equals(subObCli.get_key())) {
311 if (!(subOb.get_vlanId().equals(subObCli.get_vlanId())))
313 if (!(subOb.get_gatewayIp().equals(subObCli.get_gatewayIp())))
320 public boolean checkTepPerTzPerDpn(String tzone, BigInteger dpnId) {
321 // check in local cache
322 if (tZones.containsKey(tzone)) {
323 Map<SubnetObject, List<Vteps>> subVtepMapTemp = (Map<SubnetObject, List<Vteps>>) tZones.get(tzone);
324 for (SubnetObject subOb : subVtepMapTemp.keySet()) {
325 List<Vteps> vtepList = subVtepMapTemp.get(subOb);
326 for (Vteps vtep : vtepList)
327 if (vtep.getDpnId().equals(dpnId))
333 InstanceIdentifier<TransportZone> tzonePath =
334 InstanceIdentifier.builder(TransportZones.class)
335 .child(TransportZone.class, new TransportZoneKey(tzone)).build();
336 Optional<TransportZone> tZoneOptional =
337 ItmUtils.read(LogicalDatastoreType.CONFIGURATION, tzonePath, dataBroker);
338 if (tZoneOptional.isPresent()) {
339 TransportZone tz = tZoneOptional.get();
340 if (tz.getSubnets() == null || tz.getSubnets().isEmpty())
342 for (Subnets sub : tz.getSubnets()) {
343 if (sub.getVteps() == null || sub.getVteps().isEmpty())
345 for (Vteps vtep : sub.getVteps()) {
346 if (vtep.getDpnId().equals(dpnId)) {
355 public void buildTeps() {
356 TransportZones tZonesBuilt = null;
357 TransportZone tZone = null;
359 LOG.debug("no of teps added" + check);
360 if (tZones != null || !tZones.isEmpty()) {
361 tZoneList = new ArrayList<TransportZone>();
362 for (String tZ : tZones.keySet()) {
363 LOG.debug("tZones" + tZ);
364 subnetList = new ArrayList<Subnets>();
365 Map<SubnetObject, List<Vteps>> subVtepMapTemp = (Map<SubnetObject, List<Vteps>>) tZones.get(tZ);
366 for (SubnetObject subOb : subVtepMapTemp.keySet()) {
367 LOG.debug("subnets" + subOb.get_prefix());
368 List<Vteps> vtepList = subVtepMapTemp.get(subOb);
370 new SubnetsBuilder().setGatewayIp(subOb.get_gatewayIp())
371 .setKey(subOb.get_key()).setPrefix(subOb.get_prefix())
372 .setVlanId(subOb.get_vlanId()).setVteps(vtepList).build();
373 subnetList.add(subnet);
374 LOG.debug("vteps" + vtepList);
376 InstanceIdentifier<TransportZone> tZonepath =
377 InstanceIdentifier.builder(TransportZones.class)
378 .child(TransportZone.class, new TransportZoneKey(tZ)).build();
379 Optional<TransportZone> tZoneOptional =
380 ItmUtils.read(LogicalDatastoreType.CONFIGURATION, tZonepath, dataBroker);
381 LOG.debug("read container from DS");
382 if (tZoneOptional.isPresent()) {
383 TransportZone tzoneFromDs = tZoneOptional.get();
384 LOG.debug("read tzone container" + tzoneFromDs.toString());
385 if (tzoneFromDs.getTunnelType() == null
386 || (tzoneFromDs.getTunnelType()).equals(TunnelTypeVxlan.class)) {
388 new TransportZoneBuilder().setKey(new TransportZoneKey(tZ))
389 .setTunnelType(TunnelTypeVxlan.class).setSubnets(subnetList)
390 .setZoneName(tZ).build();
391 } else if ((tzoneFromDs.getTunnelType()).equals(TunnelTypeGre.class)) {
393 new TransportZoneBuilder().setKey(new TransportZoneKey(tZ))
394 .setTunnelType(TunnelTypeGre.class).setSubnets(subnetList)
395 .setZoneName(tZ).build();
399 new TransportZoneBuilder().setKey(new TransportZoneKey(tZ))
400 .setTunnelType(TunnelTypeVxlan.class).setSubnets(subnetList).setZoneName(tZ)
403 LOG.debug("tzone object" + tZone);
404 tZoneList.add(tZone);
406 tZonesBuilt = new TransportZonesBuilder().setTransportZone(tZoneList).build();
407 InstanceIdentifier<TransportZones> path = InstanceIdentifier.builder(TransportZones.class).build();
408 LOG.debug("InstanceIdentifier" + path);
409 ItmUtils.asyncUpdate(LogicalDatastoreType.CONFIGURATION, path, tZonesBuilt, dataBroker,
410 ItmUtils.DEFAULT_CALLBACK);
411 LOG.debug("wrote to Config DS" + tZonesBuilt);
415 LOG.debug("Everything cleared");
417 LOG.debug("NO vteps were configured");
419 } catch (Exception e) {
424 public void showTeps(boolean monitorEnabled, int monitorInterval) {
425 boolean flag = false;
426 InstanceIdentifier<TransportZones> path = InstanceIdentifier.builder(TransportZones.class).build();
427 Optional<TransportZones> tZonesOptional = ItmUtils.read(LogicalDatastoreType.CONFIGURATION, path, dataBroker);
428 if (tZonesOptional.isPresent()) {
429 TransportZones tZones = tZonesOptional.get();
430 if(tZones.getTransportZone() == null || tZones.getTransportZone().isEmpty())
432 System.out.println("No teps configured");
435 List<String> result = new ArrayList<String>();
436 result.add(String.format("Tunnel Monitoring (for VXLAN tunnels): %s", (monitorEnabled ? "On" : "Off")));
437 result.add(String.format("Tunnel Monitoring Interval (for VXLAN tunnels): %d", monitorInterval));
438 result.add(System.lineSeparator());
439 result.add(String.format("%-16s %-16s %-16s %-12s %-12s %-12s %-16s %-12s", "TransportZone", "TunnelType", "SubnetMask",
440 "GatewayIP", "VlanID", "DpnID", "IPAddress", "PortName"));
441 result.add("------------------------------------------------------------------------------------------------------------------------------");
442 for (TransportZone tZ : tZones.getTransportZone()) {
443 if (tZ.getSubnets() == null || tZ.getSubnets().isEmpty()) {
444 LOG.error("Transport Zone " + tZ.getZoneName() + "has no subnets");
447 for (Subnets sub : tZ.getSubnets()) {
448 if (sub.getVteps() == null || sub.getVteps().isEmpty()) {
449 LOG.error("Transport Zone " + tZ.getZoneName() + "subnet " + sub.getPrefix() + "has no vteps");
452 for (Vteps vtep : sub.getVteps()) {
454 String strTunnelType ;
455 if( (tZ.getTunnelType()).equals(TunnelTypeGre.class) )
456 strTunnelType = ITMConstants.TUNNEL_TYPE_GRE ;
458 strTunnelType = ITMConstants.TUNNEL_TYPE_VXLAN ;
459 result.add(String.format("%-16s %-16s %-16s %-12s %-12s %-12s %-16s %-12s", tZ.getZoneName(), strTunnelType, sub
460 .getPrefix().getIpv4Prefix().getValue(), sub.getGatewayIp().getIpv4Address()
461 .getValue(), sub.getVlanId().toString(), vtep.getDpnId().toString(), vtep
462 .getIpAddress().getIpv4Address().getValue(), vtep.getPortname().toString()));
467 for (String p : result) {
468 System.out.println(p);
471 System.out.println("No teps to display");
473 System.out.println("No teps configured");
477 public void deleteVtep(BigInteger dpnId, String portName, Integer vlanId, String ipAddress, String subnetMask,
478 String gatewayIp, String transportZone) {
480 IpAddress ipAddressObj = null;
481 IpAddress gatewayIpObj = null;
482 IpPrefix subnetMaskObj = null;
483 VtepsKey vtepkey = new VtepsKey(dpnId, portName);
485 ipAddressObj = new IpAddress(ipAddress.toCharArray());
486 gatewayIpObj = new IpAddress("0.0.0.0".toCharArray());
487 if (gatewayIp != null) {
488 gatewayIpObj = new IpAddress(gatewayIp.toCharArray());
490 LOG.debug("gateway is null");
492 } catch (Exception e) {
493 System.out.println("Invalid IpAddress. Expected: 1.0.0.0 to 254.255.255.255");
497 subnetMaskObj = new IpPrefix(subnetMask.toCharArray());
498 } catch (Exception e) {
499 System.out.println("Invalid Subnet Mask. Expected: 0.0.0.0/0 to 255.255.255.255/32");
503 if (!validateIPs(ipAddress, subnetMask, gatewayIp)) {
504 System.out.println("IpAddress and gateWayIp should belong to the subnet provided");
507 SubnetsKey subnetsKey = new SubnetsKey(subnetMaskObj);
508 Vteps vtepCli = null;
509 Subnets subCli = null;
511 InstanceIdentifier<Vteps> vpath =
512 InstanceIdentifier.builder(TransportZones.class)
513 .child(TransportZone.class, new TransportZoneKey(transportZone))
514 .child(Subnets.class, subnetsKey).child(Vteps.class, vtepkey).build();
516 // check if present in tzones and delete from cache
517 boolean existsInCache = isInCache(dpnId, portName, vlanId, ipAddress, subnetMask, gatewayIp, transportZone);
518 if (!existsInCache) {
519 Optional<Vteps> vtepOptional = ItmUtils.read(LogicalDatastoreType.CONFIGURATION, vpath, dataBroker);
520 if (vtepOptional.isPresent()) {
521 vtepCli = vtepOptional.get();
522 if(vtepCli.getIpAddress().equals(ipAddressObj)){
523 InstanceIdentifier<Subnets> spath =
525 .builder(TransportZones.class)
526 .child(TransportZone.class, new TransportZoneKey(transportZone))
527 .child(Subnets.class, subnetsKey).build();
528 Optional<Subnets> subOptional = ItmUtils.read(LogicalDatastoreType.CONFIGURATION, spath, dataBroker);
529 if (subOptional.isPresent()) {
530 subCli = subOptional.get();
531 if(subCli.getGatewayIp().equals(gatewayIpObj) && subCli.getVlanId().equals(vlanId)){
532 vtepDelCommitList.add(vtepCli);
535 System.out.println(String.format("vtep with this vlan or gateway doesnt exist"));
539 System.out.println(String.format("Vtep with this ipaddress doesnt exist"));
541 System.out.println(String.format("Vtep Doesnt exist"));
546 public <T extends DataObject> void deleteOnCommit() {
547 List<InstanceIdentifier<T>> vtepPaths = new ArrayList<>();
548 List<InstanceIdentifier<T>> subnetPaths = new ArrayList<>();
549 List<InstanceIdentifier<T>> tzPaths = new ArrayList<>();
550 List<Subnets> subDelList = new ArrayList<Subnets>();
551 List<TransportZone> tzDelList = new ArrayList<TransportZone>();
552 List<Vteps> vtepDelList = new ArrayList<Vteps>();
553 List<InstanceIdentifier<T>> allPaths = new ArrayList<>();
555 if (vtepDelCommitList != null && !vtepDelCommitList.isEmpty()) {
556 InstanceIdentifier<TransportZones> path = InstanceIdentifier.builder(TransportZones.class).build();
557 Optional<TransportZones> tZonesOptional =
558 ItmUtils.read(LogicalDatastoreType.CONFIGURATION, path, dataBroker);
559 if (tZonesOptional.isPresent()) {
560 TransportZones tZones = tZonesOptional.get();
561 for (TransportZone tZ : tZones.getTransportZone()) {
562 if (tZ.getSubnets() == null || tZ.getSubnets().isEmpty())
564 for (Subnets sub : tZ.getSubnets()) {
565 vtepDelList.addAll(vtepDelCommitList);
566 for (Vteps vtep : vtepDelList) {
567 InstanceIdentifier<T> vpath =
568 (InstanceIdentifier<T>) InstanceIdentifier
569 .builder(TransportZones.class)
570 .child(TransportZone.class, tZ.getKey())
571 .child(Subnets.class, sub.getKey())
572 .child(Vteps.class, vtep.getKey()).build();
573 if (sub.getVteps().remove(vtep)) {
574 vtepPaths.add(vpath);
575 if (sub.getVteps().size() == 0 || sub.getVteps() == null) {
584 for (TransportZone tZ : tZones.getTransportZone()) {
585 if (tZ.getSubnets() == null || tZ.getSubnets().isEmpty())
587 for (Subnets sub : subDelList) {
588 if (tZ.getSubnets().remove(sub)) {
589 InstanceIdentifier<T> spath =
590 (InstanceIdentifier<T>) InstanceIdentifier
591 .builder(TransportZones.class)
592 .child(TransportZone.class, tZ.getKey())
593 .child(Subnets.class, sub.getKey()).build();
594 subnetPaths.add(spath);
595 if (tZ.getSubnets() == null || tZ.getSubnets().size() == 0) {
602 for (TransportZone tZ : tzDelList) {
603 if (tZones.getTransportZone().remove(tZ)) {
604 InstanceIdentifier<T> tpath =
605 (InstanceIdentifier<T>) InstanceIdentifier.builder(TransportZones.class)
606 .child(TransportZone.class, tZ.getKey()).build();
608 if (tZones.getTransportZone() == null || tZones.getTransportZone().size() == 0) {
609 MDSALDataStoreUtils.asyncRemove(dataBroker, LogicalDatastoreType.CONFIGURATION, path,
610 ItmUtils.DEFAULT_CALLBACK);
615 allPaths.addAll(vtepPaths);
616 allPaths.addAll(subnetPaths);
617 allPaths.addAll(tzPaths);
618 ItmUtils.asyncBulkRemove(dataBroker, LogicalDatastoreType.CONFIGURATION, allPaths,
619 ItmUtils.DEFAULT_CALLBACK);
625 vtepDelCommitList.clear();
627 } catch (Exception e) {
632 public void showState(TunnelList tunnels, boolean tunnelMonitorEnabled) {
633 IfTunnel tunnelInterface = null;
634 IfL2vlan l2Vlan = null;
635 List<InternalTunnel> tunnelLists = tunnels.getInternalTunnel();
636 if (tunnelLists == null || tunnelLists.isEmpty()) {
637 System.out.println("No Internal Tunnels Exist");
640 if (!tunnelMonitorEnabled) {
641 System.out.println("Tunnel Monitoring is Off");
643 String displayFormat = "%-16s %-16s %-16s %-16s %-16s %-8s %-10s %-10s";
644 System.out.println(String.format(displayFormat, "Tunnel Name", "Source-DPN",
645 "Destination-DPN", "Source-IP", "Destination-IP", "Vlan Id", "Trunk-State", "Transport Type"));
646 System.out.println("-------------------------------------------------------------------------------------------------------------------------------------");
648 for (InternalTunnel tunnel : tunnelLists) {
649 String tunnelInterfaceName = tunnel.getTunnelInterfaceName();
650 LOG.trace("tunnelInterfaceName::: {}", tunnelInterfaceName);
652 InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> ifStateId =
653 ItmUtils.buildStateInterfaceId(tunnelInterfaceName);
654 Optional<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> ifStateOptional =
655 ItmUtils.read(LogicalDatastoreType.OPERATIONAL, ifStateId, dataBroker);
656 String tunnelState = "DOWN" ;
657 if (ifStateOptional.isPresent()) {
658 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface iface = ifStateOptional.get() ;
659 if(iface.getAdminStatus() == AdminStatus.Up && iface.getOperStatus() == OperStatus.Up)
662 InstanceIdentifier<Interface> trunkIdentifier = ItmUtils.buildId(tunnelInterfaceName);
663 Optional<Interface> ifaceObj = ItmUtils.read(LogicalDatastoreType.CONFIGURATION, trunkIdentifier, dataBroker) ;
664 if (ifaceObj.isPresent()) {
665 l2Vlan = (IfL2vlan) ifaceObj.get().getAugmentation(IfL2vlan.class);
666 tunnelInterface = (IfTunnel) ifaceObj.get().getAugmentation(IfTunnel.class);
668 Class<? extends TunnelTypeBase> tunType = tunnelInterface.getTunnelInterfaceType();
669 String tunnelType = ITMConstants.TUNNEL_TYPE_VXLAN;
670 if( tunType.equals(TunnelTypeVxlan.class))
671 tunnelType = ITMConstants.TUNNEL_TYPE_VXLAN ;
672 else if( tunType.equals(TunnelTypeGre.class) )
673 tunnelType = ITMConstants.TUNNEL_TYPE_GRE ;
675 if( l2Vlan != null ) {
676 vlanId = l2Vlan.getVlanId().getValue() ;
678 System.out.println(String.format(displayFormat, tunnel.getTunnelInterfaceName(), tunnel
679 .getSourceDPN().toString(), tunnel.getDestinationDPN().toString(), tunnelInterface.getTunnelSource().getIpv4Address().getValue(), tunnelInterface.getTunnelDestination().getIpv4Address().getValue(),vlanId, tunnelState ,
685 // deletes from ADD-cache if it exists.
686 public boolean isInCache(BigInteger dpnId, String portName, Integer vlanId, String ipAddress, String subnetMask,
687 String gatewayIp, String transportZone) {
688 boolean exists = false;
689 VtepsKey vtepkey = new VtepsKey(dpnId, portName);
690 IpAddress ipAddressObj = new IpAddress(ipAddress.toCharArray());
691 IpPrefix subnetMaskObj = new IpPrefix(subnetMask.toCharArray());
692 IpAddress gatewayIpObj = new IpAddress("0.0.0.0".toCharArray());
693 if (gatewayIp != null) {
694 gatewayIpObj = new IpAddress(gatewayIp.toCharArray());
696 LOG.debug("gateway is null");
698 SubnetsKey subnetsKey = new SubnetsKey(subnetMaskObj);
700 new VtepsBuilder().setDpnId(dpnId).setIpAddress(ipAddressObj).setKey(vtepkey)
701 .setPortname(portName).build();
702 SubnetObject subObCli = new SubnetObject(gatewayIpObj, subnetsKey, subnetMaskObj, vlanId);
704 if (tZones.containsKey(transportZone)) {
705 Map<SubnetObject, List<Vteps>> subVtepMapTemp = (Map<SubnetObject, List<Vteps>>) tZones.get(transportZone);
706 if (subVtepMapTemp.containsKey(subObCli)) { // if Subnet exists
707 List<Vteps> vtepListTemp = (List<Vteps>) subVtepMapTemp.get(subObCli);
708 if (vtepListTemp.contains(vtepCli)) {
709 exists = true; // return true if tzones has vtep
710 vtepListTemp.remove(vtepCli);
711 if (vtepListTemp.size() == 0) {
712 subVtepMapTemp.remove(subObCli);
713 if (subVtepMapTemp.size() == 0) {
714 tZones.remove(transportZone);
718 System.out.println("Vtep " + "has not been configured");
725 public void configureTunnelType(String tZoneName, String tunnelType) {
726 LOG.debug("configureTunnelType {} for transportZone {}", tunnelType, tZoneName);
728 TransportZone tZoneFromConfigDS = getTransportZoneFromConfigDS(tZoneName);
729 validateTunnelType(tZoneName, tunnelType,tZoneFromConfigDS);
731 if (tZoneFromConfigDS != null) {
732 LOG.debug("Transport zone {} with tunnel type {} already exists. No action required.", tZoneName,
736 TransportZones transportZones = null;
737 List<TransportZone> tZoneList = null;
738 InstanceIdentifier<TransportZones> path = InstanceIdentifier.builder(TransportZones.class).build();
739 Optional<TransportZones> tZones = ItmUtils.read(LogicalDatastoreType.CONFIGURATION, path, dataBroker);
740 Class<? extends TunnelTypeBase> tunType = TunnelTypeVxlan.class;
741 if( tunnelType.equals(ITMConstants.TUNNEL_TYPE_VXLAN))
742 tunType = TunnelTypeVxlan.class ;
743 else if( tunnelType.equals(ITMConstants.TUNNEL_TYPE_GRE) )
744 tunType = TunnelTypeGre.class ;
746 TransportZone tZone = new TransportZoneBuilder().setKey(new TransportZoneKey(tZoneName))
747 .setTunnelType(tunType).build();
748 if (tZones.isPresent()) {
749 tZoneList = tZones.get().getTransportZone();
750 if(tZoneList == null || tZoneList.isEmpty()) {
751 tZoneList = new ArrayList<TransportZone>();
754 tZoneList = new ArrayList<TransportZone>();
756 tZoneList.add(tZone);
757 transportZones = new TransportZonesBuilder().setTransportZone(tZoneList).build();
758 ItmUtils.syncWrite(LogicalDatastoreType.CONFIGURATION, path, transportZones, dataBroker);
763 * Validate tunnel type.
770 private void validateTunnelType(String tZoneName, String tunnelType,TransportZone tZoneFromConfigDS) {
772 String strTunnelType = ItmUtils.validateTunnelType(tunnelType);
774 TransportZone tZone = getTransportZone(tZoneName);
776 if (!StringUtils.equalsIgnoreCase(strTunnelType, tZone.getTunnelType())
777 && ItmUtils.isNotEmpty(tZone.getSubnets())) {
778 String errorMsg = new StringBuilder("Changing the tunnel type from ").append(tZone.getTunnelType())
779 .append(" to ").append(strTunnelType)
780 .append(" is not allowed for already configured transport zone [").append(tZoneName)
781 .append("].").toString();
782 Preconditions.checkArgument(false, errorMsg);
786 String strTunnelType = ItmUtils.validateTunnelType(tunnelType);
787 Class<? extends TunnelTypeBase> tunType ;
788 if( strTunnelType.equals(ITMConstants.TUNNEL_TYPE_VXLAN))
789 tunType = TunnelTypeVxlan.class ;
791 tunType = TunnelTypeGre.class ;
792 //TransportZone tZone = getTransportZone(tZoneName);
793 // if (tZone != null) {
794 if (tZoneFromConfigDS != null) {
795 if( (!tZoneFromConfigDS.getTunnelType().equals(tunType)) && ItmUtils.isNotEmpty(tZoneFromConfigDS.getSubnets())) {
796 String errorMsg = new StringBuilder("Changing the tunnel type from ").append(tZoneFromConfigDS.getTunnelType())
797 .append(" to ").append(strTunnelType)
798 .append(" is not allowed for already configured transport zone [").append(tZoneName)
799 .append("].").toString();
800 Preconditions.checkArgument(false, errorMsg);
805 public void configureTunnelMonitorParams(boolean monitorEnabled, String monitorProtocol) {
806 InstanceIdentifier<TunnelMonitorParams> path = InstanceIdentifier.builder(TunnelMonitorParams.class).build();
807 Optional<TunnelMonitorParams> storedTunnelMonitor = ItmUtils.read(LogicalDatastoreType.CONFIGURATION, path, dataBroker);
808 Class<? extends TunnelMonitoringTypeBase> monitorType ;
809 if(storedTunnelMonitor.isPresent() && storedTunnelMonitor.get().getMonitorProtocol()!=null )
810 monitorType = storedTunnelMonitor.get().getMonitorProtocol();
813 if(monitorProtocol!=null && monitorProtocol.equalsIgnoreCase(ITMConstants.MONITOR_TYPE_LLDP))
814 monitorType = TunnelMonitoringTypeLldp.class ;
816 monitorType = TunnelMonitoringTypeBfd.class ;
818 if (!storedTunnelMonitor.isPresent()|| storedTunnelMonitor.get().isEnabled() != monitorEnabled) {
819 TunnelMonitorParams tunnelMonitor = new TunnelMonitorParamsBuilder().setEnabled(monitorEnabled).setMonitorProtocol(monitorType).build();
820 ItmUtils.asyncUpdate(LogicalDatastoreType.CONFIGURATION, path, tunnelMonitor, dataBroker,
821 ItmUtils.DEFAULT_CALLBACK);
826 public void configureTunnelMonitorInterval(int interval) {
827 InstanceIdentifier<TunnelMonitorInterval> path = InstanceIdentifier.builder(TunnelMonitorInterval.class).build();
828 Optional<TunnelMonitorInterval> storedTunnelMonitor = ItmUtils.read(LogicalDatastoreType.CONFIGURATION, path, dataBroker);
829 if (!storedTunnelMonitor.isPresent() || storedTunnelMonitor.get().getInterval() != interval) {
830 TunnelMonitorInterval tunnelMonitor = new TunnelMonitorIntervalBuilder().setInterval(interval).build();
831 ItmUtils.asyncUpdate(LogicalDatastoreType.CONFIGURATION, path, tunnelMonitor, dataBroker,
832 ItmUtils.DEFAULT_CALLBACK);