blob: eeb22441a6f92a5a545dac157ce5858e6e826e23 [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 */
Sho SHIMIZU6c28f832015-02-20 16:12:19 -080016package org.onosproject.net.intent.impl.compiler;
weibitf32383b2014-10-22 10:17:31 -070017
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;
Brian O'Connorabafb502014-12-02 22:26:20 -080024import org.onosproject.net.ConnectPoint;
25import org.onosproject.net.Link;
26import org.onosproject.net.Path;
27import org.onosproject.net.intent.Intent;
28import org.onosproject.net.intent.IntentCompiler;
29import org.onosproject.net.intent.IntentExtensionService;
30import org.onosproject.net.intent.OpticalConnectivityIntent;
31import org.onosproject.net.intent.OpticalPathIntent;
Sho SHIMIZU6c28f832015-02-20 16:12:19 -080032import org.onosproject.net.intent.impl.PathNotFoundException;
Brian O'Connorabafb502014-12-02 22:26:20 -080033import org.onosproject.net.resource.LinkResourceAllocations;
34import org.onosproject.net.topology.LinkWeight;
35import org.onosproject.net.topology.Topology;
36import org.onosproject.net.topology.TopologyEdge;
37import org.onosproject.net.topology.TopologyService;
Thomas Vachuska425a2d72014-10-29 11:28:28 -070038
39import java.util.List;
40import java.util.Set;
weibitf32383b2014-10-22 10:17:31 -070041
42/**
Brian O'Connorabafb502014-12-02 22:26:20 -080043 * An intent compiler for {@link org.onosproject.net.intent.OpticalConnectivityIntent}.
weibitf32383b2014-10-22 10:17:31 -070044 */
45@Component(immediate = true)
46public class OpticalConnectivityIntentCompiler implements IntentCompiler<OpticalConnectivityIntent> {
47
weibitf32383b2014-10-22 10:17:31 -070048 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
49 protected IntentExtensionService intentManager;
50
51 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
weibit7e583462014-10-23 10:14:05 -070052 protected TopologyService topologyService;
weibitf32383b2014-10-22 10:17:31 -070053
weibitf32383b2014-10-22 10:17:31 -070054 @Activate
55 public void activate() {
weibitf32383b2014-10-22 10:17:31 -070056 intentManager.registerCompiler(OpticalConnectivityIntent.class, this);
57 }
58
59 @Deactivate
60 public void deactivate() {
61 intentManager.unregisterCompiler(OpticalConnectivityIntent.class);
62 }
63
64 @Override
Brian O'Connorfa81eae2014-10-30 13:20:05 -070065 public List<Intent> compile(OpticalConnectivityIntent intent,
66 List<Intent> installable,
67 Set<LinkResourceAllocations> resources) {
weibit7e583462014-10-23 10:14:05 -070068 // TODO: compute multiple paths using the K-shortest path algorithm
Jonathan Hartc9d76732014-11-18 10:52:20 -080069 Path path = calculateOpticalPath(intent.getSrc(), intent.getDst());
weibit7e583462014-10-23 10:14:05 -070070 Intent newIntent = new OpticalPathIntent(intent.appId(),
Jonathan Hartc9d76732014-11-18 10:52:20 -080071 intent.getSrc(),
Thomas Vachuska425a2d72014-10-29 11:28:28 -070072 intent.getDst(),
73 path);
74 return ImmutableList.of(newIntent);
weibitf32383b2014-10-22 10:17:31 -070075 }
76
weibit50eb95b2014-10-25 21:47:54 -070077 private Path calculateOpticalPath(ConnectPoint start, ConnectPoint end) {
weibit7e583462014-10-23 10:14:05 -070078 // TODO: support user policies
79 Topology topology = topologyService.currentTopology();
80 LinkWeight weight = new LinkWeight() {
81 @Override
82 public double weight(TopologyEdge edge) {
Praseed Balakrishnan00dd1f92014-11-19 17:12:36 -080083 if (edge.link().state() == Link.State.INACTIVE) {
84 return -1;
85 }
Thomas Vachuska425a2d72014-10-29 11:28:28 -070086 return edge.link().type() == Link.Type.OPTICAL ? +1 : -1;
weibit7e583462014-10-23 10:14:05 -070087 }
88 };
weibitf32383b2014-10-22 10:17:31 -070089
Thomas Vachuska425a2d72014-10-29 11:28:28 -070090 Set<Path> paths = topologyService.getPaths(topology, start.deviceId(),
91 end.deviceId(), weight);
92 if (paths.isEmpty()) {
Sho SHIMIZU877ec2c2015-02-09 12:50:36 -080093 throw new PathNotFoundException(start.elementId(), end.elementId());
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}