blob: 09fd0b9959b3fad31283d9c3bff3fa5e0fa2f180 [file] [log] [blame]
Sho SHIMIZU91210a72015-04-29 12:54:28 -07001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
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 */
16package org.onosproject.net;
17
Marc De Leenheer2c305302015-12-07 21:37:44 -080018import com.google.common.collect.ImmutableSet;
Sho SHIMIZU91210a72015-04-29 12:54:28 -070019import org.onlab.util.Frequency;
Marc De Leenheerb0fb41d2015-12-03 22:16:53 -080020import org.onlab.util.Spectrum;
Sho SHIMIZU91210a72015-04-29 12:54:28 -070021
Marc De Leenheer2c305302015-12-07 21:37:44 -080022import java.util.List;
Sho SHIMIZU91210a72015-04-29 12:54:28 -070023import java.util.Objects;
Marc De Leenheer2c305302015-12-07 21:37:44 -080024import java.util.Set;
25import java.util.SortedSet;
Marc De Leenheer2c305302015-12-07 21:37:44 -080026import java.util.stream.Collectors;
27import java.util.stream.IntStream;
Sho SHIMIZU91210a72015-04-29 12:54:28 -070028
29import static com.google.common.base.Preconditions.checkArgument;
30import static com.google.common.base.Preconditions.checkNotNull;
HIGUCHI Yutaca8cb4e2015-12-11 13:57:05 -080031import static org.onosproject.net.ChannelSpacing.CHL_12P5GHZ;
32import static org.onosproject.net.ChannelSpacing.CHL_6P25GHZ;
33import static org.onosproject.net.GridType.DWDM;
34import static org.onosproject.net.GridType.FLEX;
Sho SHIMIZU91210a72015-04-29 12:54:28 -070035
36/**
37 * Implementation of Lambda representing OCh (Optical Channel) Signal.
38 *
39 * <p>
40 * See ITU G.709 "Interfaces for the Optical Transport Network (OTN)".
HIGUCHI Yutaca8cb4e2015-12-11 13:57:05 -080041 * ITU G.694.1 "Spectral grids for WDM applications: DWDM frequency grid".
Sho SHIMIZU91210a72015-04-29 12:54:28 -070042 * </p>
43 */
Sho SHIMIZU91210a72015-04-29 12:54:28 -070044public class OchSignal implements Lambda {
45
Marc De Leenheer2c305302015-12-07 21:37:44 -080046 public static final Set<Integer> FIXED_GRID_SLOT_GRANULARITIES = ImmutableSet.of(1, 2, 4, 8);
Marc De Leenheerc9733082015-06-04 12:22:38 -070047 private static final GridType DEFAULT_OCH_GRIDTYPE = GridType.DWDM;
48 private static final ChannelSpacing DEFAULT_CHANNEL_SPACING = ChannelSpacing.CHL_50GHZ;
49
Sho SHIMIZU91210a72015-04-29 12:54:28 -070050 private final GridType gridType;
51 private final ChannelSpacing channelSpacing;
Marc De Leenheer2c305302015-12-07 21:37:44 -080052 // Nominal central frequency = 193.1 THz + spacingMultiplier * channelSpacing
Sho SHIMIZU91210a72015-04-29 12:54:28 -070053 private final int spacingMultiplier;
54 // Slot width = slotGranularity * 12.5 GHz
55 private final int slotGranularity;
56
57 /**
58 * Creates an instance with the specified arguments.
Sho SHIMIZU4fea4fd2015-05-07 11:50:23 -070059 * It it recommended to use {@link Lambda#ochSignal(GridType, ChannelSpacing, int, int)}
60 * unless you want to use the concrete type, OchSignal, directly.
Sho SHIMIZU91210a72015-04-29 12:54:28 -070061 *
62 * @param gridType grid type
63 * @param channelSpacing channel spacing
64 * @param spacingMultiplier channel spacing multiplier
65 * @param slotGranularity slot width granularity
66 */
Sho SHIMIZU4fea4fd2015-05-07 11:50:23 -070067 public OchSignal(GridType gridType, ChannelSpacing channelSpacing,
Sho SHIMIZU91210a72015-04-29 12:54:28 -070068 int spacingMultiplier, int slotGranularity) {
69 this.gridType = checkNotNull(gridType);
70 this.channelSpacing = checkNotNull(channelSpacing);
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070071 // Negative values are permitted for spacingMultiplier
Sho SHIMIZU91210a72015-04-29 12:54:28 -070072 this.spacingMultiplier = spacingMultiplier;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070073 checkArgument(slotGranularity > 0, "slotGranularity must be larger than 0, received %s", slotGranularity);
Sho SHIMIZU91210a72015-04-29 12:54:28 -070074 this.slotGranularity = slotGranularity;
75 }
76
77 /**
HIGUCHI Yutaca8cb4e2015-12-11 13:57:05 -080078 * Creates an instance of {@link OchSignal} representing a flex grid frequency slot.
79 * @param index slot index (relative to "the center frequency" 193.1THz)
80 * @return FlexGrid {@link OchSignal}
81 */
82 public static OchSignal newFlexGridSlot(int index) {
83 return new OchSignal(FLEX, CHL_6P25GHZ, index, 1);
84 }
85
86 /**
87 * Creates an instance of {@link OchSignal} representing a fixed DWDM frequency slot.
88 * @param spacing channel spacing
89 * @param index slot index (relative to "the center frequency" 193.1THz)
90 * @return DWDM {@link OchSignal}
91 */
92 public static OchSignal newDwdmSlot(ChannelSpacing spacing, int index) {
93 return new OchSignal(DWDM, spacing, index,
94 (int) (spacing.frequency().asHz() / CHL_12P5GHZ.frequency().asHz()));
95 }
96
97 /**
Marc De Leenheer0b8b2ef2015-08-03 15:39:00 -070098 * Create OCh signal from channel number.
Marc De Leenheerc9733082015-06-04 12:22:38 -070099 *
Marc De Leenheer0b8b2ef2015-08-03 15:39:00 -0700100 * @param channel channel number
Marc De Leenheerc9733082015-06-04 12:22:38 -0700101 * @param maxFrequency maximum frequency
102 * @param grid grid spacing frequency
HIGUCHI Yuta603e8d92015-12-11 09:57:30 -0800103 *
Ray Milkeyea125322016-02-16 13:35:09 -0800104 * @deprecated 1.4.0 Emu Release
Marc De Leenheerc9733082015-06-04 12:22:38 -0700105 */
HIGUCHI Yuta603e8d92015-12-11 09:57:30 -0800106 @Deprecated
Marc De Leenheer0b8b2ef2015-08-03 15:39:00 -0700107 public OchSignal(int channel, Frequency maxFrequency, Frequency grid) {
Marc De Leenheerc9733082015-06-04 12:22:38 -0700108 // Calculate center frequency
109 Frequency centerFrequency = maxFrequency.subtract(grid.multiply(channel - 1));
110
111 this.gridType = DEFAULT_OCH_GRIDTYPE;
112 this.channelSpacing = DEFAULT_CHANNEL_SPACING;
Marc De Leenheerb0fb41d2015-12-03 22:16:53 -0800113 this.spacingMultiplier = (int) (centerFrequency.subtract(Spectrum.CENTER_FREQUENCY).asHz() / grid.asHz());
Marc De Leenheerc9733082015-06-04 12:22:38 -0700114 this.slotGranularity = (int) Math.round((double) grid.asHz() / ChannelSpacing.CHL_12P5GHZ.frequency().asHz());
115 }
116
Ray Milkeyea125322016-02-16 13:35:09 -0800117 /**
118 * Creates OCh signal.
119 *
120 * @param centerFrequency frequency
121 * @param channelSpacing spacing
122 * @param slotGranularity granularity
123 * @deprecated 1.4.0 Emu Release
124 */
HIGUCHI Yuta603e8d92015-12-11 09:57:30 -0800125 @Deprecated
Marc De Leenheerc9733082015-06-04 12:22:38 -0700126 public OchSignal(Frequency centerFrequency, ChannelSpacing channelSpacing, int slotGranularity) {
127 this.gridType = DEFAULT_OCH_GRIDTYPE;
128 this.channelSpacing = channelSpacing;
129 this.spacingMultiplier = (int) Math.round((double) centerFrequency.
Marc De Leenheerb0fb41d2015-12-03 22:16:53 -0800130 subtract(Spectrum.CENTER_FREQUENCY).asHz() / channelSpacing().frequency().asHz());
Marc De Leenheerc9733082015-06-04 12:22:38 -0700131 this.slotGranularity = slotGranularity;
132 }
133
134 /**
Sho SHIMIZU91210a72015-04-29 12:54:28 -0700135 * Returns grid type.
136 *
137 * @return grid type
138 */
139 public GridType gridType() {
140 return gridType;
141 }
142
143 /**
144 * Returns channel spacing.
145 *
146 * @return channel spacing
147 */
148 public ChannelSpacing channelSpacing() {
149 return channelSpacing;
150 }
151
152 /**
153 * Returns spacing multiplier.
154 *
155 * @return spacing multiplier
156 */
157 public int spacingMultiplier() {
158 return spacingMultiplier;
159 }
160
161 /**
Marc De Leenheer2c305302015-12-07 21:37:44 -0800162 * Returns slot width granularity.
Sho SHIMIZU91210a72015-04-29 12:54:28 -0700163 *
Marc De Leenheer2c305302015-12-07 21:37:44 -0800164 * @return slot width granularity
Sho SHIMIZU91210a72015-04-29 12:54:28 -0700165 */
166 public int slotGranularity() {
167 return slotGranularity;
168 }
169
170 /**
171 * Returns central frequency in MHz.
172 *
173 * @return frequency in MHz
174 */
175 public Frequency centralFrequency() {
Marc De Leenheerb0fb41d2015-12-03 22:16:53 -0800176 return Spectrum.CENTER_FREQUENCY.add(channelSpacing().frequency().multiply(spacingMultiplier));
Sho SHIMIZU91210a72015-04-29 12:54:28 -0700177 }
178
179 /**
180 * Returns slot width.
181 *
182 * @return slot width
183 */
184 public Frequency slotWidth() {
Marc De Leenheer2c305302015-12-07 21:37:44 -0800185 return ChannelSpacing.CHL_12P5GHZ.frequency().multiply(slotGranularity);
186 }
187
188 /**
189 * Convert fixed grid OCh signal to sorted set of flex grid slots with 6.25 GHz spacing and 12.5 GHz slot width.
190 *
191 * @param ochSignal fixed grid lambda
192 * @return sorted set of flex grid OCh lambdas
193 */
194 public static SortedSet<OchSignal> toFlexGrid(OchSignal ochSignal) {
195 checkArgument(ochSignal.gridType() != GridType.FLEX);
196 checkArgument(ochSignal.channelSpacing() != ChannelSpacing.CHL_6P25GHZ);
197 checkArgument(FIXED_GRID_SLOT_GRANULARITIES.contains(ochSignal.slotGranularity()));
198
199 int startMultiplier = (int) (1 - ochSignal.slotGranularity() +
200 ochSignal.spacingMultiplier() * ochSignal.channelSpacing().frequency().asHz() /
201 ChannelSpacing.CHL_6P25GHZ.frequency().asHz());
202
Marc De Leenheer2c305302015-12-07 21:37:44 -0800203 return IntStream.range(0, ochSignal.slotGranularity())
204 .mapToObj(i -> new OchSignal(GridType.FLEX, ChannelSpacing.CHL_6P25GHZ, startMultiplier + 2 * i, 1))
HIGUCHI Yutaca8cb4e2015-12-11 13:57:05 -0800205 .collect(Collectors.toCollection(DefaultOchSignalComparator::newOchSignalTreeSet));
Marc De Leenheer2c305302015-12-07 21:37:44 -0800206 }
207
208 /**
209 * Convert list of lambdas with flex grid 6.25 GHz spacing and 12.5 GHz width into fixed grid OCh signal.
210 *
211 * @param lambdas list of flex grid lambdas in sorted order
212 * @param spacing desired fixed grid spacing
213 * @return fixed grid lambda
214 */
215 public static OchSignal toFixedGrid(List<OchSignal> lambdas, ChannelSpacing spacing) {
216 // Number of slots of 12.5 GHz that fit into requested spacing
217 int ratio = (int) (spacing.frequency().asHz() / ChannelSpacing.CHL_12P5GHZ.frequency().asHz());
218 checkArgument(lambdas.size() == ratio);
219 lambdas.forEach(x -> checkArgument(x.gridType() == GridType.FLEX));
220 lambdas.forEach(x -> checkArgument(x.channelSpacing() == ChannelSpacing.CHL_6P25GHZ));
221 lambdas.forEach(x -> checkArgument(x.slotGranularity() == 1));
222 // Consecutive lambdas (multiplier increments by 2 because spacing is 6.25 GHz but slot width is 12.5 GHz)
223 IntStream.range(1, lambdas.size())
224 .forEach(i -> checkArgument(
225 lambdas.get(i).spacingMultiplier() == lambdas.get(i - 1).spacingMultiplier() + 2));
226 // Is center frequency compatible with requested spacing
227 Frequency center = lambdas.get(ratio / 2).centralFrequency().subtract(ChannelSpacing.CHL_6P25GHZ.frequency());
228 checkArgument(Spectrum.CENTER_FREQUENCY.subtract(center).asHz() % spacing.frequency().asHz() == 0);
229
230 // Multiplier sits in middle of given lambdas, then convert from 6.25 to requested spacing
HIGUCHI Yutaca8cb4e2015-12-11 13:57:05 -0800231 int spacingMultiplier = lambdas.stream()
232 .mapToInt(OchSignal::spacingMultiplier)
233 .sum() / lambdas.size()
234 / (int) (spacing.frequency().asHz() / CHL_6P25GHZ.frequency().asHz());
Marc De Leenheer2c305302015-12-07 21:37:44 -0800235
236 return new OchSignal(GridType.DWDM, spacing, spacingMultiplier, lambdas.size());
Sho SHIMIZU91210a72015-04-29 12:54:28 -0700237 }
238
239 @Override
240 public int hashCode() {
241 return Objects.hash(gridType, channelSpacing, spacingMultiplier, slotGranularity);
242 }
243
244 @Override
245 public boolean equals(Object obj) {
246 if (this == obj) {
247 return true;
248 }
249 if (!(obj instanceof OchSignal)) {
250 return false;
251 }
252 final OchSignal other = (OchSignal) obj;
253 return Objects.equals(this.gridType, other.gridType)
254 && Objects.equals(this.channelSpacing, other.channelSpacing)
255 && Objects.equals(this.spacingMultiplier, other.spacingMultiplier)
256 && Objects.equals(this.slotGranularity, other.slotGranularity);
257 }
258
259 @Override
260 public String toString() {
Naoki Shiota0b5ccd22015-12-11 15:47:01 -0800261 return String.format("%s{%+d×%.2fGHz ± %.2fGHz}",
262 this.getClass().getSimpleName(),
263 spacingMultiplier,
Marc De Leenheer6215fbc2016-02-05 16:14:08 -0800264 (double) channelSpacing.frequency().asHz() / Frequency.ofGHz(1).asHz(),
Naoki Shiota0b5ccd22015-12-11 15:47:01 -0800265 (double) slotGranularity * ChannelSpacing.CHL_12P5GHZ.frequency().asHz()
266 / Frequency.ofGHz(1).asHz() / 2.0);
Sho SHIMIZU91210a72015-04-29 12:54:28 -0700267 }
268}