8de1a77513f14d9096b9c946edb940fbdaa74d2a
[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.inet.types.rev100924.IpAddress;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.UniAugmentation;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.UniAugmentationBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed100MBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed10GBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed10MBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed1GBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.uni.Speed;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 @Command(name = "uni-add",
29          scope = "uni",
30          description = "Adds an uni to the controller.")
31 public class UniAddShellCommand extends OsgiCommandSupport {
32
33     private static final Logger LOG = LoggerFactory.getLogger(UniAddShellCommand.class);
34     protected IUnimgrConsoleProvider provider;
35
36     @Option(name = "-pm",
37             aliases = { "--physical-medium" },
38             description = "The physical medium.\n-pm / --physical-medium <physical-medium>",
39             required = false,
40             multiValued = false)
41     private String physicalMedium = "UNI TypeFull Duplex 2 Physical Interface";
42
43     @Option(name = "-ma",
44             aliases = { "--mac-address" },
45             description = "The mac address.\n-ma / --mac-address <mac-address>",
46             required = true,
47             multiValued = false)
48     private String macAddress = "any";
49
50     @Option(name = "-m",
51             aliases = { "--mode" },
52             description = "The mode.\n-m / --mode <mode>",
53             required = false,
54             multiValued = false)
55     private String mode = "Full Duplex";
56
57     @Option(name = "-ml",
58             aliases = { "--mac-layer" },
59             description = "The mac layer.\n-ml / --mac-layer <mac-layer",
60             required = false,
61             multiValued = false)
62     private String macLayer = "IEEE 802.3-2005";
63
64     @Option(name = "-t",
65             aliases = { "--type" },
66             description = "The type.\n-t / --type <type>",
67             required = false,
68             multiValued = false)
69     private String type = "";
70
71     @Option(name = "-ms",
72             aliases = { "--mtu-size" },
73             description = "The mtu size.\n-ms / --mtu-size <mtu-size>",
74             required = false,
75             multiValued = false)
76     private String mtuSize = "0";
77
78     @Option(name = "-s",
79             aliases = { "--speed" },
80             description = "Spped.\n-s / --speed 10M/100M/1G/10G",
81             required = false,
82             multiValued = true)
83     private String speed = "";
84
85     @Option(name = "-ip",
86             aliases = { "--ipAddress" },
87             description = "IpAddress of the Uni",
88             required = true,
89             multiValued = false)
90     private String ipAddress = "any";
91
92     public UniAddShellCommand(IUnimgrConsoleProvider provider) {
93         this.provider = provider;
94     }
95
96     private Object getSpeed() {
97         Object speedObject = null;
98         if (speed.equals("10M")) {
99             speedObject = new Speed10MBuilder().build();
100         }
101         else if (speed.equals("100M")) {
102             speedObject = new Speed100MBuilder().build();
103         }
104         else if (speed.equals("1G")) {
105             speedObject = new Speed1GBuilder().build();
106         }
107         else if (speed.equals("10G")) {
108             speedObject = new Speed10GBuilder().build();
109         }
110         return speedObject;
111     }
112
113     @Override
114     protected Object doExecute() throws Exception {
115         UniAugmentation 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                         .setIpAddress(new IpAddress(ipAddress.toCharArray()))
124                         .build();
125
126         if (provider.addUni(uni)) {
127             return new String("Uni with ip " +ipAddress+" created");
128         } else {
129             return new String("Error creating new Uni");
130         }
131     }
132 }