Vxlan/Gre co-existence,Alarms,tunnelstate,TR fixes
[vpnservice.git] / itm / itm-impl / src / main / java / org / opendaylight / vpnservice / itm / cli / VtepSchemaShow.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
9 package org.opendaylight.vpnservice.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
17 import org.apache.commons.lang3.StringUtils;
18 import org.apache.karaf.shell.commands.Argument;
19 import org.apache.karaf.shell.commands.Command;
20 import org.apache.karaf.shell.console.OsgiCommandSupport;
21 import org.opendaylight.vpnservice.itm.api.IITMProvider;
22 import org.opendaylight.vpnservice.itm.globals.ITMConstants;
23 import org.opendaylight.vpnservice.itm.impl.ItmUtils;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.TunnelTypeBase;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.TunnelTypeGre;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.config.rev151102.vtep.config.schemas.VtepConfigSchema;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * The Class which implements karaf command "vtep:schema-show".
32  */
33 @Command(scope = "vtep", name = "schema-show", description = "Show all VTEP schemas.")
34 public class VtepSchemaShow extends OsgiCommandSupport {
35
36     /** The schema name. */
37     @Argument(index = 0, name = "schemaName", description = "Schema name", required = false, multiValued = false)
38     private String schemaName;
39
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 = "---------------------------------------------------------------------------------------------------------------------------------------";
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     @Override
65     protected Object doExecute() {
66         LOG.debug("Executing command: schema-show {} ", this.schemaName);
67         try {
68             if (this.schemaName != null) {
69                 VtepConfigSchema schema = this.itmProvider.getVtepConfigSchema(this.schemaName);
70                 if (schema == null) {
71                     System.out.println("No VTEP schema present with name: " + this.schemaName);
72                 } else {
73                     System.out.println(getHeaderOutput());
74                     printSchema(schema);
75                 }
76             } else {
77                 printAllVtepSchemas();
78             }
79         } catch (Exception e) {
80             LOG.error("Exception occurred during execution of command \"vtep:schema-show\": ", e);
81         }
82         return null;
83     }
84
85     /**
86      * Prints all vtep schemas.
87      */
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.");
92             return;
93         }
94         System.out.println(getHeaderOutput());
95         for (VtepConfigSchema schema : schemas) {
96             printSchema(schema);
97         }
98     }
99
100     /**
101      * Prints the schema.
102      *
103      * @param schema
104      *            the schema
105      */
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());
110
111         Iterator<BigInteger> dpnIterator = lstDpnIds.iterator();
112         Iterator<String> ipFilterIterator = lstIpFilter.iterator();
113
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 ;
122
123         Class<? extends TunnelTypeBase> tunType = schema.getTunnelType();
124
125         if( tunType.equals(TunnelTypeGre.class) )
126             strTunnelType = ITMConstants.TUNNEL_TYPE_GRE ;
127         else
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);
132
133         // Print first row
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));
143         }
144         System.out.println(System.lineSeparator());
145     }
146
147     /**
148      * Gets the exclude ip filter as list.
149      *
150      * @param excludeIpFilter
151      *            the exclude ip filter
152      * @return the exclude ip filter as list
153      */
154     private List<String> getExcludeIpFilterAsList(String excludeIpFilter) {
155         if (StringUtils.isBlank(excludeIpFilter)) {
156             return Collections.emptyList();
157         }
158         final String[] arrIpsOrRange = StringUtils.split(excludeIpFilter, ',');
159         return Arrays.asList(arrIpsOrRange);
160     }
161
162     /**
163      * Gets the vtep config schema header output.
164      *
165      * @return the vtep config schema header output
166      */
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();
174     }
175 }