Brian O'Connor | ce2d8b5 | 2015-07-29 16:24:13 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Brian O'Connor | ce2d8b5 | 2015-07-29 16:24:13 -0700 | [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 | package org.onosproject.incubator.net.domain.impl; |
| 17 | |
Brian O'Connor | ce2d8b5 | 2015-07-29 16:24:13 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableSet; |
| 19 | import com.google.common.collect.Maps; |
Brian O'Connor | ce2d8b5 | 2015-07-29 16:24:13 -0700 | [diff] [blame] | 20 | import org.apache.felix.scr.annotations.Activate; |
| 21 | import org.apache.felix.scr.annotations.Component; |
| 22 | import org.apache.felix.scr.annotations.Deactivate; |
Brian O'Connor | ce2d8b5 | 2015-07-29 16:24:13 -0700 | [diff] [blame] | 23 | import org.apache.felix.scr.annotations.Service; |
Brian O'Connor | ce2d8b5 | 2015-07-29 16:24:13 -0700 | [diff] [blame] | 24 | import org.onosproject.incubator.net.domain.IntentDomain; |
Brian O'Connor | bd7f878 | 2015-11-19 23:12:37 -0800 | [diff] [blame] | 25 | import org.onosproject.incubator.net.domain.IntentDomainEvent; |
Brian O'Connor | ce2d8b5 | 2015-07-29 16:24:13 -0700 | [diff] [blame] | 26 | import org.onosproject.incubator.net.domain.IntentDomainId; |
| 27 | import org.onosproject.incubator.net.domain.IntentDomainListener; |
| 28 | import org.onosproject.incubator.net.domain.IntentDomainProvider; |
Brian O'Connor | bd7f878 | 2015-11-19 23:12:37 -0800 | [diff] [blame] | 29 | import org.onosproject.incubator.net.domain.IntentDomainProviderRegistry; |
| 30 | import org.onosproject.incubator.net.domain.IntentDomainProviderService; |
Brian O'Connor | ce2d8b5 | 2015-07-29 16:24:13 -0700 | [diff] [blame] | 31 | import org.onosproject.incubator.net.domain.IntentDomainService; |
Brian O'Connor | bd7f878 | 2015-11-19 23:12:37 -0800 | [diff] [blame] | 32 | import org.onosproject.incubator.net.domain.IntentPrimitive; |
| 33 | import org.onosproject.incubator.net.domain.IntentResource; |
Brian O'Connor | ce2d8b5 | 2015-07-29 16:24:13 -0700 | [diff] [blame] | 34 | import org.onosproject.net.ConnectPoint; |
| 35 | import org.onosproject.net.DeviceId; |
Brian O'Connor | bd7f878 | 2015-11-19 23:12:37 -0800 | [diff] [blame] | 36 | import org.onosproject.net.provider.AbstractListenerProviderRegistry; |
| 37 | import org.onosproject.net.provider.AbstractProviderService; |
Brian O'Connor | ce2d8b5 | 2015-07-29 16:24:13 -0700 | [diff] [blame] | 38 | import org.slf4j.Logger; |
| 39 | import org.slf4j.LoggerFactory; |
| 40 | |
Brian O'Connor | bd7f878 | 2015-11-19 23:12:37 -0800 | [diff] [blame] | 41 | import java.util.List; |
Brian O'Connor | ce2d8b5 | 2015-07-29 16:24:13 -0700 | [diff] [blame] | 42 | import java.util.Set; |
| 43 | import java.util.concurrent.ConcurrentMap; |
| 44 | import java.util.stream.Collectors; |
| 45 | |
| 46 | /** |
| 47 | * Implementation of the intent domain service. |
| 48 | */ |
| 49 | @Component(immediate = true) |
| 50 | @Service |
| 51 | public class IntentDomainManager |
Brian O'Connor | bd7f878 | 2015-11-19 23:12:37 -0800 | [diff] [blame] | 52 | extends AbstractListenerProviderRegistry<IntentDomainEvent, IntentDomainListener, |
| 53 | IntentDomainProvider, IntentDomainProviderService> |
| 54 | implements IntentDomainService, IntentDomainProviderRegistry { |
Brian O'Connor | ce2d8b5 | 2015-07-29 16:24:13 -0700 | [diff] [blame] | 55 | |
| 56 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 57 | |
Brian O'Connor | ce2d8b5 | 2015-07-29 16:24:13 -0700 | [diff] [blame] | 58 | private final ConcurrentMap<IntentDomainId, IntentDomain> domains = Maps.newConcurrentMap(); |
| 59 | |
Brian O'Connor | ce2d8b5 | 2015-07-29 16:24:13 -0700 | [diff] [blame] | 60 | @Activate |
| 61 | protected void activate() { |
Brian O'Connor | ce2d8b5 | 2015-07-29 16:24:13 -0700 | [diff] [blame] | 62 | log.info("Started"); |
| 63 | } |
| 64 | |
Brian O'Connor | ce2d8b5 | 2015-07-29 16:24:13 -0700 | [diff] [blame] | 65 | @Deactivate |
| 66 | protected void deactivate() { |
Brian O'Connor | ce2d8b5 | 2015-07-29 16:24:13 -0700 | [diff] [blame] | 67 | log.info("Stopped"); |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public IntentDomain getDomain(IntentDomainId id) { |
| 72 | return domains.get(id); |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public Set<IntentDomain> getDomains() { |
| 77 | return ImmutableSet.copyOf(domains.values()); |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public Set<IntentDomain> getDomains(DeviceId deviceId) { |
| 82 | return domains.values().stream() |
| 83 | .filter(domain -> |
| 84 | domain.internalDevices().contains(deviceId) || |
Brian O'Connor | bd7f878 | 2015-11-19 23:12:37 -0800 | [diff] [blame] | 85 | domain.edgePorts().stream() |
| 86 | .map(ConnectPoint::deviceId) |
| 87 | .anyMatch(d -> d.equals(deviceId))) |
Brian O'Connor | ce2d8b5 | 2015-07-29 16:24:13 -0700 | [diff] [blame] | 88 | .collect(Collectors.toSet()); |
| 89 | } |
| 90 | |
| 91 | @Override |
Brian O'Connor | bd7f878 | 2015-11-19 23:12:37 -0800 | [diff] [blame] | 92 | public List<IntentResource> request(IntentDomainId domainId, IntentPrimitive primitive) { |
| 93 | IntentDomain domain = getDomain(domainId); |
| 94 | return domain.provider().request(domain, primitive); |
Brian O'Connor | ce2d8b5 | 2015-07-29 16:24:13 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | @Override |
Brian O'Connor | bd7f878 | 2015-11-19 23:12:37 -0800 | [diff] [blame] | 98 | public void submit(IntentDomainId domainId, IntentResource resource) { |
| 99 | getDomain(domainId).provider().apply(resource); |
Brian O'Connor | ce2d8b5 | 2015-07-29 16:24:13 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | @Override |
Brian O'Connor | bd7f878 | 2015-11-19 23:12:37 -0800 | [diff] [blame] | 103 | protected IntentDomainProviderService createProviderService(IntentDomainProvider provider) { |
| 104 | return new InternalDomainProviderService(provider); |
Brian O'Connor | ce2d8b5 | 2015-07-29 16:24:13 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Brian O'Connor | bd7f878 | 2015-11-19 23:12:37 -0800 | [diff] [blame] | 107 | private class InternalDomainProviderService |
| 108 | extends AbstractProviderService<IntentDomainProvider> |
| 109 | implements IntentDomainProviderService { |
Brian O'Connor | ce2d8b5 | 2015-07-29 16:24:13 -0700 | [diff] [blame] | 110 | |
Brian O'Connor | bd7f878 | 2015-11-19 23:12:37 -0800 | [diff] [blame] | 111 | InternalDomainProviderService(IntentDomainProvider provider) { |
| 112 | super(provider); |
Brian O'Connor | ce2d8b5 | 2015-07-29 16:24:13 -0700 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | } |