Michele Santuari | a271faa | 2017-04-18 11:36:37 +0200 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Michele Santuari | a271faa | 2017-04-18 11:36:37 +0200 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.net.domain.impl; |
| 18 | |
Michele Santuari | a271faa | 2017-04-18 11:36:37 +0200 | [diff] [blame] | 19 | import org.onosproject.net.Device; |
| 20 | import org.onosproject.net.DeviceId; |
| 21 | import org.onosproject.net.device.DeviceService; |
| 22 | import org.onosproject.net.domain.DomainId; |
| 23 | import org.onosproject.net.domain.DomainService; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 24 | import org.osgi.service.component.annotations.Activate; |
| 25 | import org.osgi.service.component.annotations.Component; |
| 26 | import org.osgi.service.component.annotations.Deactivate; |
| 27 | import org.osgi.service.component.annotations.Reference; |
| 28 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Michele Santuari | a271faa | 2017-04-18 11:36:37 +0200 | [diff] [blame] | 29 | import org.slf4j.Logger; |
| 30 | import org.slf4j.LoggerFactory; |
| 31 | |
| 32 | import java.util.HashSet; |
Yuta HIGUCHI | 435f23c | 2018-03-09 09:01:53 -0800 | [diff] [blame] | 33 | import java.util.Optional; |
Michele Santuari | a271faa | 2017-04-18 11:36:37 +0200 | [diff] [blame] | 34 | import java.util.Set; |
| 35 | |
| 36 | import static com.google.common.base.Preconditions.checkNotNull; |
| 37 | |
| 38 | /** |
| 39 | * Exposes domain topology elements and listen for updates of such elements. |
| 40 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 41 | @Component(immediate = true, service = DomainService.class) |
Michele Santuari | a271faa | 2017-04-18 11:36:37 +0200 | [diff] [blame] | 42 | public 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 Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 47 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Michele Santuari | a271faa | 2017-04-18 11:36:37 +0200 | [diff] [blame] | 48 | 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 HIGUCHI | 435f23c | 2018-03-09 09:01:53 -0800 | [diff] [blame] | 83 | return Optional.ofNullable(deviceService.getDevice(deviceId)) |
| 84 | .map(this::getAnnotatedDomainId) |
| 85 | .orElse(null); |
Michele Santuari | a271faa | 2017-04-18 11:36:37 +0200 | [diff] [blame] | 86 | } |
| 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 | } |