blob: 37cf84be57bf695515901567f06252e60acc5539 [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'Connor66630c82014-10-02 21:08:19 -070016package org.onlab.onos.net.intent.impl;
17
Ray Milkeycaa450b2014-10-29 15:54:24 -070018import java.util.Arrays;
19import java.util.List;
20import java.util.Set;
21
Brian O'Connor66630c82014-10-02 21:08:19 -070022import org.apache.felix.scr.annotations.Activate;
23import org.apache.felix.scr.annotations.Component;
24import org.apache.felix.scr.annotations.Deactivate;
25import org.apache.felix.scr.annotations.Reference;
26import org.apache.felix.scr.annotations.ReferenceCardinality;
tomf5c9d922014-10-03 15:22:03 -070027import org.onlab.onos.net.Host;
28import org.onlab.onos.net.HostId;
Thomas Vachuska425a2d72014-10-29 11:28:28 -070029import org.onlab.onos.net.Link;
Brian O'Connor66630c82014-10-02 21:08:19 -070030import org.onlab.onos.net.Path;
tomf5c9d922014-10-03 15:22:03 -070031import org.onlab.onos.net.flow.TrafficSelector;
32import org.onlab.onos.net.host.HostService;
Brian O'Connor66630c82014-10-02 21:08:19 -070033import org.onlab.onos.net.intent.HostToHostIntent;
Brian O'Connor66630c82014-10-02 21:08:19 -070034import org.onlab.onos.net.intent.Intent;
35import org.onlab.onos.net.intent.IntentCompiler;
36import org.onlab.onos.net.intent.IntentExtensionService;
Brian O'Connor66630c82014-10-02 21:08:19 -070037import org.onlab.onos.net.intent.PathIntent;
Thomas Vachuska425a2d72014-10-29 11:28:28 -070038import org.onlab.onos.net.topology.LinkWeight;
Ray Milkeycaa450b2014-10-29 15:54:24 -070039import org.onlab.onos.net.resource.LinkResourceRequest;
Brian O'Connor66630c82014-10-02 21:08:19 -070040import org.onlab.onos.net.topology.PathService;
Thomas Vachuska425a2d72014-10-29 11:28:28 -070041import org.onlab.onos.net.topology.TopologyEdge;
Brian O'Connor66630c82014-10-02 21:08:19 -070042
tomf5c9d922014-10-03 15:22:03 -070043import static org.onlab.onos.net.flow.DefaultTrafficSelector.builder;
44
Brian O'Connor66630c82014-10-02 21:08:19 -070045/**
46 * A intent compiler for {@link HostToHostIntent}.
47 */
48@Component(immediate = true)
49public class HostToHostIntentCompiler
50 implements IntentCompiler<HostToHostIntent> {
51
52 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
53 protected IntentExtensionService intentManager;
54
55 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
56 protected PathService pathService;
57
tomf5c9d922014-10-03 15:22:03 -070058 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
59 protected HostService hostService;
60
Brian O'Connor66630c82014-10-02 21:08:19 -070061 @Activate
62 public void activate() {
Brian O'Connor66630c82014-10-02 21:08:19 -070063 intentManager.registerCompiler(HostToHostIntent.class, this);
64 }
65
66 @Deactivate
67 public void deactivate() {
68 intentManager.unregisterCompiler(HostToHostIntent.class);
69 }
70
71 @Override
72 public List<Intent> compile(HostToHostIntent intent) {
tomf5c9d922014-10-03 15:22:03 -070073 Path pathOne = getPath(intent.one(), intent.two());
74 Path pathTwo = getPath(intent.two(), intent.one());
Brian O'Connor66630c82014-10-02 21:08:19 -070075
tomf5c9d922014-10-03 15:22:03 -070076 Host one = hostService.getHost(intent.one());
77 Host two = hostService.getHost(intent.two());
78
79 return Arrays.asList(createPathIntent(pathOne, one, two, intent),
80 createPathIntent(pathTwo, two, one, intent));
81 }
82
83 // Creates a path intent from the specified path and original connectivity intent.
84 private Intent createPathIntent(Path path, Host src, Host dst,
85 HostToHostIntent intent) {
tom85258ee2014-10-07 00:10:02 -070086 TrafficSelector selector = builder(intent.selector())
tomf5c9d922014-10-03 15:22:03 -070087 .matchEthSrc(src.mac()).matchEthDst(dst.mac()).build();
Thomas Vachuskab97cf282014-10-20 23:31:12 -070088 return new PathIntent(intent.appId(), selector, intent.treatment(),
Ray Milkeycaa450b2014-10-29 15:54:24 -070089 path, new LinkResourceRequest[0]);
tomf5c9d922014-10-03 15:22:03 -070090 }
91
92 private Path getPath(HostId one, HostId two) {
Thomas Vachuska425a2d72014-10-29 11:28:28 -070093 Set<Path> paths = pathService.getPaths(one, two, new LinkWeight() {
94 @Override
95 public double weight(TopologyEdge edge) {
96 return edge.link().type() == Link.Type.OPTICAL ? -1 : +1;
97 }
98 });
99
tomf5c9d922014-10-03 15:22:03 -0700100 if (paths.isEmpty()) {
101 throw new PathNotFoundException("No path from host " + one + " to " + two);
102 }
103 // TODO: let's be more intelligent about this eventually
104 return paths.iterator().next();
Brian O'Connor66630c82014-10-02 21:08:19 -0700105 }
106}