blob: d63d37999da6b09c3812e7cc8237a47b2a608425 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 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 Hartf5e35802014-12-01 20:45:18 -080018import java.util.Arrays;
19import java.util.HashMap;
20import java.util.List;
21import java.util.Map;
22import java.util.Set;
23
Ray Milkey0742ec92014-10-13 08:39:55 -070024import org.apache.felix.scr.annotations.Activate;
25import org.apache.felix.scr.annotations.Component;
26import org.apache.felix.scr.annotations.Deactivate;
27import org.apache.felix.scr.annotations.Reference;
28import org.apache.felix.scr.annotations.ReferenceCardinality;
Brian O'Connorabafb502014-12-02 22:26:20 -080029import org.onosproject.net.ConnectPoint;
30import org.onosproject.net.DeviceId;
31import org.onosproject.net.Link;
32import org.onosproject.net.Path;
33import org.onosproject.net.intent.Intent;
34import org.onosproject.net.intent.IntentCompiler;
35import org.onosproject.net.intent.IntentExtensionService;
36import org.onosproject.net.intent.LinkCollectionIntent;
37import org.onosproject.net.intent.MultiPointToSinglePointIntent;
38import org.onosproject.net.intent.PointToPointIntent;
Sho SHIMIZU6c28f832015-02-20 16:12:19 -080039import org.onosproject.net.intent.impl.PathNotFoundException;
Brian O'Connorabafb502014-12-02 22:26:20 -080040import org.onosproject.net.resource.LinkResourceAllocations;
41import org.onosproject.net.topology.PathService;
Ray Milkey0742ec92014-10-13 08:39:55 -070042
Pavlin Radoslavov2811c402015-02-25 14:30:17 -080043import com.google.common.collect.ImmutableSet;
Jonathan Hartf5e35802014-12-01 20:45:18 -080044import com.google.common.collect.Sets;
Thomas Vachuskab97cf282014-10-20 23:31:12 -070045
Ray Milkey6e0fb302015-04-16 14:44:12 -070046import static org.onosproject.net.DefaultEdgeLink.createEdgeLink;
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
Ray Milkey0742ec92014-10-13 08:39:55 -070062 @Activate
63 public void activate() {
Ray Milkey0742ec92014-10-13 08:39:55 -070064 intentManager.registerCompiler(MultiPointToSinglePointIntent.class, this);
65 }
66
67 @Deactivate
68 public void deactivate() {
69 intentManager.unregisterCompiler(PointToPointIntent.class);
70 }
71
72 @Override
Brian O'Connorfa81eae2014-10-30 13:20:05 -070073 public List<Intent> compile(MultiPointToSinglePointIntent intent, List<Intent> installable,
74 Set<LinkResourceAllocations> resources) {
Jonathan Hartf5e35802014-12-01 20:45:18 -080075 Map<DeviceId, Link> links = new HashMap<>();
Ray Milkey6e0fb302015-04-16 14:44:12 -070076 Map<DeviceId, Link> edgeLinks = new HashMap<>();
77 ConnectPoint egressPoint = intent.egressPoint();
Ray Milkey0742ec92014-10-13 08:39:55 -070078
79 for (ConnectPoint ingressPoint : intent.ingressPoints()) {
Ray Milkey6e0fb302015-04-16 14:44:12 -070080 if (ingressPoint.deviceId().equals(egressPoint.deviceId())) {
81 edgeLinks.put(ingressPoint.deviceId(), createEdgeLink(ingressPoint, true));
82 edgeLinks.put(egressPoint.deviceId(), createEdgeLink(egressPoint, false));
83 } else {
84 Path path = getPath(ingressPoint, intent.egressPoint());
85 for (Link link : path.links()) {
86 if (links.containsKey(link.src().deviceId())) {
87 // We've already reached the existing tree with the first
88 // part of this path. Add the merging point with different
89 // incoming port, but don't add the remainder of the path
90 // in case it differs from the path we already have.
91 links.put(link.src().deviceId(), link);
92 break;
93 }
Jonathan Hartf5e35802014-12-01 20:45:18 -080094
Ray Milkey6e0fb302015-04-16 14:44:12 -070095 links.put(link.src().deviceId(), link);
96 }
Jonathan Hartf5e35802014-12-01 20:45:18 -080097 }
Ray Milkey0742ec92014-10-13 08:39:55 -070098 }
99
Ray Milkey6e0fb302015-04-16 14:44:12 -0700100 Set<Link> allLinks = Sets.newHashSet(links.values());
101 allLinks.addAll(edgeLinks.values());
Ray Milkeyebc5d222015-03-18 15:45:36 -0700102 Intent result = LinkCollectionIntent.builder()
103 .appId(intent.appId())
104 .selector(intent.selector())
105 .treatment(intent.treatment())
Ray Milkey6e0fb302015-04-16 14:44:12 -0700106 .links(Sets.newHashSet(allLinks))
Ray Milkeyebc5d222015-03-18 15:45:36 -0700107 .ingressPoints(intent.ingressPoints())
108 .egressPoints(ImmutableSet.of(intent.egressPoint()))
109 .priority(intent.priority())
110 .build();
111
Ray Milkey0742ec92014-10-13 08:39:55 -0700112 return Arrays.asList(result);
113 }
114
115 /**
116 * Computes a path between two ConnectPoints.
117 *
118 * @param one start of the path
119 * @param two end of the path
120 * @return Path between the two
Brian O'Connorabafb502014-12-02 22:26:20 -0800121 * @throws org.onosproject.net.intent.impl.PathNotFoundException if a path cannot be found
Ray Milkey0742ec92014-10-13 08:39:55 -0700122 */
123 private Path getPath(ConnectPoint one, ConnectPoint two) {
124 Set<Path> paths = pathService.getPaths(one.deviceId(), two.deviceId());
125 if (paths.isEmpty()) {
Sho SHIMIZU877ec2c2015-02-09 12:50:36 -0800126 throw new PathNotFoundException(one.elementId(), two.elementId());
Ray Milkey0742ec92014-10-13 08:39:55 -0700127 }
128 // TODO: let's be more intelligent about this eventually
129 return paths.iterator().next();
130 }
131}