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.vpnservice.itm.cli;
10 import java.math.BigInteger;
11 import java.util.ArrayList;
12 import java.util.Arrays;
13 import java.util.HashMap;
14 import java.util.List;
17 import org.apache.commons.lang3.StringUtils;
18 import org.apache.commons.net.util.SubnetUtils;
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.vpnservice.interfacemgr.exceptions.InterfaceNotFoundException;
22 import org.opendaylight.vpnservice.interfacemgr.interfaces.IInterfaceManager;
23 //import org.opendaylight.vpnservice.interfacemgr.util.OperationalIfmUtil;
24 import org.opendaylight.vpnservice.itm.globals.ITMConstants;
25 import org.opendaylight.vpnservice.itm.impl.ItmUtils;
26 import org.opendaylight.vpnservice.mdsalutil.MDSALDataStoreUtils;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.config.rev151102.TunnelMonitorEnabled;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.config.rev151102.TunnelMonitorEnabledBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.config.rev151102.TunnelMonitorInterval;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.config.rev151102.TunnelMonitorIntervalBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.TunnelTypeBase;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.TunnelTypeGre;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.TunnelTypeVxlan;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.rev150701.TransportZones;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.rev150701.TransportZonesBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.rev150701.transport.zones.TransportZone;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.rev150701.transport.zones.TransportZoneBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.rev150701.transport.zones.TransportZoneKey;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.rev150701.transport.zones.transport.zone.Subnets;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.rev150701.transport.zones.transport.zone.SubnetsBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.rev150701.transport.zones.transport.zone.SubnetsKey;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.rev150701.transport.zones.transport.zone.subnets.Vteps;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.rev150701.transport.zones.transport.zone.subnets.VtepsBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.rev150701.transport.zones.transport.zone.subnets.VtepsKey;
47 import org.opendaylight.yangtools.yang.binding.DataObject;
48 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
52 import com.google.common.base.Optional;
53 import com.google.common.base.Preconditions;
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 %-12s %-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 %-12s %-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(TunnelsState tunnelsState, boolean tunnelMonitorEnabled) {
633 List<StateTunnelList> tunnelLists = tunnelsState.getStateTunnelList();
634 if (tunnelLists == null || tunnelLists.isEmpty()) {
635 System.out.println("No Logical Tunnels Exist");
638 if (!tunnelMonitorEnabled) {
639 System.out.println("Tunnel Monitoring is Off");
641 System.out.println(String.format("%-16s %-16s %-16s %-10s %-16s %-8s %-10s %-10s", "Source-DPN",
642 "Destination-DPN", "SourcePortName", "Source-IP", "Destination-IP", "VLan-ID", "Trunk-State",
643 "Logical-Tunnel-State"));
644 System.out.println("----------------------------------------------------------------------------------------------------------------------");
646 for (StateTunnelList tunnel : tunnelLists) {
647 String logicaltunnelState = (tunnel.isLogicalTunnelState()) ? "UP" : "DOWN";
649 List<String> trunks =
650 interfaceManager.getTunnelInterfacesOfLogicalGroup(tunnel.getLogicalTunnelGroupName());
651 if (trunks != null && !trunks.isEmpty()) {
652 for (String trunk : trunks) {
653 List<String> params = Arrays.asList(trunk.split(":"));
654 LOG.trace("trunk {} for LogicalIf {} ", trunk, tunnel.getLogicalTunnelGroupName());
655 String trunkState = (OperationalIfmUtil.isInterfaceUp(dataBroker, trunk)) ? "UP" : "DOWN";
656 System.out.println(String.format("%-16s %-16s %-16s %-10s %-16s %-8s %-10s %-10s", tunnel
657 .getSourceDPN().toString(), tunnel.getDestinationDPN().toString(), params
658 .get(1), params.get(3), params.get(4), params.get(2), trunkState,
659 logicaltunnelState));
662 LOG.error("No trunks for " + tunnel.getLogicalTunnelGroupName());
665 } catch (InterfaceNotFoundException e) {
666 LOG.error("if not found " + tunnel.getLogicalTunnelGroupName());
671 // deletes from ADD-cache if it exists.
672 public boolean isInCache(BigInteger dpnId, String portName, Integer vlanId, String ipAddress, String subnetMask,
673 String gatewayIp, String transportZone) {
674 boolean exists = false;
675 VtepsKey vtepkey = new VtepsKey(dpnId, portName);
676 IpAddress ipAddressObj = new IpAddress(ipAddress.toCharArray());
677 IpPrefix subnetMaskObj = new IpPrefix(subnetMask.toCharArray());
678 IpAddress gatewayIpObj = new IpAddress("0.0.0.0".toCharArray());
679 if (gatewayIp != null) {
680 gatewayIpObj = new IpAddress(gatewayIp.toCharArray());
682 LOG.debug("gateway is null");
684 SubnetsKey subnetsKey = new SubnetsKey(subnetMaskObj);
686 new VtepsBuilder().setDpnId(dpnId).setIpAddress(ipAddressObj).setKey(vtepkey)
687 .setPortname(portName).build();
688 SubnetObject subObCli = new SubnetObject(gatewayIpObj, subnetsKey, subnetMaskObj, vlanId);
690 if (tZones.containsKey(transportZone)) {
691 Map<SubnetObject, List<Vteps>> subVtepMapTemp = (Map<SubnetObject, List<Vteps>>) tZones.get(transportZone);
692 if (subVtepMapTemp.containsKey(subObCli)) { // if Subnet exists
693 List<Vteps> vtepListTemp = (List<Vteps>) subVtepMapTemp.get(subObCli);
694 if (vtepListTemp.contains(vtepCli)) {
695 exists = true; // return true if tzones has vtep
696 vtepListTemp.remove(vtepCli);
697 if (vtepListTemp.size() == 0) {
698 subVtepMapTemp.remove(subObCli);
699 if (subVtepMapTemp.size() == 0) {
700 tZones.remove(transportZone);
704 System.out.println("Vtep " + "has not been configured");
711 public void configureTunnelType(String tZoneName, String tunnelType) {
712 LOG.debug("configureTunnelType {} for transportZone {}", tunnelType, tZoneName);
714 TransportZone tZoneFromConfigDS = getTransportZoneFromConfigDS(tZoneName);
715 validateTunnelType(tZoneName, tunnelType,tZoneFromConfigDS);
717 if (tZoneFromConfigDS != null) {
718 LOG.debug("Transport zone {} with tunnel type {} already exists. No action required.", tZoneName,
722 TransportZones transportZones = null;
723 List<TransportZone> tZoneList = null;
724 InstanceIdentifier<TransportZones> path = InstanceIdentifier.builder(TransportZones.class).build();
725 Optional<TransportZones> tZones = ItmUtils.read(LogicalDatastoreType.CONFIGURATION, path, dataBroker);
726 Class<? extends TunnelTypeBase> tunType = TunnelTypeVxlan.class;
727 if( tunnelType.equals(ITMConstants.TUNNEL_TYPE_VXLAN))
728 tunType = TunnelTypeVxlan.class ;
729 else if( tunnelType.equals(ITMConstants.TUNNEL_TYPE_GRE) )
730 tunType = TunnelTypeGre.class ;
732 TransportZone tZone = new TransportZoneBuilder().setKey(new TransportZoneKey(tZoneName))
733 .setTunnelType(tunType).build();
734 if (tZones.isPresent()) {
735 tZoneList = tZones.get().getTransportZone();
736 if(tZoneList == null || tZoneList.isEmpty()) {
737 tZoneList = new ArrayList<TransportZone>();
740 tZoneList = new ArrayList<TransportZone>();
742 tZoneList.add(tZone);
743 transportZones = new TransportZonesBuilder().setTransportZone(tZoneList).build();
744 ItmUtils.syncWrite(LogicalDatastoreType.CONFIGURATION, path, transportZones, dataBroker);
749 * Validate tunnel type.
756 private void validateTunnelType(String tZoneName, String tunnelType,TransportZone tZoneFromConfigDS) {
758 String strTunnelType = ItmUtils.validateTunnelType(tunnelType);
760 TransportZone tZone = getTransportZone(tZoneName);
762 if (!StringUtils.equalsIgnoreCase(strTunnelType, tZone.getTunnelType())
763 && ItmUtils.isNotEmpty(tZone.getSubnets())) {
764 String errorMsg = new StringBuilder("Changing the tunnel type from ").append(tZone.getTunnelType())
765 .append(" to ").append(strTunnelType)
766 .append(" is not allowed for already configured transport zone [").append(tZoneName)
767 .append("].").toString();
768 Preconditions.checkArgument(false, errorMsg);
772 String strTunnelType = ItmUtils.validateTunnelType(tunnelType);
773 Class<? extends TunnelTypeBase> tunType ;
774 if( strTunnelType.equals(ITMConstants.TUNNEL_TYPE_VXLAN))
775 tunType = TunnelTypeVxlan.class ;
777 tunType = TunnelTypeGre.class ;
778 //TransportZone tZone = getTransportZone(tZoneName);
779 // if (tZone != null) {
780 if (tZoneFromConfigDS != null) {
781 if( (!tZoneFromConfigDS.getTunnelType().equals(tunType)) && ItmUtils.isNotEmpty(tZoneFromConfigDS.getSubnets())) {
782 String errorMsg = new StringBuilder("Changing the tunnel type from ").append(tZoneFromConfigDS.getTunnelType())
783 .append(" to ").append(strTunnelType)
784 .append(" is not allowed for already configured transport zone [").append(tZoneName)
785 .append("].").toString();
786 Preconditions.checkArgument(false, errorMsg);
791 public void configureTunnelMonitorEnabled(boolean monitorEnabled) {
792 InstanceIdentifier<TunnelMonitorEnabled> path = InstanceIdentifier.builder(TunnelMonitorEnabled.class).build();
793 Optional<TunnelMonitorEnabled> storedTunnelMonitor = ItmUtils.read(LogicalDatastoreType.CONFIGURATION, path, dataBroker);
794 if (!storedTunnelMonitor.isPresent() || storedTunnelMonitor.get().isEnabled() != monitorEnabled) {
795 TunnelMonitorEnabled tunnelMonitor = new TunnelMonitorEnabledBuilder().setEnabled(monitorEnabled).build();
796 ItmUtils.asyncUpdate(LogicalDatastoreType.CONFIGURATION, path, tunnelMonitor, dataBroker,
797 ItmUtils.DEFAULT_CALLBACK);
801 public void configureTunnelMonitorInterval(int interval) {
802 InstanceIdentifier<TunnelMonitorInterval> path = InstanceIdentifier.builder(TunnelMonitorInterval.class).build();
803 Optional<TunnelMonitorInterval> storedTunnelMonitor = ItmUtils.read(LogicalDatastoreType.CONFIGURATION, path, dataBroker);
804 if (!storedTunnelMonitor.isPresent() || storedTunnelMonitor.get().getInterval() != interval) {
805 TunnelMonitorInterval tunnelMonitor = new TunnelMonitorIntervalBuilder().setInterval(interval).build();
806 ItmUtils.asyncUpdate(LogicalDatastoreType.CONFIGURATION, path, tunnelMonitor, dataBroker,
807 ItmUtils.DEFAULT_CALLBACK);