blob: dc20afa6965f3504ed8012c7ebfd5e0daaae730b [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
Ray Milkeyd84f89b2018-08-17 14:54:17 -070019import org.apache.karaf.shell.api.action.Argument;
20import org.apache.karaf.shell.api.action.Command;
Ray Milkey0068fd02018-10-11 15:45:39 -070021import org.apache.karaf.shell.api.action.Completion;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070022import org.apache.karaf.shell.api.action.lifecycle.Service;
23import org.apache.karaf.shell.api.action.Option;
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +090024import org.onlab.packet.IpAddress;
andreaed976a42015-10-05 14:38:25 -070025import org.onosproject.cli.AbstractShellCommand;
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +090026import org.onosproject.net.Annotations;
27import org.onosproject.net.DefaultAnnotations;
andreaed976a42015-10-05 14:38:25 -070028import org.onosproject.net.DeviceId;
29import org.onosproject.net.behaviour.ControllerConfig;
30import org.onosproject.net.behaviour.ControllerInfo;
31import org.onosproject.net.driver.DriverHandler;
32import org.onosproject.net.driver.DriverService;
33
34import java.util.ArrayList;
35import java.util.Arrays;
36import java.util.List;
37
38/**
39 * Sets role of the controller node for the given infrastructure device.
40 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070041@Service
andreaed976a42015-10-05 14:38:25 -070042@Command(scope = "onos", name = "device-setcontrollers",
43 description = "sets the list of controllers for the given infrastructure device")
44public class DeviceSetControllersCommand extends AbstractShellCommand {
45
46 @Argument(index = 0, name = "uri", description = "Device ID",
47 required = true, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070048 @Completion(DeviceIdCompleter.class)
andreaed976a42015-10-05 14:38:25 -070049 String uri = null;
50
51 @Argument(index = 1, name = "controllersListStrings", description = "list of " +
52 "controllers to set for the specified device",
Palash Kala67de5972018-03-22 14:10:50 +090053 required = false, multiValued = true)
andreaed976a42015-10-05 14:38:25 -070054 String[] controllersListStrings = null;
55
Palash Kala67de5972018-03-22 14:10:50 +090056 @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
andreaed976a42015-10-05 14:38:25 -070065 private DeviceId deviceId;
Palash Kala67de5972018-03-22 14:10:50 +090066 private List<ControllerInfo> controllers = new ArrayList<>();
andreaed976a42015-10-05 14:38:25 -070067
68 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070069 protected void doExecute() {
andreaed976a42015-10-05 14:38:25 -070070
Andrea Campanellaaf1fa392018-06-26 11:16:51 +020071 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 }
andreaed976a42015-10-05 14:38:25 -070084 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()));
andreaeb70a942015-10-16 21:34:46 -070090 try {
Palash Kala67de5972018-03-22 14:10:50 +090091 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 }
andreaeb70a942015-10-16 21:34:46 -0700112 } catch (NullPointerException e) {
113 print("No Device with requested parameters {} ", uri);
114 }
andreaed976a42015-10-05 14:38:25 -0700115 print("after:");
116 config.getControllers().forEach(c -> print(c.target()));
117 print("size %d", config.getControllers().size());
118 }
119
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +0900120
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 Campanellaaf1fa392018-06-26 11:16:51 +0200136 return getControllerInfo(annotation, config[0]);
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +0900137 } else {
Andrea Campanellaaf1fa392018-06-26 11:16:51 +0200138 return getControllerInfo(null, config[0]);
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +0900139 }
140 }
Andrea Campanellaaf1fa392018-06-26 11:16:51 +0200141
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 }
andreaed976a42015-10-05 14:38:25 -0700156}