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
9 package org.opendaylight.genius.itm.cli;
11 import org.apache.commons.lang3.StringUtils;
12 import org.apache.karaf.shell.commands.Argument;
13 import org.apache.karaf.shell.commands.Command;
14 import org.apache.karaf.shell.console.OsgiCommandSupport;
15 import org.opendaylight.genius.itm.api.IITMProvider;
16 import org.opendaylight.genius.itm.globals.ITMConstants;
17 import org.opendaylight.genius.itm.impl.ItmUtils;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelTypeBase;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelTypeGre;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.config.rev160406.vtep.config.schemas.VtepConfigSchema;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
24 import java.math.BigInteger;
25 import java.util.Arrays;
26 import java.util.Collections;
27 import java.util.Iterator;
28 import java.util.List;
31 * The Class which implements karaf command "vtep:schema-show".
33 @Command(scope = "vtep", name = "schema-show", description = "Show all VTEP schemas.")
34 public class VtepSchemaShow extends OsgiCommandSupport {
36 /** The schema name. */
37 @Argument(index = 0, name = "schemaName", description = "Schema name", required = false, multiValued = false)
38 private String schemaName;
40 private static String VTEP_CONFIG_SCHEMA_CLI_FORMAT = "%-14s %-12s %-8s %-16s %-13s %-14s %-11s %-20s %-32s";
41 public static final String HEADER_UNDERLINE = "---------------------------------------------------------------------------------------------------------------------------------------";
43 /** The Constant logger. */
44 private static final Logger LOG = LoggerFactory.getLogger(VtepSchemaShow.class);
46 /** The itm provider. */
47 private IITMProvider itmProvider;
50 * Sets the itm provider.
53 * the new itm provider
55 public void setItmProvider(IITMProvider itmProvider) {
56 this.itmProvider = itmProvider;
62 * @see org.apache.karaf.shell.console.AbstractAction#doExecute()
65 protected Object doExecute() {
66 LOG.debug("Executing command: schema-show {} ", this.schemaName);
68 if (this.schemaName != null) {
69 VtepConfigSchema schema = this.itmProvider.getVtepConfigSchema(this.schemaName);
71 System.out.println("No VTEP schema present with name: " + this.schemaName);
73 System.out.println(getHeaderOutput());
77 printAllVtepSchemas();
79 } catch (Exception e) {
80 LOG.error("Exception occurred during execution of command \"vtep:schema-show\": ", e);
86 * Prints all vtep schemas.
88 private void printAllVtepSchemas() {
89 List<VtepConfigSchema> schemas = this.itmProvider.getAllVtepConfigSchemas();
90 if (schemas == null || schemas.isEmpty()) {
91 System.out.println("No VTEP schemas present.");
94 System.out.println(getHeaderOutput());
95 for (VtepConfigSchema schema : schemas) {
106 private void printSchema(VtepConfigSchema schema) {
107 List<BigInteger> lstDpnIds = (schema.getDpnIds() == null) ? Collections.<BigInteger> emptyList()
108 : ItmUtils.getDpnIdList(schema.getDpnIds());
109 List<String> lstIpFilter = getExcludeIpFilterAsList(schema.getExcludeIpFilter());
111 Iterator<BigInteger> dpnIterator = lstDpnIds.iterator();
112 Iterator<String> ipFilterIterator = lstIpFilter.iterator();
114 String portName = StringUtils.defaultString(schema.getPortName());
115 String vlanId = String.valueOf(schema.getVlanId());
116 String subnetCIDR = (schema.getSubnet() == null) ? StringUtils.EMPTY
117 : String.valueOf(schema.getSubnet().getValue());
118 String gatewayIp = (schema.getGatewayIp() == null) ? StringUtils.EMPTY
119 : String.valueOf(schema.getGatewayIp().getValue());
120 String transportZone = StringUtils.defaultString(schema.getTransportZoneName());
121 String strTunnelType ;
123 Class<? extends TunnelTypeBase> tunType = schema.getTunnelType();
125 if( tunType.equals(TunnelTypeGre.class) )
126 strTunnelType = ITMConstants.TUNNEL_TYPE_GRE ;
128 strTunnelType = ITMConstants.TUNNEL_TYPE_VXLAN ;
129 String dpnId = (dpnIterator.hasNext() ? String.valueOf(dpnIterator.next()) : StringUtils.EMPTY);
130 String excludeIpFilter = (ipFilterIterator.hasNext() ? String.valueOf(ipFilterIterator.next())
131 : StringUtils.EMPTY);
134 System.out.println(String.format(VTEP_CONFIG_SCHEMA_CLI_FORMAT, schema.getSchemaName(), portName, vlanId,
135 subnetCIDR, gatewayIp, transportZone, strTunnelType, dpnId, excludeIpFilter));
136 while (dpnIterator.hasNext() || ipFilterIterator.hasNext()) {
137 dpnId = (dpnIterator.hasNext() ? String.valueOf(dpnIterator.next()) : StringUtils.EMPTY);
138 excludeIpFilter = (ipFilterIterator.hasNext() ? String.valueOf(ipFilterIterator.next())
139 : StringUtils.EMPTY);
140 System.out.println(String.format(VTEP_CONFIG_SCHEMA_CLI_FORMAT, StringUtils.EMPTY, StringUtils.EMPTY,
141 StringUtils.EMPTY, StringUtils.EMPTY, StringUtils.EMPTY, StringUtils.EMPTY, StringUtils.EMPTY,
142 dpnId, excludeIpFilter));
144 System.out.println(System.lineSeparator());
148 * Gets the exclude ip filter as list.
150 * @param excludeIpFilter
151 * the exclude ip filter
152 * @return the exclude ip filter as list
154 private List<String> getExcludeIpFilterAsList(String excludeIpFilter) {
155 if (StringUtils.isBlank(excludeIpFilter)) {
156 return Collections.emptyList();
158 final String[] arrIpsOrRange = StringUtils.split(excludeIpFilter, ',');
159 return Arrays.asList(arrIpsOrRange);
163 * Gets the vtep config schema header output.
165 * @return the vtep config schema header output
167 private String getHeaderOutput() {
168 StringBuilder headerBuilder = new StringBuilder();
169 headerBuilder.append(String.format(VTEP_CONFIG_SCHEMA_CLI_FORMAT, "SchemaName", "PortName", "VlanID", "Subnet",
170 "GatewayIP", "TransportZone", "TunnelType", "DPN-IDS", "ExcludeIpFilter"));
171 headerBuilder.append('\n');
172 headerBuilder.append(HEADER_UNDERLINE);
173 return headerBuilder.toString();