blob: e8d320145030b660d90e8efb897e02dd87b84171 [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
Andrea Campanellaaf1fa392018-06-26 11:16:51 +020067 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 }
andreaed976a42015-10-05 14:38:25 -070080 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()));
andreaeb70a942015-10-16 21:34:46 -070086 try {
Palash Kala67de5972018-03-22 14:10:50 +090087 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 }
andreaeb70a942015-10-16 21:34:46 -0700108 } catch (NullPointerException e) {
109 print("No Device with requested parameters {} ", uri);
110 }
andreaed976a42015-10-05 14:38:25 -0700111 print("after:");
112 config.getControllers().forEach(c -> print(c.target()));
113 print("size %d", config.getControllers().size());
114 }
115
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +0900116
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 Campanellaaf1fa392018-06-26 11:16:51 +0200132 return getControllerInfo(annotation, config[0]);
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +0900133 } else {
Andrea Campanellaaf1fa392018-06-26 11:16:51 +0200134 return getControllerInfo(null, config[0]);
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +0900135 }
136 }
Andrea Campanellaaf1fa392018-06-26 11:16:51 +0200137
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 }
andreaed976a42015-10-05 14:38:25 -0700152}