blob: b244812e8036116573da86dc265bab41b8f4e4fb [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;
Brian O'Connor66630c82014-10-02 21:08:19 -070025import org.onlab.onos.net.Path;
tomf5c9d922014-10-03 15:22:03 -070026import org.onlab.onos.net.flow.TrafficSelector;
27import org.onlab.onos.net.host.HostService;
Brian O'Connor66630c82014-10-02 21:08:19 -070028import org.onlab.onos.net.intent.HostToHostIntent;
Brian O'Connor66630c82014-10-02 21:08:19 -070029import org.onlab.onos.net.intent.Intent;
30import org.onlab.onos.net.intent.IntentCompiler;
31import org.onlab.onos.net.intent.IntentExtensionService;
Brian O'Connor66630c82014-10-02 21:08:19 -070032import org.onlab.onos.net.intent.PathIntent;
33import org.onlab.onos.net.topology.PathService;
34
tomf5c9d922014-10-03 15:22:03 -070035import java.util.Arrays;
36import java.util.List;
37import java.util.Set;
38
39import static org.onlab.onos.net.flow.DefaultTrafficSelector.builder;
40
Brian O'Connor66630c82014-10-02 21:08:19 -070041/**
42 * A intent compiler for {@link HostToHostIntent}.
43 */
44@Component(immediate = true)
45public class HostToHostIntentCompiler
46 implements IntentCompiler<HostToHostIntent> {
47
48 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
49 protected IntentExtensionService intentManager;
50
51 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
52 protected PathService pathService;
53
tomf5c9d922014-10-03 15:22:03 -070054 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
55 protected HostService hostService;
56
Brian O'Connor66630c82014-10-02 21:08:19 -070057 @Activate
58 public void activate() {
Brian O'Connor66630c82014-10-02 21:08:19 -070059 intentManager.registerCompiler(HostToHostIntent.class, this);
60 }
61
62 @Deactivate
63 public void deactivate() {
64 intentManager.unregisterCompiler(HostToHostIntent.class);
65 }
66
67 @Override
68 public List<Intent> compile(HostToHostIntent intent) {
tomf5c9d922014-10-03 15:22:03 -070069 Path pathOne = getPath(intent.one(), intent.two());
70 Path pathTwo = getPath(intent.two(), intent.one());
Brian O'Connor66630c82014-10-02 21:08:19 -070071
tomf5c9d922014-10-03 15:22:03 -070072 Host one = hostService.getHost(intent.one());
73 Host two = hostService.getHost(intent.two());
74
75 return Arrays.asList(createPathIntent(pathOne, one, two, intent),
76 createPathIntent(pathTwo, two, one, intent));
77 }
78
79 // Creates a path intent from the specified path and original connectivity intent.
80 private Intent createPathIntent(Path path, Host src, Host dst,
81 HostToHostIntent intent) {
tom85258ee2014-10-07 00:10:02 -070082 TrafficSelector selector = builder(intent.selector())
tomf5c9d922014-10-03 15:22:03 -070083 .matchEthSrc(src.mac()).matchEthDst(dst.mac()).build();
Thomas Vachuskab97cf282014-10-20 23:31:12 -070084 return new PathIntent(intent.appId(), selector, intent.treatment(),
85 path);
tomf5c9d922014-10-03 15:22:03 -070086 }
87
88 private Path getPath(HostId one, HostId two) {
89 Set<Path> paths = pathService.getPaths(one, two);
90 if (paths.isEmpty()) {
91 throw new PathNotFoundException("No path from host " + one + " to " + two);
92 }
93 // TODO: let's be more intelligent about this eventually
94 return paths.iterator().next();
Brian O'Connor66630c82014-10-02 21:08:19 -070095 }
96}