blob: 372d65b771b4a7114e231e12b5b8403a4ca3933a [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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
Ray Milkeye076c792015-03-24 09:38:30 -070018import java.util.List;
19import java.util.Set;
20
weibitf32383b2014-10-22 10:17:31 -070021import org.apache.felix.scr.annotations.Activate;
22import org.apache.felix.scr.annotations.Component;
23import org.apache.felix.scr.annotations.Deactivate;
24import org.apache.felix.scr.annotations.Reference;
25import org.apache.felix.scr.annotations.ReferenceCardinality;
Brian O'Connorabafb502014-12-02 22:26:20 -080026import org.onosproject.net.ConnectPoint;
27import org.onosproject.net.Link;
28import org.onosproject.net.Path;
29import org.onosproject.net.intent.Intent;
30import org.onosproject.net.intent.IntentCompiler;
31import org.onosproject.net.intent.IntentExtensionService;
32import org.onosproject.net.intent.OpticalConnectivityIntent;
33import org.onosproject.net.intent.OpticalPathIntent;
Sho SHIMIZU6c28f832015-02-20 16:12:19 -080034import org.onosproject.net.intent.impl.PathNotFoundException;
Brian O'Connorabafb502014-12-02 22:26:20 -080035import org.onosproject.net.resource.LinkResourceAllocations;
36import org.onosproject.net.topology.LinkWeight;
37import org.onosproject.net.topology.Topology;
38import org.onosproject.net.topology.TopologyEdge;
39import org.onosproject.net.topology.TopologyService;
Thomas Vachuska425a2d72014-10-29 11:28:28 -070040
Ray Milkeye076c792015-03-24 09:38:30 -070041import com.google.common.collect.ImmutableList;
weibitf32383b2014-10-22 10:17:31 -070042
43/**
Brian O'Connorabafb502014-12-02 22:26:20 -080044 * An intent compiler for {@link org.onosproject.net.intent.OpticalConnectivityIntent}.
weibitf32383b2014-10-22 10:17:31 -070045 */
46@Component(immediate = true)
47public class OpticalConnectivityIntentCompiler implements IntentCompiler<OpticalConnectivityIntent> {
48
weibitf32383b2014-10-22 10:17:31 -070049 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
50 protected IntentExtensionService intentManager;
51
52 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
weibit7e583462014-10-23 10:14:05 -070053 protected TopologyService topologyService;
weibitf32383b2014-10-22 10:17:31 -070054
weibitf32383b2014-10-22 10:17:31 -070055 @Activate
56 public void activate() {
weibitf32383b2014-10-22 10:17:31 -070057 intentManager.registerCompiler(OpticalConnectivityIntent.class, this);
58 }
59
60 @Deactivate
61 public void deactivate() {
62 intentManager.unregisterCompiler(OpticalConnectivityIntent.class);
63 }
64
65 @Override
Brian O'Connorfa81eae2014-10-30 13:20:05 -070066 public List<Intent> compile(OpticalConnectivityIntent intent,
67 List<Intent> installable,
68 Set<LinkResourceAllocations> resources) {
weibit7e583462014-10-23 10:14:05 -070069 // TODO: compute multiple paths using the K-shortest path algorithm
Jonathan Hartc9d76732014-11-18 10:52:20 -080070 Path path = calculateOpticalPath(intent.getSrc(), intent.getDst());
Ray Milkeye076c792015-03-24 09:38:30 -070071 Intent newIntent = OpticalPathIntent.builder()
72 .appId(intent.appId())
73 .src(intent.getSrc())
74 .dst(intent.getDst())
75 .path(path)
76 .build();
Thomas Vachuska425a2d72014-10-29 11:28:28 -070077 return ImmutableList.of(newIntent);
weibitf32383b2014-10-22 10:17:31 -070078 }
79
weibit50eb95b2014-10-25 21:47:54 -070080 private Path calculateOpticalPath(ConnectPoint start, ConnectPoint end) {
weibit7e583462014-10-23 10:14:05 -070081 // TODO: support user policies
82 Topology topology = topologyService.currentTopology();
83 LinkWeight weight = new LinkWeight() {
84 @Override
85 public double weight(TopologyEdge edge) {
Praseed Balakrishnan00dd1f92014-11-19 17:12:36 -080086 if (edge.link().state() == Link.State.INACTIVE) {
87 return -1;
88 }
Thomas Vachuska425a2d72014-10-29 11:28:28 -070089 return edge.link().type() == Link.Type.OPTICAL ? +1 : -1;
weibit7e583462014-10-23 10:14:05 -070090 }
91 };
weibitf32383b2014-10-22 10:17:31 -070092
Thomas Vachuska425a2d72014-10-29 11:28:28 -070093 Set<Path> paths = topologyService.getPaths(topology, start.deviceId(),
94 end.deviceId(), weight);
95 if (paths.isEmpty()) {
Sho SHIMIZU877ec2c2015-02-09 12:50:36 -080096 throw new PathNotFoundException(start.elementId(), end.elementId());
weibit7e583462014-10-23 10:14:05 -070097 }
98
Thomas Vachuska425a2d72014-10-29 11:28:28 -070099 // TODO: let's be more intelligent about this eventually
100 return paths.iterator().next();
weibitf32383b2014-10-22 10:17:31 -0700101 }
102
103}