blob: 85c9f295c12b3b79b12c8f90c84fb1a1b962c294 [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 */
weibitf32383b2014-10-22 10:17:31 -070016package org.onlab.onos.net.intent.impl;
17
Thomas Vachuska425a2d72014-10-29 11:28:28 -070018import com.google.common.collect.ImmutableList;
weibitf32383b2014-10-22 10:17:31 -070019import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
22import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
24import org.onlab.onos.net.ConnectPoint;
weibitf32383b2014-10-22 10:17:31 -070025import org.onlab.onos.net.Link;
26import org.onlab.onos.net.Path;
weibitf32383b2014-10-22 10:17:31 -070027import org.onlab.onos.net.intent.Intent;
28import org.onlab.onos.net.intent.IntentCompiler;
29import org.onlab.onos.net.intent.IntentExtensionService;
weibitf32383b2014-10-22 10:17:31 -070030import org.onlab.onos.net.intent.OpticalConnectivityIntent;
31import org.onlab.onos.net.intent.OpticalPathIntent;
Brian O'Connorfa81eae2014-10-30 13:20:05 -070032import org.onlab.onos.net.resource.LinkResourceAllocations;
weibit7e583462014-10-23 10:14:05 -070033import org.onlab.onos.net.topology.LinkWeight;
weibit7e583462014-10-23 10:14:05 -070034import org.onlab.onos.net.topology.Topology;
35import org.onlab.onos.net.topology.TopologyEdge;
weibit7e583462014-10-23 10:14:05 -070036import org.onlab.onos.net.topology.TopologyService;
Thomas Vachuska425a2d72014-10-29 11:28:28 -070037
38import java.util.List;
39import java.util.Set;
weibitf32383b2014-10-22 10:17:31 -070040
41/**
Thomas Vachuska425a2d72014-10-29 11:28:28 -070042 * An intent compiler for {@link org.onlab.onos.net.intent.OpticalConnectivityIntent}.
weibitf32383b2014-10-22 10:17:31 -070043 */
44@Component(immediate = true)
45public class OpticalConnectivityIntentCompiler implements IntentCompiler<OpticalConnectivityIntent> {
46
weibitf32383b2014-10-22 10:17:31 -070047 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
48 protected IntentExtensionService intentManager;
49
50 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
weibit7e583462014-10-23 10:14:05 -070051 protected TopologyService topologyService;
weibitf32383b2014-10-22 10:17:31 -070052
weibitf32383b2014-10-22 10:17:31 -070053 @Activate
54 public void activate() {
weibitf32383b2014-10-22 10:17:31 -070055 intentManager.registerCompiler(OpticalConnectivityIntent.class, this);
56 }
57
58 @Deactivate
59 public void deactivate() {
60 intentManager.unregisterCompiler(OpticalConnectivityIntent.class);
61 }
62
63 @Override
Brian O'Connorfa81eae2014-10-30 13:20:05 -070064 public List<Intent> compile(OpticalConnectivityIntent intent,
65 List<Intent> installable,
66 Set<LinkResourceAllocations> resources) {
weibit7e583462014-10-23 10:14:05 -070067 // TODO: compute multiple paths using the K-shortest path algorithm
Jonathan Hartc9d76732014-11-18 10:52:20 -080068 Path path = calculateOpticalPath(intent.getSrc(), intent.getDst());
weibit7e583462014-10-23 10:14:05 -070069 Intent newIntent = new OpticalPathIntent(intent.appId(),
Jonathan Hartc9d76732014-11-18 10:52:20 -080070 intent.getSrc(),
Thomas Vachuska425a2d72014-10-29 11:28:28 -070071 intent.getDst(),
72 path);
73 return ImmutableList.of(newIntent);
weibitf32383b2014-10-22 10:17:31 -070074 }
75
weibit50eb95b2014-10-25 21:47:54 -070076 private Path calculateOpticalPath(ConnectPoint start, ConnectPoint end) {
weibit7e583462014-10-23 10:14:05 -070077 // TODO: support user policies
78 Topology topology = topologyService.currentTopology();
79 LinkWeight weight = new LinkWeight() {
80 @Override
81 public double weight(TopologyEdge edge) {
Praseed Balakrishnan00dd1f92014-11-19 17:12:36 -080082 if (edge.link().state() == Link.State.INACTIVE) {
83 return -1;
84 }
Thomas Vachuska425a2d72014-10-29 11:28:28 -070085 return edge.link().type() == Link.Type.OPTICAL ? +1 : -1;
weibit7e583462014-10-23 10:14:05 -070086 }
87 };
weibitf32383b2014-10-22 10:17:31 -070088
Thomas Vachuska425a2d72014-10-29 11:28:28 -070089 Set<Path> paths = topologyService.getPaths(topology, start.deviceId(),
90 end.deviceId(), weight);
91 if (paths.isEmpty()) {
Praseed Balakrishnan00dd1f92014-11-19 17:12:36 -080092 throw new PathNotFoundException("No Optical path found from " +
93 start + " to " + end);
weibit7e583462014-10-23 10:14:05 -070094 }
95
Thomas Vachuska425a2d72014-10-29 11:28:28 -070096 // TODO: let's be more intelligent about this eventually
97 return paths.iterator().next();
weibitf32383b2014-10-22 10:17:31 -070098 }
99
100}