blob: fdb24670acaa0a2f75f26efcac49e6830010287d [file] [log] [blame]
Thomas Vachuska26be4f32016-03-31 01:10:27 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Thomas Vachuska26be4f32016-03-31 01:10:27 -07003 *
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
17package org.onosproject.ui.impl;
18
19import com.fasterxml.jackson.databind.node.ArrayNode;
20import com.fasterxml.jackson.databind.node.ObjectNode;
Thomas Vachuska26be4f32016-03-31 01:10:27 -070021import com.google.common.collect.ImmutableSet;
22import org.onosproject.ui.RequestHandler;
23import org.onosproject.ui.UiMessageHandler;
Steven Burrows3a9a6442016-05-05 15:31:16 +010024import org.onosproject.ui.UiExtensionService;
25import org.onosproject.ui.UiTopoMapFactory;
26import org.onosproject.ui.UiTopoMap;
Thomas Vachuska26be4f32016-03-31 01:10:27 -070027
28import java.util.Collection;
29import java.util.List;
30
31/**
32 * Message handler for map selection functionality.
33 */
34class 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 Burrows3a9a6442016-05-05 15:31:16 +010043 private static final String FILE_PATH = "filePath";
Thomas Vachuska26be4f32016-03-31 01:10:27 -070044 private static final String SCALE = "scale";
45
Thomas Vachuska26be4f32016-03-31 01:10:27 -070046 @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
59 public void process(long sid, ObjectNode payload) {
60 sendMessage(MAP_LIST_RESP, 0, mapsJson());
61 }
62 }
63
64 private ObjectNode mapsJson() {
Steven Burrows3a9a6442016-05-05 15:31:16 +010065
Thomas Vachuska26be4f32016-03-31 01:10:27 -070066 ObjectNode payload = objectNode();
67 ArrayNode order = arrayNode();
68 ObjectNode maps = objectNode();
69 payload.set(ORDER, order);
70 payload.set(MAPS, maps);
Steven Burrows3a9a6442016-05-05 15:31:16 +010071
72 UiExtensionService service = get(UiExtensionService.class);
73 service.getExtensions().forEach(ext -> {
74 UiTopoMapFactory mapFactory = ext.topoMapFactory();
75
76 if (mapFactory != null) {
Simon Hunt8add9ee2016-09-20 17:05:07 -070077 List<UiTopoMap> topoMaps = mapFactory.geoMaps();
Steven Burrows3a9a6442016-05-05 15:31:16 +010078
79 topoMaps.forEach(m -> {
Simon Hunt8add9ee2016-09-20 17:05:07 -070080 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 Burrows3a9a6442016-05-05 15:31:16 +010085 });
86 }
Thomas Vachuska26be4f32016-03-31 01:10:27 -070087 });
Steven Burrows3a9a6442016-05-05 15:31:16 +010088
Thomas Vachuska26be4f32016-03-31 01:10:27 -070089 return payload;
90 }
Thomas Vachuska26be4f32016-03-31 01:10:27 -070091}