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 | |
| 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
| 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 24 | import org.apache.felix.scr.annotations.Service; |
| 25 | import org.onosproject.net.Device; |
| 26 | import org.onosproject.net.DeviceId; |
| 27 | import org.onosproject.net.device.DeviceService; |
| 28 | import org.onosproject.net.domain.DomainId; |
| 29 | import org.onosproject.net.domain.DomainService; |
| 30 | import org.slf4j.Logger; |
| 31 | import org.slf4j.LoggerFactory; |
| 32 | |
| 33 | import java.util.HashSet; |
| 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 | */ |
| 41 | @Component(immediate = true) |
| 42 | @Service |
| 43 | public 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 | } |