blob: 20c7cf186579f5ade32c81b49a71792dc52f4e57 [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;
21import com.google.common.collect.ImmutableList;
22import com.google.common.collect.ImmutableSet;
23import org.onosproject.ui.RequestHandler;
24import org.onosproject.ui.UiMessageHandler;
25
26import java.util.Collection;
27import java.util.List;
28
29/**
30 * Message handler for map selection functionality.
31 */
32class 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 Hunt90053ed2016-04-06 12:49:02 -070045 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 Vachuska26be4f32016-03-31 01:10:27 -070055
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}