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