Thomas Vachuska | 4844808 | 2016-02-19 22:14:54 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
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 | |
| 19 | import org.apache.felix.scr.annotations.Activate; |
Jian Li | 0c45180 | 2016-02-24 22:39:25 +0900 | [diff] [blame] | 20 | import org.apache.felix.scr.annotations.Component; |
Thomas Vachuska | 4844808 | 2016-02-19 22:14:54 -0800 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Deactivate; |
| 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Jian Li | 0c45180 | 2016-02-24 22:39:25 +0900 | [diff] [blame] | 24 | import org.apache.felix.scr.annotations.Service; |
Thomas Vachuska | 4844808 | 2016-02-19 22:14:54 -0800 | [diff] [blame] | 25 | import org.onosproject.cluster.NodeId; |
| 26 | import org.onosproject.event.AbstractListenerManager; |
| 27 | import org.onosproject.net.DeviceId; |
Simon Hunt | 4aef6c3 | 2016-10-13 19:23:50 -0700 | [diff] [blame^] | 28 | import org.onosproject.net.Host; |
Steven Burrows | 19e6e4f | 2016-10-05 13:27:07 -0500 | [diff] [blame] | 29 | import org.onosproject.net.HostId; |
Simon Hunt | 4aef6c3 | 2016-10-13 19:23:50 -0700 | [diff] [blame^] | 30 | import org.onosproject.net.host.HostService; |
Thomas Vachuska | 4844808 | 2016-02-19 22:14:54 -0800 | [diff] [blame] | 31 | import org.onosproject.net.region.Region; |
| 32 | import org.onosproject.net.region.RegionAdminService; |
| 33 | import org.onosproject.net.region.RegionEvent; |
| 34 | import org.onosproject.net.region.RegionId; |
| 35 | import org.onosproject.net.region.RegionListener; |
| 36 | import org.onosproject.net.region.RegionService; |
| 37 | import org.onosproject.net.region.RegionStore; |
| 38 | import org.onosproject.net.region.RegionStoreDelegate; |
| 39 | import org.slf4j.Logger; |
| 40 | |
| 41 | import java.util.Collection; |
Simon Hunt | 4aef6c3 | 2016-10-13 19:23:50 -0700 | [diff] [blame^] | 42 | import java.util.HashSet; |
Thomas Vachuska | 4844808 | 2016-02-19 22:14:54 -0800 | [diff] [blame] | 43 | import java.util.List; |
| 44 | import java.util.Set; |
Simon Hunt | 4aef6c3 | 2016-10-13 19:23:50 -0700 | [diff] [blame^] | 45 | import java.util.stream.Collectors; |
Thomas Vachuska | 4844808 | 2016-02-19 22:14:54 -0800 | [diff] [blame] | 46 | |
| 47 | import static com.google.common.base.Preconditions.checkNotNull; |
| 48 | import static com.google.common.base.Preconditions.checkState; |
| 49 | import static com.google.common.collect.ImmutableList.of; |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 50 | import static org.onosproject.security.AppGuard.checkPermission; |
| 51 | import static org.onosproject.security.AppPermission.Type.REGION_READ; |
Simon Hunt | 4aef6c3 | 2016-10-13 19:23:50 -0700 | [diff] [blame^] | 52 | import static org.slf4j.LoggerFactory.getLogger; |
Thomas Vachuska | 4844808 | 2016-02-19 22:14:54 -0800 | [diff] [blame] | 53 | |
| 54 | /** |
| 55 | * Provides implementation of the region service APIs. |
| 56 | */ |
Jian Li | 0c45180 | 2016-02-24 22:39:25 +0900 | [diff] [blame] | 57 | @Component(immediate = true) |
| 58 | @Service |
Thomas Vachuska | 4844808 | 2016-02-19 22:14:54 -0800 | [diff] [blame] | 59 | public class RegionManager extends AbstractListenerManager<RegionEvent, RegionListener> |
| 60 | implements RegionAdminService, RegionService { |
| 61 | |
| 62 | private static final String REGION_ID_NULL = "Region ID cannot be null"; |
| 63 | private static final String REGION_TYPE_NULL = "Region type cannot be null"; |
| 64 | private static final String DEVICE_ID_NULL = "Device ID cannot be null"; |
| 65 | private static final String DEVICE_IDS_NULL = "Device IDs cannot be null"; |
| 66 | private static final String DEVICE_IDS_EMPTY = "Device IDs cannot be empty"; |
| 67 | private static final String NAME_NULL = "Name cannot be null"; |
| 68 | |
| 69 | private final Logger log = getLogger(getClass()); |
| 70 | |
| 71 | private RegionStoreDelegate delegate = this::post; |
| 72 | |
| 73 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 74 | protected RegionStore store; |
| 75 | |
Simon Hunt | 4aef6c3 | 2016-10-13 19:23:50 -0700 | [diff] [blame^] | 76 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 77 | protected HostService hostService; |
| 78 | |
Thomas Vachuska | 4844808 | 2016-02-19 22:14:54 -0800 | [diff] [blame] | 79 | @Activate |
| 80 | public void activate() { |
| 81 | store.setDelegate(delegate); |
| 82 | eventDispatcher.addSink(RegionEvent.class, listenerRegistry); |
| 83 | log.info("Started"); |
| 84 | } |
| 85 | |
| 86 | @Deactivate |
| 87 | public void deactivate() { |
| 88 | store.unsetDelegate(delegate); |
| 89 | eventDispatcher.removeSink(RegionEvent.class); |
| 90 | log.info("Stopped"); |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public Region createRegion(RegionId regionId, String name, Region.Type type, |
| 95 | List<Set<NodeId>> masterNodeIds) { |
| 96 | checkNotNull(regionId, REGION_ID_NULL); |
| 97 | checkNotNull(name, NAME_NULL); |
| 98 | checkNotNull(name, REGION_TYPE_NULL); |
| 99 | return store.createRegion(regionId, name, type, masterNodeIds == null ? of() : masterNodeIds); |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public Region updateRegion(RegionId regionId, String name, Region.Type type, |
| 104 | List<Set<NodeId>> masterNodeIds) { |
| 105 | checkNotNull(regionId, REGION_ID_NULL); |
| 106 | checkNotNull(name, NAME_NULL); |
| 107 | checkNotNull(name, REGION_TYPE_NULL); |
| 108 | return store.updateRegion(regionId, name, type, masterNodeIds == null ? of() : masterNodeIds); |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public void removeRegion(RegionId regionId) { |
| 113 | checkNotNull(regionId, REGION_ID_NULL); |
| 114 | store.removeRegion(regionId); |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public void addDevices(RegionId regionId, Collection<DeviceId> deviceIds) { |
| 119 | checkNotNull(regionId, REGION_ID_NULL); |
| 120 | checkNotNull(deviceIds, DEVICE_IDS_NULL); |
| 121 | checkState(!deviceIds.isEmpty(), DEVICE_IDS_EMPTY); |
| 122 | store.addDevices(regionId, deviceIds); |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public void removeDevices(RegionId regionId, Collection<DeviceId> deviceIds) { |
| 127 | checkNotNull(regionId, REGION_ID_NULL); |
| 128 | checkNotNull(deviceIds, DEVICE_IDS_NULL); |
| 129 | checkState(!deviceIds.isEmpty(), DEVICE_IDS_EMPTY); |
| 130 | store.removeDevices(regionId, deviceIds); |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public Set<Region> getRegions() { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 135 | checkPermission(REGION_READ); |
Thomas Vachuska | 4844808 | 2016-02-19 22:14:54 -0800 | [diff] [blame] | 136 | return store.getRegions(); |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public Region getRegion(RegionId regionId) { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 141 | checkPermission(REGION_READ); |
Thomas Vachuska | 4844808 | 2016-02-19 22:14:54 -0800 | [diff] [blame] | 142 | checkNotNull(regionId, REGION_ID_NULL); |
| 143 | return store.getRegion(regionId); |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public Region getRegionForDevice(DeviceId deviceId) { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 148 | checkPermission(REGION_READ); |
Thomas Vachuska | 4844808 | 2016-02-19 22:14:54 -0800 | [diff] [blame] | 149 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 150 | return store.getRegionForDevice(deviceId); |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public Set<DeviceId> getRegionDevices(RegionId regionId) { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 155 | checkPermission(REGION_READ); |
Thomas Vachuska | 4844808 | 2016-02-19 22:14:54 -0800 | [diff] [blame] | 156 | checkNotNull(regionId, REGION_ID_NULL); |
| 157 | return store.getRegionDevices(regionId); |
| 158 | } |
| 159 | |
Steven Burrows | 19e6e4f | 2016-10-05 13:27:07 -0500 | [diff] [blame] | 160 | @Override |
| 161 | public Set<HostId> getRegionHosts(RegionId regionId) { |
| 162 | checkPermission(REGION_READ); |
| 163 | checkNotNull(regionId, REGION_ID_NULL); |
Simon Hunt | 4aef6c3 | 2016-10-13 19:23:50 -0700 | [diff] [blame^] | 164 | Set<DeviceId> devs = getRegionDevices(regionId); |
| 165 | Set<HostId> hostIds = new HashSet<>(); |
| 166 | for (DeviceId d : devs) { |
| 167 | Set<HostId> ids = hostService.getConnectedHosts(d).stream() |
| 168 | .map(Host::id) |
| 169 | .collect(Collectors.toSet()); |
| 170 | hostIds.addAll(ids); |
| 171 | } |
| 172 | return hostIds; |
Steven Burrows | 19e6e4f | 2016-10-05 13:27:07 -0500 | [diff] [blame] | 173 | } |
Thomas Vachuska | 4844808 | 2016-02-19 22:14:54 -0800 | [diff] [blame] | 174 | } |