blob: 2f0831c64933c360ce769f19dadd0fbdec600ac6 [file] [log] [blame]
Sho SHIMIZUc15ce512015-05-26 16:54:08 -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.provider.of.flow.impl;
17
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;
Sho SHIMIZUc15ce512015-05-26 16:54:08 -070023
24/**
25 * Collection of helper methods to convert protocol agnostic models to values used in OpenFlow spec.
26 */
Sho SHIMIZUc17042d2015-05-28 12:07:23 -070027final class OpenFlowValueMapper {
Sho SHIMIZUc15ce512015-05-26 16:54:08 -070028
Sho SHIMIZUc15ce512015-05-26 16:54:08 -070029 // prohibit instantiation
Sho SHIMIZUc17042d2015-05-28 12:07:23 -070030 private OpenFlowValueMapper() {}
Sho SHIMIZUc15ce512015-05-26 16:54:08 -070031
Sho SHIMIZU43f65ed2015-05-28 11:59:06 -070032 private static final BiMap<GridType, Byte> GRID_TYPES = EnumHashBiMap.create(GridType.class);
33 static {
34 // See ONF "Optical Transport Protocol Extensions Version 1.0" for the following values
35 GRID_TYPES.put(GridType.DWDM, (byte) 1); // OFPGRIDT_DWDM of enum ofp_grid_type
36 GRID_TYPES.put(GridType.CWDM, (byte) 2); // OFPGRIDT_CWDM of enum ofp_grid_type
37 GRID_TYPES.put(GridType.FLEX, (byte) 3); // OFPGRIDT_FLEX of enum ofp_grid_type
38 }
39
40 private static final BiMap<ChannelSpacing, Byte> CHANNEL_SPACING = EnumHashBiMap.create(ChannelSpacing.class);
41 static {
42 // See ONF "Optical Transport Protocol Extensions Version 1.0" for the following values
43 CHANNEL_SPACING.put(ChannelSpacing.CHL_100GHZ, (byte) 1); // OFPCS_100GHZ of enum ofp_chl_spacing
44 CHANNEL_SPACING.put(ChannelSpacing.CHL_50GHZ, (byte) 2); // OFPCS_50GHZ of enum ofp_chl_spacing
45 CHANNEL_SPACING.put(ChannelSpacing.CHL_25GHZ, (byte) 3); // OFPCS_25GHZ of enum ofp_chl_spacing
46 CHANNEL_SPACING.put(ChannelSpacing.CHL_12P5GHZ, (byte) 4); // OFPCS_12P5GHZ of enum ofp_chl_spacing
47 CHANNEL_SPACING.put(ChannelSpacing.CHL_6P25GHZ, (byte) 5); // OFPCS_6P25GHZ of enum ofp_chl_spacing
48 }
49
50 private static final BiMap<OchSignalType, Byte> OCH_SIGNAL_TYPES = EnumHashBiMap.create(OchSignalType.class);
51 static {
52 // See ONF "Optical Transport Protocol Extensions Version 1.0" for the following values
53 OCH_SIGNAL_TYPES.put(OchSignalType.FIXED_GRID, (byte) 1); // OFPOCHT_FIX_GRID of enum ofp_och_signal_type
54 OCH_SIGNAL_TYPES.put(OchSignalType.FLEX_GRID, (byte) 2); // OFPOCHT_FLEX_GRID of enum ofp_och_signal_type
55 }
56
57 /**
Sho SHIMIZUc17042d2015-05-28 12:07:23 -070058 * Looks up the specified input value to the corresponding value with the specified map.
Sho SHIMIZU43f65ed2015-05-28 11:59:06 -070059 *
60 * @param map bidirectional mapping
61 * @param input input value
62 * @param cls class of output value
63 * @param <I> type of input value
64 * @param <O> type of output value
65 * @return the corresponding value stored in the specified map
Sho SHIMIZUc17042d2015-05-28 12:07:23 -070066 * @throws NoMappingFoundException if no corresponding value is found
Sho SHIMIZU43f65ed2015-05-28 11:59:06 -070067 */
Sho SHIMIZUc17042d2015-05-28 12:07:23 -070068 private static <I, O> O lookup(BiMap<I, O> map, I input, Class<O> cls) {
Sho SHIMIZU43f65ed2015-05-28 11:59:06 -070069 if (!map.containsKey(input)) {
Sho SHIMIZUc17042d2015-05-28 12:07:23 -070070 throw new NoMappingFoundException(input, cls);
Sho SHIMIZU43f65ed2015-05-28 11:59:06 -070071 }
72
73 return map.get(input);
74 }
75
Sho SHIMIZUc15ce512015-05-26 16:54:08 -070076 /**
Sho SHIMIZUc17042d2015-05-28 12:07:23 -070077 * Looks up the corresponding byte value defined in
78 * ONF "Optical Transport Protocol Extensions Version 1.0"
79 * from the specified {@link GridType} instance.
Sho SHIMIZUc15ce512015-05-26 16:54:08 -070080 *
81 * @param type grid type
82 * @return the byte value corresponding to the specified grid type
Sho SHIMIZUc17042d2015-05-28 12:07:23 -070083 * @throws NoMappingFoundException if the specified grid type is not found
Sho SHIMIZUc15ce512015-05-26 16:54:08 -070084 */
Sho SHIMIZUc17042d2015-05-28 12:07:23 -070085 static byte lookupGridType(GridType type) {
86 return lookup(GRID_TYPES, type, Byte.class);
Sho SHIMIZUc15ce512015-05-26 16:54:08 -070087 }
88
89 /**
Sho SHIMIZUc17042d2015-05-28 12:07:23 -070090 * Looks up the corresponding {@link GridType} instance
91 * from the specified byte value for grid type
92 * defined in ONF "Optical Transport Protocol Extensions Version 1.0".
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -070093 *
94 * @param type byte value as grid type defined the spec
95 * @return the corresponding GridType instance
96 */
Sho SHIMIZUc17042d2015-05-28 12:07:23 -070097 static GridType lookupGridType(byte type) {
98 return lookup(GRID_TYPES.inverse(), type, GridType.class);
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -070099 }
100
101 /**
Sho SHIMIZUc17042d2015-05-28 12:07:23 -0700102 * Looks up the corresponding byte value for channel spacing defined in
103 * ONF "Optical Transport Protocol Extensions Version 1.0"
104 * from the specified {@link ChannelSpacing} instance.
Sho SHIMIZUc15ce512015-05-26 16:54:08 -0700105 *
106 * @param spacing channel spacing
107 * @return byte value corresponding to the specified channel spacing
Sho SHIMIZUc17042d2015-05-28 12:07:23 -0700108 * @throws NoMappingFoundException if the specified channel spacing is not found
Sho SHIMIZUc15ce512015-05-26 16:54:08 -0700109 */
Sho SHIMIZUc17042d2015-05-28 12:07:23 -0700110 static byte lookupChannelSpacing(ChannelSpacing spacing) {
111 return lookup(CHANNEL_SPACING, spacing, Byte.class);
Sho SHIMIZUc15ce512015-05-26 16:54:08 -0700112 }
Marc De Leenheerd24420f2015-05-27 09:40:59 -0700113
114 /**
Sho SHIMIZUc17042d2015-05-28 12:07:23 -0700115 * Looks up the corresponding {@link ChannelSpacing} instance
116 * from the specified byte value for channel spacing
117 * defined in ONF "Optical Transport Protocol Extensions Version 1.0".
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -0700118 *
119 * @param spacing byte value as channel spacing defined the spec
120 * @return the corresponding ChannelSpacing instance
Sho SHIMIZUc17042d2015-05-28 12:07:23 -0700121 * @throws NoMappingFoundException if the specified channel spacing is not found
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -0700122 */
Sho SHIMIZUc17042d2015-05-28 12:07:23 -0700123 static ChannelSpacing lookupChannelSpacing(byte spacing) {
124 return lookup(CHANNEL_SPACING.inverse(), spacing, ChannelSpacing.class);
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -0700125 }
126
127 /**
Sho SHIMIZUc17042d2015-05-28 12:07:23 -0700128 * Looks up the corresponding byte value for Och signal type defined in
129 * ONF "Optical Transport Protocol Extensions Version 1.0"
130 * from the specified {@link OchSignalType} instance.
Marc De Leenheerd24420f2015-05-27 09:40:59 -0700131 *
132 * @param signalType optical signal type
133 * @return byte value corresponding to the specified OCh signal type
Sho SHIMIZUc17042d2015-05-28 12:07:23 -0700134 * @throws NoMappingFoundException if the specified Och signal type is not found
Marc De Leenheerd24420f2015-05-27 09:40:59 -0700135 */
Sho SHIMIZUc17042d2015-05-28 12:07:23 -0700136 static byte lookupOchSignalType(OchSignalType signalType) {
137 return lookup(OCH_SIGNAL_TYPES, signalType, Byte.class);
Marc De Leenheerd24420f2015-05-27 09:40:59 -0700138 }
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -0700139
140 /**
Sho SHIMIZUc17042d2015-05-28 12:07:23 -0700141 * Looks up the the corresponding {@link OchSignalType} instance
142 * from the specified byte value for Och signal type defined in
143 * ONF "Optical Transport Protocol Extensions Version 1.0".
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -0700144 *
145 * @param signalType byte value as Och singal type defined the spec
146 * @return the corresponding OchSignalType instance
Sho SHIMIZUc17042d2015-05-28 12:07:23 -0700147 * @throws NoMappingFoundException if the specified Och signal type is not found
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -0700148 */
Sho SHIMIZUc17042d2015-05-28 12:07:23 -0700149 static OchSignalType lookupOchSignalType(byte signalType) {
150 return lookup(OCH_SIGNAL_TYPES.inverse(), signalType, OchSignalType.class);
Sho SHIMIZU4f828ee2015-05-28 09:38:21 -0700151 }
Sho SHIMIZUc15ce512015-05-26 16:54:08 -0700152}