alshabib | ab98466 | 2014-12-04 18:56:18 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
alshabib | ab98466 | 2014-12-04 18:56:18 -0800 | [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 | */ |
Sho SHIMIZU | 6c28f83 | 2015-02-20 16:12:19 -0800 | [diff] [blame] | 16 | package org.onosproject.net.intent.impl.compiler; |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 17 | |
Jonathan Hart | 066244c | 2015-06-23 09:46:19 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableSet; |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
Luca Prete | d26ea65 | 2017-01-03 15:59:30 -0800 | [diff] [blame] | 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 24 | import org.onosproject.net.ConnectPoint; |
| 25 | import org.onosproject.net.Link; |
| 26 | import org.onosproject.net.Path; |
Luca Prete | d26ea65 | 2017-01-03 15:59:30 -0800 | [diff] [blame] | 27 | import org.onosproject.net.device.DeviceService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 28 | import org.onosproject.net.intent.Intent; |
Luca Prete | d26ea65 | 2017-01-03 15:59:30 -0800 | [diff] [blame] | 29 | import org.onosproject.net.intent.IntentException; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 30 | import org.onosproject.net.intent.LinkCollectionIntent; |
| 31 | import org.onosproject.net.intent.SinglePointToMultiPointIntent; |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 32 | |
Jonathan Hart | 066244c | 2015-06-23 09:46:19 -0700 | [diff] [blame] | 33 | import java.util.Collections; |
| 34 | import java.util.HashSet; |
| 35 | import java.util.List; |
| 36 | import java.util.Set; |
Pavlin Radoslavov | 2811c40 | 2015-02-25 14:30:17 -0800 | [diff] [blame] | 37 | |
Luca Prete | d26ea65 | 2017-01-03 15:59:30 -0800 | [diff] [blame] | 38 | import static org.onosproject.net.intent.constraint.PartialFailureConstraint.intentAllowsPartialFailure; |
| 39 | |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 40 | @Component(immediate = true) |
| 41 | public class SinglePointToMultiPointIntentCompiler |
| 42 | extends ConnectivityIntentCompiler<SinglePointToMultiPointIntent> { |
| 43 | |
Luca Prete | d26ea65 | 2017-01-03 15:59:30 -0800 | [diff] [blame] | 44 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 45 | protected DeviceService deviceService; |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 46 | |
| 47 | @Activate |
| 48 | public void activate() { |
Luca Prete | d26ea65 | 2017-01-03 15:59:30 -0800 | [diff] [blame] | 49 | intentManager.registerCompiler(SinglePointToMultiPointIntent.class, this); |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | @Deactivate |
| 53 | public void deactivate() { |
| 54 | intentManager.unregisterCompiler(SinglePointToMultiPointIntent.class); |
| 55 | } |
| 56 | |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 57 | @Override |
| 58 | public List<Intent> compile(SinglePointToMultiPointIntent intent, |
Sho SHIMIZU | ec07ffd | 2016-02-22 20:45:21 -0800 | [diff] [blame] | 59 | List<Intent> installable) { |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 60 | Set<Link> links = new HashSet<>(); |
Jonathan Hart | 066244c | 2015-06-23 09:46:19 -0700 | [diff] [blame] | 61 | |
Luca Prete | d26ea65 | 2017-01-03 15:59:30 -0800 | [diff] [blame] | 62 | final boolean allowMissingPaths = intentAllowsPartialFailure(intent); |
| 63 | boolean hasPaths = false; |
| 64 | boolean missingSomePaths = false; |
| 65 | |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 66 | for (ConnectPoint egressPoint : intent.egressPoints()) { |
Jonathan Hart | 066244c | 2015-06-23 09:46:19 -0700 | [diff] [blame] | 67 | if (egressPoint.deviceId().equals(intent.ingressPoint().deviceId())) { |
Luca Prete | d26ea65 | 2017-01-03 15:59:30 -0800 | [diff] [blame] | 68 | // Do not need to look for paths, since ingress and egress |
| 69 | // devices are the same. |
| 70 | if (deviceService.isAvailable(egressPoint.deviceId())) { |
| 71 | hasPaths = true; |
| 72 | } else { |
| 73 | missingSomePaths = true; |
| 74 | } |
Jonathan Hart | 066244c | 2015-06-23 09:46:19 -0700 | [diff] [blame] | 75 | continue; |
| 76 | } |
| 77 | |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 78 | Path path = getPath(intent, intent.ingressPoint().deviceId(), egressPoint.deviceId()); |
Luca Prete | d26ea65 | 2017-01-03 15:59:30 -0800 | [diff] [blame] | 79 | if (path != null) { |
| 80 | hasPaths = true; |
| 81 | links.addAll(path.links()); |
| 82 | } else { |
| 83 | missingSomePaths = true; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | if (!hasPaths) { |
| 88 | throw new IntentException("Cannot find any path between ingress and egress points."); |
| 89 | } else if (!allowMissingPaths && missingSomePaths) { |
| 90 | throw new IntentException("Missing some paths between ingress and egress points."); |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 91 | } |
| 92 | |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 93 | Intent result = LinkCollectionIntent.builder() |
| 94 | .appId(intent.appId()) |
| 95 | .key(intent.key()) |
Pier Ventre | 973bb03 | 2016-10-11 08:57:39 -0700 | [diff] [blame] | 96 | .selector(intent.selector()) |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 97 | .treatment(intent.treatment()) |
| 98 | .links(links) |
Yi Tseng | 2a81c9d | 2016-09-14 10:14:24 -0700 | [diff] [blame] | 99 | .filteredIngressPoints(ImmutableSet.of(intent.filteredIngressPoint())) |
| 100 | .filteredEgressPoints(intent.filteredEgressPoints()) |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 101 | .priority(intent.priority()) |
Nicholas Dean | 126b8af | 2016-07-18 14:43:13 -0700 | [diff] [blame] | 102 | .applyTreatmentOnEgress(true) |
Sho SHIMIZU | 1b46a97 | 2015-04-10 17:33:00 -0700 | [diff] [blame] | 103 | .constraints(intent.constraints()) |
Luca Prete | 670ac5d | 2017-02-03 15:55:43 -0800 | [diff] [blame^] | 104 | .resourceGroup(intent.resourceGroup()) |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 105 | .build(); |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 106 | |
Sho SHIMIZU | 98ffca8 | 2015-05-11 08:39:24 -0700 | [diff] [blame] | 107 | return Collections.singletonList(result); |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 108 | } |
Nicholas Dean | 126b8af | 2016-07-18 14:43:13 -0700 | [diff] [blame] | 109 | } |