blob: dfb36aeb5baaf6c60e1573391e09e6837b9ab3b5 [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;
27import java.util.TreeSet;
28import java.util.function.Supplier;
29import java.util.stream.Collectors;
30import java.util.stream.IntStream;
Sho SHIMIZU91210a72015-04-29 12:54:28 -070031
32import static com.google.common.base.Preconditions.checkArgument;
33import static com.google.common.base.Preconditions.checkNotNull;
34
35/**
36 * Implementation of Lambda representing OCh (Optical Channel) Signal.
37 *
38 * <p>
39 * See ITU G.709 "Interfaces for the Optical Transport Network (OTN)".
40 * </p>
41 */
Sho SHIMIZU91210a72015-04-29 12:54:28 -070042public class OchSignal implements Lambda {
43
Marc De Leenheer2c305302015-12-07 21:37:44 -080044 public static final Set<Integer> FIXED_GRID_SLOT_GRANULARITIES = ImmutableSet.of(1, 2, 4, 8);
Marc De Leenheerc9733082015-06-04 12:22:38 -070045 private static final GridType DEFAULT_OCH_GRIDTYPE = GridType.DWDM;
46 private static final ChannelSpacing DEFAULT_CHANNEL_SPACING = ChannelSpacing.CHL_50GHZ;
47
Sho SHIMIZU91210a72015-04-29 12:54:28 -070048 private final GridType gridType;
49 private final ChannelSpacing channelSpacing;
Marc De Leenheer2c305302015-12-07 21:37:44 -080050 // Nominal central frequency = 193.1 THz + spacingMultiplier * channelSpacing
Sho SHIMIZU91210a72015-04-29 12:54:28 -070051 private final int spacingMultiplier;
52 // Slot width = slotGranularity * 12.5 GHz
53 private final int slotGranularity;
54
55 /**
56 * Creates an instance with the specified arguments.
Sho SHIMIZU4fea4fd2015-05-07 11:50:23 -070057 * It it recommended to use {@link Lambda#ochSignal(GridType, ChannelSpacing, int, int)}
58 * unless you want to use the concrete type, OchSignal, directly.
Sho SHIMIZU91210a72015-04-29 12:54:28 -070059 *
60 * @param gridType grid type
61 * @param channelSpacing channel spacing
62 * @param spacingMultiplier channel spacing multiplier
63 * @param slotGranularity slot width granularity
64 */
Sho SHIMIZU4fea4fd2015-05-07 11:50:23 -070065 public OchSignal(GridType gridType, ChannelSpacing channelSpacing,
Sho SHIMIZU91210a72015-04-29 12:54:28 -070066 int spacingMultiplier, int slotGranularity) {
67 this.gridType = checkNotNull(gridType);
68 this.channelSpacing = checkNotNull(channelSpacing);
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070069 // Negative values are permitted for spacingMultiplier
Sho SHIMIZU91210a72015-04-29 12:54:28 -070070 this.spacingMultiplier = spacingMultiplier;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070071 checkArgument(slotGranularity > 0, "slotGranularity must be larger than 0, received %s", slotGranularity);
Sho SHIMIZU91210a72015-04-29 12:54:28 -070072 this.slotGranularity = slotGranularity;
73 }
74
75 /**
Marc De Leenheer0b8b2ef2015-08-03 15:39:00 -070076 * Create OCh signal from channel number.
Marc De Leenheerc9733082015-06-04 12:22:38 -070077 *
Marc De Leenheer0b8b2ef2015-08-03 15:39:00 -070078 * @param channel channel number
Marc De Leenheerc9733082015-06-04 12:22:38 -070079 * @param maxFrequency maximum frequency
80 * @param grid grid spacing frequency
HIGUCHI Yuta603e8d92015-12-11 09:57:30 -080081 *
82 * @deprecated in Emu (ONOS 1.4).
Marc De Leenheerc9733082015-06-04 12:22:38 -070083 */
HIGUCHI Yuta603e8d92015-12-11 09:57:30 -080084 @Deprecated
Marc De Leenheer0b8b2ef2015-08-03 15:39:00 -070085 public OchSignal(int channel, Frequency maxFrequency, Frequency grid) {
Marc De Leenheerc9733082015-06-04 12:22:38 -070086 // Calculate center frequency
87 Frequency centerFrequency = maxFrequency.subtract(grid.multiply(channel - 1));
88
89 this.gridType = DEFAULT_OCH_GRIDTYPE;
90 this.channelSpacing = DEFAULT_CHANNEL_SPACING;
Marc De Leenheerb0fb41d2015-12-03 22:16:53 -080091 this.spacingMultiplier = (int) (centerFrequency.subtract(Spectrum.CENTER_FREQUENCY).asHz() / grid.asHz());
Marc De Leenheerc9733082015-06-04 12:22:38 -070092 this.slotGranularity = (int) Math.round((double) grid.asHz() / ChannelSpacing.CHL_12P5GHZ.frequency().asHz());
93 }
94
HIGUCHI Yuta603e8d92015-12-11 09:57:30 -080095 @Deprecated
Marc De Leenheerc9733082015-06-04 12:22:38 -070096 public OchSignal(Frequency centerFrequency, ChannelSpacing channelSpacing, int slotGranularity) {
97 this.gridType = DEFAULT_OCH_GRIDTYPE;
98 this.channelSpacing = channelSpacing;
99 this.spacingMultiplier = (int) Math.round((double) centerFrequency.
Marc De Leenheerb0fb41d2015-12-03 22:16:53 -0800100 subtract(Spectrum.CENTER_FREQUENCY).asHz() / channelSpacing().frequency().asHz());
Marc De Leenheerc9733082015-06-04 12:22:38 -0700101 this.slotGranularity = slotGranularity;
102 }
103
104 /**
Sho SHIMIZU91210a72015-04-29 12:54:28 -0700105 * Returns grid type.
106 *
107 * @return grid type
108 */
109 public GridType gridType() {
110 return gridType;
111 }
112
113 /**
114 * Returns channel spacing.
115 *
116 * @return channel spacing
117 */
118 public ChannelSpacing channelSpacing() {
119 return channelSpacing;
120 }
121
122 /**
123 * Returns spacing multiplier.
124 *
125 * @return spacing multiplier
126 */
127 public int spacingMultiplier() {
128 return spacingMultiplier;
129 }
130
131 /**
Marc De Leenheer2c305302015-12-07 21:37:44 -0800132 * Returns slot width granularity.
Sho SHIMIZU91210a72015-04-29 12:54:28 -0700133 *
Marc De Leenheer2c305302015-12-07 21:37:44 -0800134 * @return slot width granularity
Sho SHIMIZU91210a72015-04-29 12:54:28 -0700135 */
136 public int slotGranularity() {
137 return slotGranularity;
138 }
139
140 /**
141 * Returns central frequency in MHz.
142 *
143 * @return frequency in MHz
144 */
145 public Frequency centralFrequency() {
Marc De Leenheerb0fb41d2015-12-03 22:16:53 -0800146 return Spectrum.CENTER_FREQUENCY.add(channelSpacing().frequency().multiply(spacingMultiplier));
Sho SHIMIZU91210a72015-04-29 12:54:28 -0700147 }
148
149 /**
150 * Returns slot width.
151 *
152 * @return slot width
153 */
154 public Frequency slotWidth() {
Marc De Leenheer2c305302015-12-07 21:37:44 -0800155 return ChannelSpacing.CHL_12P5GHZ.frequency().multiply(slotGranularity);
156 }
157
158 /**
159 * Convert fixed grid OCh signal to sorted set of flex grid slots with 6.25 GHz spacing and 12.5 GHz slot width.
160 *
161 * @param ochSignal fixed grid lambda
162 * @return sorted set of flex grid OCh lambdas
163 */
164 public static SortedSet<OchSignal> toFlexGrid(OchSignal ochSignal) {
165 checkArgument(ochSignal.gridType() != GridType.FLEX);
166 checkArgument(ochSignal.channelSpacing() != ChannelSpacing.CHL_6P25GHZ);
167 checkArgument(FIXED_GRID_SLOT_GRANULARITIES.contains(ochSignal.slotGranularity()));
168
169 int startMultiplier = (int) (1 - ochSignal.slotGranularity() +
170 ochSignal.spacingMultiplier() * ochSignal.channelSpacing().frequency().asHz() /
171 ChannelSpacing.CHL_6P25GHZ.frequency().asHz());
172
173 Supplier<SortedSet<OchSignal>> supplier = () -> new TreeSet<>(new DefaultOchSignalComparator());
174 return IntStream.range(0, ochSignal.slotGranularity())
175 .mapToObj(i -> new OchSignal(GridType.FLEX, ChannelSpacing.CHL_6P25GHZ, startMultiplier + 2 * i, 1))
176 .collect(Collectors.toCollection(supplier));
177 }
178
179 /**
180 * Convert list of lambdas with flex grid 6.25 GHz spacing and 12.5 GHz width into fixed grid OCh signal.
181 *
182 * @param lambdas list of flex grid lambdas in sorted order
183 * @param spacing desired fixed grid spacing
184 * @return fixed grid lambda
185 */
186 public static OchSignal toFixedGrid(List<OchSignal> lambdas, ChannelSpacing spacing) {
187 // Number of slots of 12.5 GHz that fit into requested spacing
188 int ratio = (int) (spacing.frequency().asHz() / ChannelSpacing.CHL_12P5GHZ.frequency().asHz());
189 checkArgument(lambdas.size() == ratio);
190 lambdas.forEach(x -> checkArgument(x.gridType() == GridType.FLEX));
191 lambdas.forEach(x -> checkArgument(x.channelSpacing() == ChannelSpacing.CHL_6P25GHZ));
192 lambdas.forEach(x -> checkArgument(x.slotGranularity() == 1));
193 // Consecutive lambdas (multiplier increments by 2 because spacing is 6.25 GHz but slot width is 12.5 GHz)
194 IntStream.range(1, lambdas.size())
195 .forEach(i -> checkArgument(
196 lambdas.get(i).spacingMultiplier() == lambdas.get(i - 1).spacingMultiplier() + 2));
197 // Is center frequency compatible with requested spacing
198 Frequency center = lambdas.get(ratio / 2).centralFrequency().subtract(ChannelSpacing.CHL_6P25GHZ.frequency());
199 checkArgument(Spectrum.CENTER_FREQUENCY.subtract(center).asHz() % spacing.frequency().asHz() == 0);
200
201 // Multiplier sits in middle of given lambdas, then convert from 6.25 to requested spacing
202 int spacingMultiplier = (lambdas.get(ratio / 2).spacingMultiplier() + 1) / (ratio * 2);
203
204 return new OchSignal(GridType.DWDM, spacing, spacingMultiplier, lambdas.size());
Sho SHIMIZU91210a72015-04-29 12:54:28 -0700205 }
206
207 @Override
208 public int hashCode() {
209 return Objects.hash(gridType, channelSpacing, spacingMultiplier, slotGranularity);
210 }
211
212 @Override
213 public boolean equals(Object obj) {
214 if (this == obj) {
215 return true;
216 }
217 if (!(obj instanceof OchSignal)) {
218 return false;
219 }
220 final OchSignal other = (OchSignal) obj;
221 return Objects.equals(this.gridType, other.gridType)
222 && Objects.equals(this.channelSpacing, other.channelSpacing)
223 && Objects.equals(this.spacingMultiplier, other.spacingMultiplier)
224 && Objects.equals(this.slotGranularity, other.slotGranularity);
225 }
226
227 @Override
228 public String toString() {
229 return MoreObjects.toStringHelper(this)
230 .add("gridType", gridType)
231 .add("channelSpacing", channelSpacing)
232 .add("spacingMultiplier", spacingMultiplier)
233 .add("slotGranularity", slotGranularity)
234 .toString();
235 }
236}