Thomas Vachuska | 4844808 | 2016-02-19 22:14:54 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Thomas Vachuska | 4844808 | 2016-02-19 22:14:54 -0800 | [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.region.impl; |
| 18 | |
Simon Hunt | 1bee529 | 2016-10-14 11:02:33 -0700 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableSet; |
Simon Hunt | 8f60ff8 | 2017-04-24 17:19:30 -0700 | [diff] [blame] | 20 | import org.onlab.util.ItemNotFoundException; |
Thomas Vachuska | 4844808 | 2016-02-19 22:14:54 -0800 | [diff] [blame] | 21 | import org.onosproject.cluster.NodeId; |
| 22 | import org.onosproject.event.AbstractListenerManager; |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 23 | import org.onosproject.net.Annotations; |
| 24 | import org.onosproject.net.DefaultAnnotations; |
Thomas Vachuska | 4844808 | 2016-02-19 22:14:54 -0800 | [diff] [blame] | 25 | import org.onosproject.net.DeviceId; |
Steven Burrows | 19e6e4f | 2016-10-05 13:27:07 -0500 | [diff] [blame] | 26 | import org.onosproject.net.HostId; |
Simon Hunt | 8f60ff8 | 2017-04-24 17:19:30 -0700 | [diff] [blame] | 27 | import org.onosproject.net.config.NetworkConfigEvent; |
| 28 | import org.onosproject.net.config.NetworkConfigListener; |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 29 | import org.onosproject.net.config.NetworkConfigService; |
| 30 | import org.onosproject.net.config.basics.BasicElementConfig; |
| 31 | import org.onosproject.net.config.basics.BasicRegionConfig; |
Thomas Vachuska | 4844808 | 2016-02-19 22:14:54 -0800 | [diff] [blame] | 32 | import org.onosproject.net.region.Region; |
| 33 | import org.onosproject.net.region.RegionAdminService; |
| 34 | import org.onosproject.net.region.RegionEvent; |
| 35 | import org.onosproject.net.region.RegionId; |
| 36 | import org.onosproject.net.region.RegionListener; |
| 37 | import org.onosproject.net.region.RegionService; |
| 38 | import org.onosproject.net.region.RegionStore; |
| 39 | import org.onosproject.net.region.RegionStoreDelegate; |
Simon Hunt | 8f60ff8 | 2017-04-24 17:19:30 -0700 | [diff] [blame] | 40 | import org.onosproject.ui.topo.LayoutLocation; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 41 | import org.osgi.service.component.annotations.Activate; |
| 42 | import org.osgi.service.component.annotations.Component; |
| 43 | import org.osgi.service.component.annotations.Deactivate; |
| 44 | import org.osgi.service.component.annotations.Reference; |
| 45 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Thomas Vachuska | 4844808 | 2016-02-19 22:14:54 -0800 | [diff] [blame] | 46 | import org.slf4j.Logger; |
| 47 | |
| 48 | import java.util.Collection; |
| 49 | import java.util.List; |
| 50 | import java.util.Set; |
| 51 | |
| 52 | import static com.google.common.base.Preconditions.checkNotNull; |
| 53 | import static com.google.common.base.Preconditions.checkState; |
| 54 | import static com.google.common.collect.ImmutableList.of; |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 55 | import static org.onosproject.security.AppGuard.checkPermission; |
| 56 | import static org.onosproject.security.AppPermission.Type.REGION_READ; |
Simon Hunt | 8f60ff8 | 2017-04-24 17:19:30 -0700 | [diff] [blame] | 57 | import static org.onosproject.ui.topo.LayoutLocation.toCompactListString; |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 58 | import static org.slf4j.LoggerFactory.getLogger; |
Thomas Vachuska | 4844808 | 2016-02-19 22:14:54 -0800 | [diff] [blame] | 59 | |
| 60 | /** |
| 61 | * Provides implementation of the region service APIs. |
| 62 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 63 | @Component(immediate = true, service = {RegionAdminService.class, RegionService.class}) |
Thomas Vachuska | 4844808 | 2016-02-19 22:14:54 -0800 | [diff] [blame] | 64 | public class RegionManager extends AbstractListenerManager<RegionEvent, RegionListener> |
| 65 | implements RegionAdminService, RegionService { |
| 66 | |
| 67 | private static final String REGION_ID_NULL = "Region ID cannot be null"; |
| 68 | private static final String REGION_TYPE_NULL = "Region type cannot be null"; |
| 69 | private static final String DEVICE_ID_NULL = "Device ID cannot be null"; |
| 70 | private static final String DEVICE_IDS_NULL = "Device IDs cannot be null"; |
| 71 | private static final String DEVICE_IDS_EMPTY = "Device IDs cannot be empty"; |
| 72 | private static final String NAME_NULL = "Name cannot be null"; |
| 73 | |
Simon Hunt | 8f60ff8 | 2017-04-24 17:19:30 -0700 | [diff] [blame] | 74 | private static final String PEER_LOCATIONS = "peerLocations"; |
| 75 | |
Thomas Vachuska | 4844808 | 2016-02-19 22:14:54 -0800 | [diff] [blame] | 76 | private final Logger log = getLogger(getClass()); |
| 77 | |
Simon Hunt | 8f60ff8 | 2017-04-24 17:19:30 -0700 | [diff] [blame] | 78 | private final NetworkConfigListener networkConfigListener = |
| 79 | new InternalNetworkConfigListener(); |
| 80 | |
Thomas Vachuska | 4844808 | 2016-02-19 22:14:54 -0800 | [diff] [blame] | 81 | private RegionStoreDelegate delegate = this::post; |
| 82 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 83 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | 4844808 | 2016-02-19 22:14:54 -0800 | [diff] [blame] | 84 | protected RegionStore store; |
| 85 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 86 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 87 | protected NetworkConfigService networkConfigService; |
| 88 | |
Thomas Vachuska | 4844808 | 2016-02-19 22:14:54 -0800 | [diff] [blame] | 89 | @Activate |
| 90 | public void activate() { |
| 91 | store.setDelegate(delegate); |
| 92 | eventDispatcher.addSink(RegionEvent.class, listenerRegistry); |
Simon Hunt | 8f60ff8 | 2017-04-24 17:19:30 -0700 | [diff] [blame] | 93 | networkConfigService.addListener(networkConfigListener); |
Thomas Vachuska | 4844808 | 2016-02-19 22:14:54 -0800 | [diff] [blame] | 94 | log.info("Started"); |
| 95 | } |
| 96 | |
| 97 | @Deactivate |
| 98 | public void deactivate() { |
| 99 | store.unsetDelegate(delegate); |
Simon Hunt | 8f60ff8 | 2017-04-24 17:19:30 -0700 | [diff] [blame] | 100 | networkConfigService.removeListener(networkConfigListener); |
Thomas Vachuska | 4844808 | 2016-02-19 22:14:54 -0800 | [diff] [blame] | 101 | eventDispatcher.removeSink(RegionEvent.class); |
| 102 | log.info("Stopped"); |
| 103 | } |
| 104 | |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 105 | private String dstr(double d) { |
| 106 | return Double.toString(d); |
| 107 | } |
| 108 | |
| 109 | private Annotations genAnnots(RegionId id) { |
| 110 | BasicRegionConfig cfg = |
| 111 | networkConfigService.getConfig(id, BasicRegionConfig.class); |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 112 | if (cfg == null) { |
| 113 | return DefaultAnnotations.builder().build(); |
| 114 | } |
Simon Hunt | 8f60ff8 | 2017-04-24 17:19:30 -0700 | [diff] [blame] | 115 | return genAnnots(cfg, id); |
| 116 | } |
| 117 | |
| 118 | private Annotations genAnnots(BasicRegionConfig cfg, RegionId rid) { |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 119 | |
| 120 | DefaultAnnotations.Builder builder = DefaultAnnotations.builder() |
| 121 | .set(BasicElementConfig.NAME, cfg.name()) |
| 122 | .set(BasicElementConfig.LATITUDE, dstr(cfg.latitude())) |
| 123 | .set(BasicElementConfig.LONGITUDE, dstr(cfg.longitude())); |
| 124 | |
| 125 | // only set the UI_TYPE annotation if it is not null in the config |
| 126 | String uiType = cfg.uiType(); |
| 127 | if (uiType != null) { |
| 128 | builder.set(BasicElementConfig.UI_TYPE, uiType); |
| 129 | } |
| 130 | |
Simon Hunt | 8f60ff8 | 2017-04-24 17:19:30 -0700 | [diff] [blame] | 131 | List<LayoutLocation> locMappings = cfg.getMappings(); |
| 132 | builder.set(PEER_LOCATIONS, toCompactListString(locMappings)); |
| 133 | |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 134 | return builder.build(); |
| 135 | } |
| 136 | |
Thomas Vachuska | 4844808 | 2016-02-19 22:14:54 -0800 | [diff] [blame] | 137 | @Override |
| 138 | public Region createRegion(RegionId regionId, String name, Region.Type type, |
| 139 | List<Set<NodeId>> masterNodeIds) { |
| 140 | checkNotNull(regionId, REGION_ID_NULL); |
| 141 | checkNotNull(name, NAME_NULL); |
Sean Condon | 436c60a | 2021-01-01 14:23:29 +0000 | [diff] [blame] | 142 | checkNotNull(type, REGION_TYPE_NULL); |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 143 | |
| 144 | return store.createRegion(regionId, name, type, genAnnots(regionId), |
Simon Hunt | 8f60ff8 | 2017-04-24 17:19:30 -0700 | [diff] [blame] | 145 | masterNodeIds == null ? of() : masterNodeIds); |
Thomas Vachuska | 4844808 | 2016-02-19 22:14:54 -0800 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public Region updateRegion(RegionId regionId, String name, Region.Type type, |
| 150 | List<Set<NodeId>> masterNodeIds) { |
| 151 | checkNotNull(regionId, REGION_ID_NULL); |
| 152 | checkNotNull(name, NAME_NULL); |
Sean Condon | 436c60a | 2021-01-01 14:23:29 +0000 | [diff] [blame] | 153 | checkNotNull(type, REGION_TYPE_NULL); |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 154 | |
| 155 | return store.updateRegion(regionId, name, type, genAnnots(regionId), |
Simon Hunt | 8f60ff8 | 2017-04-24 17:19:30 -0700 | [diff] [blame] | 156 | masterNodeIds == null ? of() : masterNodeIds); |
Thomas Vachuska | 4844808 | 2016-02-19 22:14:54 -0800 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public void removeRegion(RegionId regionId) { |
| 161 | checkNotNull(regionId, REGION_ID_NULL); |
| 162 | store.removeRegion(regionId); |
| 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public void addDevices(RegionId regionId, Collection<DeviceId> deviceIds) { |
| 167 | checkNotNull(regionId, REGION_ID_NULL); |
| 168 | checkNotNull(deviceIds, DEVICE_IDS_NULL); |
| 169 | checkState(!deviceIds.isEmpty(), DEVICE_IDS_EMPTY); |
| 170 | store.addDevices(regionId, deviceIds); |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public void removeDevices(RegionId regionId, Collection<DeviceId> deviceIds) { |
| 175 | checkNotNull(regionId, REGION_ID_NULL); |
| 176 | checkNotNull(deviceIds, DEVICE_IDS_NULL); |
| 177 | checkState(!deviceIds.isEmpty(), DEVICE_IDS_EMPTY); |
| 178 | store.removeDevices(regionId, deviceIds); |
| 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public Set<Region> getRegions() { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 183 | checkPermission(REGION_READ); |
Thomas Vachuska | 4844808 | 2016-02-19 22:14:54 -0800 | [diff] [blame] | 184 | return store.getRegions(); |
| 185 | } |
| 186 | |
| 187 | @Override |
| 188 | public Region getRegion(RegionId regionId) { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 189 | checkPermission(REGION_READ); |
Thomas Vachuska | 4844808 | 2016-02-19 22:14:54 -0800 | [diff] [blame] | 190 | checkNotNull(regionId, REGION_ID_NULL); |
| 191 | return store.getRegion(regionId); |
| 192 | } |
| 193 | |
| 194 | @Override |
| 195 | public Region getRegionForDevice(DeviceId deviceId) { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 196 | checkPermission(REGION_READ); |
Thomas Vachuska | 4844808 | 2016-02-19 22:14:54 -0800 | [diff] [blame] | 197 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 198 | return store.getRegionForDevice(deviceId); |
| 199 | } |
| 200 | |
| 201 | @Override |
| 202 | public Set<DeviceId> getRegionDevices(RegionId regionId) { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 203 | checkPermission(REGION_READ); |
Thomas Vachuska | 4844808 | 2016-02-19 22:14:54 -0800 | [diff] [blame] | 204 | checkNotNull(regionId, REGION_ID_NULL); |
| 205 | return store.getRegionDevices(regionId); |
| 206 | } |
| 207 | |
Steven Burrows | 19e6e4f | 2016-10-05 13:27:07 -0500 | [diff] [blame] | 208 | @Override |
| 209 | public Set<HostId> getRegionHosts(RegionId regionId) { |
| 210 | checkPermission(REGION_READ); |
| 211 | checkNotNull(regionId, REGION_ID_NULL); |
Simon Hunt | 1bee529 | 2016-10-14 11:02:33 -0700 | [diff] [blame] | 212 | // TODO: compute hosts from region devices |
| 213 | return ImmutableSet.of(); |
Steven Burrows | 19e6e4f | 2016-10-05 13:27:07 -0500 | [diff] [blame] | 214 | } |
Simon Hunt | 351282d | 2016-10-14 16:27:48 +0000 | [diff] [blame] | 215 | |
Simon Hunt | 8f60ff8 | 2017-04-24 17:19:30 -0700 | [diff] [blame] | 216 | // by default allowed, otherwise check flag |
| 217 | private boolean isAllowed(BasicRegionConfig cfg) { |
| 218 | return (cfg == null || cfg.isAllowed()); |
| 219 | } |
| 220 | |
| 221 | private class InternalNetworkConfigListener implements NetworkConfigListener { |
| 222 | @Override |
| 223 | public void event(NetworkConfigEvent event) { |
| 224 | RegionId rid = (RegionId) event.subject(); |
| 225 | BasicRegionConfig cfg = |
| 226 | networkConfigService.getConfig(rid, BasicRegionConfig.class); |
| 227 | |
| 228 | if (!isAllowed(cfg)) { |
| 229 | kickOutBadRegion(rid); |
| 230 | |
| 231 | } else { |
| 232 | // (1) Find the region |
| 233 | // (2) Syntehsize new region + cfg details |
| 234 | // (3) re-insert new region element into store |
| 235 | |
| 236 | try { |
| 237 | Region region = getRegion(rid); |
| 238 | String name = region.name(); |
| 239 | Region.Type type = region.type(); |
| 240 | Annotations annots = genAnnots(cfg, rid); |
| 241 | List<Set<NodeId>> masterNodeIds = region.masters(); |
| 242 | |
| 243 | store.updateRegion(rid, name, type, annots, masterNodeIds); |
| 244 | |
| 245 | } catch (ItemNotFoundException infe) { |
| 246 | log.debug("warn: no region found with id {}", rid); |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | @Override |
| 252 | public boolean isRelevant(NetworkConfigEvent event) { |
| 253 | return (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED |
| 254 | || event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) |
| 255 | && (event.configClass().equals(BasicRegionConfig.class)); |
| 256 | } |
| 257 | |
| 258 | private void kickOutBadRegion(RegionId regionId) { |
| 259 | Region badRegion = getRegion(regionId); |
| 260 | if (badRegion != null) { |
| 261 | removeRegion(regionId); |
| 262 | } |
| 263 | } |
| 264 | } |
Thomas Vachuska | 4844808 | 2016-02-19 22:14:54 -0800 | [diff] [blame] | 265 | } |