Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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.drivers.server; |
| 18 | |
| 19 | import org.onosproject.net.DeviceId; |
| 20 | import org.onosproject.net.behaviour.ControllerConfig; |
| 21 | import org.onosproject.net.behaviour.ControllerInfo; |
| 22 | import org.onosproject.mastership.MastershipService; |
| 23 | |
| 24 | import org.onlab.packet.IpAddress; |
| 25 | import org.onlab.packet.TpPort; |
| 26 | import org.slf4j.Logger; |
| 27 | |
| 28 | import com.google.common.collect.Lists; |
| 29 | import com.fasterxml.jackson.databind.JsonNode; |
| 30 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 31 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 32 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 33 | |
| 34 | import java.io.ByteArrayInputStream; |
| 35 | import java.io.IOException; |
| 36 | import java.io.InputStream; |
| 37 | import java.util.List; |
| 38 | import java.util.Map; |
| 39 | import javax.ws.rs.ProcessingException; |
| 40 | |
| 41 | import static com.google.common.base.Preconditions.checkNotNull; |
Georgios Katsikas | 740d328 | 2020-03-18 12:05:03 +0100 | [diff] [blame] | 42 | import static org.onosproject.drivers.server.Constants.JSON; |
| 43 | import static org.onosproject.drivers.server.Constants.MSG_DEVICE_ID_NULL; |
| 44 | import static org.onosproject.drivers.server.Constants.MSG_MASTERSHIP_NULL; |
| 45 | import static org.onosproject.drivers.server.Constants.PARAM_CTRL; |
| 46 | import static org.onosproject.drivers.server.Constants.PARAM_CTRL_IP; |
| 47 | import static org.onosproject.drivers.server.Constants.PARAM_CTRL_PORT; |
| 48 | import static org.onosproject.drivers.server.Constants.PARAM_CTRL_TYPE; |
| 49 | import static org.onosproject.drivers.server.Constants.SLASH; |
| 50 | import static org.onosproject.drivers.server.Constants.URL_CONTROLLERS_GET; |
| 51 | import static org.onosproject.drivers.server.Constants.URL_CONTROLLERS_DEL; |
| 52 | import static org.onosproject.drivers.server.Constants.URL_CONTROLLERS_SET; |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 53 | import static org.slf4j.LoggerFactory.getLogger; |
| 54 | |
| 55 | /** |
Georgios Katsikas | 740d328 | 2020-03-18 12:05:03 +0100 | [diff] [blame] | 56 | * Implementation of controller configuration behaviour for server devices. |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 57 | */ |
Georgios Katsikas | 740d328 | 2020-03-18 12:05:03 +0100 | [diff] [blame] | 58 | public class ServerControllerConfig |
| 59 | extends BasicServerDriver |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 60 | implements ControllerConfig { |
| 61 | |
| 62 | private final Logger log = getLogger(getClass()); |
| 63 | |
| 64 | /** |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 65 | * Constructs controller configuration for server. |
| 66 | */ |
| 67 | public ServerControllerConfig() { |
| 68 | super(); |
| 69 | log.debug("Started"); |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public List<ControllerInfo> getControllers() { |
| 74 | List<ControllerInfo> controllers = Lists.newArrayList(); |
| 75 | |
Georgios Katsikas | 740d328 | 2020-03-18 12:05:03 +0100 | [diff] [blame] | 76 | DeviceId deviceId = getDeviceId(); |
| 77 | checkNotNull(deviceId, MSG_DEVICE_ID_NULL); |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 78 | |
| 79 | MastershipService mastershipService = getHandler().get(MastershipService.class); |
Georgios Katsikas | 740d328 | 2020-03-18 12:05:03 +0100 | [diff] [blame] | 80 | checkNotNull(deviceId, MSG_MASTERSHIP_NULL); |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 81 | |
| 82 | if (!mastershipService.isLocalMaster(deviceId)) { |
| 83 | log.warn( |
| 84 | "I am not master for {}. " + |
| 85 | "Please use master {} to get controllers for this device", |
Georgios Katsikas | 9a5ed3a | 2018-06-26 19:07:22 +0200 | [diff] [blame] | 86 | deviceId, mastershipService.getMasterFor(deviceId)); |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 87 | return controllers; |
| 88 | } |
| 89 | |
| 90 | // Hit the path that provides the server's controllers |
| 91 | InputStream response = null; |
| 92 | try { |
Georgios Katsikas | 740d328 | 2020-03-18 12:05:03 +0100 | [diff] [blame] | 93 | response = getController().get(deviceId, URL_CONTROLLERS_GET, JSON); |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 94 | } catch (ProcessingException pEx) { |
| 95 | log.error("Failed to get controllers of device: {}", deviceId); |
| 96 | return controllers; |
| 97 | } |
| 98 | |
| 99 | // Load the JSON into objects |
| 100 | ObjectMapper mapper = new ObjectMapper(); |
| 101 | Map<String, Object> jsonMap = null; |
| 102 | JsonNode jsonNode = null; |
| 103 | ObjectNode objNode = null; |
| 104 | try { |
| 105 | jsonMap = mapper.readValue(response, Map.class); |
| 106 | jsonNode = mapper.convertValue(jsonMap, JsonNode.class); |
| 107 | objNode = (ObjectNode) jsonNode; |
| 108 | } catch (IOException ioEx) { |
| 109 | log.error("Failed to get controllers of device: {}", deviceId); |
| 110 | return controllers; |
| 111 | } |
| 112 | |
| 113 | if (jsonMap == null) { |
| 114 | log.error("Failed to get controllers of device: {}", deviceId); |
| 115 | return controllers; |
| 116 | } |
| 117 | |
Georgios Katsikas | 740d328 | 2020-03-18 12:05:03 +0100 | [diff] [blame] | 118 | // Fetch controllers' array |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 119 | JsonNode ctrlNode = objNode.path(PARAM_CTRL); |
| 120 | |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 121 | for (JsonNode cn : ctrlNode) { |
| 122 | ObjectNode ctrlObjNode = (ObjectNode) cn; |
| 123 | |
| 124 | // Get the attributes of a controller |
| 125 | String ctrlIpStr = get(cn, PARAM_CTRL_IP); |
| 126 | int ctrlPort = ctrlObjNode.path(PARAM_CTRL_PORT).asInt(); |
| 127 | String ctrlType = get(cn, PARAM_CTRL_TYPE); |
| 128 | |
Georgios Katsikas | 9a5ed3a | 2018-06-26 19:07:22 +0200 | [diff] [blame] | 129 | // Implies no controller |
| 130 | if (ctrlIpStr.isEmpty()) { |
| 131 | continue; |
| 132 | } |
| 133 | |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 134 | // Check data format and range |
| 135 | IpAddress ctrlIp = null; |
| 136 | try { |
| 137 | ctrlIp = IpAddress.valueOf(ctrlIpStr); |
| 138 | } catch (IllegalArgumentException e) { |
| 139 | throw new IllegalArgumentException(e); |
| 140 | } |
| 141 | |
| 142 | if ((ctrlPort < 0) || (ctrlPort > TpPort.MAX_PORT)) { |
| 143 | final String msg = "Invalid controller port: " + ctrlPort; |
| 144 | throw new IllegalArgumentException(msg); |
| 145 | } |
| 146 | |
| 147 | controllers.add( |
| 148 | new ControllerInfo(ctrlIp, ctrlPort, ctrlType) |
| 149 | ); |
| 150 | } |
| 151 | |
| 152 | return controllers; |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public void setControllers(List<ControllerInfo> controllers) { |
Georgios Katsikas | 740d328 | 2020-03-18 12:05:03 +0100 | [diff] [blame] | 157 | DeviceId deviceId = getDeviceId(); |
| 158 | checkNotNull(deviceId, MSG_DEVICE_ID_NULL); |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 159 | |
| 160 | MastershipService mastershipService = getHandler().get(MastershipService.class); |
Georgios Katsikas | 740d328 | 2020-03-18 12:05:03 +0100 | [diff] [blame] | 161 | checkNotNull(deviceId, MSG_MASTERSHIP_NULL); |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 162 | |
| 163 | if (!mastershipService.isLocalMaster(deviceId)) { |
| 164 | log.warn( |
| 165 | "I am not master for {}. " + |
| 166 | "Please use master {} to set controllers for this device", |
Georgios Katsikas | 9a5ed3a | 2018-06-26 19:07:22 +0200 | [diff] [blame] | 167 | deviceId, mastershipService.getMasterFor(deviceId)); |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 168 | return; |
| 169 | } |
| 170 | |
| 171 | ObjectMapper mapper = new ObjectMapper(); |
| 172 | |
| 173 | // Create the object node to host the data |
| 174 | ObjectNode sendObjNode = mapper.createObjectNode(); |
| 175 | |
| 176 | // Insert header |
| 177 | ArrayNode ctrlsArrayNode = sendObjNode.putArray(PARAM_CTRL); |
| 178 | |
| 179 | // Add each controller's information object |
| 180 | for (ControllerInfo ctrl : controllers) { |
| 181 | ObjectNode ctrlObjNode = mapper.createObjectNode(); |
| 182 | ctrlObjNode.put(PARAM_CTRL_IP, ctrl.ip().toString()); |
| 183 | ctrlObjNode.put(PARAM_CTRL_PORT, ctrl.port()); |
| 184 | ctrlObjNode.put(PARAM_CTRL_TYPE, ctrl.type()); |
| 185 | ctrlsArrayNode.add(ctrlObjNode); |
| 186 | } |
| 187 | |
| 188 | // Post the controllers to the device |
| 189 | int response = getController().post( |
Georgios Katsikas | 740d328 | 2020-03-18 12:05:03 +0100 | [diff] [blame] | 190 | deviceId, URL_CONTROLLERS_SET, |
Georgios Katsikas | 9a5ed3a | 2018-06-26 19:07:22 +0200 | [diff] [blame] | 191 | new ByteArrayInputStream(sendObjNode.toString().getBytes()), JSON); |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 192 | |
| 193 | if (!checkStatusCode(response)) { |
| 194 | log.error("Failed to set controllers on device {}", deviceId); |
| 195 | } |
| 196 | |
| 197 | return; |
| 198 | } |
| 199 | |
| 200 | @Override |
| 201 | public void removeControllers(List<ControllerInfo> controllers) { |
Georgios Katsikas | 740d328 | 2020-03-18 12:05:03 +0100 | [diff] [blame] | 202 | DeviceId deviceId = getDeviceId(); |
| 203 | checkNotNull(deviceId, MSG_DEVICE_ID_NULL); |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 204 | |
| 205 | MastershipService mastershipService = getHandler().get(MastershipService.class); |
Georgios Katsikas | 740d328 | 2020-03-18 12:05:03 +0100 | [diff] [blame] | 206 | checkNotNull(deviceId, MSG_MASTERSHIP_NULL); |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 207 | |
| 208 | if (!mastershipService.isLocalMaster(deviceId)) { |
| 209 | log.warn( |
| 210 | "I am not master for {}. " + |
| 211 | "Please use master {} to remove controllers from this device", |
Georgios Katsikas | 9a5ed3a | 2018-06-26 19:07:22 +0200 | [diff] [blame] | 212 | deviceId, mastershipService.getMasterFor(deviceId)); |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 213 | return; |
| 214 | } |
| 215 | |
Georgios Katsikas | 9a5ed3a | 2018-06-26 19:07:22 +0200 | [diff] [blame] | 216 | for (ControllerInfo ctrl : controllers) { |
| 217 | log.info("Remove controller with {}:{}:{}", |
| 218 | ctrl.type(), ctrl.ip().toString(), ctrl.port()); |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 219 | |
Georgios Katsikas | 740d328 | 2020-03-18 12:05:03 +0100 | [diff] [blame] | 220 | String remCtrlUrl = URL_CONTROLLERS_DEL + SLASH + ctrl.ip().toString(); |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 221 | |
Georgios Katsikas | 9a5ed3a | 2018-06-26 19:07:22 +0200 | [diff] [blame] | 222 | // Remove this controller |
| 223 | int response = getController().delete(deviceId, remCtrlUrl, null, JSON); |
| 224 | |
| 225 | if (!checkStatusCode(response)) { |
| 226 | log.error("Failed to remove controller {}:{}:{} from device {}", |
| 227 | ctrl.type(), ctrl.ip().toString(), ctrl.port(), deviceId); |
| 228 | } |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | return; |
| 232 | } |
| 233 | |
| 234 | } |