Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 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.ui; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.ObjectMapper; |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import org.onlab.osgi.ServiceDirectory; |
Alan Deikman | 8d85875 | 2017-02-14 18:08:29 -0800 | [diff] [blame] | 22 | import org.onosproject.codec.CodecContext; |
| 23 | import org.onosproject.codec.CodecService; |
| 24 | import org.onosproject.codec.JsonCodec; |
Simon Hunt | c54cd1b | 2015-05-11 13:43:44 -0700 | [diff] [blame] | 25 | import org.slf4j.Logger; |
| 26 | import org.slf4j.LoggerFactory; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 27 | |
| 28 | import java.util.Collection; |
| 29 | import java.util.Collections; |
| 30 | import java.util.HashMap; |
| 31 | import java.util.Map; |
| 32 | import java.util.Set; |
| 33 | |
| 34 | import static com.google.common.base.Preconditions.checkArgument; |
| 35 | import static com.google.common.base.Preconditions.checkNotNull; |
| 36 | |
| 37 | /** |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 38 | * Abstraction of an entity capable of processing JSON messages from the user |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 39 | * interface client. |
| 40 | * <p> |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 41 | * The message structure is: |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 42 | * </p> |
| 43 | * <pre> |
| 44 | * { |
Simon Hunt | 8a0429a | 2017-01-06 16:52:47 -0800 | [diff] [blame] | 45 | * "event": "<em>event-type</em>", |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 46 | * "payload": { |
| 47 | * <em>arbitrary JSON object structure</em> |
| 48 | * } |
| 49 | * } |
| 50 | * </pre> |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 51 | * On {@link #init initialization} the handler will create and cache |
| 52 | * {@link RequestHandler} instances, each of which are bound to a particular |
| 53 | * <em>event-type</em>. On {@link #process arrival} of a new message, |
| 54 | * the <em>event-type</em> is determined, and the message dispatched to the |
| 55 | * corresponding <em>RequestHandler</em>'s |
| 56 | * {@link RequestHandler#process process} method. |
Alan Deikman | 8d85875 | 2017-02-14 18:08:29 -0800 | [diff] [blame] | 57 | * <p> |
| 58 | * For convenience the implementation includes methods to obtain JSON |
| 59 | * generating objects (mapper, objectNode, arrayNode) as well as a |
| 60 | * JsonCodecContext for preparing and digesting messages to the UI |
| 61 | * client. |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 62 | */ |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 63 | public abstract class UiMessageHandler { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 64 | |
Simon Hunt | c54cd1b | 2015-05-11 13:43:44 -0700 | [diff] [blame] | 65 | private final Logger log = LoggerFactory.getLogger(getClass()); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 66 | private final Map<String, RequestHandler> handlerMap = new HashMap<>(); |
Thomas Vachuska | 26be4f3 | 2016-03-31 01:10:27 -0700 | [diff] [blame] | 67 | |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 68 | private final ObjectMapper mapper = new ObjectMapper(); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 69 | |
| 70 | private UiConnection connection; |
| 71 | private ServiceDirectory directory; |
| 72 | |
Alan Deikman | 8d85875 | 2017-02-14 18:08:29 -0800 | [diff] [blame] | 73 | private MessageCodecContext codecContext; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 74 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 75 | /** |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 76 | * Subclasses must create and return the collection of request handlers |
| 77 | * for the message types they handle. |
| 78 | * <p> |
| 79 | * Note that request handlers should be stateless. When we are |
| 80 | * {@link #destroy destroyed}, we will simply drop our references to them |
| 81 | * and allow them to be garbage collected. |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 82 | * |
| 83 | * @return the message handler instances |
| 84 | */ |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 85 | protected abstract Collection<RequestHandler> createRequestHandlers(); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 86 | |
| 87 | /** |
| 88 | * Returns the set of message types which this handler is capable of |
| 89 | * processing. |
| 90 | * |
| 91 | * @return set of message types |
| 92 | */ |
| 93 | public Set<String> messageTypes() { |
| 94 | return Collections.unmodifiableSet(handlerMap.keySet()); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Processes a JSON message from the user interface client. |
| 99 | * |
| 100 | * @param message JSON message |
| 101 | */ |
| 102 | public void process(ObjectNode message) { |
| 103 | String type = JsonUtils.eventType(message); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 104 | ObjectNode payload = JsonUtils.payload(message); |
Simon Hunt | 8a0429a | 2017-01-06 16:52:47 -0800 | [diff] [blame] | 105 | exec(type, payload); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Finds the appropriate handler and executes the process method. |
| 110 | * |
| 111 | * @param eventType event type |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 112 | * @param payload message payload |
| 113 | */ |
Simon Hunt | 8a0429a | 2017-01-06 16:52:47 -0800 | [diff] [blame] | 114 | void exec(String eventType, ObjectNode payload) { |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 115 | RequestHandler requestHandler = handlerMap.get(eventType); |
| 116 | if (requestHandler != null) { |
Simon Hunt | 8a0429a | 2017-01-06 16:52:47 -0800 | [diff] [blame] | 117 | requestHandler.process(payload); |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 118 | } else { |
| 119 | log.warn("no request handler for event type {}", eventType); |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 123 | /** |
| 124 | * Initializes the handler with the user interface connection and |
| 125 | * service directory context. |
| 126 | * |
| 127 | * @param connection user interface connection |
| 128 | * @param directory service directory |
| 129 | */ |
| 130 | public void init(UiConnection connection, ServiceDirectory directory) { |
| 131 | this.connection = connection; |
| 132 | this.directory = directory; |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 133 | |
| 134 | Collection<RequestHandler> handlers = createRequestHandlers(); |
| 135 | checkNotNull(handlers, "Handlers cannot be null"); |
| 136 | checkArgument(!handlers.isEmpty(), "Handlers cannot be empty"); |
| 137 | |
| 138 | for (RequestHandler h : handlers) { |
| 139 | h.setParent(this); |
| 140 | handlerMap.put(h.eventType(), h); |
| 141 | } |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Destroys the message handler context. |
| 146 | */ |
| 147 | public void destroy() { |
| 148 | this.connection = null; |
| 149 | this.directory = null; |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 150 | handlerMap.clear(); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Returns the user interface connection with which this handler was primed. |
| 155 | * |
| 156 | * @return user interface connection |
| 157 | */ |
| 158 | public UiConnection connection() { |
| 159 | return connection; |
| 160 | } |
| 161 | |
| 162 | /** |
Simon Hunt | 8add9ee | 2016-09-20 17:05:07 -0700 | [diff] [blame] | 163 | * Returns the service directory with which this handler was primed. |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 164 | * |
Simon Hunt | 8add9ee | 2016-09-20 17:05:07 -0700 | [diff] [blame] | 165 | * @return service directory |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 166 | */ |
| 167 | public ServiceDirectory directory() { |
| 168 | return directory; |
| 169 | } |
| 170 | |
| 171 | /** |
Simon Hunt | 8add9ee | 2016-09-20 17:05:07 -0700 | [diff] [blame] | 172 | * Returns an implementation of the specified service class. |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 173 | * |
| 174 | * @param serviceClass service class |
| 175 | * @param <T> type of service |
| 176 | * @return implementation class |
| 177 | * @throws org.onlab.osgi.ServiceNotFoundException if no implementation found |
| 178 | */ |
| 179 | protected <T> T get(Class<T> serviceClass) { |
| 180 | return directory.get(serviceClass); |
| 181 | } |
| 182 | |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 183 | /** |
| 184 | * Returns a freshly minted object node. |
| 185 | * |
| 186 | * @return new object node |
| 187 | */ |
| 188 | protected ObjectNode objectNode() { |
| 189 | return mapper.createObjectNode(); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Returns a freshly minted array node. |
| 194 | * |
| 195 | * @return new array node |
| 196 | */ |
| 197 | protected ArrayNode arrayNode() { |
| 198 | return mapper.createArrayNode(); |
| 199 | } |
Simon Hunt | 5256066 | 2015-08-27 22:46:44 -0700 | [diff] [blame] | 200 | |
| 201 | /** |
| 202 | * Sends the specified data to the client. |
| 203 | * It is expected that the data is in the prescribed JSON format for |
| 204 | * events to the client. |
| 205 | * |
| 206 | * @param data data to be sent |
| 207 | */ |
| 208 | protected synchronized void sendMessage(ObjectNode data) { |
| 209 | UiConnection connection = connection(); |
| 210 | if (connection != null) { |
| 211 | connection.sendMessage(data); |
| 212 | } |
| 213 | } |
Alan Deikman | 8d85875 | 2017-02-14 18:08:29 -0800 | [diff] [blame] | 214 | |
| 215 | /** |
| 216 | * Obtain a CodecContext to be used in encoding and decoding objects |
| 217 | * that have a registered JsonCodec for their class. This method |
| 218 | * instantiates a private inner class which is returned on |
| 219 | * subsequent calls. |
| 220 | * |
| 221 | * @return a CodecContext. |
| 222 | */ |
| 223 | protected CodecContext getJsonCodecContext() { |
| 224 | if (codecContext != null) { |
| 225 | return codecContext; |
| 226 | } |
| 227 | codecContext = new MessageCodecContext(); |
| 228 | return codecContext; |
| 229 | } |
| 230 | |
| 231 | private class MessageCodecContext implements CodecContext { |
| 232 | |
| 233 | CodecService cs = get(CodecService.class); |
| 234 | |
| 235 | @Override |
| 236 | public ObjectMapper mapper() { |
| 237 | return mapper; |
| 238 | } |
| 239 | |
| 240 | @Override |
| 241 | public <T> JsonCodec<T> codec(Class<T> entityClass) { |
| 242 | return cs.getCodec(entityClass); |
| 243 | } |
| 244 | |
| 245 | @Override |
| 246 | public <T> T getService(Class<T> serviceClass) { |
| 247 | return get(serviceClass); |
| 248 | } |
| 249 | } |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 250 | } |