blob: 7a0e8d6cc842f05fb6cbcaf58b91b7949c4c01a2 [file] [log] [blame]
Simon Huntf59d36b2016-10-04 19:05:53 -07001/*
2 * Copyright 2016-present Open Networking Laboratory
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
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
49 @Override
50 public boolean isValid() {
Simon Hunt53612212016-12-04 17:19:52 -080051 return hasOnlyFields(ALLOWED, NAME, LATITUDE, LONGITUDE, UI_TYPE,
Simon Huntc4ca7102017-04-08 22:28:04 -070052 RACK_ADDRESS, OWNER, TYPE, DEVICES, LOC_IN_PEERS);
Simon Hunt4f3a4072016-10-17 17:52:11 -070053 }
54
55 @Override
56 public String toString() {
57 return MoreObjects.toStringHelper(this)
Simon Hunt53612212016-12-04 17:19:52 -080058 .add(NAME, name())
59 .add(TYPE, type())
60 .add(UI_TYPE, uiType())
61 .add(LATITUDE, latitude())
62 .add(LONGITUDE, longitude())
63 .add(DEVICES, devices())
Simon Hunt4f3a4072016-10-17 17:52:11 -070064 .toString();
65 }
66
67 /**
Simon Huntf59d36b2016-10-04 19:05:53 -070068 * Returns the region type.
69 *
70 * @return the region type
71 */
Simon Hunt4f3a4072016-10-17 17:52:11 -070072 public Region.Type type() {
Simon Huntf59d36b2016-10-04 19:05:53 -070073 String t = get(TYPE, null);
74 return t == null ? null : regionTypeFor(t);
75 }
76
77 private Region.Type regionTypeFor(String t) {
78 try {
79 return Region.Type.valueOf(t.toUpperCase());
80 } catch (IllegalArgumentException ignored) {
81 }
82 return null;
83 }
84
85 /**
Simon Hunt4f3a4072016-10-17 17:52:11 -070086 * Sets the region type.
87 *
88 * @param type the region type, or null to unset
89 * @return the config of the region
90 */
91 public BasicRegionConfig type(Region.Type type) {
92 String t = type == null ? null : type.name().toLowerCase();
93 return (BasicRegionConfig) setOrClear(TYPE, t);
94 }
95
96 /**
Simon Huntf59d36b2016-10-04 19:05:53 -070097 * Returns the identities of the devices in this region.
98 *
99 * @return list of device identifiers
100 */
Simon Hunt4f3a4072016-10-17 17:52:11 -0700101 public List<DeviceId> devices() {
102 return object.has(DEVICES) ? getList(DEVICES, DeviceId::deviceId) : null;
Simon Huntf59d36b2016-10-04 19:05:53 -0700103 }
104
Simon Hunt4f3a4072016-10-17 17:52:11 -0700105 /**
106 * Sets the devices of this region.
107 *
108 * @param devices the device identifiers, or null to unset
109 * @return the config of the region
110 */
111 public BasicRegionConfig devices(Set<DeviceId> devices) {
112 return (BasicRegionConfig) setOrClear(DEVICES, devices);
113 }
Simon Huntc4ca7102017-04-08 22:28:04 -0700114
115
116 // Requires some custom json-node handling for maintaining a map
117 // of peer location data...
118
119 /**
120 * Adds a peer location mapping to this region.
121 *
122 * @param peerId the region ID of the peer
123 * @param locType the type of location (geo/grid)
124 * @param latOrY geo latitude / grid y-coord
125 * @param longOrX geo longitude / grid x-coord
126 * @return self
127 */
128 public BasicRegionConfig addPeerLocMapping(String peerId, String locType,
129 Double latOrY, Double longOrX) {
130 ObjectNode map = getLocMap();
131 map.set(peerId, makeLocation(locType, latOrY, longOrX));
132 return this;
133 }
134
135 private JsonNode makeLocation(String locType, Double latOrY, Double longOrX) {
136 return mapper.createObjectNode()
137 .put(LOC_TYPE, locType)
138 .put(LAT_OR_Y, latOrY)
139 .put(LONG_OR_X, longOrX);
140 }
141
142 private ObjectNode getLocMap() {
143 ObjectNode locMap = (ObjectNode) object.get(LOC_IN_PEERS);
144 if (locMap == null) {
145 locMap = mapper.createObjectNode();
146 object.set(LOC_IN_PEERS, locMap);
147 }
148 return locMap;
149 }
150
151 /**
152 * Returns the list of layout location mappings for where peer region nodes
153 * should be placed on the layout when viewing this region.
154 *
155 * @return list of peer node locations
156 */
157 public List<LayoutLocation> getMappings() {
158 List<LayoutLocation> mappings = new ArrayList<>();
159 ObjectNode map = (ObjectNode) object.get(LOC_IN_PEERS);
160 if (map != null) {
161 for (Iterator<Map.Entry<String, JsonNode>> it = map.fields(); it.hasNext();) {
162 Map.Entry<String, JsonNode> entry = it.next();
163 String peerId = entry.getKey();
164 ObjectNode data = (ObjectNode) entry.getValue();
165
166 String lt = data.get(LOC_TYPE).asText();
167 double latY = data.get(LAT_OR_Y).asDouble();
168 double longX = data.get(LONG_OR_X).asDouble();
169
170 mappings.add(layoutLocation(peerId, lt, latY, longX));
171 }
172 }
173 return mappings;
174 }
Simon Huntf59d36b2016-10-04 19:05:53 -0700175}