blob: c1b932752dbac1c1144fa32da940049562781427 [file] [log] [blame]
Thomas Vachuska48448082016-02-19 22:14:54 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
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;
Simon Hunt8f60ff82017-04-24 17:19:30 -070020import org.onlab.util.ItemNotFoundException;
Thomas Vachuska48448082016-02-19 22:14:54 -080021import org.onosproject.cluster.NodeId;
22import org.onosproject.event.AbstractListenerManager;
Simon Hunt53612212016-12-04 17:19:52 -080023import org.onosproject.net.Annotations;
24import org.onosproject.net.DefaultAnnotations;
Thomas Vachuska48448082016-02-19 22:14:54 -080025import org.onosproject.net.DeviceId;
Steven Burrows19e6e4f2016-10-05 13:27:07 -050026import org.onosproject.net.HostId;
Simon Hunt8f60ff82017-04-24 17:19:30 -070027import org.onosproject.net.config.NetworkConfigEvent;
28import org.onosproject.net.config.NetworkConfigListener;
Simon Hunt53612212016-12-04 17:19:52 -080029import org.onosproject.net.config.NetworkConfigService;
30import org.onosproject.net.config.basics.BasicElementConfig;
31import org.onosproject.net.config.basics.BasicRegionConfig;
Thomas Vachuska48448082016-02-19 22:14:54 -080032import org.onosproject.net.region.Region;
33import org.onosproject.net.region.RegionAdminService;
34import org.onosproject.net.region.RegionEvent;
35import org.onosproject.net.region.RegionId;
36import org.onosproject.net.region.RegionListener;
37import org.onosproject.net.region.RegionService;
38import org.onosproject.net.region.RegionStore;
39import org.onosproject.net.region.RegionStoreDelegate;
Simon Hunt8f60ff82017-04-24 17:19:30 -070040import org.onosproject.ui.topo.LayoutLocation;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070041import org.osgi.service.component.annotations.Activate;
42import org.osgi.service.component.annotations.Component;
43import org.osgi.service.component.annotations.Deactivate;
44import org.osgi.service.component.annotations.Reference;
45import org.osgi.service.component.annotations.ReferenceCardinality;
Thomas Vachuska48448082016-02-19 22:14:54 -080046import org.slf4j.Logger;
47
48import java.util.Collection;
49import java.util.List;
50import java.util.Set;
51
52import static com.google.common.base.Preconditions.checkNotNull;
53import static com.google.common.base.Preconditions.checkState;
54import static com.google.common.collect.ImmutableList.of;
Heedo Kang4a47a302016-02-29 17:40:23 +090055import static org.onosproject.security.AppGuard.checkPermission;
56import static org.onosproject.security.AppPermission.Type.REGION_READ;
Simon Hunt8f60ff82017-04-24 17:19:30 -070057import static org.onosproject.ui.topo.LayoutLocation.toCompactListString;
Simon Hunt53612212016-12-04 17:19:52 -080058import static org.slf4j.LoggerFactory.getLogger;
Thomas Vachuska48448082016-02-19 22:14:54 -080059
60/**
61 * Provides implementation of the region service APIs.
62 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070063@Component(immediate = true, service = {RegionAdminService.class, RegionService.class})
Thomas Vachuska48448082016-02-19 22:14:54 -080064public class RegionManager extends AbstractListenerManager<RegionEvent, RegionListener>
65 implements RegionAdminService, RegionService {
66
67 private static final String REGION_ID_NULL = "Region ID cannot be null";
68 private static final String REGION_TYPE_NULL = "Region type cannot be null";
69 private static final String DEVICE_ID_NULL = "Device ID cannot be null";
70 private static final String DEVICE_IDS_NULL = "Device IDs cannot be null";
71 private static final String DEVICE_IDS_EMPTY = "Device IDs cannot be empty";
72 private static final String NAME_NULL = "Name cannot be null";
73
Simon Hunt8f60ff82017-04-24 17:19:30 -070074 private static final String PEER_LOCATIONS = "peerLocations";
75
Thomas Vachuska48448082016-02-19 22:14:54 -080076 private final Logger log = getLogger(getClass());
77
Simon Hunt8f60ff82017-04-24 17:19:30 -070078 private final NetworkConfigListener networkConfigListener =
79 new InternalNetworkConfigListener();
80
Thomas Vachuska48448082016-02-19 22:14:54 -080081 private RegionStoreDelegate delegate = this::post;
82
Ray Milkeyd84f89b2018-08-17 14:54:17 -070083 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuska48448082016-02-19 22:14:54 -080084 protected RegionStore store;
85
Ray Milkeyd84f89b2018-08-17 14:54:17 -070086 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Simon Hunt53612212016-12-04 17:19:52 -080087 protected NetworkConfigService networkConfigService;
88
Thomas Vachuska48448082016-02-19 22:14:54 -080089 @Activate
90 public void activate() {
91 store.setDelegate(delegate);
92 eventDispatcher.addSink(RegionEvent.class, listenerRegistry);
Simon Hunt8f60ff82017-04-24 17:19:30 -070093 networkConfigService.addListener(networkConfigListener);
Thomas Vachuska48448082016-02-19 22:14:54 -080094 log.info("Started");
95 }
96
97 @Deactivate
98 public void deactivate() {
99 store.unsetDelegate(delegate);
Simon Hunt8f60ff82017-04-24 17:19:30 -0700100 networkConfigService.removeListener(networkConfigListener);
Thomas Vachuska48448082016-02-19 22:14:54 -0800101 eventDispatcher.removeSink(RegionEvent.class);
102 log.info("Stopped");
103 }
104
Simon Hunt53612212016-12-04 17:19:52 -0800105 private String dstr(double d) {
106 return Double.toString(d);
107 }
108
109 private Annotations genAnnots(RegionId id) {
110 BasicRegionConfig cfg =
111 networkConfigService.getConfig(id, BasicRegionConfig.class);
Simon Hunt53612212016-12-04 17:19:52 -0800112 if (cfg == null) {
113 return DefaultAnnotations.builder().build();
114 }
Simon Hunt8f60ff82017-04-24 17:19:30 -0700115 return genAnnots(cfg, id);
116 }
117
118 private Annotations genAnnots(BasicRegionConfig cfg, RegionId rid) {
Simon Hunt53612212016-12-04 17:19:52 -0800119
120 DefaultAnnotations.Builder builder = DefaultAnnotations.builder()
121 .set(BasicElementConfig.NAME, cfg.name())
122 .set(BasicElementConfig.LATITUDE, dstr(cfg.latitude()))
123 .set(BasicElementConfig.LONGITUDE, dstr(cfg.longitude()));
124
125 // only set the UI_TYPE annotation if it is not null in the config
126 String uiType = cfg.uiType();
127 if (uiType != null) {
128 builder.set(BasicElementConfig.UI_TYPE, uiType);
129 }
130
Simon Hunt8f60ff82017-04-24 17:19:30 -0700131 List<LayoutLocation> locMappings = cfg.getMappings();
132 builder.set(PEER_LOCATIONS, toCompactListString(locMappings));
133
Simon Hunt53612212016-12-04 17:19:52 -0800134 return builder.build();
135 }
136
Thomas Vachuska48448082016-02-19 22:14:54 -0800137 @Override
138 public Region createRegion(RegionId regionId, String name, Region.Type type,
139 List<Set<NodeId>> masterNodeIds) {
140 checkNotNull(regionId, REGION_ID_NULL);
141 checkNotNull(name, NAME_NULL);
142 checkNotNull(name, REGION_TYPE_NULL);
Simon Hunt53612212016-12-04 17:19:52 -0800143
144 return store.createRegion(regionId, name, type, genAnnots(regionId),
Simon Hunt8f60ff82017-04-24 17:19:30 -0700145 masterNodeIds == null ? of() : masterNodeIds);
Thomas Vachuska48448082016-02-19 22:14:54 -0800146 }
147
148 @Override
149 public Region updateRegion(RegionId regionId, String name, Region.Type type,
150 List<Set<NodeId>> masterNodeIds) {
151 checkNotNull(regionId, REGION_ID_NULL);
152 checkNotNull(name, NAME_NULL);
153 checkNotNull(name, REGION_TYPE_NULL);
Simon Hunt53612212016-12-04 17:19:52 -0800154
155 return store.updateRegion(regionId, name, type, genAnnots(regionId),
Simon Hunt8f60ff82017-04-24 17:19:30 -0700156 masterNodeIds == null ? of() : masterNodeIds);
Thomas Vachuska48448082016-02-19 22:14:54 -0800157 }
158
159 @Override
160 public void removeRegion(RegionId regionId) {
161 checkNotNull(regionId, REGION_ID_NULL);
162 store.removeRegion(regionId);
163 }
164
165 @Override
166 public void addDevices(RegionId regionId, Collection<DeviceId> deviceIds) {
167 checkNotNull(regionId, REGION_ID_NULL);
168 checkNotNull(deviceIds, DEVICE_IDS_NULL);
169 checkState(!deviceIds.isEmpty(), DEVICE_IDS_EMPTY);
170 store.addDevices(regionId, deviceIds);
171 }
172
173 @Override
174 public void removeDevices(RegionId regionId, Collection<DeviceId> deviceIds) {
175 checkNotNull(regionId, REGION_ID_NULL);
176 checkNotNull(deviceIds, DEVICE_IDS_NULL);
177 checkState(!deviceIds.isEmpty(), DEVICE_IDS_EMPTY);
178 store.removeDevices(regionId, deviceIds);
179 }
180
181 @Override
182 public Set<Region> getRegions() {
Heedo Kang4a47a302016-02-29 17:40:23 +0900183 checkPermission(REGION_READ);
Thomas Vachuska48448082016-02-19 22:14:54 -0800184 return store.getRegions();
185 }
186
187 @Override
188 public Region getRegion(RegionId regionId) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900189 checkPermission(REGION_READ);
Thomas Vachuska48448082016-02-19 22:14:54 -0800190 checkNotNull(regionId, REGION_ID_NULL);
191 return store.getRegion(regionId);
192 }
193
194 @Override
195 public Region getRegionForDevice(DeviceId deviceId) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900196 checkPermission(REGION_READ);
Thomas Vachuska48448082016-02-19 22:14:54 -0800197 checkNotNull(deviceId, DEVICE_ID_NULL);
198 return store.getRegionForDevice(deviceId);
199 }
200
201 @Override
202 public Set<DeviceId> getRegionDevices(RegionId regionId) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900203 checkPermission(REGION_READ);
Thomas Vachuska48448082016-02-19 22:14:54 -0800204 checkNotNull(regionId, REGION_ID_NULL);
205 return store.getRegionDevices(regionId);
206 }
207
Steven Burrows19e6e4f2016-10-05 13:27:07 -0500208 @Override
209 public Set<HostId> getRegionHosts(RegionId regionId) {
210 checkPermission(REGION_READ);
211 checkNotNull(regionId, REGION_ID_NULL);
Simon Hunt1bee5292016-10-14 11:02:33 -0700212 // TODO: compute hosts from region devices
213 return ImmutableSet.of();
Steven Burrows19e6e4f2016-10-05 13:27:07 -0500214 }
Simon Hunt351282d2016-10-14 16:27:48 +0000215
Simon Hunt8f60ff82017-04-24 17:19:30 -0700216 // by default allowed, otherwise check flag
217 private boolean isAllowed(BasicRegionConfig cfg) {
218 return (cfg == null || cfg.isAllowed());
219 }
220
221 private class InternalNetworkConfigListener implements NetworkConfigListener {
222 @Override
223 public void event(NetworkConfigEvent event) {
224 RegionId rid = (RegionId) event.subject();
225 BasicRegionConfig cfg =
226 networkConfigService.getConfig(rid, BasicRegionConfig.class);
227
228 if (!isAllowed(cfg)) {
229 kickOutBadRegion(rid);
230
231 } else {
232 // (1) Find the region
233 // (2) Syntehsize new region + cfg details
234 // (3) re-insert new region element into store
235
236 try {
237 Region region = getRegion(rid);
238 String name = region.name();
239 Region.Type type = region.type();
240 Annotations annots = genAnnots(cfg, rid);
241 List<Set<NodeId>> masterNodeIds = region.masters();
242
243 store.updateRegion(rid, name, type, annots, masterNodeIds);
244
245 } catch (ItemNotFoundException infe) {
246 log.debug("warn: no region found with id {}", rid);
247 }
248 }
249 }
250
251 @Override
252 public boolean isRelevant(NetworkConfigEvent event) {
253 return (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED
254 || event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED)
255 && (event.configClass().equals(BasicRegionConfig.class));
256 }
257
258 private void kickOutBadRegion(RegionId regionId) {
259 Region badRegion = getRegion(regionId);
260 if (badRegion != null) {
261 removeRegion(regionId);
262 }
263 }
264 }
Thomas Vachuska48448082016-02-19 22:14:54 -0800265}