56236408908ee71a8ff4934b58651fb0f9bdeef1
[onosfw.git] /
1 /*
2  * Copyright 2015 Open Networking Laboratory
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.onosproject.vtnrsc.cli.subnet;
17
18 import java.util.Set;
19
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;
36
37 import com.google.common.collect.Sets;
38
39 /**
40  * Supports for creating a subnet.
41  */
42 @Command(scope = "onos", name = "subnet-create", description = "Supports for creating a subnet")
43 public class SubnetCreateCommand extends AbstractShellCommand {
44
45     @Argument(index = 0, name = "id", description = "Subnet Id", required = true,
46             multiValued = false)
47     String id = null;
48
49     @Argument(index = 1, name = "subnetName", description = "Subnet String name", required = true,
50             multiValued = false)
51     String subnetName = null;
52
53     @Argument(index = 2, name = "networkId", description = "Subnet Network Id", required = true,
54             multiValued = false)
55     String networkId = null;
56
57     @Argument(index = 3, name = "tenantId", description = "Subnet Tenant Id", required = true,
58             multiValued = false)
59     String tenantId = null;
60
61     @Option(name = "-i", aliases = "--ipVersion", description = "Subnet Version ipVersion",
62             required = false, multiValued = false)
63     Version ipVersion = null;
64
65     @Option(name = "-c", aliases = "--cidr", description = "Subnet IpPrefix cidr",
66             required = false, multiValued = false)
67     String cidr = "0.0.0.0/0";
68
69     @Option(name = "-g", aliases = "--gatewayIp", description = "Subnet IpAddress gatewayIp",
70             required = false, multiValued = false)
71     String gatewayIp = "0.0.0.0";
72
73     @Option(name = "-d", aliases = "--dhcpEnabled", description = "Subnet boolean dhcpEnabled",
74             required = false, multiValued = false)
75     boolean dhcpEnabled = false;
76
77     @Option(name = "-s", aliases = "--shared", description = "Subnet boolean shared",
78             required = false, multiValued = false)
79     boolean shared = false;
80
81     @Option(name = "-m", aliases = "--ipV6AddressMode",
82             description = "Subnet Mode ipV6AddressMode", required = false, multiValued = false)
83     String ipV6AddressMode = null;
84
85     @Option(name = "-r", aliases = "--ipV6RaMode", description = "Subnet Mode ipV6RaMode",
86             required = false, multiValued = false)
87     String ipV6RaMode = null;
88
89     @Option(name = "-h", aliases = "--hostRoutes", description = "Subnet jsonnode hostRoutes",
90             required = false, multiValued = false)
91     Set<HostRoute> hostRoutes = Sets.newHashSet();
92
93     @Option(name = "-a", aliases = "--allocationPools",
94             description = "Subnet jsonnode allocationPools", required = false, multiValued = false)
95     Set<AllocationPool> allocationPools = Sets.newHashSet();
96
97     @Override
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");
102             return;
103         }
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),
112                                           allocationPools);
113
114         Set<Subnet> subnetsSet = Sets.newHashSet(subnet);
115         service.createSubnets(subnetsSet);
116     }
117
118 }