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 | /** |
| 49 | * Successful HTTP status codes. |
| 50 | */ |
| 51 | private static final int STATUS_OK = Response.Status.OK.getStatusCode(); |
| 52 | private static final int STATUS_CREATED = Response.Status.CREATED.getStatusCode(); |
| 53 | private static final int STATUS_ACCEPTED = Response.Status.ACCEPTED.getStatusCode(); |
| 54 | |
| 55 | /** |
| 56 | * Messages for error handlers. |
| 57 | */ |
| 58 | protected static final String MASTERSHIP_NULL = "Mastership service is null"; |
| 59 | protected static final String CONTROLLER_NULL = "RestSB controller is null"; |
| 60 | protected static final String DEVICE_ID_NULL = "Device ID cannot be null"; |
| 61 | protected static final String HANDLER_NULL = "Handler cannot be null"; |
| 62 | protected static final String DEVICE_NULL = "Device cannot be null"; |
| 63 | |
| 64 | /** |
| 65 | * A unique controller that handles the REST-based communication. |
| 66 | */ |
| 67 | protected static RestSBController controller = null; |
| 68 | protected static DriverHandler handler = null; |
| 69 | |
| 70 | public BasicServerDriver() {}; |
| 71 | |
| 72 | /** |
| 73 | * Initialize the REST SB controller (if not already). |
| 74 | * Creates a handler and a controller only once, and |
| 75 | * then re-uses these objects. |
| 76 | * |
| 77 | * @throws ServiceNotFoundException when either the handler |
| 78 | * or the controller cannot be retrieved. |
| 79 | */ |
| 80 | private void init() { |
| 81 | // Already done |
| 82 | if ((handler != null) && (controller != null)) { |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | try { |
| 87 | handler = handler(); |
| 88 | checkNotNull(handler, HANDLER_NULL); |
| 89 | controller = handler.get(RestSBController.class); |
| 90 | checkNotNull(controller, CONTROLLER_NULL); |
| 91 | } catch (ServiceNotFoundException e) { |
| 92 | throw e; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Retrieve an instance of the REST SB controller. |
| 98 | * Method init will only be called the first time |
| 99 | * this method (or getHandler) is invoked. |
| 100 | * |
| 101 | * @return RestSBController instance |
| 102 | */ |
| 103 | protected RestSBController getController() { |
| 104 | if (controller == null) { |
| 105 | init(); |
| 106 | } |
| 107 | |
| 108 | return controller; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Retrieve an instance of the driver handler. |
| 113 | * Method init will only be called the first time |
| 114 | * this method (or getController) is invoked. |
| 115 | * |
| 116 | * @return DriverHandler instance |
| 117 | */ |
| 118 | protected DriverHandler getHandler() { |
| 119 | if (handler == null) { |
| 120 | init(); |
| 121 | } |
| 122 | |
| 123 | return handler; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Return all the enumeration's types in a space-separated string. |
| 128 | * |
| 129 | * @param <E> the expected class of the enum |
| 130 | * @param enumType the enum class to get its types |
| 131 | * @return String with all enumeration types |
| 132 | */ |
| 133 | public static <E extends Enum<E>> String enumTypesToString( |
| 134 | Class<E> enumType) { |
| 135 | String allTypes = ""; |
| 136 | for (E en : EnumSet.allOf(enumType)) { |
| 137 | allTypes += en.toString() + " "; |
| 138 | } |
| 139 | |
| 140 | return allTypes.trim(); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Return a string value after reading the input |
| 145 | * attribute from the input JSON node. |
| 146 | * |
| 147 | * @param jsonNode JSON node to read from |
| 148 | * @param attribute to lookup in the JSON node |
| 149 | * @return string value mapped to the attribute |
| 150 | */ |
| 151 | public static String get(JsonNode jsonNode, String attribute) { |
| 152 | if (jsonNode == null || (attribute == null || attribute.isEmpty())) { |
| 153 | return null; |
| 154 | } |
| 155 | |
| 156 | String result = ""; |
| 157 | |
| 158 | try { |
| 159 | result = jsonNode.get(attribute).asText(); |
| 160 | } catch (Exception ex) { |
| 161 | throw new RuntimeException( |
| 162 | "Failed to read JSON attribute: " + attribute |
| 163 | ); |
| 164 | } |
| 165 | |
| 166 | return result; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Assess a given HTTP status code. |
| 171 | * |
| 172 | * @param statusCode the HTTP status code to check |
| 173 | * @return boolean status (success or failure) |
| 174 | */ |
| 175 | public static boolean checkStatusCode(int statusCode) { |
| 176 | if (statusCode == STATUS_OK || statusCode == STATUS_CREATED || statusCode == STATUS_ACCEPTED) { |
| 177 | return true; |
| 178 | } |
| 179 | |
| 180 | return false; |
| 181 | } |
| 182 | |
| 183 | } |