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 | package org.onosproject.drivers.server; |
| 17 | |
Georgios Katsikas | 80e0b9f | 2018-07-21 20:29:18 +0200 | [diff] [blame] | 18 | import org.onosproject.drivers.server.devices.RestServerSBDevice; |
| 19 | |
| 20 | import org.onosproject.net.DeviceId; |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 21 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 22 | import org.onosproject.net.driver.DriverHandler; |
| 23 | import org.onosproject.protocol.rest.RestSBController; |
Georgios Katsikas | 30bede5 | 2018-07-28 14:46:07 +0200 | [diff] [blame^] | 24 | import org.onosproject.protocol.rest.RestSBDevice; |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 25 | |
| 26 | import org.slf4j.Logger; |
Georgios Katsikas | 80e0b9f | 2018-07-21 20:29:18 +0200 | [diff] [blame] | 27 | |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 28 | import com.fasterxml.jackson.databind.JsonNode; |
| 29 | |
| 30 | import java.util.EnumSet; |
| 31 | import javax.ws.rs.core.MediaType; |
| 32 | import javax.ws.rs.core.Response; |
| 33 | |
| 34 | import static org.slf4j.LoggerFactory.getLogger; |
| 35 | import static com.google.common.base.Preconditions.checkNotNull; |
| 36 | |
| 37 | /** |
| 38 | * The basic functionality of the server driver. |
| 39 | */ |
| 40 | public class BasicServerDriver extends AbstractHandlerBehaviour { |
| 41 | |
| 42 | private final Logger log = getLogger(getClass()); |
| 43 | |
| 44 | /** |
| 45 | * Resource endpoints of the server agent (REST server-side). |
| 46 | */ |
| 47 | public static final MediaType JSON = MediaType.valueOf(MediaType.APPLICATION_JSON); |
| 48 | protected static final String ROOT_URL = ""; |
Georgios Katsikas | 30bede5 | 2018-07-28 14:46:07 +0200 | [diff] [blame^] | 49 | protected static final String SLASH = "/"; |
| 50 | public static final String BASE_URL = ROOT_URL + SLASH + "metron"; |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 51 | |
| 52 | /** |
Georgios Katsikas | 2ebd8a0 | 2018-06-27 18:32:50 +0200 | [diff] [blame] | 53 | * Common parameters to be exchanged with the server's agent. |
| 54 | */ |
| 55 | public static final String PARAM_ID = "id"; |
| 56 | public static final String PARAM_NICS = "nics"; |
| 57 | public static final String PARAM_CPUS = "cpus"; |
| 58 | public static final String NIC_PARAM_RX_FILTER = "rxFilter"; |
| 59 | public static final String NIC_PARAM_RX_METHOD = "method"; |
| 60 | |
| 61 | |
| 62 | /** |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 63 | * Successful HTTP status codes. |
| 64 | */ |
| 65 | private static final int STATUS_OK = Response.Status.OK.getStatusCode(); |
| 66 | private static final int STATUS_CREATED = Response.Status.CREATED.getStatusCode(); |
| 67 | private static final int STATUS_ACCEPTED = Response.Status.ACCEPTED.getStatusCode(); |
| 68 | |
| 69 | /** |
| 70 | * Messages for error handlers. |
| 71 | */ |
| 72 | protected static final String MASTERSHIP_NULL = "Mastership service is null"; |
| 73 | protected static final String CONTROLLER_NULL = "RestSB controller is null"; |
| 74 | protected static final String DEVICE_ID_NULL = "Device ID cannot be null"; |
| 75 | protected static final String HANDLER_NULL = "Handler cannot be null"; |
| 76 | protected static final String DEVICE_NULL = "Device cannot be null"; |
| 77 | |
| 78 | /** |
| 79 | * A unique controller that handles the REST-based communication. |
| 80 | */ |
| 81 | protected static RestSBController controller = null; |
| 82 | protected static DriverHandler handler = null; |
Ray Milkey | c6c9b17 | 2018-02-26 09:36:31 -0800 | [diff] [blame] | 83 | private static final Object CONTROLLER_LOCK = new Object(); |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 84 | |
| 85 | public BasicServerDriver() {}; |
| 86 | |
| 87 | /** |
Georgios Katsikas | 30bede5 | 2018-07-28 14:46:07 +0200 | [diff] [blame^] | 88 | * Retrieve an instance of the driver handler. |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 89 | * |
Georgios Katsikas | 30bede5 | 2018-07-28 14:46:07 +0200 | [diff] [blame^] | 90 | * @return DriverHandler instance |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 91 | */ |
Georgios Katsikas | 30bede5 | 2018-07-28 14:46:07 +0200 | [diff] [blame^] | 92 | protected DriverHandler getHandler() { |
Ray Milkey | c6c9b17 | 2018-02-26 09:36:31 -0800 | [diff] [blame] | 93 | synchronized (CONTROLLER_LOCK) { |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 94 | handler = handler(); |
| 95 | checkNotNull(handler, HANDLER_NULL); |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 96 | } |
Georgios Katsikas | 30bede5 | 2018-07-28 14:46:07 +0200 | [diff] [blame^] | 97 | |
| 98 | return handler; |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Retrieve an instance of the REST SB controller. |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 103 | * |
| 104 | * @return RestSBController instance |
| 105 | */ |
Georgios Katsikas | 2ebd8a0 | 2018-06-27 18:32:50 +0200 | [diff] [blame] | 106 | public RestSBController getController() { |
Ray Milkey | c6c9b17 | 2018-02-26 09:36:31 -0800 | [diff] [blame] | 107 | synchronized (CONTROLLER_LOCK) { |
| 108 | if (controller == null) { |
Georgios Katsikas | 30bede5 | 2018-07-28 14:46:07 +0200 | [diff] [blame^] | 109 | controller = getHandler().get(RestSBController.class); |
| 110 | checkNotNull(controller, CONTROLLER_NULL); |
Ray Milkey | c6c9b17 | 2018-02-26 09:36:31 -0800 | [diff] [blame] | 111 | } |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 112 | } |
Georgios Katsikas | 30bede5 | 2018-07-28 14:46:07 +0200 | [diff] [blame^] | 113 | |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 114 | return controller; |
| 115 | } |
| 116 | |
| 117 | /** |
Georgios Katsikas | 80e0b9f | 2018-07-21 20:29:18 +0200 | [diff] [blame] | 118 | * Finds the NIC name that corresponds to a device's port number. |
| 119 | * |
| 120 | * @param deviceId a device ID |
| 121 | * @param port a NIC port number |
| 122 | * @return device's NIC name |
| 123 | */ |
| 124 | public static String findNicInterfaceWithPort(DeviceId deviceId, long port) { |
Georgios Katsikas | 30bede5 | 2018-07-28 14:46:07 +0200 | [diff] [blame^] | 125 | if (controller == null) { |
| 126 | return null; |
| 127 | } |
| 128 | |
Georgios Katsikas | 80e0b9f | 2018-07-21 20:29:18 +0200 | [diff] [blame] | 129 | RestServerSBDevice device = null; |
| 130 | try { |
| 131 | device = (RestServerSBDevice) controller.getDevice(deviceId); |
| 132 | } catch (ClassCastException ccEx) { |
| 133 | return null; |
| 134 | } |
| 135 | checkNotNull(device, DEVICE_NULL); |
| 136 | |
| 137 | return device.portNameFromNumber(port); |
| 138 | } |
| 139 | |
| 140 | /** |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 141 | * Return all the enumeration's types in a space-separated string. |
| 142 | * |
| 143 | * @param <E> the expected class of the enum |
| 144 | * @param enumType the enum class to get its types |
| 145 | * @return String with all enumeration types |
| 146 | */ |
Georgios Katsikas | 30bede5 | 2018-07-28 14:46:07 +0200 | [diff] [blame^] | 147 | public static <E extends Enum<E>> String enumTypesToString(Class<E> enumType) { |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 148 | String allTypes = ""; |
| 149 | for (E en : EnumSet.allOf(enumType)) { |
| 150 | allTypes += en.toString() + " "; |
| 151 | } |
| 152 | |
| 153 | return allTypes.trim(); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Return a string value after reading the input |
| 158 | * attribute from the input JSON node. |
| 159 | * |
| 160 | * @param jsonNode JSON node to read from |
| 161 | * @param attribute to lookup in the JSON node |
| 162 | * @return string value mapped to the attribute |
| 163 | */ |
| 164 | public static String get(JsonNode jsonNode, String attribute) { |
| 165 | if (jsonNode == null || (attribute == null || attribute.isEmpty())) { |
| 166 | return null; |
| 167 | } |
| 168 | |
| 169 | String result = ""; |
| 170 | |
| 171 | try { |
| 172 | result = jsonNode.get(attribute).asText(); |
| 173 | } catch (Exception ex) { |
Ray Milkey | 067c44b | 2018-02-26 12:48:23 -0800 | [diff] [blame] | 174 | throw new IllegalArgumentException( |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 175 | "Failed to read JSON attribute: " + attribute |
| 176 | ); |
| 177 | } |
| 178 | |
| 179 | return result; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Assess a given HTTP status code. |
| 184 | * |
| 185 | * @param statusCode the HTTP status code to check |
| 186 | * @return boolean status (success or failure) |
| 187 | */ |
| 188 | public static boolean checkStatusCode(int statusCode) { |
| 189 | if (statusCode == STATUS_OK || statusCode == STATUS_CREATED || statusCode == STATUS_ACCEPTED) { |
| 190 | return true; |
| 191 | } |
| 192 | |
| 193 | return false; |
| 194 | } |
| 195 | |
Georgios Katsikas | 30bede5 | 2018-07-28 14:46:07 +0200 | [diff] [blame^] | 196 | /** |
| 197 | * Upon a failure to contact a device, the driver |
| 198 | * raises a disconnect event by resetting the |
| 199 | * activity flag of this device. |
| 200 | * |
| 201 | * @param device a device to disconnect |
| 202 | */ |
| 203 | protected void raiseDeviceDisconnect(RestSBDevice device) { |
| 204 | // Already done! |
| 205 | if (!device.isActive()) { |
| 206 | return; |
| 207 | } |
| 208 | |
| 209 | log.info("Setting device {} inactive", device.deviceId()); |
| 210 | device.setActive(false); |
| 211 | } |
| 212 | |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 213 | } |