blob: eb1cce87d2177ee6cf45990999e37429ecabe70d [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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
Thomas Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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.
Thomas Vachuska781d18b2014-10-27 10:31:25 -070015 */
weibitf32383b2014-10-22 10:17:31 -070016package org.onlab.onos.optical.provisioner;
17
weibit7e583462014-10-23 10:14:05 -070018import java.util.ArrayList;
19import java.util.HashMap;
weibitf32383b2014-10-22 10:17:31 -070020import java.util.Iterator;
weibit7e583462014-10-23 10:14:05 -070021import java.util.Map;
22import java.util.Map.Entry;
weibitf32383b2014-10-22 10:17:31 -070023import 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.ApplicationId;
31import org.onlab.onos.core.CoreService;
weibitf32383b2014-10-22 10:17:31 -070032import org.onlab.onos.net.ConnectPoint;
weibitf32383b2014-10-22 10:17:31 -070033import org.onlab.onos.net.Link;
weibit7e583462014-10-23 10:14:05 -070034import org.onlab.onos.net.Path;
weibitf32383b2014-10-22 10:17:31 -070035import org.onlab.onos.net.device.DeviceService;
weibitf32383b2014-10-22 10:17:31 -070036import org.onlab.onos.net.intent.Intent;
weibitf32383b2014-10-22 10:17:31 -070037import org.onlab.onos.net.intent.IntentEvent;
38import org.onlab.onos.net.intent.IntentExtensionService;
weibitf32383b2014-10-22 10:17:31 -070039import org.onlab.onos.net.intent.IntentListener;
40import org.onlab.onos.net.intent.IntentService;
41import org.onlab.onos.net.intent.OpticalConnectivityIntent;
42import org.onlab.onos.net.intent.PointToPointIntent;
43import org.onlab.onos.net.link.LinkService;
weibit7e583462014-10-23 10:14:05 -070044import org.onlab.onos.net.resource.LinkResourceService;
45import org.onlab.onos.net.topology.LinkWeight;
46import org.onlab.onos.net.topology.Topology;
47import org.onlab.onos.net.topology.TopologyEdge;
weibit9e622ac2014-10-23 13:45:44 -070048
weibitf32383b2014-10-22 10:17:31 -070049import org.onlab.onos.net.topology.TopologyService;
50import org.slf4j.Logger;
51import org.slf4j.LoggerFactory;
52
53/**
54 * OpticalPathProvisioner listens event notifications from the Intent F/W.
55 * It generates one or more opticalConnectivityIntent(s) and submits (or withdraws) to Intent F/W
56 * for adding/releasing capacity at the packet layer.
57 *
58 */
59
60@Component(immediate = true)
61public class OpticalPathProvisioner {
62
63 protected static final Logger log = LoggerFactory
64 .getLogger(OpticalPathProvisioner.class);
65
66 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
67 private IntentService intentService;
68
69 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
70 private IntentExtensionService intentExtensionService;
71
72 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
73 protected LinkService linkService;
74
75 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
76 protected DeviceService deviceService;
77
78 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
79 protected TopologyService topologyService;
80
81 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
82 protected CoreService coreService;
83
weibit7e583462014-10-23 10:14:05 -070084 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
85 protected LinkResourceService resourceService;
weibitf32383b2014-10-22 10:17:31 -070086
87 private ApplicationId appId;
weibitf32383b2014-10-22 10:17:31 -070088
weibit7e583462014-10-23 10:14:05 -070089 //protected <IntentId> intentIdGenerator;
weibitf32383b2014-10-22 10:17:31 -070090
91 private final InternalOpticalPathProvisioner pathProvisioner = new InternalOpticalPathProvisioner();
92
93 @Activate
94 protected void activate() {
95 intentService.addListener(pathProvisioner);
96 appId = coreService.registerApplication("org.onlab.onos.optical");
97 log.info("Starting optical path provisoning...");
98 }
99
100 @Deactivate
101 protected void deactivate() {
102 intentService.removeListener(pathProvisioner);
103 }
104
105 public class InternalOpticalPathProvisioner implements IntentListener {
106 @Override
107 public void event(IntentEvent event) {
108 switch (event.type()) {
109 case SUBMITTED:
110 break;
111 case INSTALLED:
112 break;
113 case FAILED:
weibit50eb95b2014-10-25 21:47:54 -0700114 log.info("packet intent {} failed, calling optical path provisioning APP.", event.subject());
weibitf32383b2014-10-22 10:17:31 -0700115 setuplightpath(event.subject());
116 break;
117 case WITHDRAWN:
118 log.info("intent {} withdrawn.", event.subject());
119 teardownLightpath(event.subject());
120 break;
121 default:
122 break;
123 }
124 }
125
126 private void setuplightpath(Intent intent) {
weibit50eb95b2014-10-25 21:47:54 -0700127 // TODO support more packet intent types
128 if (!intent.getClass().equals(PointToPointIntent.class)) {
weibitf32383b2014-10-22 10:17:31 -0700129 return;
130 }
131
132 PointToPointIntent pktIntent = (PointToPointIntent) intent;
133 if (pktIntent.ingressPoint() == null || pktIntent.egressPoint() == null) {
134 return;
135 }
136
weibit7e583462014-10-23 10:14:05 -0700137 Topology topology = topologyService.currentTopology();
weibitf32383b2014-10-22 10:17:31 -0700138
weibit7e583462014-10-23 10:14:05 -0700139 LinkWeight weight = new LinkWeight() {
140 @Override
141 public double weight(TopologyEdge edge) {
weibit50eb95b2014-10-25 21:47:54 -0700142 if (isOpticalLink(edge.link())) {
143 return 1000.0; // optical links
weibit7e583462014-10-23 10:14:05 -0700144 } else {
weibit50eb95b2014-10-25 21:47:54 -0700145 return 1.0; // packet links
weibit7e583462014-10-23 10:14:05 -0700146 }
147 }
148 };
weibitf32383b2014-10-22 10:17:31 -0700149
weibit7e583462014-10-23 10:14:05 -0700150 Set<Path> paths = topologyService.getPaths(topology,
151 pktIntent.ingressPoint().deviceId(),
152 pktIntent.egressPoint().deviceId(),
153 weight);
154
155 if (paths.isEmpty()) {
weibitf32383b2014-10-22 10:17:31 -0700156 return;
157 }
158
weibit7e583462014-10-23 10:14:05 -0700159 ConnectPoint srcWdmPoint = null;
160 ConnectPoint dstWdmPoint = null;
161 Iterator<Path> itrPath = paths.iterator();
162 Path firstPath = itrPath.next();
163 log.info(firstPath.toString());
weibitf32383b2014-10-22 10:17:31 -0700164
weibit7e583462014-10-23 10:14:05 -0700165 ArrayList<Map<ConnectPoint, ConnectPoint>> connectionList = new ArrayList<>();
166
167 Iterator<Link> itrLink = firstPath.links().iterator();
168 while (itrLink.hasNext()) {
169 Link link1 = itrLink.next();
170 if (!isOpticalLink(link1)) {
171 continue;
172 } else {
173 srcWdmPoint = link1.dst();
174 dstWdmPoint = srcWdmPoint;
175 }
176
177 while (true) {
weibit7e583462014-10-23 10:14:05 -0700178 if (itrLink.hasNext()) {
179 Link link2 = itrLink.next();
180 dstWdmPoint = link2.src();
181 } else {
182 break;
183 }
weibit7e583462014-10-23 10:14:05 -0700184 if (itrLink.hasNext()) {
185 Link link3 = itrLink.next();
186 if (!isOpticalLink(link3)) {
187 break;
188 }
189 } else {
190 break;
191 }
192 }
193
194 Map<ConnectPoint, ConnectPoint> pair =
195 new HashMap<ConnectPoint, ConnectPoint>();
196 pair.put(srcWdmPoint, dstWdmPoint);
197
198 connectionList.add(pair);
weibitf32383b2014-10-22 10:17:31 -0700199 }
200
weibit7e583462014-10-23 10:14:05 -0700201 for (Map<ConnectPoint, ConnectPoint> map : connectionList) {
202 for (Entry<ConnectPoint, ConnectPoint> entry : map.entrySet()) {
weibitf32383b2014-10-22 10:17:31 -0700203
weibit7e583462014-10-23 10:14:05 -0700204 ConnectPoint src = entry.getKey();
205 ConnectPoint dst = entry.getValue();
weibitf32383b2014-10-22 10:17:31 -0700206
weibit7e583462014-10-23 10:14:05 -0700207 Intent opticalIntent = new OpticalConnectivityIntent(appId,
208 srcWdmPoint,
209 dstWdmPoint);
210
211 intentService.submit(opticalIntent);
212
213 log.info(opticalIntent.toString());
214 }
215 }
216
217 }
218
219 private boolean isOpticalLink(Link link) {
220 boolean isOptical = false;
weibit50eb95b2014-10-25 21:47:54 -0700221 Link.Type lt = link.type();
222 if (lt == Link.Type.OPTICAL) {
223 isOptical = true;
weibit7e583462014-10-23 10:14:05 -0700224 }
225 return isOptical;
weibitf32383b2014-10-22 10:17:31 -0700226 }
227
228 private void teardownLightpath(Intent intent) {
229 // TODO: tear down the idle lightpath if the utilization is close to zero.
230 }
231
232 }
233
234}