Sho SHIMIZU | c15ce51 | 2015-05-26 16:54:08 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.provider.of.flow.impl; |
| 17 | |
Sho SHIMIZU | 43f65ed | 2015-05-28 11:59:06 -0700 | [diff] [blame] | 18 | import com.google.common.collect.BiMap; |
| 19 | import com.google.common.collect.EnumHashBiMap; |
Sho SHIMIZU | c15ce51 | 2015-05-26 16:54:08 -0700 | [diff] [blame] | 20 | import org.onosproject.net.ChannelSpacing; |
| 21 | import org.onosproject.net.GridType; |
Marc De Leenheer | d24420f | 2015-05-27 09:40:59 -0700 | [diff] [blame] | 22 | import org.onosproject.net.OchSignalType; |
Sho SHIMIZU | c15ce51 | 2015-05-26 16:54:08 -0700 | [diff] [blame] | 23 | |
| 24 | /** |
| 25 | * Collection of helper methods to convert protocol agnostic models to values used in OpenFlow spec. |
| 26 | */ |
Sho SHIMIZU | c17042d | 2015-05-28 12:07:23 -0700 | [diff] [blame] | 27 | final class OpenFlowValueMapper { |
Sho SHIMIZU | c15ce51 | 2015-05-26 16:54:08 -0700 | [diff] [blame] | 28 | |
Sho SHIMIZU | c15ce51 | 2015-05-26 16:54:08 -0700 | [diff] [blame] | 29 | // prohibit instantiation |
Sho SHIMIZU | c17042d | 2015-05-28 12:07:23 -0700 | [diff] [blame] | 30 | private OpenFlowValueMapper() {} |
Sho SHIMIZU | c15ce51 | 2015-05-26 16:54:08 -0700 | [diff] [blame] | 31 | |
Sho SHIMIZU | 43f65ed | 2015-05-28 11:59:06 -0700 | [diff] [blame] | 32 | 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 SHIMIZU | c17042d | 2015-05-28 12:07:23 -0700 | [diff] [blame] | 58 | * Looks up the specified input value to the corresponding value with the specified map. |
Sho SHIMIZU | 43f65ed | 2015-05-28 11:59:06 -0700 | [diff] [blame] | 59 | * |
| 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 SHIMIZU | c17042d | 2015-05-28 12:07:23 -0700 | [diff] [blame] | 66 | * @throws NoMappingFoundException if no corresponding value is found |
Sho SHIMIZU | 43f65ed | 2015-05-28 11:59:06 -0700 | [diff] [blame] | 67 | */ |
Sho SHIMIZU | c17042d | 2015-05-28 12:07:23 -0700 | [diff] [blame] | 68 | private static <I, O> O lookup(BiMap<I, O> map, I input, Class<O> cls) { |
Sho SHIMIZU | 43f65ed | 2015-05-28 11:59:06 -0700 | [diff] [blame] | 69 | if (!map.containsKey(input)) { |
Sho SHIMIZU | c17042d | 2015-05-28 12:07:23 -0700 | [diff] [blame] | 70 | throw new NoMappingFoundException(input, cls); |
Sho SHIMIZU | 43f65ed | 2015-05-28 11:59:06 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | return map.get(input); |
| 74 | } |
| 75 | |
Sho SHIMIZU | c15ce51 | 2015-05-26 16:54:08 -0700 | [diff] [blame] | 76 | /** |
Sho SHIMIZU | c17042d | 2015-05-28 12:07:23 -0700 | [diff] [blame] | 77 | * 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 SHIMIZU | c15ce51 | 2015-05-26 16:54:08 -0700 | [diff] [blame] | 80 | * |
| 81 | * @param type grid type |
| 82 | * @return the byte value corresponding to the specified grid type |
Sho SHIMIZU | c17042d | 2015-05-28 12:07:23 -0700 | [diff] [blame] | 83 | * @throws NoMappingFoundException if the specified grid type is not found |
Sho SHIMIZU | c15ce51 | 2015-05-26 16:54:08 -0700 | [diff] [blame] | 84 | */ |
Sho SHIMIZU | c17042d | 2015-05-28 12:07:23 -0700 | [diff] [blame] | 85 | static byte lookupGridType(GridType type) { |
| 86 | return lookup(GRID_TYPES, type, Byte.class); |
Sho SHIMIZU | c15ce51 | 2015-05-26 16:54:08 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /** |
Sho SHIMIZU | c17042d | 2015-05-28 12:07:23 -0700 | [diff] [blame] | 90 | * 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 SHIMIZU | 4f828ee | 2015-05-28 09:38:21 -0700 | [diff] [blame] | 93 | * |
| 94 | * @param type byte value as grid type defined the spec |
| 95 | * @return the corresponding GridType instance |
| 96 | */ |
Sho SHIMIZU | c17042d | 2015-05-28 12:07:23 -0700 | [diff] [blame] | 97 | static GridType lookupGridType(byte type) { |
| 98 | return lookup(GRID_TYPES.inverse(), type, GridType.class); |
Sho SHIMIZU | 4f828ee | 2015-05-28 09:38:21 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | /** |
Sho SHIMIZU | c17042d | 2015-05-28 12:07:23 -0700 | [diff] [blame] | 102 | * 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 SHIMIZU | c15ce51 | 2015-05-26 16:54:08 -0700 | [diff] [blame] | 105 | * |
| 106 | * @param spacing channel spacing |
| 107 | * @return byte value corresponding to the specified channel spacing |
Sho SHIMIZU | c17042d | 2015-05-28 12:07:23 -0700 | [diff] [blame] | 108 | * @throws NoMappingFoundException if the specified channel spacing is not found |
Sho SHIMIZU | c15ce51 | 2015-05-26 16:54:08 -0700 | [diff] [blame] | 109 | */ |
Sho SHIMIZU | c17042d | 2015-05-28 12:07:23 -0700 | [diff] [blame] | 110 | static byte lookupChannelSpacing(ChannelSpacing spacing) { |
| 111 | return lookup(CHANNEL_SPACING, spacing, Byte.class); |
Sho SHIMIZU | c15ce51 | 2015-05-26 16:54:08 -0700 | [diff] [blame] | 112 | } |
Marc De Leenheer | d24420f | 2015-05-27 09:40:59 -0700 | [diff] [blame] | 113 | |
| 114 | /** |
Sho SHIMIZU | c17042d | 2015-05-28 12:07:23 -0700 | [diff] [blame] | 115 | * 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 SHIMIZU | 4f828ee | 2015-05-28 09:38:21 -0700 | [diff] [blame] | 118 | * |
| 119 | * @param spacing byte value as channel spacing defined the spec |
| 120 | * @return the corresponding ChannelSpacing instance |
Sho SHIMIZU | c17042d | 2015-05-28 12:07:23 -0700 | [diff] [blame] | 121 | * @throws NoMappingFoundException if the specified channel spacing is not found |
Sho SHIMIZU | 4f828ee | 2015-05-28 09:38:21 -0700 | [diff] [blame] | 122 | */ |
Sho SHIMIZU | c17042d | 2015-05-28 12:07:23 -0700 | [diff] [blame] | 123 | static ChannelSpacing lookupChannelSpacing(byte spacing) { |
| 124 | return lookup(CHANNEL_SPACING.inverse(), spacing, ChannelSpacing.class); |
Sho SHIMIZU | 4f828ee | 2015-05-28 09:38:21 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | /** |
Sho SHIMIZU | c17042d | 2015-05-28 12:07:23 -0700 | [diff] [blame] | 128 | * 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 Leenheer | d24420f | 2015-05-27 09:40:59 -0700 | [diff] [blame] | 131 | * |
| 132 | * @param signalType optical signal type |
| 133 | * @return byte value corresponding to the specified OCh signal type |
Sho SHIMIZU | c17042d | 2015-05-28 12:07:23 -0700 | [diff] [blame] | 134 | * @throws NoMappingFoundException if the specified Och signal type is not found |
Marc De Leenheer | d24420f | 2015-05-27 09:40:59 -0700 | [diff] [blame] | 135 | */ |
Sho SHIMIZU | c17042d | 2015-05-28 12:07:23 -0700 | [diff] [blame] | 136 | static byte lookupOchSignalType(OchSignalType signalType) { |
| 137 | return lookup(OCH_SIGNAL_TYPES, signalType, Byte.class); |
Marc De Leenheer | d24420f | 2015-05-27 09:40:59 -0700 | [diff] [blame] | 138 | } |
Sho SHIMIZU | 4f828ee | 2015-05-28 09:38:21 -0700 | [diff] [blame] | 139 | |
| 140 | /** |
Sho SHIMIZU | c17042d | 2015-05-28 12:07:23 -0700 | [diff] [blame] | 141 | * 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 SHIMIZU | 4f828ee | 2015-05-28 09:38:21 -0700 | [diff] [blame] | 144 | * |
| 145 | * @param signalType byte value as Och singal type defined the spec |
| 146 | * @return the corresponding OchSignalType instance |
Sho SHIMIZU | c17042d | 2015-05-28 12:07:23 -0700 | [diff] [blame] | 147 | * @throws NoMappingFoundException if the specified Och signal type is not found |
Sho SHIMIZU | 4f828ee | 2015-05-28 09:38:21 -0700 | [diff] [blame] | 148 | */ |
Sho SHIMIZU | c17042d | 2015-05-28 12:07:23 -0700 | [diff] [blame] | 149 | static OchSignalType lookupOchSignalType(byte signalType) { |
| 150 | return lookup(OCH_SIGNAL_TYPES.inverse(), signalType, OchSignalType.class); |
Sho SHIMIZU | 4f828ee | 2015-05-28 09:38:21 -0700 | [diff] [blame] | 151 | } |
Sho SHIMIZU | c15ce51 | 2015-05-26 16:54:08 -0700 | [diff] [blame] | 152 | } |