blob: fd8fc0962aaf68b3fcd78894c9df5f2a5cd5a486 [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;
21import org.apache.karaf.shell.api.action.lifecycle.Service;
22import org.apache.karaf.shell.api.action.Option;
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +090023import org.onlab.packet.IpAddress;
andreaed976a42015-10-05 14:38:25 -070024import org.onosproject.cli.AbstractShellCommand;
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +090025import org.onosproject.net.Annotations;
26import org.onosproject.net.DefaultAnnotations;
andreaed976a42015-10-05 14:38:25 -070027import org.onosproject.net.DeviceId;
28import org.onosproject.net.behaviour.ControllerConfig;
29import org.onosproject.net.behaviour.ControllerInfo;
30import org.onosproject.net.driver.DriverHandler;
31import org.onosproject.net.driver.DriverService;
32
33import java.util.ArrayList;
34import java.util.Arrays;
35import java.util.List;
36
37/**
38 * Sets role of the controller node for the given infrastructure device.
39 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070040@Service
andreaed976a42015-10-05 14:38:25 -070041@Command(scope = "onos", name = "device-setcontrollers",
42 description = "sets the list of controllers for the given infrastructure device")
43public class DeviceSetControllersCommand extends AbstractShellCommand {
44
45 @Argument(index = 0, name = "uri", description = "Device ID",
46 required = true, multiValued = false)
47 String uri = null;
48
49 @Argument(index = 1, name = "controllersListStrings", description = "list of " +
50 "controllers to set for the specified device",
Palash Kala67de5972018-03-22 14:10:50 +090051 required = false, multiValued = true)
andreaed976a42015-10-05 14:38:25 -070052 String[] controllersListStrings = null;
53
Palash Kala67de5972018-03-22 14:10:50 +090054 @Option(name = "--remove",
55 description = "Remove specified controllers configuration")
56 private boolean removeCont = false;
57
58 @Option(name = "--remove-all",
59 description = "Remove all controllers configuration, " +
60 "does not require any input")
61 private boolean removeAll = false;
62
andreaed976a42015-10-05 14:38:25 -070063 private DeviceId deviceId;
Palash Kala67de5972018-03-22 14:10:50 +090064 private List<ControllerInfo> controllers = new ArrayList<>();
andreaed976a42015-10-05 14:38:25 -070065
66 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070067 protected void doExecute() {
andreaed976a42015-10-05 14:38:25 -070068
Andrea Campanellaaf1fa392018-06-26 11:16:51 +020069 if (controllersListStrings == null && !removeCont && !removeAll) {
70 print("No controller are given, skipping.");
71 return;
72 }
73 if (controllersListStrings != null) {
74 Arrays.asList(controllersListStrings).forEach(
75 cInfoString -> {
76 ControllerInfo controllerInfo = parseCInfoString(cInfoString);
77 if (controllerInfo != null) {
78 controllers.add(controllerInfo);
79 }
80 });
81 }
andreaed976a42015-10-05 14:38:25 -070082 DriverService service = get(DriverService.class);
83 deviceId = DeviceId.deviceId(uri);
84 DriverHandler h = service.createHandler(deviceId);
85 ControllerConfig config = h.behaviour(ControllerConfig.class);
86 print("before:");
87 config.getControllers().forEach(c -> print(c.target()));
andreaeb70a942015-10-16 21:34:46 -070088 try {
Palash Kala67de5972018-03-22 14:10:50 +090089 if (removeAll) {
90 if (!controllers.isEmpty()) {
91 print("Controllers list should be empty to remove all controllers");
92 } else {
93 List<ControllerInfo> controllersToRemove = config.getControllers();
94 controllersToRemove.forEach(c -> print("Will remove " + c.target()));
95 config.removeControllers(controllersToRemove);
96 }
97 } else {
98 if (controllers.isEmpty()) {
99 print("Controllers list is empty, cannot set/remove empty controllers");
100 } else {
101 if (removeCont) {
102 print("Will remove specified controllers");
103 config.removeControllers(controllers);
104 } else {
105 print("Will add specified controllers");
106 config.setControllers(controllers);
107 }
108 }
109 }
andreaeb70a942015-10-16 21:34:46 -0700110 } catch (NullPointerException e) {
111 print("No Device with requested parameters {} ", uri);
112 }
andreaed976a42015-10-05 14:38:25 -0700113 print("after:");
114 config.getControllers().forEach(c -> print(c.target()));
115 print("size %d", config.getControllers().size());
116 }
117
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +0900118
119 private ControllerInfo parseCInfoString(String cInfoString) {
120 Annotations annotation;
121
122 String[] config = cInfoString.split(",");
123 if (config.length == 2) {
124 String[] pair = config[1].split("=");
125
126 if (pair.length == 2) {
127 annotation = DefaultAnnotations.builder()
128 .set(pair[0], pair[1]).build();
129 } else {
130 print("Wrong format {}", config[1]);
131 return null;
132 }
133
Andrea Campanellaaf1fa392018-06-26 11:16:51 +0200134 return getControllerInfo(annotation, config[0]);
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +0900135 } else {
Andrea Campanellaaf1fa392018-06-26 11:16:51 +0200136 return getControllerInfo(null, config[0]);
Akihiro Yamanouchi54f28e22016-06-24 13:25:21 +0900137 }
138 }
Andrea Campanellaaf1fa392018-06-26 11:16:51 +0200139
140 private ControllerInfo getControllerInfo(Annotations annotation, String s) {
141 String[] data = s.split(":");
142 if (data.length != 3) {
143 print("Wrong format of the controller %s, should be in the format <protocol>:<ip>:<port>", s);
144 return null;
145 }
146 String type = data[0];
147 IpAddress ip = IpAddress.valueOf(data[1]);
148 int port = Integer.parseInt(data[2]);
149 if (annotation != null) {
150 return new ControllerInfo(ip, port, type, annotation);
151 }
152 return new ControllerInfo(ip, port, type);
153 }
andreaed976a42015-10-05 14:38:25 -0700154}