blob: 1fae0f94aa111ab2c3070d4a5798c5b659d7ca59 [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
18import static org.slf4j.LoggerFactory.getLogger;
19
20import java.util.ArrayList;
weibit7e583462014-10-23 10:14:05 -070021import java.util.Iterator;
weibitf32383b2014-10-22 10:17:31 -070022import java.util.List;
23import java.util.Set;
24
25import org.apache.felix.scr.annotations.Activate;
26import org.apache.felix.scr.annotations.Component;
27import org.apache.felix.scr.annotations.Deactivate;
28import org.apache.felix.scr.annotations.Reference;
29import org.apache.felix.scr.annotations.ReferenceCardinality;
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070030import org.onlab.onos.core.CoreService;
weibitf32383b2014-10-22 10:17:31 -070031import org.onlab.onos.net.ConnectPoint;
weibitf32383b2014-10-22 10:17:31 -070032import org.onlab.onos.net.Link;
33import org.onlab.onos.net.Path;
weibitf32383b2014-10-22 10:17:31 -070034import org.onlab.onos.net.intent.Intent;
35import org.onlab.onos.net.intent.IntentCompiler;
36import org.onlab.onos.net.intent.IntentExtensionService;
weibitf32383b2014-10-22 10:17:31 -070037import org.onlab.onos.net.intent.OpticalConnectivityIntent;
38import org.onlab.onos.net.intent.OpticalPathIntent;
weibitf32383b2014-10-22 10:17:31 -070039import org.onlab.onos.net.provider.ProviderId;
weibit7e583462014-10-23 10:14:05 -070040import org.onlab.onos.net.resource.LinkResourceService;
41import org.onlab.onos.net.topology.LinkWeight;
weibitf32383b2014-10-22 10:17:31 -070042import org.onlab.onos.net.topology.PathService;
weibit7e583462014-10-23 10:14:05 -070043import org.onlab.onos.net.topology.Topology;
44import org.onlab.onos.net.topology.TopologyEdge;
weibit7e583462014-10-23 10:14:05 -070045import org.onlab.onos.net.topology.TopologyService;
weibitf32383b2014-10-22 10:17:31 -070046import org.slf4j.Logger;
47
48/**
49 * Optical compiler for OpticalConnectivityIntent.
50 * It firstly computes K-shortest paths in the optical-layer, then choose the optimal one to assign a wavelength.
51 * Finally, it generates one or more opticalPathintent(s) with opticalMatchs and opticalActions.
52 */
53@Component(immediate = true)
54public class OpticalConnectivityIntentCompiler implements IntentCompiler<OpticalConnectivityIntent> {
55
56 private final Logger log = getLogger(getClass());
57 private static final ProviderId PID = new ProviderId("core", "org.onlab.onos.core", true);
58
59 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
60 protected IntentExtensionService intentManager;
61
62 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
63 protected PathService pathService;
64
weibit7e583462014-10-23 10:14:05 -070065 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
66 protected TopologyService topologyService;
weibitf32383b2014-10-22 10:17:31 -070067
weibit7e583462014-10-23 10:14:05 -070068 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
69 protected LinkResourceService resourceService;
70
71 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
72 protected CoreService coreService;
weibitf32383b2014-10-22 10:17:31 -070073
74 @Activate
75 public void activate() {
weibitf32383b2014-10-22 10:17:31 -070076 intentManager.registerCompiler(OpticalConnectivityIntent.class, this);
77 }
78
79 @Deactivate
80 public void deactivate() {
81 intentManager.unregisterCompiler(OpticalConnectivityIntent.class);
82 }
83
84 @Override
85 public List<Intent> compile(OpticalConnectivityIntent intent) {
weibit7e583462014-10-23 10:14:05 -070086 // TODO: compute multiple paths using the K-shortest path algorithm
87 List<Intent> retList = new ArrayList<>();
weibitaca14602014-10-24 10:26:26 -070088 log.info("The system is comipling the OpticalConnectivityIntent:" + intent.toString());
weibit50eb95b2014-10-25 21:47:54 -070089 Path path = calculateOpticalPath(intent.getSrcConnectPoint(), intent.getDst());
weibit7e583462014-10-23 10:14:05 -070090 if (path == null) {
91 return retList;
92 } else {
93 log.info("the computed lightpath is : {}.", path.toString());
94 }
weibitf32383b2014-10-22 10:17:31 -070095
96 List<Link> links = new ArrayList<>();
weibit9e622ac2014-10-23 13:45:44 -070097 // links.add(DefaultEdgeLink.createEdgeLink(intent.getSrcConnectPoint(), true));
weibitf32383b2014-10-22 10:17:31 -070098 links.addAll(path.links());
weibit9e622ac2014-10-23 13:45:44 -070099 //links.add(DefaultEdgeLink.createEdgeLink(intent.getDst(), false));
weibitf32383b2014-10-22 10:17:31 -0700100
weibitf32383b2014-10-22 10:17:31 -0700101 // create a new opticalPathIntent
weibit7e583462014-10-23 10:14:05 -0700102 Intent newIntent = new OpticalPathIntent(intent.appId(),
weibit253c8652014-10-23 16:30:03 -0700103 intent.getSrcConnectPoint(),
104 intent.getDst(),
weibitf32383b2014-10-22 10:17:31 -0700105 path);
106
weibitaca14602014-10-24 10:26:26 -0700107 log.info("a new OpticalPathIntent was created: " + newIntent.toString());
108
weibitf32383b2014-10-22 10:17:31 -0700109 retList.add(newIntent);
110
111 return retList;
112 }
113
weibit50eb95b2014-10-25 21:47:54 -0700114 private Path calculateOpticalPath(ConnectPoint start, ConnectPoint end) {
weibit7e583462014-10-23 10:14:05 -0700115 // TODO: support user policies
116 Topology topology = topologyService.currentTopology();
117 LinkWeight weight = new LinkWeight() {
118 @Override
119 public double weight(TopologyEdge edge) {
weibit253c8652014-10-23 16:30:03 -0700120 Link.Type lt = edge.link().type();
weibit253c8652014-10-23 16:30:03 -0700121 if (lt == Link.Type.OPTICAL) {
weibit50eb95b2014-10-25 21:47:54 -0700122 return 1.0;
weibit7e583462014-10-23 10:14:05 -0700123 } else {
weibit50eb95b2014-10-25 21:47:54 -0700124 return 1000.0;
weibit7e583462014-10-23 10:14:05 -0700125 }
126 }
127 };
weibitf32383b2014-10-22 10:17:31 -0700128
weibit7e583462014-10-23 10:14:05 -0700129 Set<Path> paths = topologyService.getPaths(topology,
130 start.deviceId(),
131 end.deviceId(),
132 weight);
133
weibit50eb95b2014-10-25 21:47:54 -0700134 ArrayList<Path> localPaths = new ArrayList<>();
weibit7e583462014-10-23 10:14:05 -0700135 Iterator<Path> itr = paths.iterator();
136 while (itr.hasNext()) {
137 Path path = itr.next();
weibit50eb95b2014-10-25 21:47:54 -0700138 if (path.cost() >= 1000) {
139 continue;
weibit7e583462014-10-23 10:14:05 -0700140 }
weibit50eb95b2014-10-25 21:47:54 -0700141 localPaths.add(path);
weibit7e583462014-10-23 10:14:05 -0700142 }
143
weibit50eb95b2014-10-25 21:47:54 -0700144 if (localPaths.isEmpty()) {
145 throw new PathNotFoundException("No fiber path from " + start + " to " + end);
weibit7e583462014-10-23 10:14:05 -0700146 } else {
weibit50eb95b2014-10-25 21:47:54 -0700147 return localPaths.iterator().next();
weibit7e583462014-10-23 10:14:05 -0700148 }
149
weibitf32383b2014-10-22 10:17:31 -0700150 }
151
152}