Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -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.net.config.basics; |
| 18 | |
Simon Hunt | c4ca710 | 2017-04-08 22:28:04 -0700 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.JsonNode; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 21 | import com.google.common.base.MoreObjects; |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 22 | import org.onosproject.net.DeviceId; |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 23 | import org.onosproject.net.region.Region; |
| 24 | import org.onosproject.net.region.RegionId; |
Simon Hunt | c4ca710 | 2017-04-08 22:28:04 -0700 | [diff] [blame] | 25 | import org.onosproject.ui.topo.LayoutLocation; |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 26 | |
Simon Hunt | c4ca710 | 2017-04-08 22:28:04 -0700 | [diff] [blame] | 27 | import java.util.ArrayList; |
| 28 | import java.util.Iterator; |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 29 | import java.util.List; |
Simon Hunt | c4ca710 | 2017-04-08 22:28:04 -0700 | [diff] [blame] | 30 | import java.util.Map; |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 31 | import java.util.Set; |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 32 | |
Simon Hunt | c4ca710 | 2017-04-08 22:28:04 -0700 | [diff] [blame] | 33 | import static org.onosproject.ui.topo.LayoutLocation.layoutLocation; |
| 34 | |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 35 | /** |
| 36 | * Basic configuration for network regions. |
| 37 | */ |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 38 | public final class BasicRegionConfig extends BasicElementConfig<RegionId> { |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 39 | |
| 40 | private static final String TYPE = "type"; |
| 41 | private static final String DEVICES = "devices"; |
Simon Hunt | c4ca710 | 2017-04-08 22:28:04 -0700 | [diff] [blame] | 42 | 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 Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 48 | @Override |
| 49 | public boolean isValid() { |
Jordan Halterman | 83949a1 | 2017-06-21 10:35:38 -0700 | [diff] [blame] | 50 | // 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 Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public String toString() { |
| 61 | return MoreObjects.toStringHelper(this) |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 62 | .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 Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 68 | .toString(); |
| 69 | } |
| 70 | |
| 71 | /** |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 72 | * Returns the region type. |
| 73 | * |
| 74 | * @return the region type |
| 75 | */ |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 76 | public Region.Type type() { |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 77 | 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 Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 90 | * 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 Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 101 | * Returns the identities of the devices in this region. |
| 102 | * |
| 103 | * @return list of device identifiers |
| 104 | */ |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 105 | public List<DeviceId> devices() { |
| 106 | return object.has(DEVICES) ? getList(DEVICES, DeviceId::deviceId) : null; |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 109 | /** |
| 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 Hunt | c4ca710 | 2017-04-08 22:28:04 -0700 | [diff] [blame] | 118 | |
| 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 Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 179 | } |