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