blob: cac5d6899b2bdfff1a2c93ecece64cc3e5a59452 [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;
31import org.onosproject.net.intent.IntentCompiler;
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070032import org.onosproject.net.intent.IntentException;
Brian O'Connorabafb502014-12-02 22:26:20 -080033import org.onosproject.net.intent.IntentExtensionService;
34import org.onosproject.net.intent.LinkCollectionIntent;
35import org.onosproject.net.intent.MultiPointToSinglePointIntent;
36import org.onosproject.net.intent.PointToPointIntent;
Brian O'Connorabafb502014-12-02 22:26:20 -080037import org.onosproject.net.topology.PathService;
Ray Milkey0742ec92014-10-13 08:39:55 -070038
Jonathan Hart066244c2015-06-23 09:46:19 -070039import java.util.Collections;
40import java.util.HashMap;
41import java.util.List;
42import java.util.Map;
43import java.util.Set;
Ray Milkey6e0fb302015-04-16 14:44:12 -070044
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070045import static org.onosproject.net.intent.constraint.PartialFailureConstraint.intentAllowsPartialFailure;
46
47
Ray Milkey0742ec92014-10-13 08:39:55 -070048/**
49 * An intent compiler for
Brian O'Connorabafb502014-12-02 22:26:20 -080050 * {@link org.onosproject.net.intent.MultiPointToSinglePointIntent}.
Ray Milkey0742ec92014-10-13 08:39:55 -070051 */
52@Component(immediate = true)
53public class MultiPointToSinglePointIntentCompiler
54 implements IntentCompiler<MultiPointToSinglePointIntent> {
55
56 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
57 protected IntentExtensionService intentManager;
58
59 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
60 protected PathService pathService;
61
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070062 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
63 protected DeviceService deviceService;
64
Ray Milkey0742ec92014-10-13 08:39:55 -070065 @Activate
66 public void activate() {
Ray Milkey0742ec92014-10-13 08:39:55 -070067 intentManager.registerCompiler(MultiPointToSinglePointIntent.class, this);
68 }
69
70 @Deactivate
71 public void deactivate() {
72 intentManager.unregisterCompiler(PointToPointIntent.class);
73 }
74
75 @Override
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -080076 public List<Intent> compile(MultiPointToSinglePointIntent intent, List<Intent> installable) {
Jonathan Hartf5e35802014-12-01 20:45:18 -080077 Map<DeviceId, Link> links = new HashMap<>();
Ray Milkey6e0fb302015-04-16 14:44:12 -070078 ConnectPoint egressPoint = intent.egressPoint();
Ray Milkey0742ec92014-10-13 08:39:55 -070079
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070080 final boolean allowMissingPaths = intentAllowsPartialFailure(intent);
81 boolean partialTree = false;
82 boolean anyMissingPaths = false;
Ray Milkey0742ec92014-10-13 08:39:55 -070083 for (ConnectPoint ingressPoint : intent.ingressPoints()) {
Ray Milkey6e0fb302015-04-16 14:44:12 -070084 if (ingressPoint.deviceId().equals(egressPoint.deviceId())) {
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070085 if (deviceService.isAvailable(ingressPoint.deviceId())) {
86 partialTree = true;
87 } else {
88 anyMissingPaths = true;
Ray Milkey6e0fb302015-04-16 14:44:12 -070089 }
Jonathan Hart066244c2015-06-23 09:46:19 -070090
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070091 continue;
Jonathan Hartf5e35802014-12-01 20:45:18 -080092 }
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070093
94 Path path = getPath(ingressPoint, intent.egressPoint());
95 if (path != null) {
96 partialTree = true;
97
98 for (Link link : path.links()) {
Jonathan Hart4cb39882015-08-12 23:50:55 -040099 if (links.containsKey(link.dst().deviceId())) {
Jonathan Hart96c5a4a2015-07-31 14:23:33 -0700100 // We've already reached the existing tree with the first
101 // part of this path. Add the merging point with different
102 // incoming port, but don't add the remainder of the path
103 // in case it differs from the path we already have.
104 links.put(link.src().deviceId(), link);
105 break;
106 }
107 links.put(link.src().deviceId(), link);
108 }
109 } else {
110 anyMissingPaths = true;
111 }
112 }
113
114 if (!partialTree) {
115 throw new IntentException("Could not find any paths between ingress and egress points.");
116 } else if (!allowMissingPaths && anyMissingPaths) {
117 throw new IntentException("Missing some paths between ingress and egress ports.");
Ray Milkey0742ec92014-10-13 08:39:55 -0700118 }
119
Ray Milkeyebc5d222015-03-18 15:45:36 -0700120 Intent result = LinkCollectionIntent.builder()
121 .appId(intent.appId())
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -0700122 .key(intent.key())
Ray Milkeyebc5d222015-03-18 15:45:36 -0700123 .treatment(intent.treatment())
Pier Ventre973bb032016-10-11 08:57:39 -0700124 .selector(intent.selector())
Jonathan Hart066244c2015-06-23 09:46:19 -0700125 .links(Sets.newHashSet(links.values()))
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700126 .filteredIngressPoints(intent.filteredIngressPoints())
127 .filteredEgressPoints(ImmutableSet.of(intent.filteredEgressPoint()))
Ray Milkeyebc5d222015-03-18 15:45:36 -0700128 .priority(intent.priority())
Sho SHIMIZU1b46a972015-04-10 17:33:00 -0700129 .constraints(intent.constraints())
Ray Milkeyebc5d222015-03-18 15:45:36 -0700130 .build();
131
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700132 return Collections.singletonList(result);
Ray Milkey0742ec92014-10-13 08:39:55 -0700133 }
134
135 /**
136 * Computes a path between two ConnectPoints.
137 *
138 * @param one start of the path
139 * @param two end of the path
140 * @return Path between the two
Ray Milkey0742ec92014-10-13 08:39:55 -0700141 */
142 private Path getPath(ConnectPoint one, ConnectPoint two) {
143 Set<Path> paths = pathService.getPaths(one.deviceId(), two.deviceId());
144 if (paths.isEmpty()) {
Jonathan Hart96c5a4a2015-07-31 14:23:33 -0700145 return null;
Ray Milkey0742ec92014-10-13 08:39:55 -0700146 }
147 // TODO: let's be more intelligent about this eventually
148 return paths.iterator().next();
149 }
150}