Thomas Vachuska | 26be4f3 | 2016-03-31 01:10:27 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
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; |
Thomas Vachuska | 26be4f3 | 2016-03-31 01:10:27 -0700 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableSet; |
| 22 | import org.onosproject.ui.RequestHandler; |
Steven Burrows | 3a9a644 | 2016-05-05 15:31:16 +0100 | [diff] [blame] | 23 | import org.onosproject.ui.UiExtensionService; |
Simon Hunt | 8a0429a | 2017-01-06 16:52:47 -0800 | [diff] [blame] | 24 | import org.onosproject.ui.UiMessageHandler; |
Steven Burrows | 3a9a644 | 2016-05-05 15:31:16 +0100 | [diff] [blame] | 25 | import org.onosproject.ui.UiTopoMap; |
Simon Hunt | 8a0429a | 2017-01-06 16:52:47 -0800 | [diff] [blame] | 26 | import org.onosproject.ui.UiTopoMapFactory; |
Thomas Vachuska | 26be4f3 | 2016-03-31 01:10:27 -0700 | [diff] [blame] | 27 | |
| 28 | import java.util.Collection; |
| 29 | import java.util.List; |
| 30 | |
| 31 | /** |
| 32 | * Message handler for map selection functionality. |
| 33 | */ |
| 34 | class MapSelectorMessageHandler extends UiMessageHandler { |
| 35 | |
| 36 | private static final String MAP_LIST_REQ = "mapSelectorRequest"; |
| 37 | private static final String MAP_LIST_RESP = "mapSelectorResponse"; |
| 38 | |
| 39 | private static final String ORDER = "order"; |
| 40 | private static final String MAPS = "maps"; |
| 41 | private static final String MAP_ID = "id"; |
| 42 | private static final String DESCRIPTION = "description"; |
Steven Burrows | 3a9a644 | 2016-05-05 15:31:16 +0100 | [diff] [blame] | 43 | private static final String FILE_PATH = "filePath"; |
Thomas Vachuska | 26be4f3 | 2016-03-31 01:10:27 -0700 | [diff] [blame] | 44 | private static final String SCALE = "scale"; |
| 45 | |
Thomas Vachuska | 26be4f3 | 2016-03-31 01:10:27 -0700 | [diff] [blame] | 46 | @Override |
| 47 | protected Collection<RequestHandler> createRequestHandlers() { |
| 48 | return ImmutableSet.of( |
| 49 | new MapListHandler() |
| 50 | ); |
| 51 | } |
| 52 | |
| 53 | private final class MapListHandler extends RequestHandler { |
| 54 | private MapListHandler() { |
| 55 | super(MAP_LIST_REQ); |
| 56 | } |
| 57 | |
| 58 | @Override |
Simon Hunt | 8a0429a | 2017-01-06 16:52:47 -0800 | [diff] [blame] | 59 | public void process(ObjectNode payload) { |
| 60 | sendMessage(MAP_LIST_RESP, mapsJson()); |
Thomas Vachuska | 26be4f3 | 2016-03-31 01:10:27 -0700 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | |
| 64 | private ObjectNode mapsJson() { |
Steven Burrows | 3a9a644 | 2016-05-05 15:31:16 +0100 | [diff] [blame] | 65 | |
Thomas Vachuska | 26be4f3 | 2016-03-31 01:10:27 -0700 | [diff] [blame] | 66 | ObjectNode payload = objectNode(); |
| 67 | ArrayNode order = arrayNode(); |
| 68 | ObjectNode maps = objectNode(); |
| 69 | payload.set(ORDER, order); |
| 70 | payload.set(MAPS, maps); |
Steven Burrows | 3a9a644 | 2016-05-05 15:31:16 +0100 | [diff] [blame] | 71 | |
| 72 | UiExtensionService service = get(UiExtensionService.class); |
| 73 | service.getExtensions().forEach(ext -> { |
| 74 | UiTopoMapFactory mapFactory = ext.topoMapFactory(); |
| 75 | |
| 76 | if (mapFactory != null) { |
Simon Hunt | 8add9ee | 2016-09-20 17:05:07 -0700 | [diff] [blame] | 77 | List<UiTopoMap> topoMaps = mapFactory.geoMaps(); |
Steven Burrows | 3a9a644 | 2016-05-05 15:31:16 +0100 | [diff] [blame] | 78 | |
| 79 | topoMaps.forEach(m -> { |
Simon Hunt | 8add9ee | 2016-09-20 17:05:07 -0700 | [diff] [blame] | 80 | maps.set(m.id(), objectNode().put(MAP_ID, m.id()) |
| 81 | .put(DESCRIPTION, m.description()) |
| 82 | .put(FILE_PATH, m.filePath()) |
| 83 | .put(SCALE, m.scale())); |
| 84 | order.add(m.id()); |
Steven Burrows | 3a9a644 | 2016-05-05 15:31:16 +0100 | [diff] [blame] | 85 | }); |
| 86 | } |
Thomas Vachuska | 26be4f3 | 2016-03-31 01:10:27 -0700 | [diff] [blame] | 87 | }); |
Steven Burrows | 3a9a644 | 2016-05-05 15:31:16 +0100 | [diff] [blame] | 88 | |
Thomas Vachuska | 26be4f3 | 2016-03-31 01:10:27 -0700 | [diff] [blame] | 89 | return payload; |
| 90 | } |
Thomas Vachuska | 26be4f3 | 2016-03-31 01:10:27 -0700 | [diff] [blame] | 91 | } |