aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/onos/apps/vtn/vtnrsc/src/main/java/org/onosproject/vtnrsc/cli/subnet/SubnetCreateCommand.java
blob: de8cfe5360acbdfde7c37b3749902ec85a46e807 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
 * Copyright 2015 Open Networking Laboratory
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.onosproject.vtnrsc.cli.subnet;

import java.util.Set;

import org.apache.karaf.shell.commands.Argument;
import org.apache.karaf.shell.commands.Command;
import org.apache.karaf.shell.commands.Option;
import org.onlab.packet.IpAddress;
import org.onlab.packet.IpAddress.Version;
import org.onlab.packet.IpPrefix;
import org.onosproject.cli.AbstractShellCommand;
import org.onosproject.vtnrsc.AllocationPool;
import org.onosproject.vtnrsc.DefaultSubnet;
import org.onosproject.vtnrsc.HostRoute;
import org.onosproject.vtnrsc.Subnet;
import org.onosproject.vtnrsc.Subnet.Mode;
import org.onosproject.vtnrsc.SubnetId;
import org.onosproject.vtnrsc.TenantId;
import org.onosproject.vtnrsc.TenantNetworkId;
import org.onosproject.vtnrsc.subnet.SubnetService;

import com.google.common.collect.Sets;

/**
 * Supports for creating a subnet.
 */
@Command(scope = "onos", name = "subnet-create", description = "Supports for creating a subnet")
public class SubnetCreateCommand extends AbstractShellCommand {

    @Argument(index = 0, name = "id", description = "Subnet Id", required = true,
            multiValued = false)
    String id = null;

    @Argument(index = 1, name = "subnetName", description = "Subnet String name", required = true,
            multiValued = false)
    String subnetName = null;

    @Argument(index = 2, name = "networkId", description = "Subnet Network Id", required = true,
            multiValued = false)
    String networkId = null;

    @Argument(index = 3, name = "tenantId", description = "Subnet Tenant Id", required = true,
            multiValued = false)
    String tenantId = null;

    @Option(name = "-i", aliases = "--ipVersion", description = "Subnet Version ipVersion",
            required = false, multiValued = false)
    Version ipVersion = null;

    @Option(name = "-c", aliases = "--cidr", description = "Subnet IpPrefix cidr",
            required = false, multiValued = false)
    String cidr = "0.0.0.0/0";

    @Option(name = "-g", aliases = "--gatewayIp", description = "Subnet IpAddress gatewayIp",
            required = false, multiValued = false)
    String gatewayIp = "0.0.0.0";

    @Option(name = "-d", aliases = "--dhcpEnabled", description = "Subnet boolean dhcpEnabled",
            required = false, multiValued = false)
    boolean dhcpEnabled = false;

    @Option(name = "-s", aliases = "--shared", description = "Subnet boolean shared",
            required = false, multiValued = false)
    boolean shared = false;

    @Option(name = "-m", aliases = "--ipV6AddressMode",
            description = "Subnet Mode ipV6AddressMode", required = false, multiValued = false)
    String ipV6AddressMode = null;

    @Option(name = "-r", aliases = "--ipV6RaMode", description = "Subnet Mode ipV6RaMode",
            required = false, multiValued = false)
    String ipV6RaMode = null;

    @Option(name = "-h", aliases = "--hostRoutes", description = "Subnet jsonnode hostRoutes",
            required = false, multiValued = false)
    Set<HostRoute> hostRoutes = Sets.newHashSet();

    @Option(name = "-a", aliases = "--allocationPools",
            description = "Subnet jsonnode allocationPools", required = false, multiValued = false)
    Set<AllocationPool> allocationPools = Sets.newHashSet();

    @Override
    protected void execute() {
        SubnetService service = get(SubnetService.class);
        if (id == null || networkId == null || tenantId == null) {
            print("id,networkId,tenantId can not be null");
            return;
        }
        Subnet subnet = new DefaultSubnet(SubnetId.subnetId(id), subnetName,
                                          TenantNetworkId.networkId(networkId),
                                          TenantId.tenantId(tenantId), ipVersion,
                                          cidr == null ? null : IpPrefix.valueOf(cidr),
                                          gatewayIp == null ? null : IpAddress.valueOf(gatewayIp),
                                          dhcpEnabled, shared, hostRoutes,
                                          ipV6AddressMode == null ? null : Mode.valueOf(ipV6AddressMode),
                                          ipV6RaMode == null ? null : Mode.valueOf(ipV6RaMode),
                                          allocationPools);

        Set<Subnet> subnetsSet = Sets.newHashSet(subnet);
        service.createSubnets(subnetsSet);
    }

}