blob: b988021810dfd38e8bf3f2bb4216445b9880ad63 [file] [log] [blame]
Thomas Vachuska48448082016-02-19 22:14:54 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Thomas Vachuska48448082016-02-19 22:14:54 -08003 *
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.region.impl;
18
19import org.apache.felix.scr.annotations.Activate;
Jian Li0c451802016-02-24 22:39:25 +090020import org.apache.felix.scr.annotations.Component;
Thomas Vachuska48448082016-02-19 22:14:54 -080021import org.apache.felix.scr.annotations.Deactivate;
22import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
Jian Li0c451802016-02-24 22:39:25 +090024import org.apache.felix.scr.annotations.Service;
Thomas Vachuska48448082016-02-19 22:14:54 -080025import org.onosproject.cluster.NodeId;
26import org.onosproject.event.AbstractListenerManager;
27import org.onosproject.net.DeviceId;
Simon Hunt4aef6c32016-10-13 19:23:50 -070028import org.onosproject.net.Host;
Steven Burrows19e6e4f2016-10-05 13:27:07 -050029import org.onosproject.net.HostId;
Simon Hunt4aef6c32016-10-13 19:23:50 -070030import org.onosproject.net.host.HostService;
Thomas Vachuska48448082016-02-19 22:14:54 -080031import org.onosproject.net.region.Region;
32import org.onosproject.net.region.RegionAdminService;
33import org.onosproject.net.region.RegionEvent;
34import org.onosproject.net.region.RegionId;
35import org.onosproject.net.region.RegionListener;
36import org.onosproject.net.region.RegionService;
37import org.onosproject.net.region.RegionStore;
38import org.onosproject.net.region.RegionStoreDelegate;
39import org.slf4j.Logger;
40
41import java.util.Collection;
Simon Hunt4aef6c32016-10-13 19:23:50 -070042import java.util.HashSet;
Thomas Vachuska48448082016-02-19 22:14:54 -080043import java.util.List;
44import java.util.Set;
Simon Hunt4aef6c32016-10-13 19:23:50 -070045import java.util.stream.Collectors;
Thomas Vachuska48448082016-02-19 22:14:54 -080046
47import static com.google.common.base.Preconditions.checkNotNull;
48import static com.google.common.base.Preconditions.checkState;
49import static com.google.common.collect.ImmutableList.of;
Heedo Kang4a47a302016-02-29 17:40:23 +090050import static org.onosproject.security.AppGuard.checkPermission;
51import static org.onosproject.security.AppPermission.Type.REGION_READ;
Simon Hunt4aef6c32016-10-13 19:23:50 -070052import static org.slf4j.LoggerFactory.getLogger;
Thomas Vachuska48448082016-02-19 22:14:54 -080053
54/**
55 * Provides implementation of the region service APIs.
56 */
Jian Li0c451802016-02-24 22:39:25 +090057@Component(immediate = true)
58@Service
Thomas Vachuska48448082016-02-19 22:14:54 -080059public 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 Hunt4aef6c32016-10-13 19:23:50 -070076 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
77 protected HostService hostService;
78
Thomas Vachuska48448082016-02-19 22:14:54 -080079 @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 Kang4a47a302016-02-29 17:40:23 +0900135 checkPermission(REGION_READ);
Thomas Vachuska48448082016-02-19 22:14:54 -0800136 return store.getRegions();
137 }
138
139 @Override
140 public Region getRegion(RegionId regionId) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900141 checkPermission(REGION_READ);
Thomas Vachuska48448082016-02-19 22:14:54 -0800142 checkNotNull(regionId, REGION_ID_NULL);
143 return store.getRegion(regionId);
144 }
145
146 @Override
147 public Region getRegionForDevice(DeviceId deviceId) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900148 checkPermission(REGION_READ);
Thomas Vachuska48448082016-02-19 22:14:54 -0800149 checkNotNull(deviceId, DEVICE_ID_NULL);
150 return store.getRegionForDevice(deviceId);
151 }
152
153 @Override
154 public Set<DeviceId> getRegionDevices(RegionId regionId) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900155 checkPermission(REGION_READ);
Thomas Vachuska48448082016-02-19 22:14:54 -0800156 checkNotNull(regionId, REGION_ID_NULL);
157 return store.getRegionDevices(regionId);
158 }
159
Steven Burrows19e6e4f2016-10-05 13:27:07 -0500160 @Override
161 public Set<HostId> getRegionHosts(RegionId regionId) {
162 checkPermission(REGION_READ);
163 checkNotNull(regionId, REGION_ID_NULL);
Simon Hunt4aef6c32016-10-13 19:23:50 -0700164 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 Burrows19e6e4f2016-10-05 13:27:07 -0500173 }
Thomas Vachuska48448082016-02-19 22:14:54 -0800174}