5e74bb16c1833ba6e190d422b109e7d20ea87b8d
[vpnservice.git] / itm / itm-impl / src / main / java / org / opendaylight / vpnservice / itm / cli / VtepSchemaAdd.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 org.apache.karaf.shell.commands.Command;
12 import org.apache.karaf.shell.commands.Option;
13 import org.apache.karaf.shell.console.OsgiCommandSupport;
14 import org.opendaylight.vpnservice.itm.api.IITMProvider;
15 import org.opendaylight.vpnservice.itm.globals.ITMConstants;
16 import org.opendaylight.vpnservice.itm.impl.ItmUtils;
17 import org.opendaylight.vpnservice.itm.cli.ItmCliUtils;
18 //import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.itm.config.rev151102.vtep.config.schemas.VtepConfigSchema;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.config.rev151102.vtep.config.schemas.VtepConfigSchema;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * The Class which implements karaf command "vtep:schema-add".
25  */
26 @Command(scope = "vtep", name = "schema-add", description = "Adding a VTEP schema.")
27 public class VtepSchemaAdd extends OsgiCommandSupport {
28
29     private static final String SCHEMA_NAME = "--schema-name";
30     private static final String PORT_NAME = "--port-name";
31     private static final String VLAN_ID = "--vlan-id";
32     private static final String SUBNET_CIDR = "--subnet-cidr";
33     private static final String TRANSPORT_ZONE = "--transport-zone";
34     private static final String DPN_IDS = "--dpn-ids";
35     private static final String GATEWAY_IP = "--gateway-ip";
36     private static final String TUNNEL_TYPE = "--tunnel-type";
37     private static final String EXCLUDE_IP_FILTER = "--exclude-ip-filter";
38
39     /** The schema name. */
40     @Option(name = SCHEMA_NAME, aliases = { "-s" }, description = "Schema name", required = true, multiValued = false)
41     private String schemaName;
42
43     /** The port name. */
44     @Option(name = PORT_NAME, aliases = { "-p" }, description = "Port name", required = true, multiValued = false)
45     private String portName;
46
47     /** The vlan id. */
48     @Option(name = VLAN_ID, aliases = { "-v" }, description = "VLAN ID", required = true, multiValued = false)
49     private Integer vlanId;
50
51     /** The subnet mask. */
52     @Option(name = SUBNET_CIDR, aliases = {
53             "-sc" }, description = "Subnet Mask in CIDR-notation string, e.g. 10.0.0.0/24", required = true, multiValued = false)
54     private String subnetCIDR;
55
56     /** The transport zone. */
57     @Option(name = TRANSPORT_ZONE, aliases = {
58             "-tz" }, description = "Transport zone", required = true, multiValued = false)
59     private String transportZone;
60
61     /** The dpn ids. */
62     @Option(name = DPN_IDS, aliases = {
63             "-d" }, description = "DPN ID's in comma separated values. e.g: 2,3,10", required = false, multiValued = false)
64     private String dpnIds;
65
66     /** The gateway ip. */
67     @Option(name = GATEWAY_IP, aliases = {
68             "-g" }, description = "Gateway IP address", required = false, multiValued = false)
69     private String gatewayIp;
70
71     /** The tunnel type. */
72     @Option(name = TUNNEL_TYPE, aliases = {
73             "-t" }, description = "Tunnel type. Value: VXLAN | GRE. Default: VXLAN", required = false, multiValued = false)
74     private String tunnelType;
75
76     /** The exclude ip filter. */
77     @Option(name = EXCLUDE_IP_FILTER, aliases = {
78             "-ex" }, description = "IP Addresses which needs to be excluded from the specified subnet. IP address range or comma separated IP addresses can to be specified. e.g: 10.0.0.1-10.0.0.20,10.0.0.30,10.0.0.35", required = false, multiValued = false)
79     private String excludeIpFilter;
80
81     /** The Constant logger. */
82     private static final Logger LOG = LoggerFactory.getLogger(VtepSchemaAdd.class);
83
84     /** The itm provider. */
85     private IITMProvider itmProvider;
86
87     /**
88      * Sets the itm provider.
89      *
90      * @param itmProvider
91      *            the new itm provider
92      */
93     public void setItmProvider(IITMProvider itmProvider) {
94         this.itmProvider = itmProvider;
95     }
96
97     /**
98      * Command Usage.
99      */
100     private void usage() {
101         System.out.println(String.format(
102                 "usage: vtep:schema-add [%s schema-name] [%s port-name] [%s vlan-id] [%s subnet-cidr] [%s transport-zone] [%s dpn-ids] [%s gateway-ip] [%s tunnel-type] [%s exclude-ip-filter]",
103                 SCHEMA_NAME, PORT_NAME, VLAN_ID, SUBNET_CIDR, TRANSPORT_ZONE, DPN_IDS, GATEWAY_IP, TUNNEL_TYPE,
104                 EXCLUDE_IP_FILTER));
105     }
106
107     /*
108      * (non-Javadoc)
109      * 
110      * @see org.apache.karaf.shell.console.AbstractAction#doExecute()
111      */
112     @Override
113     protected Object doExecute() {
114         try {
115             if (this.schemaName == null || this.portName == null || this.vlanId == null || this.subnetCIDR == null
116                     || this.transportZone == null) {
117                 usage();
118                 return null;
119             }
120             LOG.debug("Executing vtep:schema-add command\t {} \t {} \t {} \t {} \t {} \t {} \t {} \t {} \t {}", schemaName,
121                     portName, vlanId, subnetCIDR, gatewayIp, transportZone, tunnelType, dpnIds, excludeIpFilter);
122
123             if( null == tunnelType) {
124                 tunnelType = ITMConstants.TUNNEL_TYPE_VXLAN ;
125             }
126             VtepConfigSchema schema = ItmUtils.constructVtepConfigSchema(schemaName, portName, vlanId, subnetCIDR,
127                     gatewayIp, transportZone, tunnelType, ItmCliUtils.constructDpnIdList(dpnIds), excludeIpFilter);
128             this.itmProvider.addVtepConfigSchema(schema);
129
130         } catch (IllegalArgumentException e) {
131             System.out.println(e.getMessage());
132         } catch (Exception e) {
133             LOG.error("Exception occurred during execution of command \"vtep:schema-add\": ", e);
134         }
135         return null;
136     }
137
138 }