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