blob: 29802bb20696aea8f3e703a1d3b3df36f32b4667 [file] [log] [blame]
Michele Santuaria271faa2017-04-18 11:36:37 +02001/*
2 * Copyright 2016-present 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.domain.impl;
18
19import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
22import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
24import org.apache.felix.scr.annotations.Service;
25import org.onosproject.net.Device;
26import org.onosproject.net.DeviceId;
27import org.onosproject.net.device.DeviceService;
28import org.onosproject.net.domain.DomainId;
29import org.onosproject.net.domain.DomainService;
30import org.slf4j.Logger;
31import org.slf4j.LoggerFactory;
32
33import java.util.HashSet;
34import java.util.Set;
35
36import static com.google.common.base.Preconditions.checkNotNull;
37
38/**
39 * Exposes domain topology elements and listen for updates of such elements.
40 */
41@Component(immediate = true)
42@Service
43public class DomainManager implements DomainService {
44
45 private static final String DOMAIN_ID = "domainId";
46 private static final String LOCAL_DOMAIN = "local";
47 private final Logger log = LoggerFactory.getLogger(getClass());
48 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
49 protected DeviceService deviceService;
50
51 @Activate
52 public void activate() {
53
54 log.info("Started");
55 }
56
57 @Deactivate
58 public void deactivate() {
59 log.info("Stopped");
60 }
61
62 @Override
63 public Set<DomainId> getDomainIds() {
64 Set<DomainId> domIds = new HashSet<>();
65 deviceService.getAvailableDevices().forEach(dev ->
66 domIds.add(getAnnotatedDomainId(dev)));
67 return domIds;
68 }
69
70 @Override
71 public Set<DeviceId> getDeviceIds(DomainId domainId) {
72 Set<DeviceId> domainIds = new HashSet<>();
73 deviceService.getAvailableDevices().forEach(dev -> {
74 if (getAnnotatedDomainId(dev).equals(domainId)) {
75 domainIds.add(dev.id());
76 }
77 });
78 return domainIds;
79 }
80
81 @Override
82 public DomainId getDomain(DeviceId deviceId) {
83 checkNotNull(deviceId);
84 return checkNotNull(getAnnotatedDomainId(deviceService.getDevice(deviceId)));
85 }
86
87 private DomainId getAnnotatedDomainId(Device device) {
88 if (!device.annotations().keys().contains(DOMAIN_ID)) {
89 return DomainId.LOCAL;
90 } else {
91 return DomainId.domainId(
92 device.annotations().value(DOMAIN_ID));
93 }
94 }
95}