blob: f0580bdd648d990054d3f5b285208f0d6681c782 [file] [log] [blame]
Thomas Vachuska48448082016-02-19 22:14:54 -08001/*
2 * Copyright 2016 Open Networking Laboratory
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
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;
28import org.onosproject.net.region.Region;
29import org.onosproject.net.region.RegionAdminService;
30import org.onosproject.net.region.RegionEvent;
31import org.onosproject.net.region.RegionId;
32import org.onosproject.net.region.RegionListener;
33import org.onosproject.net.region.RegionService;
34import org.onosproject.net.region.RegionStore;
35import org.onosproject.net.region.RegionStoreDelegate;
36import org.slf4j.Logger;
37
38import java.util.Collection;
39import java.util.List;
40import java.util.Set;
41
42import static com.google.common.base.Preconditions.checkNotNull;
43import static com.google.common.base.Preconditions.checkState;
44import static com.google.common.collect.ImmutableList.of;
45import static org.slf4j.LoggerFactory.getLogger;
Heedo Kang4a47a302016-02-29 17:40:23 +090046import static org.onosproject.security.AppGuard.checkPermission;
47import static org.onosproject.security.AppPermission.Type.REGION_READ;
Thomas Vachuska48448082016-02-19 22:14:54 -080048
49/**
50 * Provides implementation of the region service APIs.
51 */
Jian Li0c451802016-02-24 22:39:25 +090052@Component(immediate = true)
53@Service
Thomas Vachuska48448082016-02-19 22:14:54 -080054public class RegionManager extends AbstractListenerManager<RegionEvent, RegionListener>
55 implements RegionAdminService, RegionService {
56
57 private static final String REGION_ID_NULL = "Region ID cannot be null";
58 private static final String REGION_TYPE_NULL = "Region type cannot be null";
59 private static final String DEVICE_ID_NULL = "Device ID cannot be null";
60 private static final String DEVICE_IDS_NULL = "Device IDs cannot be null";
61 private static final String DEVICE_IDS_EMPTY = "Device IDs cannot be empty";
62 private static final String NAME_NULL = "Name cannot be null";
63
64 private final Logger log = getLogger(getClass());
65
66 private RegionStoreDelegate delegate = this::post;
67
68 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
69 protected RegionStore store;
70
71 @Activate
72 public void activate() {
73 store.setDelegate(delegate);
74 eventDispatcher.addSink(RegionEvent.class, listenerRegistry);
75 log.info("Started");
76 }
77
78 @Deactivate
79 public void deactivate() {
80 store.unsetDelegate(delegate);
81 eventDispatcher.removeSink(RegionEvent.class);
82 log.info("Stopped");
83 }
84
85 @Override
86 public Region createRegion(RegionId regionId, String name, Region.Type type,
87 List<Set<NodeId>> masterNodeIds) {
88 checkNotNull(regionId, REGION_ID_NULL);
89 checkNotNull(name, NAME_NULL);
90 checkNotNull(name, REGION_TYPE_NULL);
91 return store.createRegion(regionId, name, type, masterNodeIds == null ? of() : masterNodeIds);
92 }
93
94 @Override
95 public Region updateRegion(RegionId regionId, String name, Region.Type type,
96 List<Set<NodeId>> masterNodeIds) {
97 checkNotNull(regionId, REGION_ID_NULL);
98 checkNotNull(name, NAME_NULL);
99 checkNotNull(name, REGION_TYPE_NULL);
100 return store.updateRegion(regionId, name, type, masterNodeIds == null ? of() : masterNodeIds);
101 }
102
103 @Override
104 public void removeRegion(RegionId regionId) {
105 checkNotNull(regionId, REGION_ID_NULL);
106 store.removeRegion(regionId);
107 }
108
109 @Override
110 public void addDevices(RegionId regionId, Collection<DeviceId> deviceIds) {
111 checkNotNull(regionId, REGION_ID_NULL);
112 checkNotNull(deviceIds, DEVICE_IDS_NULL);
113 checkState(!deviceIds.isEmpty(), DEVICE_IDS_EMPTY);
114 store.addDevices(regionId, deviceIds);
115 }
116
117 @Override
118 public void removeDevices(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.removeDevices(regionId, deviceIds);
123 }
124
125 @Override
126 public Set<Region> getRegions() {
Heedo Kang4a47a302016-02-29 17:40:23 +0900127 checkPermission(REGION_READ);
Thomas Vachuska48448082016-02-19 22:14:54 -0800128 return store.getRegions();
129 }
130
131 @Override
132 public Region getRegion(RegionId regionId) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900133 checkPermission(REGION_READ);
Thomas Vachuska48448082016-02-19 22:14:54 -0800134 checkNotNull(regionId, REGION_ID_NULL);
135 return store.getRegion(regionId);
136 }
137
138 @Override
139 public Region getRegionForDevice(DeviceId deviceId) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900140 checkPermission(REGION_READ);
Thomas Vachuska48448082016-02-19 22:14:54 -0800141 checkNotNull(deviceId, DEVICE_ID_NULL);
142 return store.getRegionForDevice(deviceId);
143 }
144
145 @Override
146 public Set<DeviceId> getRegionDevices(RegionId regionId) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900147 checkPermission(REGION_READ);
Thomas Vachuska48448082016-02-19 22:14:54 -0800148 checkNotNull(regionId, REGION_ID_NULL);
149 return store.getRegionDevices(regionId);
150 }
151
152}