blob: 3b4dca9cb867a7d04668398a96e4b82e47e2353a [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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;
Ray Milkey0742ec92014-10-13 08:39:55 -070017
Jonathan Hart066244c2015-06-23 09:46:19 -070018import com.google.common.collect.ImmutableSet;
19import com.google.common.collect.Sets;
Ray Milkey0742ec92014-10-13 08:39:55 -070020import org.apache.felix.scr.annotations.Activate;
21import org.apache.felix.scr.annotations.Component;
22import org.apache.felix.scr.annotations.Deactivate;
23import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
Brian O'Connorabafb502014-12-02 22:26:20 -080025import org.onosproject.net.ConnectPoint;
26import org.onosproject.net.DeviceId;
27import org.onosproject.net.Link;
28import org.onosproject.net.Path;
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070029import org.onosproject.net.device.DeviceService;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.net.intent.Intent;
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070031import org.onosproject.net.intent.IntentException;
Brian O'Connorabafb502014-12-02 22:26:20 -080032import org.onosproject.net.intent.IntentExtensionService;
33import org.onosproject.net.intent.LinkCollectionIntent;
34import org.onosproject.net.intent.MultiPointToSinglePointIntent;
35import org.onosproject.net.intent.PointToPointIntent;
Ray Milkey0742ec92014-10-13 08:39:55 -070036
Jonathan Hart066244c2015-06-23 09:46:19 -070037import java.util.Collections;
38import java.util.HashMap;
39import java.util.List;
40import java.util.Map;
Ray Milkey6e0fb302015-04-16 14:44:12 -070041
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070042import static org.onosproject.net.intent.constraint.PartialFailureConstraint.intentAllowsPartialFailure;
43
44
Ray Milkey0742ec92014-10-13 08:39:55 -070045/**
46 * An intent compiler for
Brian O'Connorabafb502014-12-02 22:26:20 -080047 * {@link org.onosproject.net.intent.MultiPointToSinglePointIntent}.
Ray Milkey0742ec92014-10-13 08:39:55 -070048 */
49@Component(immediate = true)
50public class MultiPointToSinglePointIntentCompiler
Luca Preted26ea652017-01-03 15:59:30 -080051 extends ConnectivityIntentCompiler<MultiPointToSinglePointIntent> {
Ray Milkey0742ec92014-10-13 08:39:55 -070052
53 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
54 protected IntentExtensionService intentManager;
55
56 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070057 protected DeviceService deviceService;
58
Ray Milkey0742ec92014-10-13 08:39:55 -070059 @Activate
60 public void activate() {
Ray Milkey0742ec92014-10-13 08:39:55 -070061 intentManager.registerCompiler(MultiPointToSinglePointIntent.class, this);
62 }
63
64 @Deactivate
65 public void deactivate() {
66 intentManager.unregisterCompiler(PointToPointIntent.class);
67 }
68
69 @Override
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -080070 public List<Intent> compile(MultiPointToSinglePointIntent intent, List<Intent> installable) {
Jonathan Hartf5e35802014-12-01 20:45:18 -080071 Map<DeviceId, Link> links = new HashMap<>();
Ray Milkey6e0fb302015-04-16 14:44:12 -070072 ConnectPoint egressPoint = intent.egressPoint();
Ray Milkey0742ec92014-10-13 08:39:55 -070073
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070074 final boolean allowMissingPaths = intentAllowsPartialFailure(intent);
Luca Preted26ea652017-01-03 15:59:30 -080075 boolean hasPaths = false;
76 boolean missingSomePaths = false;
77
Ray Milkey0742ec92014-10-13 08:39:55 -070078 for (ConnectPoint ingressPoint : intent.ingressPoints()) {
Ray Milkey6e0fb302015-04-16 14:44:12 -070079 if (ingressPoint.deviceId().equals(egressPoint.deviceId())) {
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070080 if (deviceService.isAvailable(ingressPoint.deviceId())) {
Luca Preted26ea652017-01-03 15:59:30 -080081 hasPaths = true;
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070082 } else {
Luca Preted26ea652017-01-03 15:59:30 -080083 missingSomePaths = true;
Ray Milkey6e0fb302015-04-16 14:44:12 -070084 }
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070085 continue;
Jonathan Hartf5e35802014-12-01 20:45:18 -080086 }
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070087
Luca Preted26ea652017-01-03 15:59:30 -080088 Path path = getPath(intent, ingressPoint.deviceId(), intent.egressPoint().deviceId());
89
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070090 if (path != null) {
Luca Preted26ea652017-01-03 15:59:30 -080091 hasPaths = true;
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070092
93 for (Link link : path.links()) {
Jonathan Hart4cb39882015-08-12 23:50:55 -040094 if (links.containsKey(link.dst().deviceId())) {
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070095 // We've already reached the existing tree with the first
96 // part of this path. Add the merging point with different
97 // incoming port, but don't add the remainder of the path
98 // in case it differs from the path we already have.
99 links.put(link.src().deviceId(), link);
100 break;
101 }
102 links.put(link.src().deviceId(), link);
103 }
104 } else {
Luca Preted26ea652017-01-03 15:59:30 -0800105 missingSomePaths = true;
Jonathan Hart96c5a4a2015-07-31 14:23:33 -0700106 }
107 }
108
Luca Preted26ea652017-01-03 15:59:30 -0800109 if (!hasPaths) {
110 throw new IntentException("Cannot find any path between ingress and egress points.");
111 } else if (!allowMissingPaths && missingSomePaths) {
112 throw new IntentException("Missing some paths between ingress and egress points.");
Ray Milkey0742ec92014-10-13 08:39:55 -0700113 }
114
Ray Milkeyebc5d222015-03-18 15:45:36 -0700115 Intent result = LinkCollectionIntent.builder()
116 .appId(intent.appId())
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -0700117 .key(intent.key())
Ray Milkeyebc5d222015-03-18 15:45:36 -0700118 .treatment(intent.treatment())
Pier Ventre973bb032016-10-11 08:57:39 -0700119 .selector(intent.selector())
Jonathan Hart066244c2015-06-23 09:46:19 -0700120 .links(Sets.newHashSet(links.values()))
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700121 .filteredIngressPoints(intent.filteredIngressPoints())
122 .filteredEgressPoints(ImmutableSet.of(intent.filteredEgressPoint()))
Ray Milkeyebc5d222015-03-18 15:45:36 -0700123 .priority(intent.priority())
Sho SHIMIZU1b46a972015-04-10 17:33:00 -0700124 .constraints(intent.constraints())
Luca Prete670ac5d2017-02-03 15:55:43 -0800125 .resourceGroup(intent.resourceGroup())
Ray Milkeyebc5d222015-03-18 15:45:36 -0700126 .build();
127
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700128 return Collections.singletonList(result);
Ray Milkey0742ec92014-10-13 08:39:55 -0700129 }
Ray Milkey0742ec92014-10-13 08:39:55 -0700130}