blob: fc60aaa2570383a096efba2bb40e2c73967fa4b5 [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
18import com.google.common.base.MoreObjects;
Marc De Leenheer2c305302015-12-07 21:37:44 -080019import com.google.common.collect.ImmutableSet;
Sho SHIMIZU91210a72015-04-29 12:54:28 -070020import org.onlab.util.Frequency;
Marc De Leenheerb0fb41d2015-12-03 22:16:53 -080021import org.onlab.util.Spectrum;
Sho SHIMIZU91210a72015-04-29 12:54:28 -070022
Marc De Leenheer2c305302015-12-07 21:37:44 -080023import java.util.List;
Sho SHIMIZU91210a72015-04-29 12:54:28 -070024import java.util.Objects;
Marc De Leenheer2c305302015-12-07 21:37:44 -080025import java.util.Set;
26import java.util.SortedSet;
Marc De Leenheer2c305302015-12-07 21:37:44 -080027import java.util.stream.Collectors;
28import java.util.stream.IntStream;
Sho SHIMIZU91210a72015-04-29 12:54:28 -070029
30import static com.google.common.base.Preconditions.checkArgument;
31import static com.google.common.base.Preconditions.checkNotNull;
HIGUCHI Yutaca8cb4e2015-12-11 13:57:05 -080032import static org.onosproject.net.ChannelSpacing.CHL_12P5GHZ;
33import static org.onosproject.net.ChannelSpacing.CHL_6P25GHZ;
34import static org.onosproject.net.GridType.DWDM;
35import static org.onosproject.net.GridType.FLEX;
Sho SHIMIZU91210a72015-04-29 12:54:28 -070036
37/**
38 * Implementation of Lambda representing OCh (Optical Channel) Signal.
39 *
40 * <p>
41 * See ITU G.709 "Interfaces for the Optical Transport Network (OTN)".
HIGUCHI Yutaca8cb4e2015-12-11 13:57:05 -080042 * ITU G.694.1 "Spectral grids for WDM applications: DWDM frequency grid".
Sho SHIMIZU91210a72015-04-29 12:54:28 -070043 * </p>
44 */
Sho SHIMIZU91210a72015-04-29 12:54:28 -070045public class OchSignal implements Lambda {
46
Marc De Leenheer2c305302015-12-07 21:37:44 -080047 public static final Set<Integer> FIXED_GRID_SLOT_GRANULARITIES = ImmutableSet.of(1, 2, 4, 8);
Marc De Leenheerc9733082015-06-04 12:22:38 -070048 private static final GridType DEFAULT_OCH_GRIDTYPE = GridType.DWDM;
49 private static final ChannelSpacing DEFAULT_CHANNEL_SPACING = ChannelSpacing.CHL_50GHZ;
50
Sho SHIMIZU91210a72015-04-29 12:54:28 -070051 private final GridType gridType;
52 private final ChannelSpacing channelSpacing;
Marc De Leenheer2c305302015-12-07 21:37:44 -080053 // Nominal central frequency = 193.1 THz + spacingMultiplier * channelSpacing
Sho SHIMIZU91210a72015-04-29 12:54:28 -070054 private final int spacingMultiplier;
55 // Slot width = slotGranularity * 12.5 GHz
56 private final int slotGranularity;
57
58 /**
59 * Creates an instance with the specified arguments.
Sho SHIMIZU4fea4fd2015-05-07 11:50:23 -070060 * It it recommended to use {@link Lambda#ochSignal(GridType, ChannelSpacing, int, int)}
61 * unless you want to use the concrete type, OchSignal, directly.
Sho SHIMIZU91210a72015-04-29 12:54:28 -070062 *
63 * @param gridType grid type
64 * @param channelSpacing channel spacing
65 * @param spacingMultiplier channel spacing multiplier
66 * @param slotGranularity slot width granularity
67 */
Sho SHIMIZU4fea4fd2015-05-07 11:50:23 -070068 public OchSignal(GridType gridType, ChannelSpacing channelSpacing,
Sho SHIMIZU91210a72015-04-29 12:54:28 -070069 int spacingMultiplier, int slotGranularity) {
70 this.gridType = checkNotNull(gridType);
71 this.channelSpacing = checkNotNull(channelSpacing);
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070072 // Negative values are permitted for spacingMultiplier
Sho SHIMIZU91210a72015-04-29 12:54:28 -070073 this.spacingMultiplier = spacingMultiplier;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070074 checkArgument(slotGranularity > 0, "slotGranularity must be larger than 0, received %s", slotGranularity);
Sho SHIMIZU91210a72015-04-29 12:54:28 -070075 this.slotGranularity = slotGranularity;
76 }
77
78 /**
HIGUCHI Yutaca8cb4e2015-12-11 13:57:05 -080079 * Creates an instance of {@link OchSignal} representing a flex grid frequency slot.
80 * @param index slot index (relative to "the center frequency" 193.1THz)
81 * @return FlexGrid {@link OchSignal}
82 */
83 public static OchSignal newFlexGridSlot(int index) {
84 return new OchSignal(FLEX, CHL_6P25GHZ, index, 1);
85 }
86
87 /**
88 * Creates an instance of {@link OchSignal} representing a fixed DWDM frequency slot.
89 * @param spacing channel spacing
90 * @param index slot index (relative to "the center frequency" 193.1THz)
91 * @return DWDM {@link OchSignal}
92 */
93 public static OchSignal newDwdmSlot(ChannelSpacing spacing, int index) {
94 return new OchSignal(DWDM, spacing, index,
95 (int) (spacing.frequency().asHz() / CHL_12P5GHZ.frequency().asHz()));
96 }
97
98 /**
Marc De Leenheer0b8b2ef2015-08-03 15:39:00 -070099 * Create OCh signal from channel number.
Marc De Leenheerc9733082015-06-04 12:22:38 -0700100 *
Marc De Leenheer0b8b2ef2015-08-03 15:39:00 -0700101 * @param channel channel number
Marc De Leenheerc9733082015-06-04 12:22:38 -0700102 * @param maxFrequency maximum frequency
103 * @param grid grid spacing frequency
HIGUCHI Yuta603e8d92015-12-11 09:57:30 -0800104 *
105 * @deprecated in Emu (ONOS 1.4).
Marc De Leenheerc9733082015-06-04 12:22:38 -0700106 */
HIGUCHI Yuta603e8d92015-12-11 09:57:30 -0800107 @Deprecated
Marc De Leenheer0b8b2ef2015-08-03 15:39:00 -0700108 public OchSignal(int channel, Frequency maxFrequency, Frequency grid) {
Marc De Leenheerc9733082015-06-04 12:22:38 -0700109 // Calculate center frequency
110 Frequency centerFrequency = maxFrequency.subtract(grid.multiply(channel - 1));
111
112 this.gridType = DEFAULT_OCH_GRIDTYPE;
113 this.channelSpacing = DEFAULT_CHANNEL_SPACING;
Marc De Leenheerb0fb41d2015-12-03 22:16:53 -0800114 this.spacingMultiplier = (int) (centerFrequency.subtract(Spectrum.CENTER_FREQUENCY).asHz() / grid.asHz());
Marc De Leenheerc9733082015-06-04 12:22:38 -0700115 this.slotGranularity = (int) Math.round((double) grid.asHz() / ChannelSpacing.CHL_12P5GHZ.frequency().asHz());
116 }
117
HIGUCHI Yuta603e8d92015-12-11 09:57:30 -0800118 @Deprecated
Marc De Leenheerc9733082015-06-04 12:22:38 -0700119 public OchSignal(Frequency centerFrequency, ChannelSpacing channelSpacing, int slotGranularity) {
120 this.gridType = DEFAULT_OCH_GRIDTYPE;
121 this.channelSpacing = channelSpacing;
122 this.spacingMultiplier = (int) Math.round((double) centerFrequency.
Marc De Leenheerb0fb41d2015-12-03 22:16:53 -0800123 subtract(Spectrum.CENTER_FREQUENCY).asHz() / channelSpacing().frequency().asHz());
Marc De Leenheerc9733082015-06-04 12:22:38 -0700124 this.slotGranularity = slotGranularity;
125 }
126
127 /**
Sho SHIMIZU91210a72015-04-29 12:54:28 -0700128 * Returns grid type.
129 *
130 * @return grid type
131 */
132 public GridType gridType() {
133 return gridType;
134 }
135
136 /**
137 * Returns channel spacing.
138 *
139 * @return channel spacing
140 */
141 public ChannelSpacing channelSpacing() {
142 return channelSpacing;
143 }
144
145 /**
146 * Returns spacing multiplier.
147 *
148 * @return spacing multiplier
149 */
150 public int spacingMultiplier() {
151 return spacingMultiplier;
152 }
153
154 /**
Marc De Leenheer2c305302015-12-07 21:37:44 -0800155 * Returns slot width granularity.
Sho SHIMIZU91210a72015-04-29 12:54:28 -0700156 *
Marc De Leenheer2c305302015-12-07 21:37:44 -0800157 * @return slot width granularity
Sho SHIMIZU91210a72015-04-29 12:54:28 -0700158 */
159 public int slotGranularity() {
160 return slotGranularity;
161 }
162
163 /**
164 * Returns central frequency in MHz.
165 *
166 * @return frequency in MHz
167 */
168 public Frequency centralFrequency() {
Marc De Leenheerb0fb41d2015-12-03 22:16:53 -0800169 return Spectrum.CENTER_FREQUENCY.add(channelSpacing().frequency().multiply(spacingMultiplier));
Sho SHIMIZU91210a72015-04-29 12:54:28 -0700170 }
171
172 /**
173 * Returns slot width.
174 *
175 * @return slot width
176 */
177 public Frequency slotWidth() {
Marc De Leenheer2c305302015-12-07 21:37:44 -0800178 return ChannelSpacing.CHL_12P5GHZ.frequency().multiply(slotGranularity);
179 }
180
181 /**
182 * Convert fixed grid OCh signal to sorted set of flex grid slots with 6.25 GHz spacing and 12.5 GHz slot width.
183 *
184 * @param ochSignal fixed grid lambda
185 * @return sorted set of flex grid OCh lambdas
186 */
187 public static SortedSet<OchSignal> toFlexGrid(OchSignal ochSignal) {
188 checkArgument(ochSignal.gridType() != GridType.FLEX);
189 checkArgument(ochSignal.channelSpacing() != ChannelSpacing.CHL_6P25GHZ);
190 checkArgument(FIXED_GRID_SLOT_GRANULARITIES.contains(ochSignal.slotGranularity()));
191
192 int startMultiplier = (int) (1 - ochSignal.slotGranularity() +
193 ochSignal.spacingMultiplier() * ochSignal.channelSpacing().frequency().asHz() /
194 ChannelSpacing.CHL_6P25GHZ.frequency().asHz());
195
Marc De Leenheer2c305302015-12-07 21:37:44 -0800196 return IntStream.range(0, ochSignal.slotGranularity())
197 .mapToObj(i -> new OchSignal(GridType.FLEX, ChannelSpacing.CHL_6P25GHZ, startMultiplier + 2 * i, 1))
HIGUCHI Yutaca8cb4e2015-12-11 13:57:05 -0800198 .collect(Collectors.toCollection(DefaultOchSignalComparator::newOchSignalTreeSet));
Marc De Leenheer2c305302015-12-07 21:37:44 -0800199 }
200
201 /**
202 * Convert list of lambdas with flex grid 6.25 GHz spacing and 12.5 GHz width into fixed grid OCh signal.
203 *
204 * @param lambdas list of flex grid lambdas in sorted order
205 * @param spacing desired fixed grid spacing
206 * @return fixed grid lambda
207 */
208 public static OchSignal toFixedGrid(List<OchSignal> lambdas, ChannelSpacing spacing) {
209 // Number of slots of 12.5 GHz that fit into requested spacing
210 int ratio = (int) (spacing.frequency().asHz() / ChannelSpacing.CHL_12P5GHZ.frequency().asHz());
211 checkArgument(lambdas.size() == ratio);
212 lambdas.forEach(x -> checkArgument(x.gridType() == GridType.FLEX));
213 lambdas.forEach(x -> checkArgument(x.channelSpacing() == ChannelSpacing.CHL_6P25GHZ));
214 lambdas.forEach(x -> checkArgument(x.slotGranularity() == 1));
215 // Consecutive lambdas (multiplier increments by 2 because spacing is 6.25 GHz but slot width is 12.5 GHz)
216 IntStream.range(1, lambdas.size())
217 .forEach(i -> checkArgument(
218 lambdas.get(i).spacingMultiplier() == lambdas.get(i - 1).spacingMultiplier() + 2));
219 // Is center frequency compatible with requested spacing
220 Frequency center = lambdas.get(ratio / 2).centralFrequency().subtract(ChannelSpacing.CHL_6P25GHZ.frequency());
221 checkArgument(Spectrum.CENTER_FREQUENCY.subtract(center).asHz() % spacing.frequency().asHz() == 0);
222
223 // Multiplier sits in middle of given lambdas, then convert from 6.25 to requested spacing
HIGUCHI Yutaca8cb4e2015-12-11 13:57:05 -0800224 int spacingMultiplier = lambdas.stream()
225 .mapToInt(OchSignal::spacingMultiplier)
226 .sum() / lambdas.size()
227 / (int) (spacing.frequency().asHz() / CHL_6P25GHZ.frequency().asHz());
Marc De Leenheer2c305302015-12-07 21:37:44 -0800228
229 return new OchSignal(GridType.DWDM, spacing, spacingMultiplier, lambdas.size());
Sho SHIMIZU91210a72015-04-29 12:54:28 -0700230 }
231
232 @Override
233 public int hashCode() {
234 return Objects.hash(gridType, channelSpacing, spacingMultiplier, slotGranularity);
235 }
236
237 @Override
238 public boolean equals(Object obj) {
239 if (this == obj) {
240 return true;
241 }
242 if (!(obj instanceof OchSignal)) {
243 return false;
244 }
245 final OchSignal other = (OchSignal) obj;
246 return Objects.equals(this.gridType, other.gridType)
247 && Objects.equals(this.channelSpacing, other.channelSpacing)
248 && Objects.equals(this.spacingMultiplier, other.spacingMultiplier)
249 && Objects.equals(this.slotGranularity, other.slotGranularity);
250 }
251
252 @Override
253 public String toString() {
254 return MoreObjects.toStringHelper(this)
255 .add("gridType", gridType)
256 .add("channelSpacing", channelSpacing)
257 .add("spacingMultiplier", spacingMultiplier)
258 .add("slotGranularity", slotGranularity)
259 .toString();
260 }
261}