blob: 5d0707dcd4965957f511bf560c83da059e6f4a4d [file] [log] [blame]
andreaed976a42015-10-05 14:38:25 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
andreaed976a42015-10-05 14:38:25 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.cli.net;
18
19import org.apache.karaf.shell.commands.Argument;
20import org.apache.karaf.shell.commands.Command;
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +090021import org.onlab.packet.IpAddress;
andreaed976a42015-10-05 14:38:25 -070022import org.onosproject.cli.AbstractShellCommand;
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +090023import org.onosproject.net.Annotations;
24import org.onosproject.net.DefaultAnnotations;
andreaed976a42015-10-05 14:38:25 -070025import org.onosproject.net.DeviceId;
26import org.onosproject.net.behaviour.ControllerConfig;
27import org.onosproject.net.behaviour.ControllerInfo;
28import org.onosproject.net.driver.DriverHandler;
29import org.onosproject.net.driver.DriverService;
30
31import java.util.ArrayList;
32import java.util.Arrays;
33import java.util.List;
34
35/**
36 * Sets role of the controller node for the given infrastructure device.
37 */
38@Command(scope = "onos", name = "device-setcontrollers",
39 description = "sets the list of controllers for the given infrastructure device")
40public class DeviceSetControllersCommand extends AbstractShellCommand {
41
42 @Argument(index = 0, name = "uri", description = "Device ID",
43 required = true, multiValued = false)
44 String uri = null;
45
46 @Argument(index = 1, name = "controllersListStrings", description = "list of " +
47 "controllers to set for the specified device",
48 required = true, multiValued = true)
49 String[] controllersListStrings = null;
50
51 private DeviceId deviceId;
52 private List<ControllerInfo> newControllers = new ArrayList<>();
53
54 @Override
55 protected void execute() {
56
57 Arrays.asList(controllersListStrings).forEach(
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +090058 cInfoString -> newControllers.add(parseCInfoString(cInfoString)));
andreaed976a42015-10-05 14:38:25 -070059 DriverService service = get(DriverService.class);
60 deviceId = DeviceId.deviceId(uri);
61 DriverHandler h = service.createHandler(deviceId);
62 ControllerConfig config = h.behaviour(ControllerConfig.class);
63 print("before:");
64 config.getControllers().forEach(c -> print(c.target()));
andreaeb70a942015-10-16 21:34:46 -070065 try {
66 config.setControllers(newControllers);
67 } catch (NullPointerException e) {
68 print("No Device with requested parameters {} ", uri);
69 }
andreaed976a42015-10-05 14:38:25 -070070 print("after:");
71 config.getControllers().forEach(c -> print(c.target()));
72 print("size %d", config.getControllers().size());
73 }
74
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +090075
76 private ControllerInfo parseCInfoString(String cInfoString) {
77 Annotations annotation;
78
79 String[] config = cInfoString.split(",");
80 if (config.length == 2) {
81 String[] pair = config[1].split("=");
82
83 if (pair.length == 2) {
84 annotation = DefaultAnnotations.builder()
85 .set(pair[0], pair[1]).build();
86 } else {
87 print("Wrong format {}", config[1]);
88 return null;
89 }
90
91 String[] data = config[0].split(":");
92 String type = data[0];
93 IpAddress ip = IpAddress.valueOf(data[1]);
94 int port = Integer.parseInt(data[2]);
95
96 return new ControllerInfo(ip, port, type, annotation);
97 } else {
98 print(config[0]);
99 return new ControllerInfo(config[0]);
100 }
101 }
andreaed976a42015-10-05 14:38:25 -0700102}