blob: 74a5f08a93b2640827337afb21f79fd3c7060b07 [file] [log] [blame]
weibitf32383b2014-10-22 10:17:31 -07001package org.onlab.onos.optical.provisioner;
2
weibit7e583462014-10-23 10:14:05 -07003import java.util.ArrayList;
4import java.util.HashMap;
weibitf32383b2014-10-22 10:17:31 -07005import java.util.Iterator;
weibit7e583462014-10-23 10:14:05 -07006import java.util.Map;
7import java.util.Map.Entry;
weibitf32383b2014-10-22 10:17:31 -07008import java.util.Set;
9
10import org.apache.felix.scr.annotations.Activate;
11import org.apache.felix.scr.annotations.Component;
12import org.apache.felix.scr.annotations.Deactivate;
13import org.apache.felix.scr.annotations.Reference;
14import org.apache.felix.scr.annotations.ReferenceCardinality;
weibitf32383b2014-10-22 10:17:31 -070015import org.onlab.onos.ApplicationId;
16import org.onlab.onos.CoreService;
17import org.onlab.onos.net.ConnectPoint;
weibitf32383b2014-10-22 10:17:31 -070018import org.onlab.onos.net.Link;
weibit7e583462014-10-23 10:14:05 -070019import org.onlab.onos.net.Path;
weibitf32383b2014-10-22 10:17:31 -070020import org.onlab.onos.net.device.DeviceService;
weibitf32383b2014-10-22 10:17:31 -070021import org.onlab.onos.net.intent.Intent;
weibitf32383b2014-10-22 10:17:31 -070022import org.onlab.onos.net.intent.IntentEvent;
23import org.onlab.onos.net.intent.IntentExtensionService;
weibitf32383b2014-10-22 10:17:31 -070024import org.onlab.onos.net.intent.IntentListener;
25import org.onlab.onos.net.intent.IntentService;
26import org.onlab.onos.net.intent.OpticalConnectivityIntent;
27import org.onlab.onos.net.intent.PointToPointIntent;
28import org.onlab.onos.net.link.LinkService;
weibit7e583462014-10-23 10:14:05 -070029import org.onlab.onos.net.resource.LinkResourceService;
30import org.onlab.onos.net.topology.LinkWeight;
31import org.onlab.onos.net.topology.Topology;
32import org.onlab.onos.net.topology.TopologyEdge;
weibit9e622ac2014-10-23 13:45:44 -070033
weibitf32383b2014-10-22 10:17:31 -070034import org.onlab.onos.net.topology.TopologyService;
35import org.slf4j.Logger;
36import org.slf4j.LoggerFactory;
37
38/**
39 * OpticalPathProvisioner listens event notifications from the Intent F/W.
40 * It generates one or more opticalConnectivityIntent(s) and submits (or withdraws) to Intent F/W
41 * for adding/releasing capacity at the packet layer.
42 *
43 */
44
45@Component(immediate = true)
46public class OpticalPathProvisioner {
47
48 protected static final Logger log = LoggerFactory
49 .getLogger(OpticalPathProvisioner.class);
50
51 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
52 private IntentService intentService;
53
54 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
55 private IntentExtensionService intentExtensionService;
56
57 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
58 protected LinkService linkService;
59
60 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
61 protected DeviceService deviceService;
62
63 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
64 protected TopologyService topologyService;
65
66 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
67 protected CoreService coreService;
68
weibit7e583462014-10-23 10:14:05 -070069 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
70 protected LinkResourceService resourceService;
weibitf32383b2014-10-22 10:17:31 -070071
72 private ApplicationId appId;
weibitf32383b2014-10-22 10:17:31 -070073
weibit7e583462014-10-23 10:14:05 -070074 //protected <IntentId> intentIdGenerator;
weibitf32383b2014-10-22 10:17:31 -070075
76 private final InternalOpticalPathProvisioner pathProvisioner = new InternalOpticalPathProvisioner();
77
78 @Activate
79 protected void activate() {
80 intentService.addListener(pathProvisioner);
81 appId = coreService.registerApplication("org.onlab.onos.optical");
82 log.info("Starting optical path provisoning...");
83 }
84
85 @Deactivate
86 protected void deactivate() {
87 intentService.removeListener(pathProvisioner);
88 }
89
90 public class InternalOpticalPathProvisioner implements IntentListener {
91 @Override
92 public void event(IntentEvent event) {
93 switch (event.type()) {
94 case SUBMITTED:
95 break;
96 case INSTALLED:
97 break;
98 case FAILED:
weibit50eb95b2014-10-25 21:47:54 -070099 log.info("packet intent {} failed, calling optical path provisioning APP.", event.subject());
weibitf32383b2014-10-22 10:17:31 -0700100 setuplightpath(event.subject());
101 break;
102 case WITHDRAWN:
103 log.info("intent {} withdrawn.", event.subject());
104 teardownLightpath(event.subject());
105 break;
106 default:
107 break;
108 }
109 }
110
111 private void setuplightpath(Intent intent) {
weibit50eb95b2014-10-25 21:47:54 -0700112 // TODO support more packet intent types
113 if (!intent.getClass().equals(PointToPointIntent.class)) {
weibitf32383b2014-10-22 10:17:31 -0700114 return;
115 }
116
117 PointToPointIntent pktIntent = (PointToPointIntent) intent;
118 if (pktIntent.ingressPoint() == null || pktIntent.egressPoint() == null) {
119 return;
120 }
121
weibit7e583462014-10-23 10:14:05 -0700122 Topology topology = topologyService.currentTopology();
weibitf32383b2014-10-22 10:17:31 -0700123
weibit7e583462014-10-23 10:14:05 -0700124 LinkWeight weight = new LinkWeight() {
125 @Override
126 public double weight(TopologyEdge edge) {
weibit50eb95b2014-10-25 21:47:54 -0700127 if (isOpticalLink(edge.link())) {
128 return 1000.0; // optical links
weibit7e583462014-10-23 10:14:05 -0700129 } else {
weibit50eb95b2014-10-25 21:47:54 -0700130 return 1.0; // packet links
weibit7e583462014-10-23 10:14:05 -0700131 }
132 }
133 };
weibitf32383b2014-10-22 10:17:31 -0700134
weibit7e583462014-10-23 10:14:05 -0700135 Set<Path> paths = topologyService.getPaths(topology,
136 pktIntent.ingressPoint().deviceId(),
137 pktIntent.egressPoint().deviceId(),
138 weight);
139
140 if (paths.isEmpty()) {
weibitf32383b2014-10-22 10:17:31 -0700141 return;
142 }
143
weibit7e583462014-10-23 10:14:05 -0700144 ConnectPoint srcWdmPoint = null;
145 ConnectPoint dstWdmPoint = null;
146 Iterator<Path> itrPath = paths.iterator();
147 Path firstPath = itrPath.next();
148 log.info(firstPath.toString());
weibitf32383b2014-10-22 10:17:31 -0700149
weibit7e583462014-10-23 10:14:05 -0700150 ArrayList<Map<ConnectPoint, ConnectPoint>> connectionList = new ArrayList<>();
151
152 Iterator<Link> itrLink = firstPath.links().iterator();
153 while (itrLink.hasNext()) {
154 Link link1 = itrLink.next();
155 if (!isOpticalLink(link1)) {
156 continue;
157 } else {
158 srcWdmPoint = link1.dst();
159 dstWdmPoint = srcWdmPoint;
160 }
161
162 while (true) {
weibit7e583462014-10-23 10:14:05 -0700163 if (itrLink.hasNext()) {
164 Link link2 = itrLink.next();
165 dstWdmPoint = link2.src();
166 } else {
167 break;
168 }
weibit7e583462014-10-23 10:14:05 -0700169 if (itrLink.hasNext()) {
170 Link link3 = itrLink.next();
171 if (!isOpticalLink(link3)) {
172 break;
173 }
174 } else {
175 break;
176 }
177 }
178
179 Map<ConnectPoint, ConnectPoint> pair =
180 new HashMap<ConnectPoint, ConnectPoint>();
181 pair.put(srcWdmPoint, dstWdmPoint);
182
183 connectionList.add(pair);
weibitf32383b2014-10-22 10:17:31 -0700184 }
185
weibit7e583462014-10-23 10:14:05 -0700186 for (Map<ConnectPoint, ConnectPoint> map : connectionList) {
187 for (Entry<ConnectPoint, ConnectPoint> entry : map.entrySet()) {
weibitf32383b2014-10-22 10:17:31 -0700188
weibit7e583462014-10-23 10:14:05 -0700189 ConnectPoint src = entry.getKey();
190 ConnectPoint dst = entry.getValue();
weibitf32383b2014-10-22 10:17:31 -0700191
weibit7e583462014-10-23 10:14:05 -0700192 Intent opticalIntent = new OpticalConnectivityIntent(appId,
193 srcWdmPoint,
194 dstWdmPoint);
195
196 intentService.submit(opticalIntent);
197
198 log.info(opticalIntent.toString());
199 }
200 }
201
202 }
203
204 private boolean isOpticalLink(Link link) {
205 boolean isOptical = false;
weibit50eb95b2014-10-25 21:47:54 -0700206 Link.Type lt = link.type();
207 if (lt == Link.Type.OPTICAL) {
208 isOptical = true;
weibit7e583462014-10-23 10:14:05 -0700209 }
210 return isOptical;
weibitf32383b2014-10-22 10:17:31 -0700211 }
212
213 private void teardownLightpath(Intent intent) {
214 // TODO: tear down the idle lightpath if the utilization is close to zero.
215 }
216
217 }
218
219}