7f417d3b4230a0466bcf11a2c23a68be88c779e7
[vpnservice.git] / itm / itm-impl / src / main / java / org / opendaylight / vpnservice / itm / cli / TepCommandHelper.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.vpnservice.itm.cli;
9
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;
15 import java.util.Map;
16
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;
51
52 import com.google.common.base.Optional;
53 import com.google.common.base.Preconditions;
54
55
56 public class TepCommandHelper {
57
58     private static final Logger LOG = LoggerFactory.getLogger(TepCommandHelper.class);
59     private DataBroker dataBroker;
60     static int check = 0;
61     static short flag = 0;
62     /*
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
66      */
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;
72
73     // private List<InstanceIdentifier<? extends DataObject>> vtepPaths = new
74     // ArrayList<>();
75
76
77     public TepCommandHelper(final DataBroker broker) {
78         this.dataBroker = broker;
79     }
80
81
82     public void setInterfaceManager(IInterfaceManager interfaceManager) {
83         this.interfaceManager = interfaceManager;
84     }
85
86     public void createLocalCache(BigInteger dpnId, String portName, Integer vlanId, String ipAddress,
87                     String subnetMask, String gatewayIp, String transportZone) {
88
89         check++;
90         IpAddress ipAddressObj = null;
91         IpAddress gatewayIpObj = null;
92         IpPrefix subnetMaskObj = null;
93         VtepsKey vtepkey = new VtepsKey(dpnId, portName);
94         try {
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());
99             } else {
100                 LOG.debug("gateway is null");
101             }
102         } catch (Exception e) {
103             System.out.println("Invalid IpAddress. Expected: 1.0.0.0 to 254.255.255.255");
104             return;
105         }
106         try {
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");
110             return;
111         }
112
113         if (!validateIPs(ipAddress, subnetMask, gatewayIp)) {
114             System.out.println("IpAddress and gateWayIp should belong to the subnet provided");
115             return;
116         }
117
118         if (checkTepPerTzPerDpn(transportZone, dpnId)) {
119             System.out.println("Only one end point per transport Zone per Dpn is allowed");
120             return;
121         }
122         Vteps vtepCli = new VtepsBuilder().setDpnId(dpnId).setIpAddress(ipAddressObj).setKey(vtepkey)
123                 .setPortname(portName).build();
124         validateForDuplicates(vtepCli, transportZone);
125
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)) {
133                     // do nothing
134                 } else {
135                     vtepListTemp.add(vtepCli);
136                 }
137             } else { // subnet doesnt exist
138                 if (checkExistingSubnet(subVtepMapTemp, subObCli)) {
139                     System.out.println("subnet with subnet mask " + subObCli.get_key() + "already exists");
140                     return;
141                 }
142                 List<Vteps> vtepListTemp = new ArrayList<Vteps>();
143                 vtepListTemp.add(vtepCli);
144                 subVtepMapTemp.put(subObCli, vtepListTemp);
145             }
146         } else {
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);
152         }
153     }
154
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)))) {
158             return true;
159         } else {
160             LOG.trace("InValid IP");
161             return false;
162         }
163     }
164
165     /**
166      * Validate for duplicates.
167      *
168      * @param inputVtep
169      *            the input vtep
170      * @param transportZone
171      *            the transport zone
172      */
173     public void validateForDuplicates(Vteps inputVtep, String transportZone) {
174         Map<String, TransportZone> tZoneMap = getAllTransportZonesAsMap();
175
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);
184             }
185         }
186         // Checking for duplicates in config DS
187         for (TransportZone tZ : tZoneMap.values()) {
188             boolean isGreType = false;
189             if (tZ.getTunnelType().equals(TunnelTypeGre.class)) {
190                 isGreType = true;
191             }
192             for (Subnets sub : ItmUtils.emptyIfNull(tZ.getSubnets())) {
193                 List<Vteps> vtepList = sub.getVteps();
194                 validateForDuplicateAndSingleGreTep(inputVtep, isConfiguredTepGreType, isGreType, vtepList);
195             }
196         }
197     }
198
199     private void validateForDuplicateAndSingleGreTep(Vteps inputVtep, boolean isConfiguredTepGreType, boolean isGreType,
200             List<Vteps> vtepList) {
201         if (ItmUtils.isEmpty(vtepList)) {
202             return;
203         }
204         if (vtepList.contains(inputVtep)) {
205             Preconditions.checkArgument(false, "VTEP already exists");
206         }
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.")
213                             .toString();
214                     Preconditions.checkArgument(false, errMsg);
215                 }
216             }
217         }
218     }
219
220     /**
221      * Gets all transport zones as map.
222      *
223      * @return all transport zones as map
224      */
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);
231            }
232         }
233         return tZoneMap;
234     }
235
236     /**
237      * Checks if is gre tunnel type.
238      *
239      * @param tZoneName
240      *            the zone name
241      * @param tZoneMap
242      *            the zone map
243      * @return true, if is gre tunnel type
244      */
245     private boolean isGreTunnelType(String tZoneName, Map<String, TransportZone> tZoneMap) {
246         TransportZone tzone = tZoneMap.get(tZoneName);
247         /*
248         if (tzone != null && StringUtils.equalsIgnoreCase(ITMConstants.TUNNEL_TYPE_GRE, tzone.getTunnelType())) {
249             return true;
250         }
251         */
252         if( (tzone != null) && (tzone.getTunnelType()).equals(TunnelTypeGre.class) ) {
253            return true;
254         }
255         return false;
256     }
257
258     /**
259      * Gets the transport zone.
260      *
261      * @param tzone
262      *            the tzone
263      * @return the transport zone
264      */
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,
269                 dataBroker);
270         if (tZoneOptional.isPresent()) {
271             return tZoneOptional.get();
272         }
273         return null;
274     }
275
276     /**
277      * Gets the transport zone from config ds.
278      *
279      * @param tzone
280      *            the tzone
281      * @return the transport zone
282      */
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,
287                 dataBroker);
288         if (tZoneOptional.isPresent()) {
289             return tZoneOptional.get();
290         }
291         return null;
292     }
293
294     /**
295      * Gets all transport zones.
296      *
297      * @return all transport zones
298      */
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();
304         }
305         return null;
306     }
307
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())))
312                     return true;
313                 if (!(subOb.get_gatewayIp().equals(subObCli.get_gatewayIp())))
314                     return true;
315             }
316         }
317         return false;
318     }
319
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))
328                         return true;
329             }
330         }
331
332         // check in DS
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())
341                 return false;
342             for (Subnets sub : tz.getSubnets()) {
343                 if (sub.getVteps() == null || sub.getVteps().isEmpty())
344                     continue;
345                 for (Vteps vtep : sub.getVteps()) {
346                     if (vtep.getDpnId().equals(dpnId)) {
347                         return true;
348                     }
349                 }
350             }
351         }
352         return false;
353     }
354
355     public void buildTeps() {
356         TransportZones tZonesBuilt = null;
357         TransportZone tZone = null;
358         try {
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);
369                         Subnets subnet =
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);
375                     }
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)) {
387                             tZone =
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)) {
392                             tZone =
393                                             new TransportZoneBuilder().setKey(new TransportZoneKey(tZ))
394                                                             .setTunnelType(TunnelTypeGre.class).setSubnets(subnetList)
395                                                             .setZoneName(tZ).build();
396                         }
397                     } else {
398                         tZone =
399                                         new TransportZoneBuilder().setKey(new TransportZoneKey(tZ))
400                                                         .setTunnelType(TunnelTypeVxlan.class).setSubnets(subnetList).setZoneName(tZ)
401                                                         .build();
402                     }
403                     LOG.debug("tzone object" + tZone);
404                     tZoneList.add(tZone);
405                 }
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);
412                 tZones.clear();
413                 tZoneList.clear();
414                 subnetList.clear();
415                 LOG.debug("Everything cleared");
416             } else {
417                 LOG.debug("NO vteps were configured");
418             }
419         } catch (Exception e) {
420             e.printStackTrace();
421         }
422     }
423
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())
431             {
432                 System.out.println("No teps configured");
433                 return;
434             }
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");
445                     continue;
446                 }
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");
450                         continue;
451                     }
452                     for (Vteps vtep : sub.getVteps()) {
453                         flag = true;
454                         String strTunnelType ;
455                         if( (tZ.getTunnelType()).equals(TunnelTypeGre.class) )
456                           strTunnelType = ITMConstants.TUNNEL_TYPE_GRE ;
457                         else
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()));
463                     }
464                 }
465             }
466             if (flag == true) {
467                 for (String p : result) {
468                     System.out.println(p);
469                 }
470             } else
471                 System.out.println("No teps to display");
472         } else
473             System.out.println("No teps configured");
474     }
475
476
477     public void deleteVtep(BigInteger dpnId, String portName, Integer vlanId, String ipAddress, String subnetMask,
478                     String gatewayIp, String transportZone) {
479
480         IpAddress ipAddressObj = null;
481         IpAddress gatewayIpObj = null;
482         IpPrefix subnetMaskObj = null;
483         VtepsKey vtepkey = new VtepsKey(dpnId, portName);
484         try {
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());
489             } else {
490                 LOG.debug("gateway is null");
491             }
492         } catch (Exception e) {
493             System.out.println("Invalid IpAddress. Expected: 1.0.0.0 to 254.255.255.255");
494             return;
495         }
496         try {
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");
500             return;
501         }
502
503         if (!validateIPs(ipAddress, subnetMask, gatewayIp)) {
504             System.out.println("IpAddress and gateWayIp should belong to the subnet provided");
505             return;
506         }
507         SubnetsKey subnetsKey = new SubnetsKey(subnetMaskObj);
508         Vteps vtepCli = null;
509         Subnets subCli = null;
510
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();
515  
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 =
524                                     InstanceIdentifier
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);
533                       }
534                         else
535                             System.out.println(String.format("vtep with this vlan or gateway doesnt exist"));
536                         }
537                 }
538                 else 
539                     System.out.println(String.format("Vtep with this ipaddress doesnt exist"));
540                 } else {
541                 System.out.println(String.format("Vtep Doesnt exist"));
542             }
543         }
544     }
545
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<>();
554         try {
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())
563                             continue;
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) {
576                                         subDelList.add(sub);
577                                     }
578
579                                 }
580                             }
581                         }
582                     }
583
584                     for (TransportZone tZ : tZones.getTransportZone()) {
585                         if (tZ.getSubnets() == null || tZ.getSubnets().isEmpty())
586                             continue;
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) {
596                                     tzDelList.add(tZ);
597                                 }
598                             }
599                         }
600                     }
601
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();
607                             tzPaths.add(tpath);
608                             if (tZones.getTransportZone() == null || tZones.getTransportZone().size() == 0) {
609                                 MDSALDataStoreUtils.asyncRemove(dataBroker, LogicalDatastoreType.CONFIGURATION, path,
610                                                 ItmUtils.DEFAULT_CALLBACK);
611                                 return;
612                             }
613                         }
614                     }
615                     allPaths.addAll(vtepPaths);
616                     allPaths.addAll(subnetPaths);
617                     allPaths.addAll(tzPaths);
618                     ItmUtils.asyncBulkRemove(dataBroker, LogicalDatastoreType.CONFIGURATION, allPaths,
619                                     ItmUtils.DEFAULT_CALLBACK);
620                 }
621                 vtepPaths.clear();
622                 subnetPaths.clear();
623                 tzPaths.clear();
624                 allPaths.clear();
625                 vtepDelCommitList.clear();
626             }
627         } catch (Exception e) {
628             e.printStackTrace();
629         }
630     }
631 /*
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");
636             return;
637         }
638         if (!tunnelMonitorEnabled) {
639             System.out.println("Tunnel Monitoring is Off");
640         }
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("----------------------------------------------------------------------------------------------------------------------");
645
646         for (StateTunnelList tunnel : tunnelLists) {
647             String logicaltunnelState = (tunnel.isLogicalTunnelState()) ? "UP" : "DOWN";
648             try {
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));
660                     }
661                 } else {
662                     LOG.error("No trunks for " + tunnel.getLogicalTunnelGroupName());
663                 }
664
665             } catch (InterfaceNotFoundException e) {
666                 LOG.error("if not found " + tunnel.getLogicalTunnelGroupName());
667             }
668         }
669     }
670 */
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());
681         } else {
682             LOG.debug("gateway is null");
683         }
684         SubnetsKey subnetsKey = new SubnetsKey(subnetMaskObj);
685         Vteps vtepCli =
686                         new VtepsBuilder().setDpnId(dpnId).setIpAddress(ipAddressObj).setKey(vtepkey)
687                                         .setPortname(portName).build();
688         SubnetObject subObCli = new SubnetObject(gatewayIpObj, subnetsKey, subnetMaskObj, vlanId);
689
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);
701                         }
702                     }
703                 } else {
704                     System.out.println("Vtep " + "has not been configured");
705                 }
706             }
707         }
708         return exists;
709     }
710
711     public void configureTunnelType(String tZoneName, String tunnelType) {
712         LOG.debug("configureTunnelType {} for transportZone {}", tunnelType, tZoneName);
713
714         TransportZone tZoneFromConfigDS = getTransportZoneFromConfigDS(tZoneName);
715         validateTunnelType(tZoneName, tunnelType,tZoneFromConfigDS);
716
717         if (tZoneFromConfigDS != null) {
718             LOG.debug("Transport zone {} with tunnel type {} already exists. No action required.", tZoneName,
719                     tunnelType);
720             return;
721         }
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 ;
731
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>();
738             }
739         } else {
740             tZoneList = new ArrayList<TransportZone>();
741         }
742         tZoneList.add(tZone);
743         transportZones = new TransportZonesBuilder().setTransportZone(tZoneList).build();
744         ItmUtils.syncWrite(LogicalDatastoreType.CONFIGURATION, path, transportZones, dataBroker);
745
746     }
747
748     /**
749      * Validate tunnel type.
750      *
751      * @param tZoneName
752      *            the t zone name
753      * @param tunnelType
754      *            the tunnel type
755      */
756     private void validateTunnelType(String tZoneName, String tunnelType,TransportZone tZoneFromConfigDS) {
757         /*
758         String strTunnelType = ItmUtils.validateTunnelType(tunnelType);
759
760         TransportZone tZone = getTransportZone(tZoneName);
761         if (tZone != null) {
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);
769             }
770         }
771         */
772         String strTunnelType = ItmUtils.validateTunnelType(tunnelType);
773         Class<? extends TunnelTypeBase> tunType ;
774         if( strTunnelType.equals(ITMConstants.TUNNEL_TYPE_VXLAN))
775             tunType = TunnelTypeVxlan.class ;
776         else 
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);
787            }
788         }
789     }
790
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);
798         }
799     }
800
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);
808         }
809     }
810 }