blob: 753badc570f4451f69eef03dcd4b820ee67dd7ff [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
Michele Santuaria271faa2017-04-18 11:36:37 +020019import org.onosproject.net.Device;
20import org.onosproject.net.DeviceId;
21import org.onosproject.net.device.DeviceService;
22import org.onosproject.net.domain.DomainId;
23import org.onosproject.net.domain.DomainService;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070024import org.osgi.service.component.annotations.Activate;
25import org.osgi.service.component.annotations.Component;
26import org.osgi.service.component.annotations.Deactivate;
27import org.osgi.service.component.annotations.Reference;
28import org.osgi.service.component.annotations.ReferenceCardinality;
Michele Santuaria271faa2017-04-18 11:36:37 +020029import org.slf4j.Logger;
30import org.slf4j.LoggerFactory;
31
32import java.util.HashSet;
Yuta HIGUCHI435f23c2018-03-09 09:01:53 -080033import java.util.Optional;
Michele Santuaria271faa2017-04-18 11:36:37 +020034import 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 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070041@Component(immediate = true, service = DomainService.class)
Michele Santuaria271faa2017-04-18 11:36:37 +020042public class DomainManager implements DomainService {
43
44 private static final String DOMAIN_ID = "domainId";
45 private static final String LOCAL_DOMAIN = "local";
46 private final Logger log = LoggerFactory.getLogger(getClass());
Ray Milkeyd84f89b2018-08-17 14:54:17 -070047 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Michele Santuaria271faa2017-04-18 11:36:37 +020048 protected DeviceService deviceService;
49
50 @Activate
51 public void activate() {
52
53 log.info("Started");
54 }
55
56 @Deactivate
57 public void deactivate() {
58 log.info("Stopped");
59 }
60
61 @Override
62 public Set<DomainId> getDomainIds() {
63 Set<DomainId> domIds = new HashSet<>();
64 deviceService.getAvailableDevices().forEach(dev ->
65 domIds.add(getAnnotatedDomainId(dev)));
66 return domIds;
67 }
68
69 @Override
70 public Set<DeviceId> getDeviceIds(DomainId domainId) {
71 Set<DeviceId> domainIds = new HashSet<>();
72 deviceService.getAvailableDevices().forEach(dev -> {
73 if (getAnnotatedDomainId(dev).equals(domainId)) {
74 domainIds.add(dev.id());
75 }
76 });
77 return domainIds;
78 }
79
80 @Override
81 public DomainId getDomain(DeviceId deviceId) {
82 checkNotNull(deviceId);
Yuta HIGUCHI435f23c2018-03-09 09:01:53 -080083 return Optional.ofNullable(deviceService.getDevice(deviceId))
84 .map(this::getAnnotatedDomainId)
85 .orElse(null);
Michele Santuaria271faa2017-04-18 11:36:37 +020086 }
87
88 private DomainId getAnnotatedDomainId(Device device) {
89 if (!device.annotations().keys().contains(DOMAIN_ID)) {
90 return DomainId.LOCAL;
91 } else {
92 return DomainId.domainId(
93 device.annotations().value(DOMAIN_ID));
94 }
95 }
96}