andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 3 | * |
| 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 | |
| 17 | package org.onosproject.cli.net; |
| 18 | |
| 19 | import org.apache.karaf.shell.commands.Argument; |
| 20 | import org.apache.karaf.shell.commands.Command; |
| 21 | import org.onosproject.cli.AbstractShellCommand; |
| 22 | import org.onosproject.net.DeviceId; |
| 23 | import org.onosproject.net.behaviour.ControllerConfig; |
| 24 | import org.onosproject.net.behaviour.ControllerInfo; |
| 25 | import org.onosproject.net.driver.DriverHandler; |
| 26 | import org.onosproject.net.driver.DriverService; |
| 27 | |
| 28 | import java.util.ArrayList; |
| 29 | import java.util.Arrays; |
| 30 | import 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") |
| 37 | public 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())); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 62 | try { |
| 63 | config.setControllers(newControllers); |
| 64 | } catch (NullPointerException e) { |
| 65 | print("No Device with requested parameters {} ", uri); |
| 66 | } |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 67 | print("after:"); |
| 68 | config.getControllers().forEach(c -> print(c.target())); |
| 69 | print("size %d", config.getControllers().size()); |
| 70 | } |
| 71 | |
| 72 | } |