blob: e91235c50593667b4d9a6815a45670457c1cd178 [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;
33import org.onlab.onos.net.topology.TopologyGraph;
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:
99 log.info("intent {} failed, calling optical path provisioning APP.", event.subject());
100 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) {
weibit7e583462014-10-23 10:14:05 -0700112 // TODO: considering user policies and optical reach
weibitf32383b2014-10-22 10:17:31 -0700113 if (!intent.equals(PointToPointIntent.class)) {
114 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();
123 TopologyGraph graph = topologyService.getGraph(topology);
weibitf32383b2014-10-22 10:17:31 -0700124
weibit7e583462014-10-23 10:14:05 -0700125 LinkWeight weight = new LinkWeight() {
126 @Override
127 public double weight(TopologyEdge edge) {
128 boolean isOptical = false;
129 String t = edge.link().annotations().value("linkType");
130 if (t.equals("WDM")) {
131 isOptical = true;
132 }
133 if (isOptical) {
134 return 1000; // optical links
135 } else {
136 return 10; // packet links
137 }
138 }
139 };
weibitf32383b2014-10-22 10:17:31 -0700140
weibit7e583462014-10-23 10:14:05 -0700141 Set<Path> paths = topologyService.getPaths(topology,
142 pktIntent.ingressPoint().deviceId(),
143 pktIntent.egressPoint().deviceId(),
144 weight);
145
146 if (paths.isEmpty()) {
weibitf32383b2014-10-22 10:17:31 -0700147 return;
148 }
149
weibit7e583462014-10-23 10:14:05 -0700150 ConnectPoint srcWdmPoint = null;
151 ConnectPoint dstWdmPoint = null;
152 Iterator<Path> itrPath = paths.iterator();
153 Path firstPath = itrPath.next();
154 log.info(firstPath.toString());
weibitf32383b2014-10-22 10:17:31 -0700155
weibit7e583462014-10-23 10:14:05 -0700156 ArrayList<Map<ConnectPoint, ConnectPoint>> connectionList = new ArrayList<>();
157
158 Iterator<Link> itrLink = firstPath.links().iterator();
159 while (itrLink.hasNext()) {
160 Link link1 = itrLink.next();
161 if (!isOpticalLink(link1)) {
162 continue;
163 } else {
164 srcWdmPoint = link1.dst();
165 dstWdmPoint = srcWdmPoint;
166 }
167
168 while (true) {
169
170 if (itrLink.hasNext()) {
171 Link link2 = itrLink.next();
172 dstWdmPoint = link2.src();
173 } else {
174 break;
175 }
176
177 if (itrLink.hasNext()) {
178 Link link3 = itrLink.next();
179 if (!isOpticalLink(link3)) {
180 break;
181 }
182 } else {
183 break;
184 }
185 }
186
187 Map<ConnectPoint, ConnectPoint> pair =
188 new HashMap<ConnectPoint, ConnectPoint>();
189 pair.put(srcWdmPoint, dstWdmPoint);
190
191 connectionList.add(pair);
weibitf32383b2014-10-22 10:17:31 -0700192 }
193
weibit7e583462014-10-23 10:14:05 -0700194 for (Map<ConnectPoint, ConnectPoint> map : connectionList) {
195 for (Entry<ConnectPoint, ConnectPoint> entry : map.entrySet()) {
weibitf32383b2014-10-22 10:17:31 -0700196
weibit7e583462014-10-23 10:14:05 -0700197 ConnectPoint src = entry.getKey();
198 ConnectPoint dst = entry.getValue();
weibitf32383b2014-10-22 10:17:31 -0700199
weibit7e583462014-10-23 10:14:05 -0700200 Intent opticalIntent = new OpticalConnectivityIntent(appId,
201 srcWdmPoint,
202 dstWdmPoint);
203
204 intentService.submit(opticalIntent);
205
206 log.info(opticalIntent.toString());
207 }
208 }
209
210 }
211
212 private boolean isOpticalLink(Link link) {
213 boolean isOptical = false;
214 String t = link.annotations().value("linkType");
215 if (t.equals("WDM") || t.equals("PktOptLink")) {
216 isOptical = true;
217 }
218 return isOptical;
weibitf32383b2014-10-22 10:17:31 -0700219 }
220
221 private void teardownLightpath(Intent intent) {
222 // TODO: tear down the idle lightpath if the utilization is close to zero.
223 }
224
225 }
226
227}