blob: bf828427190ae7f374b7ca3433dfc5c86f7ca03c [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
Simon Hunt1bee5292016-10-14 11:02:33 -070019import com.google.common.collect.ImmutableSet;
Thomas Vachuska48448082016-02-19 22:14:54 -080020import org.apache.felix.scr.annotations.Activate;
Jian Li0c451802016-02-24 22:39:25 +090021import org.apache.felix.scr.annotations.Component;
Thomas Vachuska48448082016-02-19 22:14:54 -080022import org.apache.felix.scr.annotations.Deactivate;
23import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
Jian Li0c451802016-02-24 22:39:25 +090025import org.apache.felix.scr.annotations.Service;
Thomas Vachuska48448082016-02-19 22:14:54 -080026import org.onosproject.cluster.NodeId;
27import org.onosproject.event.AbstractListenerManager;
Simon Hunt53612212016-12-04 17:19:52 -080028import org.onosproject.net.Annotations;
29import org.onosproject.net.DefaultAnnotations;
Thomas Vachuska48448082016-02-19 22:14:54 -080030import org.onosproject.net.DeviceId;
Steven Burrows19e6e4f2016-10-05 13:27:07 -050031import org.onosproject.net.HostId;
Simon Hunt53612212016-12-04 17:19:52 -080032import org.onosproject.net.config.NetworkConfigService;
33import org.onosproject.net.config.basics.BasicElementConfig;
34import org.onosproject.net.config.basics.BasicRegionConfig;
Thomas Vachuska48448082016-02-19 22:14:54 -080035import org.onosproject.net.region.Region;
36import org.onosproject.net.region.RegionAdminService;
37import org.onosproject.net.region.RegionEvent;
38import org.onosproject.net.region.RegionId;
39import org.onosproject.net.region.RegionListener;
40import org.onosproject.net.region.RegionService;
41import org.onosproject.net.region.RegionStore;
42import org.onosproject.net.region.RegionStoreDelegate;
43import org.slf4j.Logger;
44
45import java.util.Collection;
46import java.util.List;
47import java.util.Set;
48
49import static com.google.common.base.Preconditions.checkNotNull;
50import static com.google.common.base.Preconditions.checkState;
51import static com.google.common.collect.ImmutableList.of;
Heedo Kang4a47a302016-02-29 17:40:23 +090052import static org.onosproject.security.AppGuard.checkPermission;
53import static org.onosproject.security.AppPermission.Type.REGION_READ;
Simon Hunt53612212016-12-04 17:19:52 -080054import static org.slf4j.LoggerFactory.getLogger;
Thomas Vachuska48448082016-02-19 22:14:54 -080055
56/**
57 * Provides implementation of the region service APIs.
58 */
Jian Li0c451802016-02-24 22:39:25 +090059@Component(immediate = true)
60@Service
Thomas Vachuska48448082016-02-19 22:14:54 -080061public class RegionManager extends AbstractListenerManager<RegionEvent, RegionListener>
62 implements RegionAdminService, RegionService {
63
64 private static final String REGION_ID_NULL = "Region ID cannot be null";
65 private static final String REGION_TYPE_NULL = "Region type cannot be null";
66 private static final String DEVICE_ID_NULL = "Device ID cannot be null";
67 private static final String DEVICE_IDS_NULL = "Device IDs cannot be null";
68 private static final String DEVICE_IDS_EMPTY = "Device IDs cannot be empty";
69 private static final String NAME_NULL = "Name cannot be null";
70
71 private final Logger log = getLogger(getClass());
72
73 private RegionStoreDelegate delegate = this::post;
74
75 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
76 protected RegionStore store;
77
Simon Hunt53612212016-12-04 17:19:52 -080078 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
79 protected NetworkConfigService networkConfigService;
80
Thomas Vachuska48448082016-02-19 22:14:54 -080081 @Activate
82 public void activate() {
83 store.setDelegate(delegate);
84 eventDispatcher.addSink(RegionEvent.class, listenerRegistry);
85 log.info("Started");
86 }
87
88 @Deactivate
89 public void deactivate() {
90 store.unsetDelegate(delegate);
91 eventDispatcher.removeSink(RegionEvent.class);
92 log.info("Stopped");
93 }
94
Simon Hunt53612212016-12-04 17:19:52 -080095 private String dstr(double d) {
96 return Double.toString(d);
97 }
98
99 private Annotations genAnnots(RegionId id) {
100 BasicRegionConfig cfg =
101 networkConfigService.getConfig(id, BasicRegionConfig.class);
102
103 if (cfg == null) {
104 return DefaultAnnotations.builder().build();
105 }
106
107 DefaultAnnotations.Builder builder = DefaultAnnotations.builder()
108 .set(BasicElementConfig.NAME, cfg.name())
109 .set(BasicElementConfig.LATITUDE, dstr(cfg.latitude()))
110 .set(BasicElementConfig.LONGITUDE, dstr(cfg.longitude()));
111
112 // only set the UI_TYPE annotation if it is not null in the config
113 String uiType = cfg.uiType();
114 if (uiType != null) {
115 builder.set(BasicElementConfig.UI_TYPE, uiType);
116 }
117
118 return builder.build();
119 }
120
Thomas Vachuska48448082016-02-19 22:14:54 -0800121 @Override
122 public Region createRegion(RegionId regionId, String name, Region.Type type,
123 List<Set<NodeId>> masterNodeIds) {
124 checkNotNull(regionId, REGION_ID_NULL);
125 checkNotNull(name, NAME_NULL);
126 checkNotNull(name, REGION_TYPE_NULL);
Simon Hunt53612212016-12-04 17:19:52 -0800127
128 return store.createRegion(regionId, name, type, genAnnots(regionId),
129 masterNodeIds == null ? of() : masterNodeIds);
Thomas Vachuska48448082016-02-19 22:14:54 -0800130 }
131
132 @Override
133 public Region updateRegion(RegionId regionId, String name, Region.Type type,
134 List<Set<NodeId>> masterNodeIds) {
135 checkNotNull(regionId, REGION_ID_NULL);
136 checkNotNull(name, NAME_NULL);
137 checkNotNull(name, REGION_TYPE_NULL);
Simon Hunt53612212016-12-04 17:19:52 -0800138
139 return store.updateRegion(regionId, name, type, genAnnots(regionId),
140 masterNodeIds == null ? of() : masterNodeIds);
Thomas Vachuska48448082016-02-19 22:14:54 -0800141 }
142
143 @Override
144 public void removeRegion(RegionId regionId) {
145 checkNotNull(regionId, REGION_ID_NULL);
146 store.removeRegion(regionId);
147 }
148
149 @Override
150 public void addDevices(RegionId regionId, Collection<DeviceId> deviceIds) {
151 checkNotNull(regionId, REGION_ID_NULL);
152 checkNotNull(deviceIds, DEVICE_IDS_NULL);
153 checkState(!deviceIds.isEmpty(), DEVICE_IDS_EMPTY);
154 store.addDevices(regionId, deviceIds);
155 }
156
157 @Override
158 public void removeDevices(RegionId regionId, Collection<DeviceId> deviceIds) {
159 checkNotNull(regionId, REGION_ID_NULL);
160 checkNotNull(deviceIds, DEVICE_IDS_NULL);
161 checkState(!deviceIds.isEmpty(), DEVICE_IDS_EMPTY);
162 store.removeDevices(regionId, deviceIds);
163 }
164
165 @Override
166 public Set<Region> getRegions() {
Heedo Kang4a47a302016-02-29 17:40:23 +0900167 checkPermission(REGION_READ);
Thomas Vachuska48448082016-02-19 22:14:54 -0800168 return store.getRegions();
169 }
170
171 @Override
172 public Region getRegion(RegionId regionId) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900173 checkPermission(REGION_READ);
Thomas Vachuska48448082016-02-19 22:14:54 -0800174 checkNotNull(regionId, REGION_ID_NULL);
175 return store.getRegion(regionId);
176 }
177
178 @Override
179 public Region getRegionForDevice(DeviceId deviceId) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900180 checkPermission(REGION_READ);
Thomas Vachuska48448082016-02-19 22:14:54 -0800181 checkNotNull(deviceId, DEVICE_ID_NULL);
182 return store.getRegionForDevice(deviceId);
183 }
184
185 @Override
186 public Set<DeviceId> getRegionDevices(RegionId regionId) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900187 checkPermission(REGION_READ);
Thomas Vachuska48448082016-02-19 22:14:54 -0800188 checkNotNull(regionId, REGION_ID_NULL);
189 return store.getRegionDevices(regionId);
190 }
191
Steven Burrows19e6e4f2016-10-05 13:27:07 -0500192 @Override
193 public Set<HostId> getRegionHosts(RegionId regionId) {
194 checkPermission(REGION_READ);
195 checkNotNull(regionId, REGION_ID_NULL);
Simon Hunt1bee5292016-10-14 11:02:33 -0700196 // TODO: compute hosts from region devices
197 return ImmutableSet.of();
Steven Burrows19e6e4f2016-10-05 13:27:07 -0500198 }
Simon Hunt351282d2016-10-14 16:27:48 +0000199
Thomas Vachuska48448082016-02-19 22:14:54 -0800200}