blob: af66ae0dcbebc0a8c754b5265afa666db716ec80 [file] [log] [blame]
andreaed976a42015-10-05 14:38:25 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
andreaed976a42015-10-05 14:38:25 -07003 *
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
17package org.onosproject.cli.net;
18
19import org.apache.karaf.shell.commands.Argument;
20import org.apache.karaf.shell.commands.Command;
Palash Kala67de5972018-03-22 14:10:50 +090021import org.apache.karaf.shell.commands.Option;
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +090022import org.onlab.packet.IpAddress;
andreaed976a42015-10-05 14:38:25 -070023import org.onosproject.cli.AbstractShellCommand;
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +090024import org.onosproject.net.Annotations;
25import org.onosproject.net.DefaultAnnotations;
andreaed976a42015-10-05 14:38:25 -070026import org.onosproject.net.DeviceId;
27import org.onosproject.net.behaviour.ControllerConfig;
28import org.onosproject.net.behaviour.ControllerInfo;
29import org.onosproject.net.driver.DriverHandler;
30import org.onosproject.net.driver.DriverService;
31
32import java.util.ArrayList;
33import java.util.Arrays;
34import 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")
41public 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 Kala67de5972018-03-22 14:10:50 +090049 required = false, multiValued = true)
andreaed976a42015-10-05 14:38:25 -070050 String[] controllersListStrings = null;
51
Palash Kala67de5972018-03-22 14:10:50 +090052 @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
andreaed976a42015-10-05 14:38:25 -070061 private DeviceId deviceId;
Palash Kala67de5972018-03-22 14:10:50 +090062 private List<ControllerInfo> controllers = new ArrayList<>();
andreaed976a42015-10-05 14:38:25 -070063
64 @Override
65 protected void execute() {
66
67 Arrays.asList(controllersListStrings).forEach(
Palash Kala67de5972018-03-22 14:10:50 +090068 cInfoString -> controllers.add(parseCInfoString(cInfoString)));
andreaed976a42015-10-05 14:38:25 -070069 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()));
andreaeb70a942015-10-16 21:34:46 -070075 try {
Palash Kala67de5972018-03-22 14:10:50 +090076 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 }
andreaeb70a942015-10-16 21:34:46 -070097 } catch (NullPointerException e) {
98 print("No Device with requested parameters {} ", uri);
99 }
andreaed976a42015-10-05 14:38:25 -0700100 print("after:");
101 config.getControllers().forEach(c -> print(c.target()));
102 print("size %d", config.getControllers().size());
103 }
104
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +0900105
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 }
andreaed976a42015-10-05 14:38:25 -0700132}