2  * Copyright 2015 Open Networking Laboratory
 
   4  * Licensed under the Apache License, Version 2.0 (the "License");
 
   5  * you may not use this file except in compliance with the License.
 
   6  * You may obtain a copy of the License at
 
   8  *     http://www.apache.org/licenses/LICENSE-2.0
 
  10  * Unless required by applicable law or agreed to in writing, software
 
  11  * distributed under the License is distributed on an "AS IS" BASIS,
 
  12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  13  * See the License for the specific language governing permissions and
 
  14  * limitations under the License.
 
  16 package org.onosproject.vtnrsc.cli.subnet;
 
  20 import org.apache.karaf.shell.commands.Argument;
 
  21 import org.apache.karaf.shell.commands.Command;
 
  22 import org.apache.karaf.shell.commands.Option;
 
  23 import org.onlab.packet.IpAddress;
 
  24 import org.onlab.packet.IpAddress.Version;
 
  25 import org.onlab.packet.IpPrefix;
 
  26 import org.onosproject.cli.AbstractShellCommand;
 
  27 import org.onosproject.vtnrsc.AllocationPool;
 
  28 import org.onosproject.vtnrsc.DefaultSubnet;
 
  29 import org.onosproject.vtnrsc.HostRoute;
 
  30 import org.onosproject.vtnrsc.Subnet;
 
  31 import org.onosproject.vtnrsc.Subnet.Mode;
 
  32 import org.onosproject.vtnrsc.SubnetId;
 
  33 import org.onosproject.vtnrsc.TenantId;
 
  34 import org.onosproject.vtnrsc.TenantNetworkId;
 
  35 import org.onosproject.vtnrsc.subnet.SubnetService;
 
  37 import com.google.common.collect.Sets;
 
  40  * Supports for creating a subnet.
 
  42 @Command(scope = "onos", name = "subnet-create", description = "Supports for creating a subnet")
 
  43 public class SubnetCreateCommand extends AbstractShellCommand {
 
  45     @Argument(index = 0, name = "id", description = "Subnet Id", required = true,
 
  49     @Argument(index = 1, name = "subnetName", description = "Subnet String name", required = true,
 
  51     String subnetName = null;
 
  53     @Argument(index = 2, name = "networkId", description = "Subnet Network Id", required = true,
 
  55     String networkId = null;
 
  57     @Argument(index = 3, name = "tenantId", description = "Subnet Tenant Id", required = true,
 
  59     String tenantId = null;
 
  61     @Option(name = "-i", aliases = "--ipVersion", description = "Subnet Version ipVersion",
 
  62             required = false, multiValued = false)
 
  63     Version ipVersion = null;
 
  65     @Option(name = "-c", aliases = "--cidr", description = "Subnet IpPrefix cidr",
 
  66             required = false, multiValued = false)
 
  67     String cidr = "0.0.0.0/0";
 
  69     @Option(name = "-g", aliases = "--gatewayIp", description = "Subnet IpAddress gatewayIp",
 
  70             required = false, multiValued = false)
 
  71     String gatewayIp = "0.0.0.0";
 
  73     @Option(name = "-d", aliases = "--dhcpEnabled", description = "Subnet boolean dhcpEnabled",
 
  74             required = false, multiValued = false)
 
  75     boolean dhcpEnabled = false;
 
  77     @Option(name = "-s", aliases = "--shared", description = "Subnet boolean shared",
 
  78             required = false, multiValued = false)
 
  79     boolean shared = false;
 
  81     @Option(name = "-m", aliases = "--ipV6AddressMode",
 
  82             description = "Subnet Mode ipV6AddressMode", required = false, multiValued = false)
 
  83     String ipV6AddressMode = null;
 
  85     @Option(name = "-r", aliases = "--ipV6RaMode", description = "Subnet Mode ipV6RaMode",
 
  86             required = false, multiValued = false)
 
  87     String ipV6RaMode = null;
 
  89     @Option(name = "-h", aliases = "--hostRoutes", description = "Subnet jsonnode hostRoutes",
 
  90             required = false, multiValued = false)
 
  91     Set<HostRoute> hostRoutes = Sets.newHashSet();
 
  93     @Option(name = "-a", aliases = "--allocationPools",
 
  94             description = "Subnet jsonnode allocationPools", required = false, multiValued = false)
 
  95     Set<AllocationPool> allocationPools = Sets.newHashSet();
 
  98     protected void execute() {
 
  99         SubnetService service = get(SubnetService.class);
 
 100         if (id == null || networkId == null || tenantId == null) {
 
 101             print(null, "id,networkId,tenantId can not be null");
 
 104         Subnet subnet = new DefaultSubnet(SubnetId.subnetId(id), subnetName,
 
 105                                           TenantNetworkId.networkId(networkId),
 
 106                                           TenantId.tenantId(tenantId), ipVersion,
 
 107                                           cidr == null ? null : IpPrefix.valueOf(cidr),
 
 108                                           gatewayIp == null ? null : IpAddress.valueOf(gatewayIp),
 
 109                                           dhcpEnabled, shared, hostRoutes,
 
 110                                           ipV6AddressMode == null ? null : Mode.valueOf(ipV6AddressMode),
 
 111                                           ipV6RaMode == null ? null : Mode.valueOf(ipV6RaMode),
 
 114         Set<Subnet> subnetsSet = Sets.newHashSet(subnet);
 
 115         service.createSubnets(subnetsSet);