blob: e99b56b2a69fcda2e4f4387ddc82609ef592f6a2 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
4 * 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
7 *
8 * 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.
15 */
Sho SHIMIZU6c28f832015-02-20 16:12:19 -080016package org.onosproject.net.intent.impl.compiler;
weibitf32383b2014-10-22 10:17:31 -070017
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070018import java.util.Collections;
Ray Milkeye076c792015-03-24 09:38:30 -070019import java.util.List;
20import java.util.Set;
21
weibitf32383b2014-10-22 10:17:31 -070022import org.apache.felix.scr.annotations.Activate;
23import org.apache.felix.scr.annotations.Component;
24import org.apache.felix.scr.annotations.Deactivate;
25import org.apache.felix.scr.annotations.Reference;
26import org.apache.felix.scr.annotations.ReferenceCardinality;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070027import org.onlab.util.Frequency;
28import org.onosproject.net.ChannelSpacing;
Brian O'Connorabafb502014-12-02 22:26:20 -080029import org.onosproject.net.ConnectPoint;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070030import org.onosproject.net.GridType;
Brian O'Connorabafb502014-12-02 22:26:20 -080031import org.onosproject.net.Link;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070032import org.onosproject.net.OchPort;
33import org.onosproject.net.OchSignal;
Marc De Leenheerd24420f2015-05-27 09:40:59 -070034import org.onosproject.net.OchSignalType;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070035import org.onosproject.net.OmsPort;
Brian O'Connorabafb502014-12-02 22:26:20 -080036import org.onosproject.net.Path;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070037import org.onosproject.net.Port;
38import org.onosproject.net.device.DeviceService;
Brian O'Connorabafb502014-12-02 22:26:20 -080039import org.onosproject.net.intent.Intent;
40import org.onosproject.net.intent.IntentCompiler;
41import org.onosproject.net.intent.IntentExtensionService;
42import org.onosproject.net.intent.OpticalConnectivityIntent;
43import org.onosproject.net.intent.OpticalPathIntent;
Brian O'Connor6de2e202015-05-21 14:30:41 -070044import org.onosproject.net.resource.link.DefaultLinkResourceRequest;
45import org.onosproject.net.resource.device.DeviceResourceService;
46import org.onosproject.net.resource.link.LambdaResource;
47import org.onosproject.net.resource.link.LambdaResourceAllocation;
48import org.onosproject.net.resource.link.LinkResourceAllocations;
49import org.onosproject.net.resource.link.LinkResourceRequest;
50import org.onosproject.net.resource.link.LinkResourceService;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070051import org.onosproject.net.resource.ResourceAllocation;
52import org.onosproject.net.resource.ResourceType;
Brian O'Connorabafb502014-12-02 22:26:20 -080053import org.onosproject.net.topology.LinkWeight;
54import org.onosproject.net.topology.Topology;
55import org.onosproject.net.topology.TopologyEdge;
56import org.onosproject.net.topology.TopologyService;
Thomas Vachuska425a2d72014-10-29 11:28:28 -070057
Ray Milkeye076c792015-03-24 09:38:30 -070058import com.google.common.collect.ImmutableList;
weibitf32383b2014-10-22 10:17:31 -070059
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070060import static com.google.common.base.Preconditions.checkArgument;
61
weibitf32383b2014-10-22 10:17:31 -070062/**
Brian O'Connorabafb502014-12-02 22:26:20 -080063 * An intent compiler for {@link org.onosproject.net.intent.OpticalConnectivityIntent}.
weibitf32383b2014-10-22 10:17:31 -070064 */
65@Component(immediate = true)
66public class OpticalConnectivityIntentCompiler implements IntentCompiler<OpticalConnectivityIntent> {
67
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070068 private static final GridType DEFAULT_OCH_GRIDTYPE = GridType.DWDM;
69 private static final ChannelSpacing DEFAULT_CHANNEL_SPACING = ChannelSpacing.CHL_50GHZ;
70
weibitf32383b2014-10-22 10:17:31 -070071 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
72 protected IntentExtensionService intentManager;
73
74 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
weibit7e583462014-10-23 10:14:05 -070075 protected TopologyService topologyService;
weibitf32383b2014-10-22 10:17:31 -070076
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070077 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
78 protected DeviceService deviceService;
79
80 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
81 protected LinkResourceService linkResourceService;
82
83 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
84 protected DeviceResourceService deviceResourceService;
85
weibitf32383b2014-10-22 10:17:31 -070086 @Activate
87 public void activate() {
weibitf32383b2014-10-22 10:17:31 -070088 intentManager.registerCompiler(OpticalConnectivityIntent.class, this);
89 }
90
91 @Deactivate
92 public void deactivate() {
93 intentManager.unregisterCompiler(OpticalConnectivityIntent.class);
94 }
95
96 @Override
Brian O'Connorfa81eae2014-10-30 13:20:05 -070097 public List<Intent> compile(OpticalConnectivityIntent intent,
98 List<Intent> installable,
99 Set<LinkResourceAllocations> resources) {
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700100 // Check if source and destination are optical OCh ports
101 ConnectPoint src = intent.getSrc();
102 ConnectPoint dst = intent.getDst();
103 Port srcPort = deviceService.getPort(src.deviceId(), src.port());
104 Port dstPort = deviceService.getPort(dst.deviceId(), dst.port());
105 checkArgument(srcPort instanceof OchPort);
106 checkArgument(dstPort instanceof OchPort);
107
108 // Calculate available light paths
109 Set<Path> paths = getOpticalPaths(intent);
110
111 // Use first path that can be successfully reserved
112 for (Path path : paths) {
113 // Request and reserve lambda on path
114 LinkResourceAllocations linkAllocs = assignWavelength(intent, path);
115 if (linkAllocs == null) {
116 continue;
117 }
118
119 OmsPort omsPort = (OmsPort) deviceService.getPort(path.src().deviceId(), path.src().port());
120
121 // Try to reserve port resources, roll back if unsuccessful
122 Set<Port> portAllocs = deviceResourceService.requestPorts(intent);
123 if (portAllocs == null) {
124 linkResourceService.releaseResources(linkAllocs);
125 continue;
126 }
127
128 // Create installable optical path intent
129 LambdaResourceAllocation lambdaAlloc = getWavelength(path, linkAllocs);
130 OchSignal ochSignal = getOchSignal(lambdaAlloc, omsPort.minFrequency(), omsPort.grid());
Marc De Leenheerd24420f2015-05-27 09:40:59 -0700131 // Only support fixed grid for now
132 OchSignalType signalType = OchSignalType.FIXED_GRID;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700133
134 Intent newIntent = OpticalPathIntent.builder()
135 .appId(intent.appId())
136 .src(intent.getSrc())
137 .dst(intent.getDst())
138 .path(path)
139 .lambda(ochSignal)
Marc De Leenheerd24420f2015-05-27 09:40:59 -0700140 .signalType(signalType)
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700141 .build();
142
143 return ImmutableList.of(newIntent);
144 }
145
146 return Collections.emptyList();
weibitf32383b2014-10-22 10:17:31 -0700147 }
148
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700149 /**
150 * Find the lambda allocated to the path.
151 *
152 * @param path the path
153 * @param linkAllocs the link allocations
154 * @return
155 */
156 private LambdaResourceAllocation getWavelength(Path path, LinkResourceAllocations linkAllocs) {
157 for (Link link : path.links()) {
158 for (ResourceAllocation alloc : linkAllocs.getResourceAllocation(link)) {
159 if (alloc.type() == ResourceType.LAMBDA) {
160 return (LambdaResourceAllocation) alloc;
161 }
162 }
163 }
164
165 return null;
166 }
167
168 /**
169 * Request and reserve first available wavelength across path.
170 *
171 * @param path path in WDM topology
172 * @return first available lambda resource allocation
173 */
174 private LinkResourceAllocations assignWavelength(Intent intent, Path path) {
175 LinkResourceRequest.Builder request =
176 DefaultLinkResourceRequest.builder(intent.id(), path.links())
177 .addLambdaRequest();
178
179 LinkResourceAllocations allocations = linkResourceService.requestResources(request.build());
180
181 checkWavelengthContinuity(allocations, path);
182
183 return allocations;
184 }
185
186 /**
187 * Checks wavelength continuity constraint across path, i.e., an identical lambda is used on all links.
188 * @return true if wavelength continuity is met, false otherwise
189 */
190 private boolean checkWavelengthContinuity(LinkResourceAllocations allocations, Path path) {
191 if (allocations == null) {
192 return false;
193 }
194
195 LambdaResource lambda = null;
196
197 for (Link link : path.links()) {
198 for (ResourceAllocation alloc : allocations.getResourceAllocation(link)) {
199 if (alloc.type() == ResourceType.LAMBDA) {
200 LambdaResource nextLambda = ((LambdaResourceAllocation) alloc).lambda();
201 if (nextLambda == null) {
202 return false;
203 }
204 if (lambda == null) {
205 lambda = nextLambda;
206 continue;
207 }
208 if (!lambda.equals(nextLambda)) {
209 return false;
210 }
211 }
212 }
213 }
214
215 return true;
216 }
217
218 /**
219 * Convert lambda resource allocation in OCh signal.
220 *
221 * @param alloc lambda resource allocation
222 * @param minFrequency minimum frequency
223 * @param grid grid spacing frequency
224 * @return OCh signal
225 */
226 private OchSignal getOchSignal(LambdaResourceAllocation alloc, Frequency minFrequency, Frequency grid) {
227 int channel = alloc.lambda().toInt();
228
229 // Calculate center frequency
230 Frequency centerFrequency = minFrequency.add(grid.multiply(channel)).add(grid.floorDivision(2));
231
232 // Build OCh signal object
233 int spacingMultiplier = (int) (centerFrequency.subtract(OchSignal.CENTER_FREQUENCY).asHz() / grid.asHz());
234 int slotGranularity = (int) (grid.asHz() / ChannelSpacing.CHL_12P5GHZ.frequency().asHz());
235 OchSignal ochSignal = new OchSignal(DEFAULT_OCH_GRIDTYPE, DEFAULT_CHANNEL_SPACING,
236 spacingMultiplier, slotGranularity);
237
238 return ochSignal;
239 }
240
241 /**
242 * Calculates optical paths in WDM topology.
243 *
244 * @param intent optical connectivity intent
245 * @return set of paths in WDM topology
246 */
247 private Set<Path> getOpticalPaths(OpticalConnectivityIntent intent) {
248 // Route in WDM topology
weibit7e583462014-10-23 10:14:05 -0700249 Topology topology = topologyService.currentTopology();
250 LinkWeight weight = new LinkWeight() {
251 @Override
252 public double weight(TopologyEdge edge) {
Praseed Balakrishnan00dd1f92014-11-19 17:12:36 -0800253 if (edge.link().state() == Link.State.INACTIVE) {
254 return -1;
255 }
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700256 if (edge.link().type() != Link.Type.OPTICAL) {
257 return -1;
258 }
259 return edge.link().annotations().value("optical.type").equals("WDM") ? +1 : -1;
weibit7e583462014-10-23 10:14:05 -0700260 }
261 };
weibitf32383b2014-10-22 10:17:31 -0700262
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700263 ConnectPoint start = intent.getSrc();
264 ConnectPoint end = intent.getDst();
Thomas Vachuska425a2d72014-10-29 11:28:28 -0700265 Set<Path> paths = topologyService.getPaths(topology, start.deviceId(),
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700266 end.deviceId(), weight);
weibit7e583462014-10-23 10:14:05 -0700267
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700268 return paths;
weibitf32383b2014-10-22 10:17:31 -0700269 }
weibitf32383b2014-10-22 10:17:31 -0700270}