blob: fbdfa9c02c9ec9c5435b281c6f706bd949676f09 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 Open Networking Laboratory
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 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 Milkey0742ec92014-10-13 08:39:55 -070046/**
47 * An intent compiler for
Brian O'Connorabafb502014-12-02 22:26:20 -080048 * {@link org.onosproject.net.intent.MultiPointToSinglePointIntent}.
Ray Milkey0742ec92014-10-13 08:39:55 -070049 */
50@Component(immediate = true)
51public class MultiPointToSinglePointIntentCompiler
52 implements IntentCompiler<MultiPointToSinglePointIntent> {
53
54 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
55 protected IntentExtensionService intentManager;
56
57 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
58 protected PathService pathService;
59
Ray Milkey0742ec92014-10-13 08:39:55 -070060 @Activate
61 public void activate() {
Ray Milkey0742ec92014-10-13 08:39:55 -070062 intentManager.registerCompiler(MultiPointToSinglePointIntent.class, this);
63 }
64
65 @Deactivate
66 public void deactivate() {
67 intentManager.unregisterCompiler(PointToPointIntent.class);
68 }
69
70 @Override
Brian O'Connorfa81eae2014-10-30 13:20:05 -070071 public List<Intent> compile(MultiPointToSinglePointIntent intent, List<Intent> installable,
72 Set<LinkResourceAllocations> resources) {
Jonathan Hartf5e35802014-12-01 20:45:18 -080073 Map<DeviceId, Link> links = new HashMap<>();
Ray Milkey0742ec92014-10-13 08:39:55 -070074
75 for (ConnectPoint ingressPoint : intent.ingressPoints()) {
76 Path path = getPath(ingressPoint, intent.egressPoint());
Jonathan Hartf5e35802014-12-01 20:45:18 -080077 for (Link link : path.links()) {
78 if (links.containsKey(link.src().deviceId())) {
79 // We've already reached the existing tree with the first
Pavlin Radoslavov2811c402015-02-25 14:30:17 -080080 // part of this path. Add the merging point with different
81 // incoming port, but don't add the remainder of the path
Jonathan Hartf5e35802014-12-01 20:45:18 -080082 // in case it differs from the path we already have.
Pavlin Radoslavov2811c402015-02-25 14:30:17 -080083 links.put(link.src().deviceId(), link);
Jonathan Hartf5e35802014-12-01 20:45:18 -080084 break;
85 }
86
87 links.put(link.src().deviceId(), link);
88 }
Ray Milkey0742ec92014-10-13 08:39:55 -070089 }
90
Ray Milkeyebc5d222015-03-18 15:45:36 -070091 Intent result = LinkCollectionIntent.builder()
92 .appId(intent.appId())
93 .selector(intent.selector())
94 .treatment(intent.treatment())
95 .links(Sets.newHashSet(links.values()))
96 .ingressPoints(intent.ingressPoints())
97 .egressPoints(ImmutableSet.of(intent.egressPoint()))
98 .priority(intent.priority())
99 .build();
100
Ray Milkey0742ec92014-10-13 08:39:55 -0700101 return Arrays.asList(result);
102 }
103
104 /**
105 * Computes a path between two ConnectPoints.
106 *
107 * @param one start of the path
108 * @param two end of the path
109 * @return Path between the two
Brian O'Connorabafb502014-12-02 22:26:20 -0800110 * @throws org.onosproject.net.intent.impl.PathNotFoundException if a path cannot be found
Ray Milkey0742ec92014-10-13 08:39:55 -0700111 */
112 private Path getPath(ConnectPoint one, ConnectPoint two) {
113 Set<Path> paths = pathService.getPaths(one.deviceId(), two.deviceId());
114 if (paths.isEmpty()) {
Sho SHIMIZU877ec2c2015-02-09 12:50:36 -0800115 throw new PathNotFoundException(one.elementId(), two.elementId());
Ray Milkey0742ec92014-10-13 08:39:55 -0700116 }
117 // TODO: let's be more intelligent about this eventually
118 return paths.iterator().next();
119 }
120}