blob: dd6a8e2571c352dd04ea4e143a9c1bf55cfe0e2e [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
weibitf32383b2014-10-22 10:17:31 -070019package org.onlab.onos.optical.provisioner;
20
weibit7e583462014-10-23 10:14:05 -070021import java.util.ArrayList;
22import java.util.HashMap;
weibitf32383b2014-10-22 10:17:31 -070023import java.util.Iterator;
weibit7e583462014-10-23 10:14:05 -070024import java.util.Map;
25import java.util.Map.Entry;
weibitf32383b2014-10-22 10:17:31 -070026import java.util.Set;
27
28import org.apache.felix.scr.annotations.Activate;
29import org.apache.felix.scr.annotations.Component;
30import org.apache.felix.scr.annotations.Deactivate;
31import org.apache.felix.scr.annotations.Reference;
32import org.apache.felix.scr.annotations.ReferenceCardinality;
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070033import org.onlab.onos.core.ApplicationId;
34import org.onlab.onos.core.CoreService;
weibitf32383b2014-10-22 10:17:31 -070035import org.onlab.onos.net.ConnectPoint;
weibitf32383b2014-10-22 10:17:31 -070036import org.onlab.onos.net.Link;
weibit7e583462014-10-23 10:14:05 -070037import org.onlab.onos.net.Path;
weibitf32383b2014-10-22 10:17:31 -070038import org.onlab.onos.net.device.DeviceService;
weibitf32383b2014-10-22 10:17:31 -070039import org.onlab.onos.net.intent.Intent;
weibitf32383b2014-10-22 10:17:31 -070040import org.onlab.onos.net.intent.IntentEvent;
41import org.onlab.onos.net.intent.IntentExtensionService;
weibitf32383b2014-10-22 10:17:31 -070042import org.onlab.onos.net.intent.IntentListener;
43import org.onlab.onos.net.intent.IntentService;
44import org.onlab.onos.net.intent.OpticalConnectivityIntent;
45import org.onlab.onos.net.intent.PointToPointIntent;
46import org.onlab.onos.net.link.LinkService;
weibit7e583462014-10-23 10:14:05 -070047import org.onlab.onos.net.resource.LinkResourceService;
48import org.onlab.onos.net.topology.LinkWeight;
49import org.onlab.onos.net.topology.Topology;
50import org.onlab.onos.net.topology.TopologyEdge;
weibit9e622ac2014-10-23 13:45:44 -070051
weibitf32383b2014-10-22 10:17:31 -070052import org.onlab.onos.net.topology.TopologyService;
53import org.slf4j.Logger;
54import org.slf4j.LoggerFactory;
55
56/**
57 * OpticalPathProvisioner listens event notifications from the Intent F/W.
58 * It generates one or more opticalConnectivityIntent(s) and submits (or withdraws) to Intent F/W
59 * for adding/releasing capacity at the packet layer.
60 *
61 */
62
63@Component(immediate = true)
64public class OpticalPathProvisioner {
65
66 protected static final Logger log = LoggerFactory
67 .getLogger(OpticalPathProvisioner.class);
68
69 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
70 private IntentService intentService;
71
72 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
73 private IntentExtensionService intentExtensionService;
74
75 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
76 protected LinkService linkService;
77
78 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
79 protected DeviceService deviceService;
80
81 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
82 protected TopologyService topologyService;
83
84 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
85 protected CoreService coreService;
86
weibit7e583462014-10-23 10:14:05 -070087 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
88 protected LinkResourceService resourceService;
weibitf32383b2014-10-22 10:17:31 -070089
90 private ApplicationId appId;
weibitf32383b2014-10-22 10:17:31 -070091
weibit7e583462014-10-23 10:14:05 -070092 //protected <IntentId> intentIdGenerator;
weibitf32383b2014-10-22 10:17:31 -070093
94 private final InternalOpticalPathProvisioner pathProvisioner = new InternalOpticalPathProvisioner();
95
96 @Activate
97 protected void activate() {
98 intentService.addListener(pathProvisioner);
99 appId = coreService.registerApplication("org.onlab.onos.optical");
100 log.info("Starting optical path provisoning...");
101 }
102
103 @Deactivate
104 protected void deactivate() {
105 intentService.removeListener(pathProvisioner);
106 }
107
108 public class InternalOpticalPathProvisioner implements IntentListener {
109 @Override
110 public void event(IntentEvent event) {
111 switch (event.type()) {
112 case SUBMITTED:
113 break;
114 case INSTALLED:
115 break;
116 case FAILED:
weibit50eb95b2014-10-25 21:47:54 -0700117 log.info("packet intent {} failed, calling optical path provisioning APP.", event.subject());
weibitf32383b2014-10-22 10:17:31 -0700118 setuplightpath(event.subject());
119 break;
120 case WITHDRAWN:
121 log.info("intent {} withdrawn.", event.subject());
122 teardownLightpath(event.subject());
123 break;
124 default:
125 break;
126 }
127 }
128
129 private void setuplightpath(Intent intent) {
weibit50eb95b2014-10-25 21:47:54 -0700130 // TODO support more packet intent types
131 if (!intent.getClass().equals(PointToPointIntent.class)) {
weibitf32383b2014-10-22 10:17:31 -0700132 return;
133 }
134
135 PointToPointIntent pktIntent = (PointToPointIntent) intent;
136 if (pktIntent.ingressPoint() == null || pktIntent.egressPoint() == null) {
137 return;
138 }
139
weibit7e583462014-10-23 10:14:05 -0700140 Topology topology = topologyService.currentTopology();
weibitf32383b2014-10-22 10:17:31 -0700141
weibit7e583462014-10-23 10:14:05 -0700142 LinkWeight weight = new LinkWeight() {
143 @Override
144 public double weight(TopologyEdge edge) {
weibit50eb95b2014-10-25 21:47:54 -0700145 if (isOpticalLink(edge.link())) {
146 return 1000.0; // optical links
weibit7e583462014-10-23 10:14:05 -0700147 } else {
weibit50eb95b2014-10-25 21:47:54 -0700148 return 1.0; // packet links
weibit7e583462014-10-23 10:14:05 -0700149 }
150 }
151 };
weibitf32383b2014-10-22 10:17:31 -0700152
weibit7e583462014-10-23 10:14:05 -0700153 Set<Path> paths = topologyService.getPaths(topology,
154 pktIntent.ingressPoint().deviceId(),
155 pktIntent.egressPoint().deviceId(),
156 weight);
157
158 if (paths.isEmpty()) {
weibitf32383b2014-10-22 10:17:31 -0700159 return;
160 }
161
weibit7e583462014-10-23 10:14:05 -0700162 ConnectPoint srcWdmPoint = null;
163 ConnectPoint dstWdmPoint = null;
164 Iterator<Path> itrPath = paths.iterator();
165 Path firstPath = itrPath.next();
166 log.info(firstPath.toString());
weibitf32383b2014-10-22 10:17:31 -0700167
weibit7e583462014-10-23 10:14:05 -0700168 ArrayList<Map<ConnectPoint, ConnectPoint>> connectionList = new ArrayList<>();
169
170 Iterator<Link> itrLink = firstPath.links().iterator();
171 while (itrLink.hasNext()) {
172 Link link1 = itrLink.next();
173 if (!isOpticalLink(link1)) {
174 continue;
175 } else {
176 srcWdmPoint = link1.dst();
177 dstWdmPoint = srcWdmPoint;
178 }
179
180 while (true) {
weibit7e583462014-10-23 10:14:05 -0700181 if (itrLink.hasNext()) {
182 Link link2 = itrLink.next();
183 dstWdmPoint = link2.src();
184 } else {
185 break;
186 }
weibit7e583462014-10-23 10:14:05 -0700187 if (itrLink.hasNext()) {
188 Link link3 = itrLink.next();
189 if (!isOpticalLink(link3)) {
190 break;
191 }
192 } else {
193 break;
194 }
195 }
196
197 Map<ConnectPoint, ConnectPoint> pair =
198 new HashMap<ConnectPoint, ConnectPoint>();
199 pair.put(srcWdmPoint, dstWdmPoint);
200
201 connectionList.add(pair);
weibitf32383b2014-10-22 10:17:31 -0700202 }
203
weibit7e583462014-10-23 10:14:05 -0700204 for (Map<ConnectPoint, ConnectPoint> map : connectionList) {
205 for (Entry<ConnectPoint, ConnectPoint> entry : map.entrySet()) {
weibitf32383b2014-10-22 10:17:31 -0700206
weibit7e583462014-10-23 10:14:05 -0700207 ConnectPoint src = entry.getKey();
208 ConnectPoint dst = entry.getValue();
weibitf32383b2014-10-22 10:17:31 -0700209
weibit7e583462014-10-23 10:14:05 -0700210 Intent opticalIntent = new OpticalConnectivityIntent(appId,
211 srcWdmPoint,
212 dstWdmPoint);
213
214 intentService.submit(opticalIntent);
215
216 log.info(opticalIntent.toString());
217 }
218 }
219
220 }
221
222 private boolean isOpticalLink(Link link) {
223 boolean isOptical = false;
weibit50eb95b2014-10-25 21:47:54 -0700224 Link.Type lt = link.type();
225 if (lt == Link.Type.OPTICAL) {
226 isOptical = true;
weibit7e583462014-10-23 10:14:05 -0700227 }
228 return isOptical;
weibitf32383b2014-10-22 10:17:31 -0700229 }
230
231 private void teardownLightpath(Intent intent) {
232 // TODO: tear down the idle lightpath if the utilization is close to zero.
233 }
234
235 }
236
237}