Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [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 | |
| 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 | * |
| 56 | * @param sid message sequence identifier |
| 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 | |
| 63 | |
| 64 | // =================================================================== |
| 65 | // === Convenience methods... |
| 66 | |
| 67 | /** |
| 68 | * Returns implementation of the specified service class. |
| 69 | * |
| 70 | * @param serviceClass service class |
| 71 | * @param <T> type of service |
| 72 | * @return implementation class |
| 73 | * @throws org.onlab.osgi.ServiceNotFoundException if no implementation found |
| 74 | */ |
| 75 | protected <T> T get(Class<T> serviceClass) { |
| 76 | return parent.directory().get(serviceClass); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Sends a message back to the client. |
| 81 | * |
| 82 | * @param eventType message event type |
| 83 | * @param sid message sequence identifier |
| 84 | * @param payload message payload |
| 85 | */ |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 86 | // TODO: remove sid from signature |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 87 | protected void sendMessage(String eventType, long sid, ObjectNode payload) { |
| 88 | parent.connection().sendMessage(eventType, sid, payload); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Sends a message back to the client. |
| 93 | * Here, the message is preformatted; the assumption is it has its |
| 94 | * eventType, sid and payload attributes already filled in. |
| 95 | * |
| 96 | * @param message the message to send |
| 97 | */ |
| 98 | protected void sendMessage(ObjectNode message) { |
| 99 | parent.connection().sendMessage(message); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Allows one request handler to pass the event on to another for |
| 104 | * further processing. |
| 105 | * Note that the message handlers must be defined in the same parent. |
| 106 | * |
| 107 | * @param eventType event type |
| 108 | * @param sid sequence identifier |
| 109 | * @param payload message payload |
| 110 | */ |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 111 | // TODO: remove sid from signature |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 112 | protected void chain(String eventType, long sid, ObjectNode payload) { |
| 113 | parent.exec(eventType, sid, payload); |
| 114 | } |
| 115 | |
| 116 | // =================================================================== |
| 117 | |
| 118 | |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 119 | /** |
| 120 | * Returns the specified node property as a string. |
| 121 | * |
| 122 | * @param node message event |
| 123 | * @param key property name |
| 124 | * @return property as a string |
| 125 | */ |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 126 | protected String string(ObjectNode node, String key) { |
| 127 | return JsonUtils.string(node, key); |
| 128 | } |
| 129 | |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 130 | /** |
| 131 | * Returns the specified node property as a string, with a default fallback. |
| 132 | * |
| 133 | * @param node object node |
| 134 | * @param key property name |
| 135 | * @param defValue fallback value if property is absent |
| 136 | * @return property as a string |
| 137 | */ |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 138 | protected String string(ObjectNode node, String key, String defValue) { |
| 139 | return JsonUtils.string(node, key, defValue); |
| 140 | } |
| 141 | |
| 142 | } |