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; |
Akihiro Yamanouchi | 54f28e2 | 2016-06-24 13:25:21 +0900 | [diff] [blame] | 21 | import org.onlab.packet.IpAddress; |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 22 | import org.onosproject.cli.AbstractShellCommand; |
Akihiro Yamanouchi | 54f28e2 | 2016-06-24 13:25:21 +0900 | [diff] [blame] | 23 | import org.onosproject.net.Annotations; |
| 24 | import org.onosproject.net.DefaultAnnotations; |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 25 | import org.onosproject.net.DeviceId; |
| 26 | import org.onosproject.net.behaviour.ControllerConfig; |
| 27 | import org.onosproject.net.behaviour.ControllerInfo; |
| 28 | import org.onosproject.net.driver.DriverHandler; |
| 29 | import org.onosproject.net.driver.DriverService; |
| 30 | |
| 31 | import java.util.ArrayList; |
| 32 | import java.util.Arrays; |
| 33 | import 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") |
| 40 | public 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 Yamanouchi | 54f28e2 | 2016-06-24 13:25:21 +0900 | [diff] [blame] | 58 | cInfoString -> newControllers.add(parseCInfoString(cInfoString))); |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 59 | 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())); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 65 | try { |
| 66 | config.setControllers(newControllers); |
| 67 | } catch (NullPointerException e) { |
| 68 | print("No Device with requested parameters {} ", uri); |
| 69 | } |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 70 | print("after:"); |
| 71 | config.getControllers().forEach(c -> print(c.target())); |
| 72 | print("size %d", config.getControllers().size()); |
| 73 | } |
| 74 | |
Akihiro Yamanouchi | 54f28e2 | 2016-06-24 13:25:21 +0900 | [diff] [blame] | 75 | |
| 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 | } |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 102 | } |