Integrate the new yang model with Network Topology augmentation
[unimgr.git] / cli / src / main / java / org / opendaylight / unimgr / cli / UniAddShellCommand.java
1 /*
2  * Copyright (c) 2015 CableLabs 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 package org.opendaylight.unimgr.cli;
9
10 import java.math.BigInteger;
11
12 import org.apache.karaf.shell.commands.Command;
13 import org.apache.karaf.shell.commands.Option;
14 import org.apache.karaf.shell.console.OsgiCommandSupport;
15 import org.opendaylight.unimgr.api.IUnimgrConsoleProvider;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.Uni;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.UniAugmentationBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed100MBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed10GBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed10MBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed1GBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.uni.Speed;
24
25 @Command(name = "add",
26          scope = "uni",
27          description = "Adds an uni to the controller.")
28 public class UniAddShellCommand extends OsgiCommandSupport {
29
30     protected IUnimgrConsoleProvider provider;
31
32     @Option(name = "-id",
33             aliases = { "--uni-id" },
34             description = "The id.\n-id / --id <id>",
35             required = true,
36             multiValued = false)
37     private String uniId = "any";
38
39     @Option(name = "-pm",
40             aliases = { "--physical-medium" },
41             description = "The physical medium.\n-pm / --physical-medium <physical-medium>",
42             required = false,
43             multiValued = false)
44     private String physicalMedium = "any";
45
46     @Option(name = "-ma",
47             aliases = { "--mac-address" },
48             description = "The mac address.\n-ma / --mac-address <mac-address>",
49             required = false,
50             multiValued = false)
51     private String macAddress = "any";
52
53     @Option(name = "-m",
54             aliases = { "--mode" },
55             description = "The mode.\n-m / --mode <mode>",
56             required = false,
57             multiValued = false)
58     private String mode = "any";
59
60     @Option(name = "-ml",
61             aliases = { "--mac-layer" },
62             description = "The mac layer.\n-ml / --mac-layer <mac-layer",
63             required = false,
64             multiValued = false)
65     private String macLayer = "any";
66
67     @Option(name = "-t",
68             aliases = { "--type" },
69             description = "The type.\n-t / --type <type>",
70             required = false,
71             multiValued = false)
72     private String type = "any";
73
74     @Option(name = "-ms",
75             aliases = { "--mtu-size" },
76             description = "The mtu size.\n-ms / --mtu-size <mtu-size>",
77             required = false,
78             multiValued = false)
79     private String mtuSize;
80
81     @Option(name = "-s",
82             aliases = { "--speed" },
83             description = "Spped.\n-s / --speed 10M/100M/1G/10G",
84             required = true,
85             multiValued = true)
86     private String speed = "any";
87
88     public UniAddShellCommand(IUnimgrConsoleProvider provider) {
89         this.provider = provider;
90     }
91
92     private Object getSpeed() {
93
94         System.out.println(speed);
95
96         Object speedObject = null;
97         if (speed.equals("10M")) {
98             speedObject = new Speed10MBuilder().build();
99         }
100         if (speed.equals("100M")) {
101             speedObject = new Speed100MBuilder().build();
102         }
103         if (speed.equals("1G")) {
104             speedObject = new Speed1GBuilder().build();
105         }
106         if (speed.equals("10G")) {
107             speedObject = new Speed10GBuilder().build();
108         }
109         System.out.println(speedObject);
110         return speedObject;
111     }
112
113     @Override
114     protected Object doExecute() throws Exception {
115         Uni uni = new UniAugmentationBuilder()
116                         .setMacAddress(new MacAddress(macAddress))
117                         .setMacLayer(macLayer)
118                         .setMode(mode)
119                         .setMtuSize(BigInteger.valueOf(Long.valueOf(mtuSize)))
120                         .setPhysicalMedium(physicalMedium)
121                         .setSpeed((Speed) getSpeed())
122                         .setType(type)
123                         .build();
124
125         if (provider.addUni(uni)) {
126             return String.format("Uni created {}", uni.getIpAddress().getIpv4Address());
127         } else {
128             return new String("Error creating new uni");
129         }
130     }
131 }