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