andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
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; |
Palash Kala | 67de597 | 2018-03-22 14:10:50 +0900 | [diff] [blame] | 21 | import org.apache.karaf.shell.commands.Option; |
Akihiro Yamanouchi | 54f28e2 | 2016-06-24 13:25:21 +0900 | [diff] [blame] | 22 | import org.onlab.packet.IpAddress; |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 23 | import org.onosproject.cli.AbstractShellCommand; |
Akihiro Yamanouchi | 54f28e2 | 2016-06-24 13:25:21 +0900 | [diff] [blame] | 24 | import org.onosproject.net.Annotations; |
| 25 | import org.onosproject.net.DefaultAnnotations; |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 26 | import org.onosproject.net.DeviceId; |
| 27 | import org.onosproject.net.behaviour.ControllerConfig; |
| 28 | import org.onosproject.net.behaviour.ControllerInfo; |
| 29 | import org.onosproject.net.driver.DriverHandler; |
| 30 | import org.onosproject.net.driver.DriverService; |
| 31 | |
| 32 | import java.util.ArrayList; |
| 33 | import java.util.Arrays; |
| 34 | import java.util.List; |
| 35 | |
| 36 | /** |
| 37 | * Sets role of the controller node for the given infrastructure device. |
| 38 | */ |
| 39 | @Command(scope = "onos", name = "device-setcontrollers", |
| 40 | description = "sets the list of controllers for the given infrastructure device") |
| 41 | public class DeviceSetControllersCommand extends AbstractShellCommand { |
| 42 | |
| 43 | @Argument(index = 0, name = "uri", description = "Device ID", |
| 44 | required = true, multiValued = false) |
| 45 | String uri = null; |
| 46 | |
| 47 | @Argument(index = 1, name = "controllersListStrings", description = "list of " + |
| 48 | "controllers to set for the specified device", |
Palash Kala | 67de597 | 2018-03-22 14:10:50 +0900 | [diff] [blame] | 49 | required = false, multiValued = true) |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 50 | String[] controllersListStrings = null; |
| 51 | |
Palash Kala | 67de597 | 2018-03-22 14:10:50 +0900 | [diff] [blame] | 52 | @Option(name = "--remove", |
| 53 | description = "Remove specified controllers configuration") |
| 54 | private boolean removeCont = false; |
| 55 | |
| 56 | @Option(name = "--remove-all", |
| 57 | description = "Remove all controllers configuration, " + |
| 58 | "does not require any input") |
| 59 | private boolean removeAll = false; |
| 60 | |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 61 | private DeviceId deviceId; |
Palash Kala | 67de597 | 2018-03-22 14:10:50 +0900 | [diff] [blame] | 62 | private List<ControllerInfo> controllers = new ArrayList<>(); |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 63 | |
| 64 | @Override |
| 65 | protected void execute() { |
| 66 | |
| 67 | Arrays.asList(controllersListStrings).forEach( |
Palash Kala | 67de597 | 2018-03-22 14:10:50 +0900 | [diff] [blame] | 68 | cInfoString -> controllers.add(parseCInfoString(cInfoString))); |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 69 | DriverService service = get(DriverService.class); |
| 70 | deviceId = DeviceId.deviceId(uri); |
| 71 | DriverHandler h = service.createHandler(deviceId); |
| 72 | ControllerConfig config = h.behaviour(ControllerConfig.class); |
| 73 | print("before:"); |
| 74 | config.getControllers().forEach(c -> print(c.target())); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 75 | try { |
Palash Kala | 67de597 | 2018-03-22 14:10:50 +0900 | [diff] [blame] | 76 | if (removeAll) { |
| 77 | if (!controllers.isEmpty()) { |
| 78 | print("Controllers list should be empty to remove all controllers"); |
| 79 | } else { |
| 80 | List<ControllerInfo> controllersToRemove = config.getControllers(); |
| 81 | controllersToRemove.forEach(c -> print("Will remove " + c.target())); |
| 82 | config.removeControllers(controllersToRemove); |
| 83 | } |
| 84 | } else { |
| 85 | if (controllers.isEmpty()) { |
| 86 | print("Controllers list is empty, cannot set/remove empty controllers"); |
| 87 | } else { |
| 88 | if (removeCont) { |
| 89 | print("Will remove specified controllers"); |
| 90 | config.removeControllers(controllers); |
| 91 | } else { |
| 92 | print("Will add specified controllers"); |
| 93 | config.setControllers(controllers); |
| 94 | } |
| 95 | } |
| 96 | } |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 97 | } catch (NullPointerException e) { |
| 98 | print("No Device with requested parameters {} ", uri); |
| 99 | } |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 100 | print("after:"); |
| 101 | config.getControllers().forEach(c -> print(c.target())); |
| 102 | print("size %d", config.getControllers().size()); |
| 103 | } |
| 104 | |
Akihiro Yamanouchi | 54f28e2 | 2016-06-24 13:25:21 +0900 | [diff] [blame] | 105 | |
| 106 | private ControllerInfo parseCInfoString(String cInfoString) { |
| 107 | Annotations annotation; |
| 108 | |
| 109 | String[] config = cInfoString.split(","); |
| 110 | if (config.length == 2) { |
| 111 | String[] pair = config[1].split("="); |
| 112 | |
| 113 | if (pair.length == 2) { |
| 114 | annotation = DefaultAnnotations.builder() |
| 115 | .set(pair[0], pair[1]).build(); |
| 116 | } else { |
| 117 | print("Wrong format {}", config[1]); |
| 118 | return null; |
| 119 | } |
| 120 | |
| 121 | String[] data = config[0].split(":"); |
| 122 | String type = data[0]; |
| 123 | IpAddress ip = IpAddress.valueOf(data[1]); |
| 124 | int port = Integer.parseInt(data[2]); |
| 125 | |
| 126 | return new ControllerInfo(ip, port, type, annotation); |
| 127 | } else { |
| 128 | print(config[0]); |
| 129 | return new ControllerInfo(config[0]); |
| 130 | } |
| 131 | } |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 132 | } |