blob: 556dcc9979dbb19899b001a4e8bf2ebf957fb06d [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;
21import org.onosproject.cli.AbstractShellCommand;
22import org.onosproject.net.DeviceId;
23import org.onosproject.net.behaviour.ControllerConfig;
24import org.onosproject.net.behaviour.ControllerInfo;
25import org.onosproject.net.driver.DriverHandler;
26import org.onosproject.net.driver.DriverService;
27
28import java.util.ArrayList;
29import java.util.Arrays;
30import java.util.List;
31
32/**
33 * Sets role of the controller node for the given infrastructure device.
34 */
35@Command(scope = "onos", name = "device-setcontrollers",
36 description = "sets the list of controllers for the given infrastructure device")
37public class DeviceSetControllersCommand extends AbstractShellCommand {
38
39 @Argument(index = 0, name = "uri", description = "Device ID",
40 required = true, multiValued = false)
41 String uri = null;
42
43 @Argument(index = 1, name = "controllersListStrings", description = "list of " +
44 "controllers to set for the specified device",
45 required = true, multiValued = true)
46 String[] controllersListStrings = null;
47
48 private DeviceId deviceId;
49 private List<ControllerInfo> newControllers = new ArrayList<>();
50
51 @Override
52 protected void execute() {
53
54 Arrays.asList(controllersListStrings).forEach(
55 cInfoString -> newControllers.add(new ControllerInfo(cInfoString)));
56 DriverService service = get(DriverService.class);
57 deviceId = DeviceId.deviceId(uri);
58 DriverHandler h = service.createHandler(deviceId);
59 ControllerConfig config = h.behaviour(ControllerConfig.class);
60 print("before:");
61 config.getControllers().forEach(c -> print(c.target()));
andreaeb70a942015-10-16 21:34:46 -070062 try {
63 config.setControllers(newControllers);
64 } catch (NullPointerException e) {
65 print("No Device with requested parameters {} ", uri);
66 }
andreaed976a42015-10-05 14:38:25 -070067 print("after:");
68 config.getControllers().forEach(c -> print(c.target()));
69 print("size %d", config.getControllers().size());
70 }
71
72}