Sho SHIMIZU
Committed by Thomas Vachuska

Refactor: Rename the class and method to make them more descriptive

- Rename from FlowModBuilderHelper to OpenFlowValueMapper
- Rename from UnsupportedConversionException to NoMappingFoundException
- For methods, use "lookup" instead of "convert"

Change-Id: I8e86e3221385a48524247aa78a497b524e16071a
......@@ -79,9 +79,9 @@ import java.util.List;
import static org.onosproject.net.flow.criteria.Criteria.matchLambda;
import static org.onosproject.net.flow.criteria.Criteria.matchOchSignalType;
import static org.onosproject.provider.of.flow.impl.FlowModBuilderHelper.convertChannelSpacing;
import static org.onosproject.provider.of.flow.impl.FlowModBuilderHelper.convertGridType;
import static org.onosproject.provider.of.flow.impl.FlowModBuilderHelper.convertOchSignalType;
import static org.onosproject.provider.of.flow.impl.OpenFlowValueMapper.lookupChannelSpacing;
import static org.onosproject.provider.of.flow.impl.OpenFlowValueMapper.lookupGridType;
import static org.onosproject.provider.of.flow.impl.OpenFlowValueMapper.lookupOchSignalType;
import static org.slf4j.LoggerFactory.getLogger;
public class FlowEntryBuilder {
......@@ -643,13 +643,13 @@ public class FlowEntryBuilder {
case OCH_SIGID:
CircuitSignalID sigId = match.get(MatchField.OCH_SIGID);
builder.add(matchLambda(Lambda.ochSignal(
convertGridType(sigId.getGridType()), convertChannelSpacing(sigId.getChannelSpacing()),
lookupGridType(sigId.getGridType()), lookupChannelSpacing(sigId.getChannelSpacing()),
sigId.getChannelNumber(), sigId.getChannelSpacing())
));
break;
case OCH_SIGTYPE:
U8 sigType = match.get(MatchField.OCH_SIGTYPE);
builder.add(matchOchSignalType(convertOchSignalType((byte) sigType.getValue())));
builder.add(matchOchSignalType(lookupOchSignalType((byte) sigType.getValue())));
break;
case ARP_OP:
case ARP_SHA:
......
......@@ -378,18 +378,18 @@ public abstract class FlowModBuilder {
try {
OchSignalCriterion ochSignalCriterion = (OchSignalCriterion) c;
OchSignal signal = ochSignalCriterion.lambda();
byte gridType = FlowModBuilderHelper.convertGridType(signal.gridType());
byte channelSpacing = FlowModBuilderHelper.convertChannelSpacing(signal.channelSpacing());
byte gridType = OpenFlowValueMapper.lookupGridType(signal.gridType());
byte channelSpacing = OpenFlowValueMapper.lookupChannelSpacing(signal.channelSpacing());
mBuilder.setExact(MatchField.OCH_SIGID,
new CircuitSignalID(gridType, channelSpacing,
(short) signal.spacingMultiplier(), (short) signal.slotGranularity()));
} catch (UnsupportedConversionException e) {
} catch (NoMappingFoundException e) {
log.warn(e.getMessage());
}
break;
case OCH_SIGTYPE:
OchSignalTypeCriterion sc = (OchSignalTypeCriterion) c;
byte signalType = FlowModBuilderHelper.convertOchSignalType(sc.signalType());
byte signalType = OpenFlowValueMapper.lookupOchSignalType(sc.signalType());
mBuilder.setExact(MatchField.OCH_SIGTYPE, U8.of(signalType));
break;
case ARP_OP:
......
......@@ -71,9 +71,6 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import static org.onosproject.provider.of.flow.impl.FlowModBuilderHelper.convertChannelSpacing;
import static org.onosproject.provider.of.flow.impl.FlowModBuilderHelper.convertGridType;
/**
* Flow mod builder for OpenFlow 1.3+.
*/
......@@ -267,7 +264,7 @@ public class FlowModBuilderVer13 extends FlowModBuilder {
case OCH:
try {
return buildModOchSignalInstruction((ModOchSignalInstruction) i);
} catch (UnsupportedConversionException e) {
} catch (NoMappingFoundException e) {
log.warn(e.getMessage());
break;
}
......@@ -285,8 +282,8 @@ public class FlowModBuilderVer13 extends FlowModBuilder {
private OFAction buildModOchSignalInstruction(ModOchSignalInstruction instruction) {
OchSignal signal = instruction.lambda();
byte gridType = convertGridType(signal.gridType());
byte channelSpacing = convertChannelSpacing(signal.channelSpacing());
byte gridType = OpenFlowValueMapper.lookupGridType(signal.gridType());
byte channelSpacing = OpenFlowValueMapper.lookupChannelSpacing(signal.channelSpacing());
return factory().actions().circuit(factory().oxms().ochSigidBasic(
new CircuitSignalID(gridType, channelSpacing,
......
......@@ -16,16 +16,16 @@
package org.onosproject.provider.of.flow.impl;
/**
* Thrown to indicate that unsupported conversion occurs.
* Thrown to indicate that no mapping for the input value is found.
*/
public class UnsupportedConversionException extends RuntimeException {
public class NoMappingFoundException extends RuntimeException {
/**
* Creates an instance with the specified values.
*
* @param input input value of conversion causing this exception
* @param output the desired class which the input value is converted to
* @param input input value of mapping causing this exception
* @param output the desired class which the input value is mapped to
*/
public UnsupportedConversionException(Object input, Class<?> output) {
public NoMappingFoundException(Object input, Class<?> output) {
super(String.format("No mapping found for %s when converting to %s", input, output.getName()));
}
}
......
......@@ -24,11 +24,10 @@ import org.onosproject.net.OchSignalType;
/**
* Collection of helper methods to convert protocol agnostic models to values used in OpenFlow spec.
*/
// TODO: Rename to a better name
final class FlowModBuilderHelper {
final class OpenFlowValueMapper {
// prohibit instantiation
private FlowModBuilderHelper() {}
private OpenFlowValueMapper() {}
private static final BiMap<GridType, Byte> GRID_TYPES = EnumHashBiMap.create(GridType.class);
static {
......@@ -56,7 +55,7 @@ final class FlowModBuilderHelper {
}
/**
* Converts the specified input value to the corresponding value with the specified map.
* Looks up the specified input value to the corresponding value with the specified map.
*
* @param map bidirectional mapping
* @param input input value
......@@ -64,83 +63,90 @@ final class FlowModBuilderHelper {
* @param <I> type of input value
* @param <O> type of output value
* @return the corresponding value stored in the specified map
* @throws UnsupportedConversionException if no corresponding value is found
* @throws NoMappingFoundException if no corresponding value is found
*/
private static <I, O> O convert(BiMap<I, O> map, I input, Class<O> cls) {
private static <I, O> O lookup(BiMap<I, O> map, I input, Class<O> cls) {
if (!map.containsKey(input)) {
throw new UnsupportedConversionException(input, cls);
throw new NoMappingFoundException(input, cls);
}
return map.get(input);
}
/**
* Converts a {@link GridType} to the corresponding byte value defined in
* ONF "Optical Transport Protocol Extensions Version 1.0".
* Looks up the corresponding byte value defined in
* ONF "Optical Transport Protocol Extensions Version 1.0"
* from the specified {@link GridType} instance.
*
* @param type grid type
* @return the byte value corresponding to the specified grid type
* @throws UnsupportedConversionException if the specified grid type is not supported
* @throws NoMappingFoundException if the specified grid type is not found
*/
static byte convertGridType(GridType type) {
return convert(GRID_TYPES, type, Byte.class);
static byte lookupGridType(GridType type) {
return lookup(GRID_TYPES, type, Byte.class);
}
/**
* Converts a byte value for grid type
* defined in ONF "Optical Transport Protocol Extensions Version 1.0"
* to the corresponding {@link GridType} instance.
* Looks up the corresponding {@link GridType} instance
* from the specified byte value for grid type
* defined in ONF "Optical Transport Protocol Extensions Version 1.0".
*
* @param type byte value as grid type defined the spec
* @return the corresponding GridType instance
*/
static GridType convertGridType(byte type) {
return convert(GRID_TYPES.inverse(), type, GridType.class);
static GridType lookupGridType(byte type) {
return lookup(GRID_TYPES.inverse(), type, GridType.class);
}
/**
* Converts a {@link ChannelSpacing} to the corresponding byte value defined in
* ONF "Optical Transport Protocol Extensions Version 1.0".
* Looks up the corresponding byte value for channel spacing defined in
* ONF "Optical Transport Protocol Extensions Version 1.0"
* from the specified {@link ChannelSpacing} instance.
*
* @param spacing channel spacing
* @return byte value corresponding to the specified channel spacing
* @throws UnsupportedConversionException if the specified channel spacing is not supported
* @throws NoMappingFoundException if the specified channel spacing is not found
*/
static byte convertChannelSpacing(ChannelSpacing spacing) {
return convert(CHANNEL_SPACING, spacing, Byte.class);
static byte lookupChannelSpacing(ChannelSpacing spacing) {
return lookup(CHANNEL_SPACING, spacing, Byte.class);
}
/**
* Converts a byte value for channel spacing
* defined in ONF "Optical Transport Protocol Extensions Version 1.0"
* to the corresponding {@link ChannelSpacing} instance.
* Looks up the corresponding {@link ChannelSpacing} instance
* from the specified byte value for channel spacing
* defined in ONF "Optical Transport Protocol Extensions Version 1.0".
*
* @param spacing byte value as channel spacing defined the spec
* @return the corresponding ChannelSpacing instance
* @throws NoMappingFoundException if the specified channel spacing is not found
*/
static ChannelSpacing convertChannelSpacing(byte spacing) {
return convert(CHANNEL_SPACING.inverse(), spacing, ChannelSpacing.class);
static ChannelSpacing lookupChannelSpacing(byte spacing) {
return lookup(CHANNEL_SPACING.inverse(), spacing, ChannelSpacing.class);
}
/**
* Converts a {@link OchSignalType} to the corresponding byte value.
* Looks up the corresponding byte value for Och signal type defined in
* ONF "Optical Transport Protocol Extensions Version 1.0"
* from the specified {@link OchSignalType} instance.
*
* @param signalType optical signal type
* @return byte value corresponding to the specified OCh signal type
* @throws NoMappingFoundException if the specified Och signal type is not found
*/
static byte convertOchSignalType(OchSignalType signalType) {
return convert(OCH_SIGNAL_TYPES, signalType, Byte.class);
static byte lookupOchSignalType(OchSignalType signalType) {
return lookup(OCH_SIGNAL_TYPES, signalType, Byte.class);
}
/**
* Converts a byte value for Och signal type
* defined in ONF "Optical Transport Protocol Extensions Version 1.0"
* to the corresponding {@link OchSignalType} instance.
* Looks up the the corresponding {@link OchSignalType} instance
* from the specified byte value for Och signal type defined in
* ONF "Optical Transport Protocol Extensions Version 1.0".
*
* @param signalType byte value as Och singal type defined the spec
* @return the corresponding OchSignalType instance
* @throws NoMappingFoundException if the specified Och signal type is not found
*/
static OchSignalType convertOchSignalType(byte signalType) {
return convert(OCH_SIGNAL_TYPES.inverse(), signalType, OchSignalType.class);
static OchSignalType lookupOchSignalType(byte signalType) {
return lookup(OCH_SIGNAL_TYPES.inverse(), signalType, OchSignalType.class);
}
}
......