blob: fcb1d6ac63ee3f612c1be9197d6ec3cfdb161c4f [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;
20import org.apache.felix.scr.annotations.Deactivate;
21import org.apache.felix.scr.annotations.Reference;
22import org.apache.felix.scr.annotations.ReferenceCardinality;
23import org.onosproject.cluster.NodeId;
24import org.onosproject.event.AbstractListenerManager;
25import org.onosproject.net.DeviceId;
26import org.onosproject.net.region.Region;
27import org.onosproject.net.region.RegionAdminService;
28import org.onosproject.net.region.RegionEvent;
29import org.onosproject.net.region.RegionId;
30import org.onosproject.net.region.RegionListener;
31import org.onosproject.net.region.RegionService;
32import org.onosproject.net.region.RegionStore;
33import org.onosproject.net.region.RegionStoreDelegate;
34import org.slf4j.Logger;
35
36import java.util.Collection;
37import java.util.List;
38import java.util.Set;
39
40import static com.google.common.base.Preconditions.checkNotNull;
41import static com.google.common.base.Preconditions.checkState;
42import static com.google.common.collect.ImmutableList.of;
43import static org.slf4j.LoggerFactory.getLogger;
44
45/**
46 * Provides implementation of the region service APIs.
47 */
48public class RegionManager extends AbstractListenerManager<RegionEvent, RegionListener>
49 implements RegionAdminService, RegionService {
50
51 private static final String REGION_ID_NULL = "Region ID cannot be null";
52 private static final String REGION_TYPE_NULL = "Region type cannot be null";
53 private static final String DEVICE_ID_NULL = "Device ID cannot be null";
54 private static final String DEVICE_IDS_NULL = "Device IDs cannot be null";
55 private static final String DEVICE_IDS_EMPTY = "Device IDs cannot be empty";
56 private static final String NAME_NULL = "Name cannot be null";
57
58 private final Logger log = getLogger(getClass());
59
60 private RegionStoreDelegate delegate = this::post;
61
62 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
63 protected RegionStore store;
64
65 @Activate
66 public void activate() {
67 store.setDelegate(delegate);
68 eventDispatcher.addSink(RegionEvent.class, listenerRegistry);
69 log.info("Started");
70 }
71
72 @Deactivate
73 public void deactivate() {
74 store.unsetDelegate(delegate);
75 eventDispatcher.removeSink(RegionEvent.class);
76 log.info("Stopped");
77 }
78
79 @Override
80 public Region createRegion(RegionId regionId, String name, Region.Type type,
81 List<Set<NodeId>> masterNodeIds) {
82 checkNotNull(regionId, REGION_ID_NULL);
83 checkNotNull(name, NAME_NULL);
84 checkNotNull(name, REGION_TYPE_NULL);
85 return store.createRegion(regionId, name, type, masterNodeIds == null ? of() : masterNodeIds);
86 }
87
88 @Override
89 public Region updateRegion(RegionId regionId, String name, Region.Type type,
90 List<Set<NodeId>> masterNodeIds) {
91 checkNotNull(regionId, REGION_ID_NULL);
92 checkNotNull(name, NAME_NULL);
93 checkNotNull(name, REGION_TYPE_NULL);
94 return store.updateRegion(regionId, name, type, masterNodeIds == null ? of() : masterNodeIds);
95 }
96
97 @Override
98 public void removeRegion(RegionId regionId) {
99 checkNotNull(regionId, REGION_ID_NULL);
100 store.removeRegion(regionId);
101 }
102
103 @Override
104 public void addDevices(RegionId regionId, Collection<DeviceId> deviceIds) {
105 checkNotNull(regionId, REGION_ID_NULL);
106 checkNotNull(deviceIds, DEVICE_IDS_NULL);
107 checkState(!deviceIds.isEmpty(), DEVICE_IDS_EMPTY);
108 store.addDevices(regionId, deviceIds);
109 }
110
111 @Override
112 public void removeDevices(RegionId regionId, Collection<DeviceId> deviceIds) {
113 checkNotNull(regionId, REGION_ID_NULL);
114 checkNotNull(deviceIds, DEVICE_IDS_NULL);
115 checkState(!deviceIds.isEmpty(), DEVICE_IDS_EMPTY);
116 store.removeDevices(regionId, deviceIds);
117 }
118
119 @Override
120 public Set<Region> getRegions() {
121 return store.getRegions();
122 }
123
124 @Override
125 public Region getRegion(RegionId regionId) {
126 checkNotNull(regionId, REGION_ID_NULL);
127 return store.getRegion(regionId);
128 }
129
130 @Override
131 public Region getRegionForDevice(DeviceId deviceId) {
132 checkNotNull(deviceId, DEVICE_ID_NULL);
133 return store.getRegionForDevice(deviceId);
134 }
135
136 @Override
137 public Set<DeviceId> getRegionDevices(RegionId regionId) {
138 checkNotNull(regionId, REGION_ID_NULL);
139 return store.getRegionDevices(regionId);
140 }
141
142}