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 | |
Andrea Campanella | af1fa39 | 2018-06-26 11:16:51 +0200 | [diff] [blame] | 67 | if (controllersListStrings == null && !removeCont && !removeAll) { |
| 68 | print("No controller are given, skipping."); |
| 69 | return; |
| 70 | } |
| 71 | if (controllersListStrings != null) { |
| 72 | Arrays.asList(controllersListStrings).forEach( |
| 73 | cInfoString -> { |
| 74 | ControllerInfo controllerInfo = parseCInfoString(cInfoString); |
| 75 | if (controllerInfo != null) { |
| 76 | controllers.add(controllerInfo); |
| 77 | } |
| 78 | }); |
| 79 | } |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 80 | DriverService service = get(DriverService.class); |
| 81 | deviceId = DeviceId.deviceId(uri); |
| 82 | DriverHandler h = service.createHandler(deviceId); |
| 83 | ControllerConfig config = h.behaviour(ControllerConfig.class); |
| 84 | print("before:"); |
| 85 | config.getControllers().forEach(c -> print(c.target())); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 86 | try { |
Palash Kala | 67de597 | 2018-03-22 14:10:50 +0900 | [diff] [blame] | 87 | if (removeAll) { |
| 88 | if (!controllers.isEmpty()) { |
| 89 | print("Controllers list should be empty to remove all controllers"); |
| 90 | } else { |
| 91 | List<ControllerInfo> controllersToRemove = config.getControllers(); |
| 92 | controllersToRemove.forEach(c -> print("Will remove " + c.target())); |
| 93 | config.removeControllers(controllersToRemove); |
| 94 | } |
| 95 | } else { |
| 96 | if (controllers.isEmpty()) { |
| 97 | print("Controllers list is empty, cannot set/remove empty controllers"); |
| 98 | } else { |
| 99 | if (removeCont) { |
| 100 | print("Will remove specified controllers"); |
| 101 | config.removeControllers(controllers); |
| 102 | } else { |
| 103 | print("Will add specified controllers"); |
| 104 | config.setControllers(controllers); |
| 105 | } |
| 106 | } |
| 107 | } |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 108 | } catch (NullPointerException e) { |
| 109 | print("No Device with requested parameters {} ", uri); |
| 110 | } |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 111 | print("after:"); |
| 112 | config.getControllers().forEach(c -> print(c.target())); |
| 113 | print("size %d", config.getControllers().size()); |
| 114 | } |
| 115 | |
Akihiro Yamanouchi | 54f28e2 | 2016-06-24 13:25:21 +0900 | [diff] [blame] | 116 | |
| 117 | private ControllerInfo parseCInfoString(String cInfoString) { |
| 118 | Annotations annotation; |
| 119 | |
| 120 | String[] config = cInfoString.split(","); |
| 121 | if (config.length == 2) { |
| 122 | String[] pair = config[1].split("="); |
| 123 | |
| 124 | if (pair.length == 2) { |
| 125 | annotation = DefaultAnnotations.builder() |
| 126 | .set(pair[0], pair[1]).build(); |
| 127 | } else { |
| 128 | print("Wrong format {}", config[1]); |
| 129 | return null; |
| 130 | } |
| 131 | |
Andrea Campanella | af1fa39 | 2018-06-26 11:16:51 +0200 | [diff] [blame] | 132 | return getControllerInfo(annotation, config[0]); |
Akihiro Yamanouchi | 54f28e2 | 2016-06-24 13:25:21 +0900 | [diff] [blame] | 133 | } else { |
Andrea Campanella | af1fa39 | 2018-06-26 11:16:51 +0200 | [diff] [blame] | 134 | return getControllerInfo(null, config[0]); |
Akihiro Yamanouchi | 54f28e2 | 2016-06-24 13:25:21 +0900 | [diff] [blame] | 135 | } |
| 136 | } |
Andrea Campanella | af1fa39 | 2018-06-26 11:16:51 +0200 | [diff] [blame] | 137 | |
| 138 | private ControllerInfo getControllerInfo(Annotations annotation, String s) { |
| 139 | String[] data = s.split(":"); |
| 140 | if (data.length != 3) { |
| 141 | print("Wrong format of the controller %s, should be in the format <protocol>:<ip>:<port>", s); |
| 142 | return null; |
| 143 | } |
| 144 | String type = data[0]; |
| 145 | IpAddress ip = IpAddress.valueOf(data[1]); |
| 146 | int port = Integer.parseInt(data[2]); |
| 147 | if (annotation != null) { |
| 148 | return new ControllerInfo(ip, port, type, annotation); |
| 149 | } |
| 150 | return new ControllerInfo(ip, port, type); |
| 151 | } |
andrea | ed976a4 | 2015-10-05 14:38:25 -0700 | [diff] [blame] | 152 | } |