blob: d0dfdf3505e5b85910f874f832c1066b5e487520 [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
81 */
Marc De Leenheer0b8b2ef2015-08-03 15:39:00 -070082 public OchSignal(int channel, Frequency maxFrequency, Frequency grid) {
Marc De Leenheerc9733082015-06-04 12:22:38 -070083 // Calculate center frequency
84 Frequency centerFrequency = maxFrequency.subtract(grid.multiply(channel - 1));
85
86 this.gridType = DEFAULT_OCH_GRIDTYPE;
87 this.channelSpacing = DEFAULT_CHANNEL_SPACING;
Marc De Leenheerb0fb41d2015-12-03 22:16:53 -080088 this.spacingMultiplier = (int) (centerFrequency.subtract(Spectrum.CENTER_FREQUENCY).asHz() / grid.asHz());
Marc De Leenheerc9733082015-06-04 12:22:38 -070089 this.slotGranularity = (int) Math.round((double) grid.asHz() / ChannelSpacing.CHL_12P5GHZ.frequency().asHz());
90 }
91
92 public OchSignal(Frequency centerFrequency, ChannelSpacing channelSpacing, int slotGranularity) {
93 this.gridType = DEFAULT_OCH_GRIDTYPE;
94 this.channelSpacing = channelSpacing;
95 this.spacingMultiplier = (int) Math.round((double) centerFrequency.
Marc De Leenheerb0fb41d2015-12-03 22:16:53 -080096 subtract(Spectrum.CENTER_FREQUENCY).asHz() / channelSpacing().frequency().asHz());
Marc De Leenheerc9733082015-06-04 12:22:38 -070097 this.slotGranularity = slotGranularity;
98 }
99
100 /**
Sho SHIMIZU91210a72015-04-29 12:54:28 -0700101 * Returns grid type.
102 *
103 * @return grid type
104 */
105 public GridType gridType() {
106 return gridType;
107 }
108
109 /**
110 * Returns channel spacing.
111 *
112 * @return channel spacing
113 */
114 public ChannelSpacing channelSpacing() {
115 return channelSpacing;
116 }
117
118 /**
119 * Returns spacing multiplier.
120 *
121 * @return spacing multiplier
122 */
123 public int spacingMultiplier() {
124 return spacingMultiplier;
125 }
126
127 /**
Marc De Leenheer2c305302015-12-07 21:37:44 -0800128 * Returns slot width granularity.
Sho SHIMIZU91210a72015-04-29 12:54:28 -0700129 *
Marc De Leenheer2c305302015-12-07 21:37:44 -0800130 * @return slot width granularity
Sho SHIMIZU91210a72015-04-29 12:54:28 -0700131 */
132 public int slotGranularity() {
133 return slotGranularity;
134 }
135
136 /**
137 * Returns central frequency in MHz.
138 *
139 * @return frequency in MHz
140 */
141 public Frequency centralFrequency() {
Marc De Leenheerb0fb41d2015-12-03 22:16:53 -0800142 return Spectrum.CENTER_FREQUENCY.add(channelSpacing().frequency().multiply(spacingMultiplier));
Sho SHIMIZU91210a72015-04-29 12:54:28 -0700143 }
144
145 /**
146 * Returns slot width.
147 *
148 * @return slot width
149 */
150 public Frequency slotWidth() {
Marc De Leenheer2c305302015-12-07 21:37:44 -0800151 return ChannelSpacing.CHL_12P5GHZ.frequency().multiply(slotGranularity);
152 }
153
154 /**
155 * Convert fixed grid OCh signal to sorted set of flex grid slots with 6.25 GHz spacing and 12.5 GHz slot width.
156 *
157 * @param ochSignal fixed grid lambda
158 * @return sorted set of flex grid OCh lambdas
159 */
160 public static SortedSet<OchSignal> toFlexGrid(OchSignal ochSignal) {
161 checkArgument(ochSignal.gridType() != GridType.FLEX);
162 checkArgument(ochSignal.channelSpacing() != ChannelSpacing.CHL_6P25GHZ);
163 checkArgument(FIXED_GRID_SLOT_GRANULARITIES.contains(ochSignal.slotGranularity()));
164
165 int startMultiplier = (int) (1 - ochSignal.slotGranularity() +
166 ochSignal.spacingMultiplier() * ochSignal.channelSpacing().frequency().asHz() /
167 ChannelSpacing.CHL_6P25GHZ.frequency().asHz());
168
169 Supplier<SortedSet<OchSignal>> supplier = () -> new TreeSet<>(new DefaultOchSignalComparator());
170 return IntStream.range(0, ochSignal.slotGranularity())
171 .mapToObj(i -> new OchSignal(GridType.FLEX, ChannelSpacing.CHL_6P25GHZ, startMultiplier + 2 * i, 1))
172 .collect(Collectors.toCollection(supplier));
173 }
174
175 /**
176 * Convert list of lambdas with flex grid 6.25 GHz spacing and 12.5 GHz width into fixed grid OCh signal.
177 *
178 * @param lambdas list of flex grid lambdas in sorted order
179 * @param spacing desired fixed grid spacing
180 * @return fixed grid lambda
181 */
182 public static OchSignal toFixedGrid(List<OchSignal> lambdas, ChannelSpacing spacing) {
183 // Number of slots of 12.5 GHz that fit into requested spacing
184 int ratio = (int) (spacing.frequency().asHz() / ChannelSpacing.CHL_12P5GHZ.frequency().asHz());
185 checkArgument(lambdas.size() == ratio);
186 lambdas.forEach(x -> checkArgument(x.gridType() == GridType.FLEX));
187 lambdas.forEach(x -> checkArgument(x.channelSpacing() == ChannelSpacing.CHL_6P25GHZ));
188 lambdas.forEach(x -> checkArgument(x.slotGranularity() == 1));
189 // Consecutive lambdas (multiplier increments by 2 because spacing is 6.25 GHz but slot width is 12.5 GHz)
190 IntStream.range(1, lambdas.size())
191 .forEach(i -> checkArgument(
192 lambdas.get(i).spacingMultiplier() == lambdas.get(i - 1).spacingMultiplier() + 2));
193 // Is center frequency compatible with requested spacing
194 Frequency center = lambdas.get(ratio / 2).centralFrequency().subtract(ChannelSpacing.CHL_6P25GHZ.frequency());
195 checkArgument(Spectrum.CENTER_FREQUENCY.subtract(center).asHz() % spacing.frequency().asHz() == 0);
196
197 // Multiplier sits in middle of given lambdas, then convert from 6.25 to requested spacing
198 int spacingMultiplier = (lambdas.get(ratio / 2).spacingMultiplier() + 1) / (ratio * 2);
199
200 return new OchSignal(GridType.DWDM, spacing, spacingMultiplier, lambdas.size());
Sho SHIMIZU91210a72015-04-29 12:54:28 -0700201 }
202
203 @Override
204 public int hashCode() {
205 return Objects.hash(gridType, channelSpacing, spacingMultiplier, slotGranularity);
206 }
207
208 @Override
209 public boolean equals(Object obj) {
210 if (this == obj) {
211 return true;
212 }
213 if (!(obj instanceof OchSignal)) {
214 return false;
215 }
216 final OchSignal other = (OchSignal) obj;
217 return Objects.equals(this.gridType, other.gridType)
218 && Objects.equals(this.channelSpacing, other.channelSpacing)
219 && Objects.equals(this.spacingMultiplier, other.spacingMultiplier)
220 && Objects.equals(this.slotGranularity, other.slotGranularity);
221 }
222
223 @Override
224 public String toString() {
225 return MoreObjects.toStringHelper(this)
226 .add("gridType", gridType)
227 .add("channelSpacing", channelSpacing)
228 .add("spacingMultiplier", spacingMultiplier)
229 .add("slotGranularity", slotGranularity)
230 .toString();
231 }
232}