blob: e045cecc1b921853da170697d66fa2945148f585 [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
Brian O'Connor66630c82014-10-02 21:08:19 -070018import org.apache.felix.scr.annotations.Activate;
19import org.apache.felix.scr.annotations.Component;
20import org.apache.felix.scr.annotations.Deactivate;
21import org.apache.felix.scr.annotations.Reference;
22import org.apache.felix.scr.annotations.ReferenceCardinality;
tomf5c9d922014-10-03 15:22:03 -070023import org.onlab.onos.net.Host;
24import org.onlab.onos.net.HostId;
Thomas Vachuska425a2d72014-10-29 11:28:28 -070025import org.onlab.onos.net.Link;
Brian O'Connor66630c82014-10-02 21:08:19 -070026import org.onlab.onos.net.Path;
tomf5c9d922014-10-03 15:22:03 -070027import org.onlab.onos.net.flow.TrafficSelector;
28import org.onlab.onos.net.host.HostService;
Brian O'Connor66630c82014-10-02 21:08:19 -070029import org.onlab.onos.net.intent.HostToHostIntent;
Brian O'Connor66630c82014-10-02 21:08:19 -070030import org.onlab.onos.net.intent.Intent;
31import org.onlab.onos.net.intent.IntentCompiler;
32import org.onlab.onos.net.intent.IntentExtensionService;
Brian O'Connor66630c82014-10-02 21:08:19 -070033import org.onlab.onos.net.intent.PathIntent;
Thomas Vachuska425a2d72014-10-29 11:28:28 -070034import org.onlab.onos.net.topology.LinkWeight;
Brian O'Connor66630c82014-10-02 21:08:19 -070035import org.onlab.onos.net.topology.PathService;
Thomas Vachuska425a2d72014-10-29 11:28:28 -070036import org.onlab.onos.net.topology.TopologyEdge;
Brian O'Connor66630c82014-10-02 21:08:19 -070037
tomf5c9d922014-10-03 15:22:03 -070038import java.util.Arrays;
39import java.util.List;
40import java.util.Set;
41
42import static org.onlab.onos.net.flow.DefaultTrafficSelector.builder;
43
Brian O'Connor66630c82014-10-02 21:08:19 -070044/**
45 * A intent compiler for {@link HostToHostIntent}.
46 */
47@Component(immediate = true)
48public class HostToHostIntentCompiler
49 implements IntentCompiler<HostToHostIntent> {
50
51 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
52 protected IntentExtensionService intentManager;
53
54 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
55 protected PathService pathService;
56
tomf5c9d922014-10-03 15:22:03 -070057 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
58 protected HostService hostService;
59
Brian O'Connor66630c82014-10-02 21:08:19 -070060 @Activate
61 public void activate() {
Brian O'Connor66630c82014-10-02 21:08:19 -070062 intentManager.registerCompiler(HostToHostIntent.class, this);
63 }
64
65 @Deactivate
66 public void deactivate() {
67 intentManager.unregisterCompiler(HostToHostIntent.class);
68 }
69
70 @Override
71 public List<Intent> compile(HostToHostIntent intent) {
tomf5c9d922014-10-03 15:22:03 -070072 Path pathOne = getPath(intent.one(), intent.two());
73 Path pathTwo = getPath(intent.two(), intent.one());
Brian O'Connor66630c82014-10-02 21:08:19 -070074
tomf5c9d922014-10-03 15:22:03 -070075 Host one = hostService.getHost(intent.one());
76 Host two = hostService.getHost(intent.two());
77
78 return Arrays.asList(createPathIntent(pathOne, one, two, intent),
79 createPathIntent(pathTwo, two, one, intent));
80 }
81
82 // Creates a path intent from the specified path and original connectivity intent.
83 private Intent createPathIntent(Path path, Host src, Host dst,
84 HostToHostIntent intent) {
tom85258ee2014-10-07 00:10:02 -070085 TrafficSelector selector = builder(intent.selector())
tomf5c9d922014-10-03 15:22:03 -070086 .matchEthSrc(src.mac()).matchEthDst(dst.mac()).build();
Thomas Vachuskab97cf282014-10-20 23:31:12 -070087 return new PathIntent(intent.appId(), selector, intent.treatment(),
88 path);
tomf5c9d922014-10-03 15:22:03 -070089 }
90
91 private Path getPath(HostId one, HostId two) {
Thomas Vachuska425a2d72014-10-29 11:28:28 -070092 Set<Path> paths = pathService.getPaths(one, two, new LinkWeight() {
93 @Override
94 public double weight(TopologyEdge edge) {
95 return edge.link().type() == Link.Type.OPTICAL ? -1 : +1;
96 }
97 });
98
tomf5c9d922014-10-03 15:22:03 -070099 if (paths.isEmpty()) {
100 throw new PathNotFoundException("No path from host " + one + " to " + two);
101 }
102 // TODO: let's be more intelligent about this eventually
103 return paths.iterator().next();
Brian O'Connor66630c82014-10-02 21:08:19 -0700104 }
105}