blob: 6f12c5280550b2dced2fb12a7ea1b6feed59fa98 [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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.intent.impl;
Ray Milkey0742ec92014-10-13 08:39:55 -070017
Jonathan Hartebc8f712015-05-14 20:08:47 -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;
29import org.onosproject.net.intent.Intent;
30import org.onosproject.net.intent.IntentCompiler;
31import org.onosproject.net.intent.IntentExtensionService;
32import org.onosproject.net.intent.LinkCollectionIntent;
33import org.onosproject.net.intent.MultiPointToSinglePointIntent;
34import org.onosproject.net.intent.PointToPointIntent;
35import org.onosproject.net.resource.LinkResourceAllocations;
36import org.onosproject.net.topology.PathService;
Ray Milkey0742ec92014-10-13 08:39:55 -070037
Jonathan Hartebc8f712015-05-14 20:08:47 -070038import java.util.Arrays;
39import java.util.Collections;
40import java.util.HashMap;
41import java.util.List;
42import java.util.Map;
43import java.util.Set;
Thomas Vachuskab97cf282014-10-20 23:31:12 -070044
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
51 implements IntentCompiler<MultiPointToSinglePointIntent> {
52
53 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
54 protected IntentExtensionService intentManager;
55
56 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
57 protected PathService pathService;
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
Brian O'Connorfa81eae2014-10-30 13:20:05 -070070 public List<Intent> compile(MultiPointToSinglePointIntent intent, List<Intent> installable,
71 Set<LinkResourceAllocations> resources) {
Jonathan Hartf5e35802014-12-01 20:45:18 -080072 Map<DeviceId, Link> links = new HashMap<>();
Ray Milkey0742ec92014-10-13 08:39:55 -070073
74 for (ConnectPoint ingressPoint : intent.ingressPoints()) {
Jonathan Hartebc8f712015-05-14 20:08:47 -070075 if (ingressPoint.deviceId().equals(intent.egressPoint().deviceId())) {
76 continue;
77 }
Ray Milkey0742ec92014-10-13 08:39:55 -070078 Path path = getPath(ingressPoint, intent.egressPoint());
Jonathan Hartf5e35802014-12-01 20:45:18 -080079 for (Link link : path.links()) {
80 if (links.containsKey(link.src().deviceId())) {
81 // We've already reached the existing tree with the first
Pavlin Radoslavov1b787232015-02-23 15:07:31 -080082 // part of this path. Add the merging point with differen
83 // incoming port, but don't add the remainder of the path
Jonathan Hartf5e35802014-12-01 20:45:18 -080084 // in case it differs from the path we already have.
Pavlin Radoslavov1b787232015-02-23 15:07:31 -080085 links.put(link.src().deviceId(), link);
Jonathan Hartf5e35802014-12-01 20:45:18 -080086 break;
87 }
88
89 links.put(link.src().deviceId(), link);
90 }
Ray Milkey0742ec92014-10-13 08:39:55 -070091 }
92
Thomas Vachuskab97cf282014-10-20 23:31:12 -070093 Intent result = new LinkCollectionIntent(intent.appId(),
94 intent.selector(), intent.treatment(),
Pavlin Radoslavov1b787232015-02-23 15:07:31 -080095 Sets.newHashSet(links.values()),
96 intent.ingressPoints(),
97 ImmutableSet.of(intent.egressPoint()),
98 Collections.emptyList());
Ray Milkey0742ec92014-10-13 08:39:55 -070099 return Arrays.asList(result);
100 }
101
102 /**
103 * Computes a path between two ConnectPoints.
104 *
105 * @param one start of the path
106 * @param two end of the path
107 * @return Path between the two
Brian O'Connorabafb502014-12-02 22:26:20 -0800108 * @throws org.onosproject.net.intent.impl.PathNotFoundException if a path cannot be found
Ray Milkey0742ec92014-10-13 08:39:55 -0700109 */
110 private Path getPath(ConnectPoint one, ConnectPoint two) {
111 Set<Path> paths = pathService.getPaths(one.deviceId(), two.deviceId());
112 if (paths.isEmpty()) {
113 throw new PathNotFoundException("No path from " + one + " to " + two);
114 }
115 // TODO: let's be more intelligent about this eventually
116 return paths.iterator().next();
117 }
118}