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