blob: 75d96ed79333ecc84f89bc080ff5962bfc98fb7f [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;
Steven Burrows19e6e4f2016-10-05 13:27:07 -050028import org.onosproject.net.HostId;
Thomas Vachuska48448082016-02-19 22:14:54 -080029import org.onosproject.net.region.Region;
30import org.onosproject.net.region.RegionAdminService;
31import org.onosproject.net.region.RegionEvent;
32import org.onosproject.net.region.RegionId;
33import org.onosproject.net.region.RegionListener;
34import org.onosproject.net.region.RegionService;
35import org.onosproject.net.region.RegionStore;
36import org.onosproject.net.region.RegionStoreDelegate;
37import org.slf4j.Logger;
38
39import java.util.Collection;
40import java.util.List;
41import java.util.Set;
42
43import static com.google.common.base.Preconditions.checkNotNull;
44import static com.google.common.base.Preconditions.checkState;
45import static com.google.common.collect.ImmutableList.of;
46import static org.slf4j.LoggerFactory.getLogger;
Heedo Kang4a47a302016-02-29 17:40:23 +090047import static org.onosproject.security.AppGuard.checkPermission;
48import static org.onosproject.security.AppPermission.Type.REGION_READ;
Thomas Vachuska48448082016-02-19 22:14:54 -080049
50/**
51 * Provides implementation of the region service APIs.
52 */
Jian Li0c451802016-02-24 22:39:25 +090053@Component(immediate = true)
54@Service
Thomas Vachuska48448082016-02-19 22:14:54 -080055public class RegionManager extends AbstractListenerManager<RegionEvent, RegionListener>
56 implements RegionAdminService, RegionService {
57
58 private static final String REGION_ID_NULL = "Region ID cannot be null";
59 private static final String REGION_TYPE_NULL = "Region type cannot be null";
60 private static final String DEVICE_ID_NULL = "Device ID cannot be null";
61 private static final String DEVICE_IDS_NULL = "Device IDs cannot be null";
62 private static final String DEVICE_IDS_EMPTY = "Device IDs cannot be empty";
63 private static final String NAME_NULL = "Name cannot be null";
64
65 private final Logger log = getLogger(getClass());
66
67 private RegionStoreDelegate delegate = this::post;
68
69 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
70 protected RegionStore store;
71
72 @Activate
73 public void activate() {
74 store.setDelegate(delegate);
75 eventDispatcher.addSink(RegionEvent.class, listenerRegistry);
76 log.info("Started");
77 }
78
79 @Deactivate
80 public void deactivate() {
81 store.unsetDelegate(delegate);
82 eventDispatcher.removeSink(RegionEvent.class);
83 log.info("Stopped");
84 }
85
86 @Override
87 public Region createRegion(RegionId regionId, String name, Region.Type type,
88 List<Set<NodeId>> masterNodeIds) {
89 checkNotNull(regionId, REGION_ID_NULL);
90 checkNotNull(name, NAME_NULL);
91 checkNotNull(name, REGION_TYPE_NULL);
92 return store.createRegion(regionId, name, type, masterNodeIds == null ? of() : masterNodeIds);
93 }
94
95 @Override
96 public Region updateRegion(RegionId regionId, String name, Region.Type type,
97 List<Set<NodeId>> masterNodeIds) {
98 checkNotNull(regionId, REGION_ID_NULL);
99 checkNotNull(name, NAME_NULL);
100 checkNotNull(name, REGION_TYPE_NULL);
101 return store.updateRegion(regionId, name, type, masterNodeIds == null ? of() : masterNodeIds);
102 }
103
104 @Override
105 public void removeRegion(RegionId regionId) {
106 checkNotNull(regionId, REGION_ID_NULL);
107 store.removeRegion(regionId);
108 }
109
110 @Override
111 public void addDevices(RegionId regionId, Collection<DeviceId> deviceIds) {
112 checkNotNull(regionId, REGION_ID_NULL);
113 checkNotNull(deviceIds, DEVICE_IDS_NULL);
114 checkState(!deviceIds.isEmpty(), DEVICE_IDS_EMPTY);
115 store.addDevices(regionId, deviceIds);
116 }
117
118 @Override
119 public void removeDevices(RegionId regionId, Collection<DeviceId> deviceIds) {
120 checkNotNull(regionId, REGION_ID_NULL);
121 checkNotNull(deviceIds, DEVICE_IDS_NULL);
122 checkState(!deviceIds.isEmpty(), DEVICE_IDS_EMPTY);
123 store.removeDevices(regionId, deviceIds);
124 }
125
126 @Override
127 public Set<Region> getRegions() {
Heedo Kang4a47a302016-02-29 17:40:23 +0900128 checkPermission(REGION_READ);
Thomas Vachuska48448082016-02-19 22:14:54 -0800129 return store.getRegions();
130 }
131
132 @Override
133 public Region getRegion(RegionId regionId) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900134 checkPermission(REGION_READ);
Thomas Vachuska48448082016-02-19 22:14:54 -0800135 checkNotNull(regionId, REGION_ID_NULL);
136 return store.getRegion(regionId);
137 }
138
139 @Override
140 public Region getRegionForDevice(DeviceId deviceId) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900141 checkPermission(REGION_READ);
Thomas Vachuska48448082016-02-19 22:14:54 -0800142 checkNotNull(deviceId, DEVICE_ID_NULL);
143 return store.getRegionForDevice(deviceId);
144 }
145
146 @Override
147 public Set<DeviceId> getRegionDevices(RegionId regionId) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900148 checkPermission(REGION_READ);
Thomas Vachuska48448082016-02-19 22:14:54 -0800149 checkNotNull(regionId, REGION_ID_NULL);
150 return store.getRegionDevices(regionId);
151 }
152
Steven Burrows19e6e4f2016-10-05 13:27:07 -0500153 @Override
154 public Set<HostId> getRegionHosts(RegionId regionId) {
155 checkPermission(REGION_READ);
156 checkNotNull(regionId, REGION_ID_NULL);
157 return store.getRegionHosts(regionId);
158 }
159
Thomas Vachuska48448082016-02-19 22:14:54 -0800160}