Sean Condon | a36f65c | 2019-05-20 08:21:41 +0100 | [diff] [blame^] | 1 | #set( $symbol_pound = '#' ) |
| 2 | #set( $symbol_dollar = '$' ) |
| 3 | #set( $symbol_escape = '\' ) |
| 4 | /* |
| 5 | * Copyright ${year}-present Open Networking Foundation |
| 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; |
| 25 | import org.slf4j.Logger; |
| 26 | import org.slf4j.LoggerFactory; |
| 27 | |
| 28 | import java.util.Collection; |
| 29 | |
| 30 | /** |
| 31 | * Skeletal ONOS UI Custom-View message handler. |
| 32 | */ |
| 33 | public class AppUiMessageHandler extends UiMessageHandler { |
| 34 | |
| 35 | private static final String SAMPLE_CUSTOM_DATA_REQ = "sampleCustomDataRequest"; |
| 36 | private static final String SAMPLE_CUSTOM_DATA_RESP = "sampleCustomDataResponse"; |
| 37 | |
| 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"; |
| 43 | |
| 44 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 45 | |
| 46 | private long someNumber = 1; |
| 47 | private long someIncrement = 1; |
| 48 | |
| 49 | @Override |
| 50 | protected Collection<RequestHandler> createRequestHandlers() { |
| 51 | return ImmutableSet.of( |
| 52 | new SampleCustomDataRequestHandler() |
| 53 | ); |
| 54 | } |
| 55 | |
| 56 | // handler for sample data requests |
| 57 | private final class SampleCustomDataRequestHandler extends RequestHandler { |
| 58 | |
| 59 | private SampleCustomDataRequestHandler() { |
| 60 | super(SAMPLE_CUSTOM_DATA_REQ); |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public void process(ObjectNode payload) { |
| 65 | someIncrement++; |
| 66 | someNumber += someIncrement; |
| 67 | log.debug("Computing data for {}...", someNumber); |
| 68 | |
| 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)); |
| 74 | sendMessage(SAMPLE_CUSTOM_DATA_RESP, result); |
| 75 | } |
| 76 | } |
| 77 | } |