blob: 52634eaa451ca554b123a1986b62512a1b9ece97 [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;
46
47/**
48 * Provides implementation of the region service APIs.
49 */
Jian Li0c451802016-02-24 22:39:25 +090050@Component(immediate = true)
51@Service
Thomas Vachuska48448082016-02-19 22:14:54 -080052public class RegionManager extends AbstractListenerManager<RegionEvent, RegionListener>
53 implements RegionAdminService, RegionService {
54
55 private static final String REGION_ID_NULL = "Region ID cannot be null";
56 private static final String REGION_TYPE_NULL = "Region type cannot be null";
57 private static final String DEVICE_ID_NULL = "Device ID cannot be null";
58 private static final String DEVICE_IDS_NULL = "Device IDs cannot be null";
59 private static final String DEVICE_IDS_EMPTY = "Device IDs cannot be empty";
60 private static final String NAME_NULL = "Name cannot be null";
61
62 private final Logger log = getLogger(getClass());
63
64 private RegionStoreDelegate delegate = this::post;
65
66 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
67 protected RegionStore store;
68
69 @Activate
70 public void activate() {
71 store.setDelegate(delegate);
72 eventDispatcher.addSink(RegionEvent.class, listenerRegistry);
73 log.info("Started");
74 }
75
76 @Deactivate
77 public void deactivate() {
78 store.unsetDelegate(delegate);
79 eventDispatcher.removeSink(RegionEvent.class);
80 log.info("Stopped");
81 }
82
83 @Override
84 public Region createRegion(RegionId regionId, String name, Region.Type type,
85 List<Set<NodeId>> masterNodeIds) {
86 checkNotNull(regionId, REGION_ID_NULL);
87 checkNotNull(name, NAME_NULL);
88 checkNotNull(name, REGION_TYPE_NULL);
89 return store.createRegion(regionId, name, type, masterNodeIds == null ? of() : masterNodeIds);
90 }
91
92 @Override
93 public Region updateRegion(RegionId regionId, String name, Region.Type type,
94 List<Set<NodeId>> masterNodeIds) {
95 checkNotNull(regionId, REGION_ID_NULL);
96 checkNotNull(name, NAME_NULL);
97 checkNotNull(name, REGION_TYPE_NULL);
98 return store.updateRegion(regionId, name, type, masterNodeIds == null ? of() : masterNodeIds);
99 }
100
101 @Override
102 public void removeRegion(RegionId regionId) {
103 checkNotNull(regionId, REGION_ID_NULL);
104 store.removeRegion(regionId);
105 }
106
107 @Override
108 public void addDevices(RegionId regionId, Collection<DeviceId> deviceIds) {
109 checkNotNull(regionId, REGION_ID_NULL);
110 checkNotNull(deviceIds, DEVICE_IDS_NULL);
111 checkState(!deviceIds.isEmpty(), DEVICE_IDS_EMPTY);
112 store.addDevices(regionId, deviceIds);
113 }
114
115 @Override
116 public void removeDevices(RegionId regionId, Collection<DeviceId> deviceIds) {
117 checkNotNull(regionId, REGION_ID_NULL);
118 checkNotNull(deviceIds, DEVICE_IDS_NULL);
119 checkState(!deviceIds.isEmpty(), DEVICE_IDS_EMPTY);
120 store.removeDevices(regionId, deviceIds);
121 }
122
123 @Override
124 public Set<Region> getRegions() {
125 return store.getRegions();
126 }
127
128 @Override
129 public Region getRegion(RegionId regionId) {
130 checkNotNull(regionId, REGION_ID_NULL);
131 return store.getRegion(regionId);
132 }
133
134 @Override
135 public Region getRegionForDevice(DeviceId deviceId) {
136 checkNotNull(deviceId, DEVICE_ID_NULL);
137 return store.getRegionForDevice(deviceId);
138 }
139
140 @Override
141 public Set<DeviceId> getRegionDevices(RegionId regionId) {
142 checkNotNull(regionId, REGION_ID_NULL);
143 return store.getRegionDevices(regionId);
144 }
145
146}