blob: 8f0db202c1b1ffff675f5b64880f6583c727f1ac [file] [log] [blame]
Michele Santuaria271faa2017-04-18 11:36:37 +02001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Michele Santuaria271faa2017-04-18 11:36:37 +02003 *
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;
Yuta HIGUCHI435f23c2018-03-09 09:01:53 -080034import java.util.Optional;
Michele Santuaria271faa2017-04-18 11:36:37 +020035import java.util.Set;
36
37import static com.google.common.base.Preconditions.checkNotNull;
38
39/**
40 * Exposes domain topology elements and listen for updates of such elements.
41 */
42@Component(immediate = true)
43@Service
44public class DomainManager implements DomainService {
45
46 private static final String DOMAIN_ID = "domainId";
47 private static final String LOCAL_DOMAIN = "local";
48 private final Logger log = LoggerFactory.getLogger(getClass());
49 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
50 protected DeviceService deviceService;
51
52 @Activate
53 public void activate() {
54
55 log.info("Started");
56 }
57
58 @Deactivate
59 public void deactivate() {
60 log.info("Stopped");
61 }
62
63 @Override
64 public Set<DomainId> getDomainIds() {
65 Set<DomainId> domIds = new HashSet<>();
66 deviceService.getAvailableDevices().forEach(dev ->
67 domIds.add(getAnnotatedDomainId(dev)));
68 return domIds;
69 }
70
71 @Override
72 public Set<DeviceId> getDeviceIds(DomainId domainId) {
73 Set<DeviceId> domainIds = new HashSet<>();
74 deviceService.getAvailableDevices().forEach(dev -> {
75 if (getAnnotatedDomainId(dev).equals(domainId)) {
76 domainIds.add(dev.id());
77 }
78 });
79 return domainIds;
80 }
81
82 @Override
83 public DomainId getDomain(DeviceId deviceId) {
84 checkNotNull(deviceId);
Yuta HIGUCHI435f23c2018-03-09 09:01:53 -080085 return Optional.ofNullable(deviceService.getDevice(deviceId))
86 .map(this::getAnnotatedDomainId)
87 .orElse(null);
Michele Santuaria271faa2017-04-18 11:36:37 +020088 }
89
90 private DomainId getAnnotatedDomainId(Device device) {
91 if (!device.annotations().keys().contains(DOMAIN_ID)) {
92 return DomainId.LOCAL;
93 } else {
94 return DomainId.domainId(
95 device.annotations().value(DOMAIN_ID));
96 }
97 }
98}