Simon Hunt | e11ce5a | 2015-07-21 12:11:04 -0700 | [diff] [blame] | 1 | #set( $symbol_pound = '#' ) |
| 2 | #set( $symbol_dollar = '$' ) |
| 3 | #set( $symbol_escape = '\' ) |
| 4 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 5 | * Copyright ${year}-present Open Networking Foundation |
Simon Hunt | e11ce5a | 2015-07-21 12:11:04 -0700 | [diff] [blame] | 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | * you may not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
| 18 | */ |
| 19 | package ${package}; |
| 20 | |
| 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 22 | import com.google.common.collect.ImmutableSet; |
| 23 | import org.onosproject.ui.RequestHandler; |
| 24 | import org.onosproject.ui.UiMessageHandler; |
Simon Hunt | e11ce5a | 2015-07-21 12:11:04 -0700 | [diff] [blame] | 25 | import org.slf4j.Logger; |
| 26 | import org.slf4j.LoggerFactory; |
| 27 | |
Simon Hunt | e11ce5a | 2015-07-21 12:11:04 -0700 | [diff] [blame] | 28 | import java.util.Collection; |
Simon Hunt | e11ce5a | 2015-07-21 12:11:04 -0700 | [diff] [blame] | 29 | |
| 30 | /** |
Simon Hunt | 84f4c2a | 2015-09-23 17:52:45 -0700 | [diff] [blame] | 31 | * Skeletal ONOS UI Custom-View message handler. |
Simon Hunt | e11ce5a | 2015-07-21 12:11:04 -0700 | [diff] [blame] | 32 | */ |
| 33 | public class AppUiMessageHandler extends UiMessageHandler { |
| 34 | |
Simon Hunt | 84f4c2a | 2015-09-23 17:52:45 -0700 | [diff] [blame] | 35 | private static final String SAMPLE_CUSTOM_DATA_REQ = "sampleCustomDataRequest"; |
| 36 | private static final String SAMPLE_CUSTOM_DATA_RESP = "sampleCustomDataResponse"; |
Simon Hunt | e11ce5a | 2015-07-21 12:11:04 -0700 | [diff] [blame] | 37 | |
Simon Hunt | 89577e5 | 2015-09-25 18:15:19 -0700 | [diff] [blame] | 38 | private static final String NUMBER = "number"; |
| 39 | private static final String SQUARE = "square"; |
| 40 | private static final String CUBE = "cube"; |
| 41 | private static final String MESSAGE = "message"; |
| 42 | private static final String MSG_FORMAT = "Next incrememt is %d units"; |
Simon Hunt | e11ce5a | 2015-07-21 12:11:04 -0700 | [diff] [blame] | 43 | |
| 44 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 45 | |
Simon Hunt | 89577e5 | 2015-09-25 18:15:19 -0700 | [diff] [blame] | 46 | private long someNumber = 1; |
| 47 | private long someIncrement = 1; |
Simon Hunt | e11ce5a | 2015-07-21 12:11:04 -0700 | [diff] [blame] | 48 | |
| 49 | @Override |
| 50 | protected Collection<RequestHandler> createRequestHandlers() { |
| 51 | return ImmutableSet.of( |
Simon Hunt | 89577e5 | 2015-09-25 18:15:19 -0700 | [diff] [blame] | 52 | new SampleCustomDataRequestHandler() |
Simon Hunt | e11ce5a | 2015-07-21 12:11:04 -0700 | [diff] [blame] | 53 | ); |
| 54 | } |
| 55 | |
Simon Hunt | 89577e5 | 2015-09-25 18:15:19 -0700 | [diff] [blame] | 56 | // handler for sample data requests |
| 57 | private final class SampleCustomDataRequestHandler extends RequestHandler { |
Simon Hunt | e11ce5a | 2015-07-21 12:11:04 -0700 | [diff] [blame] | 58 | |
Simon Hunt | 84f4c2a | 2015-09-23 17:52:45 -0700 | [diff] [blame] | 59 | private SampleCustomDataRequestHandler() { |
Simon Hunt | 89577e5 | 2015-09-25 18:15:19 -0700 | [diff] [blame] | 60 | super(SAMPLE_CUSTOM_DATA_REQ); |
Simon Hunt | e11ce5a | 2015-07-21 12:11:04 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | @Override |
Ray Milkey | 2c83565 | 2017-01-09 12:08:00 -0800 | [diff] [blame] | 64 | public void process(ObjectNode payload) { |
Simon Hunt | 89577e5 | 2015-09-25 18:15:19 -0700 | [diff] [blame] | 65 | someIncrement++; |
| 66 | someNumber += someIncrement; |
| 67 | log.debug("Computing data for {}...", someNumber); |
Simon Hunt | e11ce5a | 2015-07-21 12:11:04 -0700 | [diff] [blame] | 68 | |
Simon Hunt | 89577e5 | 2015-09-25 18:15:19 -0700 | [diff] [blame] | 69 | ObjectNode result = objectNode(); |
| 70 | result.put(NUMBER, someNumber); |
| 71 | result.put(SQUARE, someNumber * someNumber); |
| 72 | result.put(CUBE, someNumber * someNumber * someNumber); |
| 73 | result.put(MESSAGE, String.format(MSG_FORMAT, someIncrement + 1)); |
Ray Milkey | 2c83565 | 2017-01-09 12:08:00 -0800 | [diff] [blame] | 74 | sendMessage(SAMPLE_CUSTOM_DATA_RESP, result); |
Simon Hunt | e11ce5a | 2015-07-21 12:11:04 -0700 | [diff] [blame] | 75 | } |
| 76 | } |
Ray Milkey | 2c83565 | 2017-01-09 12:08:00 -0800 | [diff] [blame] | 77 | } |