Handle nullable lists
[genius.git] / itm / itm-impl / src / main / java / org / opendaylight / genius / itm / cli / VtepSchemaShow.java
1 /*
2  * Copyright (c) 2016, 2017 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
9 package org.opendaylight.genius.itm.cli;
10
11 import java.math.BigInteger;
12 import java.util.Arrays;
13 import java.util.Collections;
14 import java.util.Iterator;
15 import java.util.List;
16 import org.apache.commons.lang3.StringUtils;
17 import org.apache.karaf.shell.commands.Argument;
18 import org.apache.karaf.shell.commands.Command;
19 import org.apache.karaf.shell.console.OsgiCommandSupport;
20 import org.opendaylight.genius.itm.api.IITMProvider;
21 import org.opendaylight.genius.itm.globals.ITMConstants;
22 import org.opendaylight.genius.itm.impl.ItmUtils;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelTypeBase;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelTypeGre;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.config.rev160406.vtep.config.schemas.VtepConfigSchema;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * The Class which implements karaf command "vtep:schema-show".
31  */
32 @Command(scope = "vtep", name = "schema-show", description = "Show all VTEP schemas.")
33 public class VtepSchemaShow extends OsgiCommandSupport {
34
35     /** The schema name. */
36     @Argument(index = 0, name = "schemaName", description = "Schema name", required = false, multiValued = false)
37     private String schemaName;
38
39     private static String VTEP_CONFIG_SCHEMA_CLI_FORMAT = "%-14s %-12s %-8s %-16s %-13s %-14s %-11s %-20s %-32s";
40     public static final String HEADER_UNDERLINE = "------------------------------------------------------------------"
41             + "---------------------------------------------------------------------";
42
43     /** The Constant logger. */
44     private static final Logger LOG = LoggerFactory.getLogger(VtepSchemaShow.class);
45
46     /** The itm provider. */
47     private IITMProvider itmProvider;
48
49     /**
50      * Sets the itm provider.
51      *
52      * @param itmProvider
53      *            the new itm provider
54      */
55     public void setItmProvider(IITMProvider itmProvider) {
56         this.itmProvider = itmProvider;
57     }
58
59     /*
60      * (non-Javadoc)
61      *
62      * @see org.apache.karaf.shell.console.AbstractAction#doExecute()
63      */
64     @SuppressWarnings("checkstyle:IllegalCatch")
65     @Override
66     protected Object doExecute() {
67         LOG.debug("Executing command: schema-show {} ", this.schemaName);
68         try {
69             if (this.schemaName != null) {
70                 VtepConfigSchema schema = this.itmProvider.getVtepConfigSchema(this.schemaName);
71                 if (schema == null) {
72                     session.getConsole().println("No VTEP schema present with name: " + this.schemaName);
73                 } else {
74                     session.getConsole().println(getHeaderOutput());
75                     printSchema(schema);
76                 }
77             } else {
78                 printAllVtepSchemas();
79             }
80         } catch (Exception e) {
81             LOG.error("Exception occurred during execution of command \"vtep:schema-show\": ", e);
82         }
83         return null;
84     }
85
86     /**
87      * Prints all vtep schemas.
88      */
89     private void printAllVtepSchemas() {
90         List<VtepConfigSchema> schemas = this.itmProvider.getAllVtepConfigSchemas();
91         if (schemas == null || schemas.isEmpty()) {
92             session.getConsole().println("No VTEP schemas present.");
93             return;
94         }
95         session.getConsole().println(getHeaderOutput());
96         for (VtepConfigSchema schema : schemas) {
97             printSchema(schema);
98         }
99     }
100
101     /**
102      * Prints the schema.
103      *
104      * @param schema
105      *            the schema
106      */
107     private void printSchema(VtepConfigSchema schema) {
108         List<BigInteger> lstDpnIds = (schema.getDpnIds() == null) ? Collections.emptyList()
109                 : ItmUtils.getDpnIdList(schema.getDpnIds());
110         List<String> lstIpFilter = getExcludeIpFilterAsList(schema.getExcludeIpFilter());
111
112         Iterator<BigInteger> dpnIterator = lstDpnIds.iterator();
113         Iterator<String> ipFilterIterator = lstIpFilter.iterator();
114
115         String portName = StringUtils.defaultString(schema.getPortName());
116         String vlanId = String.valueOf(schema.getVlanId());
117         String subnetCIDR = (schema.getSubnet() == null) ? StringUtils.EMPTY
118                 : schema.getSubnet().stringValue();
119         String gatewayIp = (schema.getGatewayIp() == null) ? StringUtils.EMPTY
120                 : schema.getGatewayIp().stringValue();
121         String transportZone = StringUtils.defaultString(schema.getTransportZoneName());
122         String strTunnelType ;
123
124         Class<? extends TunnelTypeBase> tunType = schema.getTunnelType();
125
126         if (TunnelTypeGre.class.equals(tunType)) {
127             strTunnelType = ITMConstants.TUNNEL_TYPE_GRE;
128         } else {
129             strTunnelType = ITMConstants.TUNNEL_TYPE_VXLAN;
130         }
131         String dpnId = (dpnIterator.hasNext() ? String.valueOf(dpnIterator.next()) : StringUtils.EMPTY);
132         String excludeIpFilter = (ipFilterIterator.hasNext() ? String.valueOf(ipFilterIterator.next())
133                 : StringUtils.EMPTY);
134
135         // Print first row
136         session.getConsole().println(String.format(VTEP_CONFIG_SCHEMA_CLI_FORMAT, schema.getSchemaName(),
137                 portName, vlanId, subnetCIDR, gatewayIp, transportZone, strTunnelType, dpnId, excludeIpFilter));
138         while (dpnIterator.hasNext() || ipFilterIterator.hasNext()) {
139             dpnId = (dpnIterator.hasNext() ? String.valueOf(dpnIterator.next()) : StringUtils.EMPTY);
140             excludeIpFilter = (ipFilterIterator.hasNext() ? String.valueOf(ipFilterIterator.next())
141                     : StringUtils.EMPTY);
142             session.getConsole().println(String.format(VTEP_CONFIG_SCHEMA_CLI_FORMAT, StringUtils.EMPTY,
143                     StringUtils.EMPTY, StringUtils.EMPTY, StringUtils.EMPTY, StringUtils.EMPTY, StringUtils.EMPTY,
144                     StringUtils.EMPTY, dpnId, excludeIpFilter));
145         }
146         session.getConsole().println(System.lineSeparator());
147     }
148
149     /**
150      * Gets the exclude ip filter as list.
151      *
152      * @param excludeIpFilter
153      *            the exclude ip filter
154      * @return the exclude ip filter as list
155      */
156     private List<String> getExcludeIpFilterAsList(String excludeIpFilter) {
157         if (StringUtils.isBlank(excludeIpFilter)) {
158             return Collections.emptyList();
159         }
160         final String[] arrIpsOrRange = StringUtils.split(excludeIpFilter, ',');
161         return Arrays.asList(arrIpsOrRange);
162     }
163
164     /**
165      * Gets the vtep config schema header output.
166      *
167      * @return the vtep config schema header output
168      */
169     private String getHeaderOutput() {
170         String headerBuilder =
171                 String.format(VTEP_CONFIG_SCHEMA_CLI_FORMAT, "SchemaName", "PortName", "VlanID", "Subnet", "GatewayIP",
172                         "TransportZone", "TunnelType", "DPN-IDS", "ExcludeIpFilter") + '\n' + HEADER_UNDERLINE;
173         return headerBuilder;
174     }
175 }