Thomas Vachuska | 26be4f3 | 2016-03-31 01:10:27 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Thomas Vachuska | 26be4f3 | 2016-03-31 01:10:27 -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 | |
| 17 | package org.onosproject.ui.impl; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import com.google.common.collect.ImmutableList; |
| 22 | import com.google.common.collect.ImmutableSet; |
| 23 | import org.onosproject.ui.RequestHandler; |
| 24 | import org.onosproject.ui.UiMessageHandler; |
| 25 | |
| 26 | import java.util.Collection; |
| 27 | import java.util.List; |
| 28 | |
| 29 | /** |
| 30 | * Message handler for map selection functionality. |
| 31 | */ |
| 32 | class MapSelectorMessageHandler extends UiMessageHandler { |
| 33 | |
| 34 | private static final String MAP_LIST_REQ = "mapSelectorRequest"; |
| 35 | private static final String MAP_LIST_RESP = "mapSelectorResponse"; |
| 36 | |
| 37 | private static final String ORDER = "order"; |
| 38 | private static final String MAPS = "maps"; |
| 39 | private static final String MAP_ID = "id"; |
| 40 | private static final String DESCRIPTION = "description"; |
| 41 | private static final String SCALE = "scale"; |
| 42 | |
| 43 | private static final List<Map> SUPPORTED_MAPS = |
| 44 | ImmutableList.of(new Map("australia", "Australia", 1.0), |
Simon Hunt | 90053ed | 2016-04-06 12:49:02 -0700 | [diff] [blame] | 45 | new Map("ns_america", "North, Central and South America", 0.7), |
| 46 | new Map("s_america", "South America", 0.9), |
| 47 | new Map("usa", "United States", 1.0), |
| 48 | new Map("bayarea", "Bay Area, California", 1.0), |
| 49 | new Map("europe", "Europe", 2.5), |
| 50 | new Map("italy", "Italy", 0.8), |
| 51 | new Map("uk", "United Kingdom and Ireland", 0.6), |
| 52 | new Map("japan", "Japan", 0.8), |
| 53 | new Map("s_korea", "South Korea", 0.75), |
| 54 | new Map("taiwan", "Taiwan", 0.7)); |
Thomas Vachuska | 26be4f3 | 2016-03-31 01:10:27 -0700 | [diff] [blame] | 55 | |
| 56 | @Override |
| 57 | protected Collection<RequestHandler> createRequestHandlers() { |
| 58 | return ImmutableSet.of( |
| 59 | new MapListHandler() |
| 60 | ); |
| 61 | } |
| 62 | |
| 63 | private final class MapListHandler extends RequestHandler { |
| 64 | private MapListHandler() { |
| 65 | super(MAP_LIST_REQ); |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public void process(long sid, ObjectNode payload) { |
| 70 | sendMessage(MAP_LIST_RESP, 0, mapsJson()); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | private ObjectNode mapsJson() { |
| 75 | ObjectNode payload = objectNode(); |
| 76 | ArrayNode order = arrayNode(); |
| 77 | ObjectNode maps = objectNode(); |
| 78 | payload.set(ORDER, order); |
| 79 | payload.set(MAPS, maps); |
| 80 | SUPPORTED_MAPS.forEach(m -> { |
| 81 | maps.set(m.id, objectNode().put(MAP_ID, m.id) |
| 82 | .put(DESCRIPTION, m.description) |
| 83 | .put(SCALE, m.scale)); |
| 84 | order.add(m.id); |
| 85 | }); |
| 86 | return payload; |
| 87 | } |
| 88 | |
| 89 | private static final class Map { |
| 90 | private final String id; |
| 91 | private final String description; |
| 92 | private final double scale; |
| 93 | |
| 94 | private Map(String id, String description, double scale) { |
| 95 | this.id = id; |
| 96 | this.description = description; |
| 97 | this.scale = scale; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | } |