blob: 9f77cfd4b85494decc729b563c13603fbab6f0d6 [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:
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();
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) {
127 boolean isOptical = false;
128 String t = edge.link().annotations().value("linkType");
129 if (t.equals("WDM")) {
130 isOptical = true;
131 }
132 if (isOptical) {
133 return 1000; // optical links
134 } else {
135 return 10; // packet links
136 }
137 }
138 };
weibitf32383b2014-10-22 10:17:31 -0700139
weibit7e583462014-10-23 10:14:05 -0700140 Set<Path> paths = topologyService.getPaths(topology,
141 pktIntent.ingressPoint().deviceId(),
142 pktIntent.egressPoint().deviceId(),
143 weight);
144
145 if (paths.isEmpty()) {
weibitf32383b2014-10-22 10:17:31 -0700146 return;
147 }
148
weibit7e583462014-10-23 10:14:05 -0700149 ConnectPoint srcWdmPoint = null;
150 ConnectPoint dstWdmPoint = null;
151 Iterator<Path> itrPath = paths.iterator();
152 Path firstPath = itrPath.next();
153 log.info(firstPath.toString());
weibitf32383b2014-10-22 10:17:31 -0700154
weibit7e583462014-10-23 10:14:05 -0700155 ArrayList<Map<ConnectPoint, ConnectPoint>> connectionList = new ArrayList<>();
156
157 Iterator<Link> itrLink = firstPath.links().iterator();
158 while (itrLink.hasNext()) {
159 Link link1 = itrLink.next();
160 if (!isOpticalLink(link1)) {
161 continue;
162 } else {
163 srcWdmPoint = link1.dst();
164 dstWdmPoint = srcWdmPoint;
165 }
166
167 while (true) {
168
169 if (itrLink.hasNext()) {
170 Link link2 = itrLink.next();
171 dstWdmPoint = link2.src();
172 } else {
173 break;
174 }
175
176 if (itrLink.hasNext()) {
177 Link link3 = itrLink.next();
178 if (!isOpticalLink(link3)) {
179 break;
180 }
181 } else {
182 break;
183 }
184 }
185
186 Map<ConnectPoint, ConnectPoint> pair =
187 new HashMap<ConnectPoint, ConnectPoint>();
188 pair.put(srcWdmPoint, dstWdmPoint);
189
190 connectionList.add(pair);
weibitf32383b2014-10-22 10:17:31 -0700191 }
192
weibit7e583462014-10-23 10:14:05 -0700193 for (Map<ConnectPoint, ConnectPoint> map : connectionList) {
194 for (Entry<ConnectPoint, ConnectPoint> entry : map.entrySet()) {
weibitf32383b2014-10-22 10:17:31 -0700195
weibit7e583462014-10-23 10:14:05 -0700196 ConnectPoint src = entry.getKey();
197 ConnectPoint dst = entry.getValue();
weibitf32383b2014-10-22 10:17:31 -0700198
weibit7e583462014-10-23 10:14:05 -0700199 Intent opticalIntent = new OpticalConnectivityIntent(appId,
200 srcWdmPoint,
201 dstWdmPoint);
202
203 intentService.submit(opticalIntent);
204
205 log.info(opticalIntent.toString());
206 }
207 }
208
209 }
210
211 private boolean isOpticalLink(Link link) {
212 boolean isOptical = false;
213 String t = link.annotations().value("linkType");
214 if (t.equals("WDM") || t.equals("PktOptLink")) {
215 isOptical = true;
216 }
217 return isOptical;
weibitf32383b2014-10-22 10:17:31 -0700218 }
219
220 private void teardownLightpath(Intent intent) {
221 // TODO: tear down the idle lightpath if the utilization is close to zero.
222 }
223
224 }
225
226}