Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
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; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | |
| 21 | /** |
| 22 | * Abstraction of an entity that handles a specific request from the |
| 23 | * user interface client. |
| 24 | * |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 25 | * @see UiMessageHandler |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 26 | */ |
| 27 | public abstract class RequestHandler { |
| 28 | |
| 29 | protected static final ObjectMapper MAPPER = new ObjectMapper(); |
| 30 | |
| 31 | private final String eventType; |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 32 | private UiMessageHandler parent; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 33 | |
| 34 | |
Simon Hunt | 8add9ee | 2016-09-20 17:05:07 -0700 | [diff] [blame] | 35 | /** |
| 36 | * Constructs a request handler instance that will handle events |
| 37 | * of the specified type. |
| 38 | * |
| 39 | * @param eventType type of event that will be handled |
| 40 | */ |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 41 | public RequestHandler(String eventType) { |
| 42 | this.eventType = eventType; |
| 43 | } |
| 44 | |
| 45 | // package private |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 46 | void setParent(UiMessageHandler parent) { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 47 | this.parent = parent; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Returns the event type that this handler handles. |
| 52 | * |
| 53 | * @return event type |
| 54 | */ |
| 55 | public String eventType() { |
| 56 | return eventType; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Processes the incoming message payload from the client. |
| 61 | * |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 62 | * @param payload request message payload |
| 63 | */ |
Simon Hunt | 8a0429a | 2017-01-06 16:52:47 -0800 | [diff] [blame] | 64 | public abstract void process(ObjectNode payload); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 65 | |
| 66 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 67 | // =================================================================== |
| 68 | // === Convenience methods... |
| 69 | |
| 70 | /** |
Simon Hunt | 8add9ee | 2016-09-20 17:05:07 -0700 | [diff] [blame] | 71 | * Returns an implementation of the specified service class. |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 72 | * |
| 73 | * @param serviceClass service class |
| 74 | * @param <T> type of service |
| 75 | * @return implementation class |
| 76 | * @throws org.onlab.osgi.ServiceNotFoundException if no implementation found |
| 77 | */ |
| 78 | protected <T> T get(Class<T> serviceClass) { |
| 79 | return parent.directory().get(serviceClass); |
| 80 | } |
| 81 | |
| 82 | /** |
Simon Hunt | 8add9ee | 2016-09-20 17:05:07 -0700 | [diff] [blame] | 83 | * Sends a message back to the client with the given event type and payload. |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 84 | * |
| 85 | * @param eventType message event type |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 86 | * @param payload message payload |
| 87 | */ |
| 88 | protected void sendMessage(String eventType, ObjectNode payload) { |
Simon Hunt | c536818 | 2017-01-10 13:32:04 -0800 | [diff] [blame] | 89 | parent.connection().sendMessage(eventType, payload); |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Sends a message back to the client. |
Simon Hunt | 8add9ee | 2016-09-20 17:05:07 -0700 | [diff] [blame] | 94 | * Here, the message is preformatted; the assumption is that it has its |
Simon Hunt | 8a0429a | 2017-01-06 16:52:47 -0800 | [diff] [blame] | 95 | * "event" (event type) and "payload" attributes already filled in. |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 96 | * |
| 97 | * @param message the message to send |
| 98 | */ |
| 99 | protected void sendMessage(ObjectNode message) { |
| 100 | parent.connection().sendMessage(message); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Allows one request handler to pass the event on to another for |
| 105 | * further processing. |
| 106 | * Note that the message handlers must be defined in the same parent. |
| 107 | * |
| 108 | * @param eventType event type |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 109 | * @param payload message payload |
| 110 | */ |
Simon Hunt | 8a0429a | 2017-01-06 16:52:47 -0800 | [diff] [blame] | 111 | protected void chain(String eventType, ObjectNode payload) { |
| 112 | parent.exec(eventType, payload); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | // =================================================================== |
| 116 | |
| 117 | |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 118 | /** |
| 119 | * Returns the specified node property as a string. |
| 120 | * |
| 121 | * @param node message event |
Simon Hunt | b1ced8e | 2016-05-27 19:34:34 -0700 | [diff] [blame] | 122 | * @param key property name |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 123 | * @return property as a string |
| 124 | */ |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 125 | protected String string(ObjectNode node, String key) { |
| 126 | return JsonUtils.string(node, key); |
| 127 | } |
| 128 | |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 129 | /** |
| 130 | * Returns the specified node property as a string, with a default fallback. |
| 131 | * |
Simon Hunt | b1ced8e | 2016-05-27 19:34:34 -0700 | [diff] [blame] | 132 | * @param node object node |
| 133 | * @param key property name |
| 134 | * @param defValue fallback value if property is absent |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 135 | * @return property as a string |
| 136 | */ |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 137 | protected String string(ObjectNode node, String key, String defValue) { |
| 138 | return JsonUtils.string(node, key, defValue); |
| 139 | } |
| 140 | |
Simon Hunt | b1ced8e | 2016-05-27 19:34:34 -0700 | [diff] [blame] | 141 | /** |
| 142 | * Returns the specified node property as a boolean. More precisely, if |
| 143 | * the value for the given key is the string "true" then this returns true, |
| 144 | * false otherwise. |
| 145 | * |
| 146 | * @param node object node |
| 147 | * @param key property name |
| 148 | * @return property as a boolean |
| 149 | */ |
| 150 | protected boolean bool(ObjectNode node, String key) { |
| 151 | return JsonUtils.bool(node, key); |
| 152 | } |
MaoLu | 937cf42 | 2017-03-03 23:31:46 -0800 | [diff] [blame] | 153 | |
| 154 | /** |
| 155 | * Returns the specified node property as a number. |
| 156 | * |
| 157 | * @param node object node |
| 158 | * @param key property name |
| 159 | * @return property as a number |
| 160 | */ |
| 161 | protected long number(ObjectNode node, String key) { |
| 162 | return JsonUtils.number(node, key); |
| 163 | } |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 164 | } |