Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | |
Simon Hunt | 4c7edd3 | 2015-03-11 10:42:53 -0700 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.ObjectMapper; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import org.onlab.osgi.ServiceDirectory; |
| 21 | |
| 22 | import java.util.Set; |
| 23 | |
| 24 | import static com.google.common.base.Preconditions.checkArgument; |
| 25 | import static com.google.common.base.Preconditions.checkNotNull; |
| 26 | |
| 27 | /** |
| 28 | * Abstraction of an entity capable of processing a JSON message from the user |
| 29 | * interface client. |
| 30 | * <p> |
| 31 | * The message is a JSON object with the following structure: |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 32 | * </p> |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 33 | * <pre> |
| 34 | * { |
| 35 | * "type": "<em>event-type</em>", |
| 36 | * "sid": "<em>sequence-number</em>", |
| 37 | * "payload": { |
| 38 | * <em>arbitrary JSON object structure</em> |
| 39 | * } |
| 40 | * } |
| 41 | * </pre> |
| 42 | */ |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 43 | @Deprecated |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 44 | public abstract class UiMessageHandler { |
| 45 | |
| 46 | private final Set<String> messageTypes; |
| 47 | private UiConnection connection; |
| 48 | private ServiceDirectory directory; |
| 49 | |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 50 | /** |
| 51 | * Mapper for creating ObjectNodes and ArrayNodes etc. |
| 52 | */ |
Simon Hunt | 4c7edd3 | 2015-03-11 10:42:53 -0700 | [diff] [blame] | 53 | protected final ObjectMapper mapper = new ObjectMapper(); |
| 54 | |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 55 | /** |
| 56 | * Creates a new message handler for the specified set of message types. |
| 57 | * |
| 58 | * @param messageTypes set of message types |
| 59 | */ |
| 60 | protected UiMessageHandler(Set<String> messageTypes) { |
| 61 | this.messageTypes = checkNotNull(messageTypes, "Message types cannot be null"); |
| 62 | checkArgument(!messageTypes.isEmpty(), "Message types cannot be empty"); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Returns the set of message types which this handler is capable of |
| 67 | * processing. |
| 68 | * |
| 69 | * @return set of message types |
| 70 | */ |
| 71 | public Set<String> messageTypes() { |
| 72 | return messageTypes; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Processes a JSON message from the user interface client. |
| 77 | * |
| 78 | * @param message JSON message |
| 79 | */ |
| 80 | public abstract void process(ObjectNode message); |
| 81 | |
| 82 | /** |
| 83 | * Initializes the handler with the user interface connection and |
| 84 | * service directory context. |
| 85 | * |
| 86 | * @param connection user interface connection |
| 87 | * @param directory service directory |
| 88 | */ |
| 89 | public void init(UiConnection connection, ServiceDirectory directory) { |
| 90 | this.connection = connection; |
| 91 | this.directory = directory; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Destroys the message handler context. |
| 96 | */ |
| 97 | public void destroy() { |
| 98 | this.connection = null; |
| 99 | this.directory = null; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Returns the user interface connection with which this handler was primed. |
| 104 | * |
| 105 | * @return user interface connection |
| 106 | */ |
| 107 | public UiConnection connection() { |
| 108 | return connection; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Returns the user interface connection with which this handler was primed. |
| 113 | * |
| 114 | * @return user interface connection |
| 115 | */ |
| 116 | public ServiceDirectory directory() { |
| 117 | return directory; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Returns implementation of the specified service class. |
| 122 | * |
| 123 | * @param serviceClass service class |
| 124 | * @param <T> type of service |
| 125 | * @return implementation class |
| 126 | * @throws org.onlab.osgi.ServiceNotFoundException if no implementation found |
| 127 | */ |
| 128 | protected <T> T get(Class<T> serviceClass) { |
| 129 | return directory.get(serviceClass); |
| 130 | } |
| 131 | |
Simon Hunt | 4c7edd3 | 2015-03-11 10:42:53 -0700 | [diff] [blame] | 132 | /** |
| 133 | * Wraps a message payload into an event structure for the given event |
| 134 | * type and sequence ID. Generally the |
| 135 | * |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 136 | * @param type event type |
| 137 | * @param sid sequence ID |
Simon Hunt | 4c7edd3 | 2015-03-11 10:42:53 -0700 | [diff] [blame] | 138 | * @param payload event payload |
| 139 | * @return the object node representation |
| 140 | */ |
| 141 | protected ObjectNode envelope(String type, long sid, ObjectNode payload) { |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 142 | return JsonUtils.envelope(type, sid, payload); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Returns the event type from the specified event. |
| 147 | * |
| 148 | * @param event the event |
| 149 | * @return the event type |
| 150 | */ |
| 151 | protected String eventType(ObjectNode event) { |
| 152 | return JsonUtils.eventType(event); |
Simon Hunt | 4c7edd3 | 2015-03-11 10:42:53 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 155 | /** |
| 156 | * Retrieves the payload from the specified event. |
| 157 | * |
| 158 | * @param event message event |
| 159 | * @return extracted payload object |
| 160 | */ |
| 161 | protected ObjectNode payload(ObjectNode event) { |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 162 | return JsonUtils.payload(event); |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Returns the specified node property as a number. |
| 167 | * |
| 168 | * @param node message event |
| 169 | * @param name property name |
| 170 | * @return property as number |
| 171 | */ |
| 172 | protected long number(ObjectNode node, String name) { |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 173 | return JsonUtils.number(node, name); |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Returns the specified node property as a string. |
| 178 | * |
| 179 | * @param node message event |
| 180 | * @param name property name |
| 181 | * @return property as a string |
| 182 | */ |
| 183 | protected String string(ObjectNode node, String name) { |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 184 | return JsonUtils.string(node, name); |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Returns the specified node property as a string with a default fallback. |
| 189 | * |
| 190 | * @param node message event |
| 191 | * @param name property name |
| 192 | * @param defaultValue fallback value if property is absent |
| 193 | * @return property as a string |
| 194 | */ |
| 195 | protected String string(ObjectNode node, String name, String defaultValue) { |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 196 | return JsonUtils.string(node, name, defaultValue); |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 199 | /** |
| 200 | * Concatenates an arbitrary number of objects, using their |
| 201 | * toString() methods. |
| 202 | * |
| 203 | * @param items the items to concatenate |
| 204 | * @return a concatenated string |
| 205 | */ |
| 206 | protected static String concat(Object... items) { |
| 207 | StringBuilder sb = new StringBuilder(); |
| 208 | for (Object o : items) { |
| 209 | sb.append(o); |
| 210 | } |
| 211 | return sb.toString(); |
| 212 | } |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 213 | } |