blob: cf2fc9929bd6820437a13f336bd7f4296df3d2c4 [file] [log] [blame]
Sho SHIMIZUc15ce512015-05-26 16:54:08 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Sho SHIMIZUc15ce512015-05-26 16:54:08 -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 */
Thomas Vachuska95caba32016-04-04 10:42:05 -070016package org.onosproject.provider.of.flow.util;
Sho SHIMIZUc15ce512015-05-26 16:54:08 -070017
Sho SHIMIZU43f65ed2015-05-28 11:59:06 -070018import com.google.common.collect.BiMap;
19import com.google.common.collect.EnumHashBiMap;
Sho SHIMIZUc15ce512015-05-26 16:54:08 -070020import org.onosproject.net.ChannelSpacing;
21import org.onosproject.net.GridType;
Marc De Leenheerd24420f2015-05-27 09:40:59 -070022import org.onosproject.net.OchSignalType;
Yafit Hadar73514612015-11-04 10:14:21 +020023import org.onosproject.net.OduSignalType;
Sho SHIMIZUc15ce512015-05-26 16:54:08 -070024
25/**
26 * Collection of helper methods to convert protocol agnostic models to values used in OpenFlow spec.
27 */
Thomas Vachuska95caba32016-04-04 10:42:05 -070028public final class OpenFlowValueMapper {
Sho SHIMIZUc15ce512015-05-26 16:54:08 -070029
Sho SHIMIZUc15ce512015-05-26 16:54:08 -070030 // prohibit instantiation
Sho SHIMIZUc17042d2015-05-28 12:07:23 -070031 private OpenFlowValueMapper() {}
Sho SHIMIZUc15ce512015-05-26 16:54:08 -070032
Sho SHIMIZU43f65ed2015-05-28 11:59:06 -070033 private static final BiMap<GridType, Byte> GRID_TYPES = EnumHashBiMap.create(GridType.class);
34 static {
35 // See ONF "Optical Transport Protocol Extensions Version 1.0" for the following values
36 GRID_TYPES.put(GridType.DWDM, (byte) 1); // OFPGRIDT_DWDM of enum ofp_grid_type
37 GRID_TYPES.put(GridType.CWDM, (byte) 2); // OFPGRIDT_CWDM of enum ofp_grid_type
38 GRID_TYPES.put(GridType.FLEX, (byte) 3); // OFPGRIDT_FLEX of enum ofp_grid_type
39 }
40
41 private static final BiMap<ChannelSpacing, Byte> CHANNEL_SPACING = EnumHashBiMap.create(ChannelSpacing.class);
42 static {
43 // See ONF "Optical Transport Protocol Extensions Version 1.0" for the following values
44 CHANNEL_SPACING.put(ChannelSpacing.CHL_100GHZ, (byte) 1); // OFPCS_100GHZ of enum ofp_chl_spacing
45 CHANNEL_SPACING.put(ChannelSpacing.CHL_50GHZ, (byte) 2); // OFPCS_50GHZ of enum ofp_chl_spacing
46 CHANNEL_SPACING.put(ChannelSpacing.CHL_25GHZ, (byte) 3); // OFPCS_25GHZ of enum ofp_chl_spacing
47 CHANNEL_SPACING.put(ChannelSpacing.CHL_12P5GHZ, (byte) 4); // OFPCS_12P5GHZ of enum ofp_chl_spacing
48 CHANNEL_SPACING.put(ChannelSpacing.CHL_6P25GHZ, (byte) 5); // OFPCS_6P25GHZ of enum ofp_chl_spacing
49 }
50
51 private static final BiMap<OchSignalType, Byte> OCH_SIGNAL_TYPES = EnumHashBiMap.create(OchSignalType.class);
52 static {
53 // See ONF "Optical Transport Protocol Extensions Version 1.0" for the following values
54 OCH_SIGNAL_TYPES.put(OchSignalType.FIXED_GRID, (byte) 1); // OFPOCHT_FIX_GRID of enum ofp_och_signal_type
55 OCH_SIGNAL_TYPES.put(OchSignalType.FLEX_GRID, (byte) 2); // OFPOCHT_FLEX_GRID of enum ofp_och_signal_type
56 }
57
Yafit Hadar73514612015-11-04 10:14:21 +020058 private static final BiMap<OduSignalType, Byte> ODU_SIGNAL_TYPES = EnumHashBiMap.create(OduSignalType.class);
59 static {
60 // See ONF "Optical Transport Protocol Extensions Version 1.0" for the following values
61 ODU_SIGNAL_TYPES.put(OduSignalType.ODU1, (byte) 1); // OFPODUT_ODU1 of enum ofp_odu_signal_type
62 ODU_SIGNAL_TYPES.put(OduSignalType.ODU2, (byte) 2); // OFPODUT_ODU2 of enum ofp_odu_signal_type
63 ODU_SIGNAL_TYPES.put(OduSignalType.ODU3, (byte) 3); // OFPODUT_ODU3 of enum ofp_odu_signal_type
64 ODU_SIGNAL_TYPES.put(OduSignalType.ODU4, (byte) 4); // OFPODUT_ODU4 of enum ofp_odu_signal_type
65 ODU_SIGNAL_TYPES.put(OduSignalType.ODU0, (byte) 10); // OFPODUT_ODU0 of enum ofp_odu_signal_type
66 ODU_SIGNAL_TYPES.put(OduSignalType.ODU2e, (byte) 11); // OFPODUT_ODU2E of enum ofp_odu_signal_type
67 }
68
Sho SHIMIZU43f65ed2015-05-28 11:59:06 -070069 /**
Sho SHIMIZUc17042d2015-05-28 12:07:23 -070070 * Looks up the specified input value to the corresponding value with the specified map.
Sho SHIMIZU43f65ed2015-05-28 11:59:06 -070071 *
72 * @param map bidirectional mapping
73 * @param input input value
74 * @param cls class of output value
75 * @param <I> type of input value
76 * @param <O> type of output value
77 * @return the corresponding value stored in the specified map
Sho SHIMIZUc17042d2015-05-28 12:07:23 -070078 * @throws NoMappingFoundException if no corresponding value is found
Sho SHIMIZU43f65ed2015-05-28 11:59:06 -070079 */
Sho SHIMIZUc17042d2015-05-28 12:07:23 -070080 private static <I, O> O lookup(BiMap<I, O> map, I input, Class<O> cls) {
Sho SHIMIZU43f65ed2015-05-28 11:59:06 -070081 if (!map.containsKey(input)) {
Sho SHIMIZUc17042d2015-05-28 12:07:23 -070082 throw new NoMappingFoundException(input, cls);
Sho SHIMIZU43f65ed2015-05-28 11:59:06 -070083 }
84
85 return map.get(input);
86 }
87
Sho SHIMIZUc15ce512015-05-26 16:54:08 -070088 /**
Sho SHIMIZUc17042d2015-05-28 12:07:23 -070089 * Looks up the corresponding byte value defined in
90 * ONF "Optical Transport Protocol Extensions Version 1.0"
91 * from the specified {@link GridType} instance.
Sho SHIMIZUc15ce512015-05-26 16:54:08 -070092 *
93 * @param type grid type
94 * @return the byte value corresponding to the specified grid type
Sho SHIMIZUc17042d2015-05-28 12:07:23 -070095 * @throws NoMappingFoundException if the specified grid type is not found
Sho SHIMIZUc15ce512015-05-26 16:54:08 -070096 */
Thomas Vachuska95caba32016-04-04 10:42:05 -070097 public static byte lookupGridType(GridType type) {
Sho SHIMIZUc17042d2015-05-28 12:07:23 -070098 return lookup(GRID_TYPES, type, Byte.class);
Sho SHIMIZUc15ce512015-05-26 16:54:08 -070099 }
100
101 /**
Sho SHIMIZUc17042d2015-05-28 12:07:23 -0700102 * Looks up the corresponding {@link GridType} instance
103 * from the specified byte value for grid type
104 * defined in ONF "Optical Transport Protocol Extensions Version 1.0".
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -0700105 *
106 * @param type byte value as grid type defined the spec
107 * @return the corresponding GridType instance
108 */
Thomas Vachuska95caba32016-04-04 10:42:05 -0700109 public static GridType lookupGridType(byte type) {
Sho SHIMIZUc17042d2015-05-28 12:07:23 -0700110 return lookup(GRID_TYPES.inverse(), type, GridType.class);
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -0700111 }
112
113 /**
Sho SHIMIZUc17042d2015-05-28 12:07:23 -0700114 * Looks up the corresponding byte value for channel spacing defined in
115 * ONF "Optical Transport Protocol Extensions Version 1.0"
116 * from the specified {@link ChannelSpacing} instance.
Sho SHIMIZUc15ce512015-05-26 16:54:08 -0700117 *
118 * @param spacing channel spacing
119 * @return byte value corresponding to the specified channel spacing
Sho SHIMIZUc17042d2015-05-28 12:07:23 -0700120 * @throws NoMappingFoundException if the specified channel spacing is not found
Sho SHIMIZUc15ce512015-05-26 16:54:08 -0700121 */
Thomas Vachuska95caba32016-04-04 10:42:05 -0700122 public static byte lookupChannelSpacing(ChannelSpacing spacing) {
Sho SHIMIZUc17042d2015-05-28 12:07:23 -0700123 return lookup(CHANNEL_SPACING, spacing, Byte.class);
Sho SHIMIZUc15ce512015-05-26 16:54:08 -0700124 }
Marc De Leenheerd24420f2015-05-27 09:40:59 -0700125
126 /**
Sho SHIMIZUc17042d2015-05-28 12:07:23 -0700127 * Looks up the corresponding {@link ChannelSpacing} instance
128 * from the specified byte value for channel spacing
129 * defined in ONF "Optical Transport Protocol Extensions Version 1.0".
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -0700130 *
131 * @param spacing byte value as channel spacing defined the spec
132 * @return the corresponding ChannelSpacing instance
Sho SHIMIZUc17042d2015-05-28 12:07:23 -0700133 * @throws NoMappingFoundException if the specified channel spacing is not found
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -0700134 */
Thomas Vachuska95caba32016-04-04 10:42:05 -0700135 public static ChannelSpacing lookupChannelSpacing(byte spacing) {
Sho SHIMIZUc17042d2015-05-28 12:07:23 -0700136 return lookup(CHANNEL_SPACING.inverse(), spacing, ChannelSpacing.class);
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -0700137 }
138
139 /**
Sho SHIMIZUc17042d2015-05-28 12:07:23 -0700140 * Looks up the corresponding byte value for Och signal type defined in
141 * ONF "Optical Transport Protocol Extensions Version 1.0"
142 * from the specified {@link OchSignalType} instance.
Marc De Leenheerd24420f2015-05-27 09:40:59 -0700143 *
144 * @param signalType optical signal type
145 * @return byte value corresponding to the specified OCh signal type
Sho SHIMIZUc17042d2015-05-28 12:07:23 -0700146 * @throws NoMappingFoundException if the specified Och signal type is not found
Marc De Leenheerd24420f2015-05-27 09:40:59 -0700147 */
Thomas Vachuska95caba32016-04-04 10:42:05 -0700148 public static byte lookupOchSignalType(OchSignalType signalType) {
Sho SHIMIZUc17042d2015-05-28 12:07:23 -0700149 return lookup(OCH_SIGNAL_TYPES, signalType, Byte.class);
Marc De Leenheerd24420f2015-05-27 09:40:59 -0700150 }
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -0700151
152 /**
Sho SHIMIZUc17042d2015-05-28 12:07:23 -0700153 * Looks up the the corresponding {@link OchSignalType} instance
154 * from the specified byte value for Och signal type defined in
155 * ONF "Optical Transport Protocol Extensions Version 1.0".
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -0700156 *
157 * @param signalType byte value as Och singal type defined the spec
158 * @return the corresponding OchSignalType instance
Sho SHIMIZUc17042d2015-05-28 12:07:23 -0700159 * @throws NoMappingFoundException if the specified Och signal type is not found
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -0700160 */
Thomas Vachuska95caba32016-04-04 10:42:05 -0700161 public static OchSignalType lookupOchSignalType(byte signalType) {
Sho SHIMIZUc17042d2015-05-28 12:07:23 -0700162 return lookup(OCH_SIGNAL_TYPES.inverse(), signalType, OchSignalType.class);
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -0700163 }
Yafit Hadar73514612015-11-04 10:14:21 +0200164
165 /**
166 * Looks up the corresponding byte value for ODU signal type defined in
167 * ONF "Optical Transport Protocol Extensions Version 1.0"
168 * from the specified {@link OchSignalType} instance.
169 *
170 * @param signalType ODU (Optical channel Data Unit) signal type
171 * @return byte value corresponding to the specified ODU signal type
172 * @throws NoMappingFoundException if the specified ODU signal type is not found
173 */
Thomas Vachuska95caba32016-04-04 10:42:05 -0700174 public static byte lookupOduSignalType(OduSignalType signalType) {
Yafit Hadar73514612015-11-04 10:14:21 +0200175 return lookup(ODU_SIGNAL_TYPES, signalType, Byte.class);
176 }
177
178 /**
179 * Looks up the the corresponding {@link OchSignalType} instance
180 * from the specified byte value for ODU signal type defined in
181 * ONF "Optical Transport Protocol Extensions Version 1.0".
182 *
183 * @param signalType byte value as ODU (Optical channel Data Unit) signal type defined the spec
184 * @return the corresponding OchSignalType instance
185 * @throws NoMappingFoundException if the specified ODU signal type is not found
186 */
Thomas Vachuska95caba32016-04-04 10:42:05 -0700187 public static OduSignalType lookupOduSignalType(byte signalType) {
Yafit Hadar73514612015-11-04 10:14:21 +0200188 return lookup(ODU_SIGNAL_TYPES.inverse(), signalType, OduSignalType.class);
189 }
Sho SHIMIZUc15ce512015-05-26 16:54:08 -0700190}