blob: 504e4f3e8a51cebe1c5847fe947c7459b10dc77c [file] [log] [blame]
alshabibab984662014-12-04 18:56:18 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
alshabibab984662014-12-04 18:56:18 -08003 *
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 SHIMIZU6c28f832015-02-20 16:12:19 -080016package org.onosproject.net.intent.impl.compiler;
Michele Santuari4a338072014-11-05 18:38:55 +010017
Jonathan Hart066244c2015-06-23 09:46:19 -070018import com.google.common.collect.ImmutableSet;
Michele Santuari4a338072014-11-05 18:38:55 +010019import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
Luca Preted26ea652017-01-03 15:59:30 -080022import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
Brian O'Connorabafb502014-12-02 22:26:20 -080024import org.onosproject.net.ConnectPoint;
25import org.onosproject.net.Link;
26import org.onosproject.net.Path;
Luca Preted26ea652017-01-03 15:59:30 -080027import org.onosproject.net.device.DeviceService;
Brian O'Connorabafb502014-12-02 22:26:20 -080028import org.onosproject.net.intent.Intent;
Luca Preted26ea652017-01-03 15:59:30 -080029import org.onosproject.net.intent.IntentException;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.net.intent.LinkCollectionIntent;
31import org.onosproject.net.intent.SinglePointToMultiPointIntent;
Michele Santuari4a338072014-11-05 18:38:55 +010032
Jonathan Hart066244c2015-06-23 09:46:19 -070033import java.util.Collections;
34import java.util.HashSet;
35import java.util.List;
36import java.util.Set;
Pavlin Radoslavov2811c402015-02-25 14:30:17 -080037
Luca Preted26ea652017-01-03 15:59:30 -080038import static org.onosproject.net.intent.constraint.PartialFailureConstraint.intentAllowsPartialFailure;
39
Michele Santuari4a338072014-11-05 18:38:55 +010040@Component(immediate = true)
41public class SinglePointToMultiPointIntentCompiler
42 extends ConnectivityIntentCompiler<SinglePointToMultiPointIntent> {
43
Luca Preted26ea652017-01-03 15:59:30 -080044 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
45 protected DeviceService deviceService;
Michele Santuari4a338072014-11-05 18:38:55 +010046
47 @Activate
48 public void activate() {
Luca Preted26ea652017-01-03 15:59:30 -080049 intentManager.registerCompiler(SinglePointToMultiPointIntent.class, this);
Michele Santuari4a338072014-11-05 18:38:55 +010050 }
51
52 @Deactivate
53 public void deactivate() {
54 intentManager.unregisterCompiler(SinglePointToMultiPointIntent.class);
55 }
56
Michele Santuari4a338072014-11-05 18:38:55 +010057 @Override
58 public List<Intent> compile(SinglePointToMultiPointIntent intent,
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -080059 List<Intent> installable) {
Michele Santuari4a338072014-11-05 18:38:55 +010060 Set<Link> links = new HashSet<>();
Jonathan Hart066244c2015-06-23 09:46:19 -070061
Luca Preted26ea652017-01-03 15:59:30 -080062 final boolean allowMissingPaths = intentAllowsPartialFailure(intent);
63 boolean hasPaths = false;
64 boolean missingSomePaths = false;
65
Michele Santuari4a338072014-11-05 18:38:55 +010066 for (ConnectPoint egressPoint : intent.egressPoints()) {
Jonathan Hart066244c2015-06-23 09:46:19 -070067 if (egressPoint.deviceId().equals(intent.ingressPoint().deviceId())) {
Luca Preted26ea652017-01-03 15:59:30 -080068 // 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 Hart066244c2015-06-23 09:46:19 -070075 continue;
76 }
77
Michele Santuari4a338072014-11-05 18:38:55 +010078 Path path = getPath(intent, intent.ingressPoint().deviceId(), egressPoint.deviceId());
Luca Preted26ea652017-01-03 15:59:30 -080079 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 Santuari4a338072014-11-05 18:38:55 +010091 }
92
Ray Milkeyebc5d222015-03-18 15:45:36 -070093 Intent result = LinkCollectionIntent.builder()
94 .appId(intent.appId())
95 .key(intent.key())
Pier Ventre973bb032016-10-11 08:57:39 -070096 .selector(intent.selector())
Ray Milkeyebc5d222015-03-18 15:45:36 -070097 .treatment(intent.treatment())
98 .links(links)
Yi Tseng2a81c9d2016-09-14 10:14:24 -070099 .filteredIngressPoints(ImmutableSet.of(intent.filteredIngressPoint()))
100 .filteredEgressPoints(intent.filteredEgressPoints())
Ray Milkeyebc5d222015-03-18 15:45:36 -0700101 .priority(intent.priority())
Nicholas Dean126b8af2016-07-18 14:43:13 -0700102 .applyTreatmentOnEgress(true)
Sho SHIMIZU1b46a972015-04-10 17:33:00 -0700103 .constraints(intent.constraints())
Luca Prete670ac5d2017-02-03 15:55:43 -0800104 .resourceGroup(intent.resourceGroup())
Ray Milkeyebc5d222015-03-18 15:45:36 -0700105 .build();
Michele Santuari4a338072014-11-05 18:38:55 +0100106
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700107 return Collections.singletonList(result);
Michele Santuari4a338072014-11-05 18:38:55 +0100108 }
Nicholas Dean126b8af2016-07-18 14:43:13 -0700109}