blob: ad60281f8b64708165829aacd35ff4de397c3765 [file] [log] [blame]
Simon Huntf59d36b2016-10-04 19:05:53 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Simon Huntf59d36b2016-10-04 19:05:53 -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.net.config.basics;
18
Simon Huntc4ca7102017-04-08 22:28:04 -070019import com.fasterxml.jackson.databind.JsonNode;
20import com.fasterxml.jackson.databind.node.ObjectNode;
Simon Hunt4f3a4072016-10-17 17:52:11 -070021import com.google.common.base.MoreObjects;
Simon Huntf59d36b2016-10-04 19:05:53 -070022import org.onosproject.net.DeviceId;
Simon Huntf59d36b2016-10-04 19:05:53 -070023import org.onosproject.net.region.Region;
24import org.onosproject.net.region.RegionId;
Simon Huntc4ca7102017-04-08 22:28:04 -070025import org.onosproject.ui.topo.LayoutLocation;
Simon Huntf59d36b2016-10-04 19:05:53 -070026
Simon Huntc4ca7102017-04-08 22:28:04 -070027import java.util.ArrayList;
28import java.util.Iterator;
Simon Huntf59d36b2016-10-04 19:05:53 -070029import java.util.List;
Simon Huntc4ca7102017-04-08 22:28:04 -070030import java.util.Map;
Simon Hunt4f3a4072016-10-17 17:52:11 -070031import java.util.Set;
Simon Huntf59d36b2016-10-04 19:05:53 -070032
Simon Huntc4ca7102017-04-08 22:28:04 -070033import static org.onosproject.ui.topo.LayoutLocation.layoutLocation;
34
Simon Huntf59d36b2016-10-04 19:05:53 -070035/**
36 * Basic configuration for network regions.
37 */
Simon Hunt53612212016-12-04 17:19:52 -080038public final class BasicRegionConfig extends BasicElementConfig<RegionId> {
Simon Huntf59d36b2016-10-04 19:05:53 -070039
40 private static final String TYPE = "type";
41 private static final String DEVICES = "devices";
Simon Huntc4ca7102017-04-08 22:28:04 -070042 private static final String LOC_IN_PEERS = "locInPeers";
43
44 private static final String LOC_TYPE = "locType";
45 private static final String LAT_OR_Y = "latOrY";
46 private static final String LONG_OR_X = "LongOrX";
47
Simon Huntf59d36b2016-10-04 19:05:53 -070048 @Override
49 public boolean isValid() {
Jordan Halterman83949a12017-06-21 10:35:38 -070050 // Validate type/devices
51 type();
52 devices();
53
54 return super.isValid()
55 && hasOnlyFields(ALLOWED, NAME, LATITUDE, LONGITUDE, UI_TYPE,
56 RACK_ADDRESS, OWNER, TYPE, DEVICES, LOC_IN_PEERS);
Simon Hunt4f3a4072016-10-17 17:52:11 -070057 }
58
59 @Override
60 public String toString() {
61 return MoreObjects.toStringHelper(this)
Simon Hunt53612212016-12-04 17:19:52 -080062 .add(NAME, name())
63 .add(TYPE, type())
64 .add(UI_TYPE, uiType())
65 .add(LATITUDE, latitude())
66 .add(LONGITUDE, longitude())
67 .add(DEVICES, devices())
Simon Hunt4f3a4072016-10-17 17:52:11 -070068 .toString();
69 }
70
71 /**
Simon Huntf59d36b2016-10-04 19:05:53 -070072 * Returns the region type.
73 *
74 * @return the region type
75 */
Simon Hunt4f3a4072016-10-17 17:52:11 -070076 public Region.Type type() {
Simon Huntf59d36b2016-10-04 19:05:53 -070077 String t = get(TYPE, null);
78 return t == null ? null : regionTypeFor(t);
79 }
80
81 private Region.Type regionTypeFor(String t) {
82 try {
83 return Region.Type.valueOf(t.toUpperCase());
84 } catch (IllegalArgumentException ignored) {
85 }
86 return null;
87 }
88
89 /**
Simon Hunt4f3a4072016-10-17 17:52:11 -070090 * Sets the region type.
91 *
92 * @param type the region type, or null to unset
93 * @return the config of the region
94 */
95 public BasicRegionConfig type(Region.Type type) {
96 String t = type == null ? null : type.name().toLowerCase();
97 return (BasicRegionConfig) setOrClear(TYPE, t);
98 }
99
100 /**
Simon Huntf59d36b2016-10-04 19:05:53 -0700101 * Returns the identities of the devices in this region.
102 *
103 * @return list of device identifiers
104 */
Simon Hunt4f3a4072016-10-17 17:52:11 -0700105 public List<DeviceId> devices() {
106 return object.has(DEVICES) ? getList(DEVICES, DeviceId::deviceId) : null;
Simon Huntf59d36b2016-10-04 19:05:53 -0700107 }
108
Simon Hunt4f3a4072016-10-17 17:52:11 -0700109 /**
110 * Sets the devices of this region.
111 *
112 * @param devices the device identifiers, or null to unset
113 * @return the config of the region
114 */
115 public BasicRegionConfig devices(Set<DeviceId> devices) {
116 return (BasicRegionConfig) setOrClear(DEVICES, devices);
117 }
Simon Huntc4ca7102017-04-08 22:28:04 -0700118
119
120 // Requires some custom json-node handling for maintaining a map
121 // of peer location data...
122
123 /**
124 * Adds a peer location mapping to this region.
125 *
126 * @param peerId the region ID of the peer
127 * @param locType the type of location (geo/grid)
128 * @param latOrY geo latitude / grid y-coord
129 * @param longOrX geo longitude / grid x-coord
130 * @return self
131 */
132 public BasicRegionConfig addPeerLocMapping(String peerId, String locType,
133 Double latOrY, Double longOrX) {
134 ObjectNode map = getLocMap();
135 map.set(peerId, makeLocation(locType, latOrY, longOrX));
136 return this;
137 }
138
139 private JsonNode makeLocation(String locType, Double latOrY, Double longOrX) {
140 return mapper.createObjectNode()
141 .put(LOC_TYPE, locType)
142 .put(LAT_OR_Y, latOrY)
143 .put(LONG_OR_X, longOrX);
144 }
145
146 private ObjectNode getLocMap() {
147 ObjectNode locMap = (ObjectNode) object.get(LOC_IN_PEERS);
148 if (locMap == null) {
149 locMap = mapper.createObjectNode();
150 object.set(LOC_IN_PEERS, locMap);
151 }
152 return locMap;
153 }
154
155 /**
156 * Returns the list of layout location mappings for where peer region nodes
157 * should be placed on the layout when viewing this region.
158 *
159 * @return list of peer node locations
160 */
161 public List<LayoutLocation> getMappings() {
162 List<LayoutLocation> mappings = new ArrayList<>();
163 ObjectNode map = (ObjectNode) object.get(LOC_IN_PEERS);
164 if (map != null) {
165 for (Iterator<Map.Entry<String, JsonNode>> it = map.fields(); it.hasNext();) {
166 Map.Entry<String, JsonNode> entry = it.next();
167 String peerId = entry.getKey();
168 ObjectNode data = (ObjectNode) entry.getValue();
169
170 String lt = data.get(LOC_TYPE).asText();
171 double latY = data.get(LAT_OR_Y).asDouble();
172 double longX = data.get(LONG_OR_X).asDouble();
173
174 mappings.add(layoutLocation(peerId, lt, latY, longX));
175 }
176 }
177 return mappings;
178 }
Simon Huntf59d36b2016-10-04 19:05:53 -0700179}