Committed by
Gerrit Code Review
Refactor: Use bidirectional map to make methods easily consistent
Change-Id: I016bb98366351f60e4a6cc36de224a3d3163ac2d
Showing
5 changed files
with
63 additions
and
130 deletions
... | @@ -383,7 +383,7 @@ public abstract class FlowModBuilder { | ... | @@ -383,7 +383,7 @@ public abstract class FlowModBuilder { |
383 | mBuilder.setExact(MatchField.OCH_SIGID, | 383 | mBuilder.setExact(MatchField.OCH_SIGID, |
384 | new CircuitSignalID(gridType, channelSpacing, | 384 | new CircuitSignalID(gridType, channelSpacing, |
385 | (short) signal.spacingMultiplier(), (short) signal.slotGranularity())); | 385 | (short) signal.spacingMultiplier(), (short) signal.slotGranularity())); |
386 | - } catch (UnsupportedGridTypeException | UnsupportedChannelSpacingException e) { | 386 | + } catch (UnsupportedConversionException e) { |
387 | log.warn(e.getMessage()); | 387 | log.warn(e.getMessage()); |
388 | } | 388 | } |
389 | break; | 389 | break; | ... | ... |
... | @@ -15,11 +15,11 @@ | ... | @@ -15,11 +15,11 @@ |
15 | */ | 15 | */ |
16 | package org.onosproject.provider.of.flow.impl; | 16 | package org.onosproject.provider.of.flow.impl; |
17 | 17 | ||
18 | +import com.google.common.collect.BiMap; | ||
19 | +import com.google.common.collect.EnumHashBiMap; | ||
18 | import org.onosproject.net.ChannelSpacing; | 20 | import org.onosproject.net.ChannelSpacing; |
19 | import org.onosproject.net.GridType; | 21 | import org.onosproject.net.GridType; |
20 | import org.onosproject.net.OchSignalType; | 22 | import org.onosproject.net.OchSignalType; |
21 | -import org.slf4j.Logger; | ||
22 | -import org.slf4j.LoggerFactory; | ||
23 | 23 | ||
24 | /** | 24 | /** |
25 | * Collection of helper methods to convert protocol agnostic models to values used in OpenFlow spec. | 25 | * Collection of helper methods to convert protocol agnostic models to values used in OpenFlow spec. |
... | @@ -27,35 +27,63 @@ import org.slf4j.LoggerFactory; | ... | @@ -27,35 +27,63 @@ import org.slf4j.LoggerFactory; |
27 | // TODO: Rename to a better name | 27 | // TODO: Rename to a better name |
28 | final class FlowModBuilderHelper { | 28 | final class FlowModBuilderHelper { |
29 | 29 | ||
30 | - private static final Logger log = LoggerFactory.getLogger(FlowModBuilderHelper.class); | ||
31 | - | ||
32 | // prohibit instantiation | 30 | // prohibit instantiation |
33 | private FlowModBuilderHelper() {} | 31 | private FlowModBuilderHelper() {} |
34 | 32 | ||
33 | + 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 | + | ||
58 | + /** | ||
59 | + * Converts the specified input value to the corresponding value with the specified map. | ||
60 | + * | ||
61 | + * @param map bidirectional mapping | ||
62 | + * @param input input value | ||
63 | + * @param cls class of output value | ||
64 | + * @param <I> type of input value | ||
65 | + * @param <O> type of output value | ||
66 | + * @return the corresponding value stored in the specified map | ||
67 | + * @throws UnsupportedConversionException if no corresponding value is found | ||
68 | + */ | ||
69 | + private static <I, O> O convert(BiMap<I, O> map, I input, Class<O> cls) { | ||
70 | + if (!map.containsKey(input)) { | ||
71 | + throw new UnsupportedConversionException(input, cls); | ||
72 | + } | ||
73 | + | ||
74 | + return map.get(input); | ||
75 | + } | ||
76 | + | ||
35 | /** | 77 | /** |
36 | * Converts a {@link GridType} to the corresponding byte value defined in | 78 | * Converts a {@link GridType} to the corresponding byte value defined in |
37 | * ONF "Optical Transport Protocol Extensions Version 1.0". | 79 | * ONF "Optical Transport Protocol Extensions Version 1.0". |
38 | * | 80 | * |
39 | * @param type grid type | 81 | * @param type grid type |
40 | * @return the byte value corresponding to the specified grid type | 82 | * @return the byte value corresponding to the specified grid type |
41 | - * @throws UnsupportedGridTypeException if the specified grid type is not supported | 83 | + * @throws UnsupportedConversionException if the specified grid type is not supported |
42 | */ | 84 | */ |
43 | static byte convertGridType(GridType type) { | 85 | static byte convertGridType(GridType type) { |
44 | - // See ONF "Optical Transport Protocol Extensions Version 1.0" | 86 | + return convert(GRID_TYPES, type, Byte.class); |
45 | - // for the following values | ||
46 | - switch (type) { | ||
47 | - case DWDM: | ||
48 | - // OFPGRIDT_DWDM of enum ofp_grid_type | ||
49 | - return 1; | ||
50 | - case CWDM: | ||
51 | - // OFPGRIDT_CWDM of enum ofp_grid_type | ||
52 | - return 2; | ||
53 | - case FLEX: | ||
54 | - // OFPGRIDT_FLEX of enum ofp_grid_type | ||
55 | - return 3; | ||
56 | - default: | ||
57 | - throw new UnsupportedGridTypeException(type); | ||
58 | - } | ||
59 | } | 87 | } |
60 | 88 | ||
61 | /** | 89 | /** |
... | @@ -67,17 +95,7 @@ final class FlowModBuilderHelper { | ... | @@ -67,17 +95,7 @@ final class FlowModBuilderHelper { |
67 | * @return the corresponding GridType instance | 95 | * @return the corresponding GridType instance |
68 | */ | 96 | */ |
69 | static GridType convertGridType(byte type) { | 97 | static GridType convertGridType(byte type) { |
70 | - switch (type) { | 98 | + return convert(GRID_TYPES.inverse(), type, GridType.class); |
71 | - case 1: | ||
72 | - return GridType.DWDM; | ||
73 | - case 2: | ||
74 | - return GridType.CWDM; | ||
75 | - case 3: | ||
76 | - return GridType.FLEX; | ||
77 | - default: | ||
78 | - log.info("The value {} for grid type is not supported"); | ||
79 | - return null; | ||
80 | - } | ||
81 | } | 99 | } |
82 | 100 | ||
83 | /** | 101 | /** |
... | @@ -86,30 +104,10 @@ final class FlowModBuilderHelper { | ... | @@ -86,30 +104,10 @@ final class FlowModBuilderHelper { |
86 | * | 104 | * |
87 | * @param spacing channel spacing | 105 | * @param spacing channel spacing |
88 | * @return byte value corresponding to the specified channel spacing | 106 | * @return byte value corresponding to the specified channel spacing |
89 | - * @throws UnsupportedChannelSpacingException if the specified channel spacing is not supported | 107 | + * @throws UnsupportedConversionException if the specified channel spacing is not supported |
90 | */ | 108 | */ |
91 | static byte convertChannelSpacing(ChannelSpacing spacing) { | 109 | static byte convertChannelSpacing(ChannelSpacing spacing) { |
92 | - // See ONF "Optical Transport Protocol Extensions Version 1.0" | 110 | + return convert(CHANNEL_SPACING, spacing, Byte.class); |
93 | - // for the following values | ||
94 | - switch (spacing) { | ||
95 | - case CHL_100GHZ: | ||
96 | - // OFPCS_100GHZ of enum ofp_chl_spacing | ||
97 | - return 1; | ||
98 | - case CHL_50GHZ: | ||
99 | - // OFPCS_50GHZ of enum ofp_chl_spacing | ||
100 | - return 2; | ||
101 | - case CHL_25GHZ: | ||
102 | - // OFPCS_25GHZ of enum ofp_chl_spacing | ||
103 | - return 3; | ||
104 | - case CHL_12P5GHZ: | ||
105 | - // OFPCS_12P5GHZ of enum ofp_chl_spacing | ||
106 | - return 4; | ||
107 | - case CHL_6P25GHZ: | ||
108 | - // OFPCS_6P25GHZ of enum ofp_chl_spacing | ||
109 | - return 5; | ||
110 | - default: | ||
111 | - throw new UnsupportedChannelSpacingException(spacing); | ||
112 | - } | ||
113 | } | 111 | } |
114 | 112 | ||
115 | /** | 113 | /** |
... | @@ -121,21 +119,7 @@ final class FlowModBuilderHelper { | ... | @@ -121,21 +119,7 @@ final class FlowModBuilderHelper { |
121 | * @return the corresponding ChannelSpacing instance | 119 | * @return the corresponding ChannelSpacing instance |
122 | */ | 120 | */ |
123 | static ChannelSpacing convertChannelSpacing(byte spacing) { | 121 | static ChannelSpacing convertChannelSpacing(byte spacing) { |
124 | - switch (spacing) { | 122 | + return convert(CHANNEL_SPACING.inverse(), spacing, ChannelSpacing.class); |
125 | - case 1: | ||
126 | - return ChannelSpacing.CHL_100GHZ; | ||
127 | - case 2: | ||
128 | - return ChannelSpacing.CHL_50GHZ; | ||
129 | - case 3: | ||
130 | - return ChannelSpacing.CHL_25GHZ; | ||
131 | - case 4: | ||
132 | - return ChannelSpacing.CHL_12P5GHZ; | ||
133 | - case 5: | ||
134 | - return ChannelSpacing.CHL_6P25GHZ; | ||
135 | - default: | ||
136 | - log.info("The value {} for channel spacing is not supported"); | ||
137 | - return null; | ||
138 | - } | ||
139 | } | 123 | } |
140 | 124 | ||
141 | /** | 125 | /** |
... | @@ -145,15 +129,7 @@ final class FlowModBuilderHelper { | ... | @@ -145,15 +129,7 @@ final class FlowModBuilderHelper { |
145 | * @return byte value corresponding to the specified OCh signal type | 129 | * @return byte value corresponding to the specified OCh signal type |
146 | */ | 130 | */ |
147 | static byte convertOchSignalType(OchSignalType signalType) { | 131 | static byte convertOchSignalType(OchSignalType signalType) { |
148 | - switch (signalType) { | 132 | + return convert(OCH_SIGNAL_TYPES, signalType, Byte.class); |
149 | - case FIXED_GRID: | ||
150 | - return (byte) 1; | ||
151 | - case FLEX_GRID: | ||
152 | - return (byte) 2; | ||
153 | - default: | ||
154 | - log.info("OchSignalType {} is not supported", signalType); | ||
155 | - return (byte) 0; | ||
156 | - } | ||
157 | } | 133 | } |
158 | 134 | ||
159 | /** | 135 | /** |
... | @@ -165,14 +141,6 @@ final class FlowModBuilderHelper { | ... | @@ -165,14 +141,6 @@ final class FlowModBuilderHelper { |
165 | * @return the corresponding OchSignalType instance | 141 | * @return the corresponding OchSignalType instance |
166 | */ | 142 | */ |
167 | static OchSignalType convertOchSignalType(byte signalType) { | 143 | static OchSignalType convertOchSignalType(byte signalType) { |
168 | - switch (signalType) { | 144 | + return convert(OCH_SIGNAL_TYPES.inverse(), signalType, OchSignalType.class); |
169 | - case 1: | ||
170 | - return OchSignalType.FIXED_GRID; | ||
171 | - case 2: | ||
172 | - return OchSignalType.FLEX_GRID; | ||
173 | - default: | ||
174 | - log.info("The value {} for Och signal type is not supported"); | ||
175 | - return null; | ||
176 | - } | ||
177 | } | 145 | } |
178 | } | 146 | } | ... | ... |
providers/openflow/flow/src/main/java/org/onosproject/provider/of/flow/impl/FlowModBuilderVer13.java
... | @@ -267,7 +267,7 @@ public class FlowModBuilderVer13 extends FlowModBuilder { | ... | @@ -267,7 +267,7 @@ public class FlowModBuilderVer13 extends FlowModBuilder { |
267 | case OCH: | 267 | case OCH: |
268 | try { | 268 | try { |
269 | return buildModOchSignalInstruction((ModOchSignalInstruction) i); | 269 | return buildModOchSignalInstruction((ModOchSignalInstruction) i); |
270 | - } catch (UnsupportedGridTypeException | UnsupportedChannelSpacingException e) { | 270 | + } catch (UnsupportedConversionException e) { |
271 | log.warn(e.getMessage()); | 271 | log.warn(e.getMessage()); |
272 | break; | 272 | break; |
273 | } | 273 | } | ... | ... |
... | @@ -15,19 +15,17 @@ | ... | @@ -15,19 +15,17 @@ |
15 | */ | 15 | */ |
16 | package org.onosproject.provider.of.flow.impl; | 16 | package org.onosproject.provider.of.flow.impl; |
17 | 17 | ||
18 | -import org.onosproject.net.ChannelSpacing; | ||
19 | - | ||
20 | /** | 18 | /** |
21 | - * Thrown to indicate that unsupported channel spacing is referred. | 19 | + * Thrown to indicate that unsupported conversion occurs. |
22 | */ | 20 | */ |
23 | -public class UnsupportedChannelSpacingException extends RuntimeException { | 21 | +public class UnsupportedConversionException extends RuntimeException { |
24 | - | ||
25 | /** | 22 | /** |
26 | - * Creates an instance with the specified unsupported channel spacing. | 23 | + * Creates an instance with the specified values. |
27 | * | 24 | * |
28 | - * @param unsupported unsupported channel spacing | 25 | + * @param input input value of conversion causing this exception |
26 | + * @param output the desired class which the input value is converted to | ||
29 | */ | 27 | */ |
30 | - public UnsupportedChannelSpacingException(ChannelSpacing unsupported) { | 28 | + public UnsupportedConversionException(Object input, Class<?> output) { |
31 | - super("ChannelSpacing " + unsupported + " is not supported"); | 29 | + super(String.format("No mapping found for %s when converting to %s", input, output.getName())); |
32 | } | 30 | } |
33 | } | 31 | } | ... | ... |
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 | - | ||
18 | -import org.onosproject.net.GridType; | ||
19 | - | ||
20 | -/** | ||
21 | - * Thrown to indicate that unsupported gird type is referred. | ||
22 | - */ | ||
23 | -public class UnsupportedGridTypeException extends RuntimeException { | ||
24 | - | ||
25 | - /** | ||
26 | - * Creates an instance with the specified unsupported grid type. | ||
27 | - * | ||
28 | - * @param unsupported unsupported grid type | ||
29 | - */ | ||
30 | - public UnsupportedGridTypeException(GridType unsupported) { | ||
31 | - super("GridType " + unsupported + " is not supported"); | ||
32 | - } | ||
33 | -} |
-
Please register or login to post a comment