4618080ef3399b0095a60b0bf74a74d3374b156f
[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
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
26 @Command(name = "uni-update",
27 scope = "uni",
28 description = "Updates an uni to the controller.")
29 public class UniUpdateShellCommand extends OsgiCommandSupport{
30     protected IUnimgrConsoleProvider provider;
31
32     @Option(name = "-pm",
33             aliases = { "--physical-medium" },
34             description = "The physical medium.\n-pm / --physical-medium <physical-medium>",
35             required = false,
36             multiValued = false)
37     private final String physicalMedium = "UNI TypeFull Duplex 2 Physical Interface";
38
39     @Option(name = "-ma",
40             aliases = { "--mac-address" },
41             description = "The mac address.\n-ma / --mac-address <mac-address>",
42             required = true,
43             multiValued = false)
44     private final String macAddress = "any";
45
46     @Option(name = "-m",
47             aliases = { "--mode" },
48             description = "The mode.\n-m / --mode <mode>",
49             required = false,
50             multiValued = false)
51     private final String mode = "Full Duplex";
52
53     @Option(name = "-ml",
54             aliases = { "--mac-layer" },
55             description = "The mac layer.\n-ml / --mac-layer <mac-layer",
56             required = false,
57             multiValued = false)
58     private final String macLayer = "IEEE 802.3-2005";
59
60     @Option(name = "-t",
61             aliases = { "--type" },
62             description = "The type.\n-t / --type <type>",
63             required = false,
64             multiValued = false)
65     private final String type = "";
66
67     @Option(name = "-ms",
68             aliases = { "--mtu-size" },
69             description = "The mtu size.\n-ms / --mtu-size <mtu-size>",
70             required = false,
71             multiValued = false)
72     private final String mtuSize = "0";
73
74     @Option(name = "-s",
75             aliases = { "--speed" },
76             description = "Spped.\n-s / --speed 10M/100M/1G/10G",
77             required = false,
78             multiValued = true)
79     private final String speed = "";
80
81     @Option(name = "-ip",
82             aliases = { "--ipAddress" },
83             description = "IpAddress of the Uni",
84             required = true,
85             multiValued = false)
86     private final String ipAddress = "any";
87
88     public UniUpdateShellCommand(IUnimgrConsoleProvider provider) {
89         this.provider = provider;
90     }
91
92     private Object getSpeed() {
93         Object speedObject = null;
94         if (speed.equals("10M")) {
95             speedObject = new Speed10MBuilder().build();
96         }
97         else if (speed.equals("100M")) {
98             speedObject = new Speed100MBuilder().build();
99         }
100         else if (speed.equals("1G")) {
101             speedObject = new Speed1GBuilder().build();
102         }
103         else if (speed.equals("10G")) {
104             speedObject = new Speed10GBuilder().build();
105         }
106         return speedObject;
107     }
108
109     @Override
110     protected Object doExecute() throws Exception {
111         final UniAugmentation uniAug = new UniAugmentationBuilder()
112                 .setMacAddress(new MacAddress(macAddress))
113                 .setMacLayer(macLayer)
114                 .setMode(mode)
115                 .setMtuSize(BigInteger.valueOf(Long.valueOf(mtuSize)))
116                 .setPhysicalMedium(physicalMedium)
117                 .setSpeed((Speed) getSpeed())
118                 .setType(type)
119                 .setIpAddress(new IpAddress(ipAddress.toCharArray()))
120                 .build();
121         if (provider.updateUni(uniAug)) {
122             return new String("Uni with ip " +ipAddress+" updated");
123         } else {
124             return new String("Error updating new Uni");
125         }
126     }
127 }