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