yjimmyy | cfcb053 | 2016-07-11 16:03:48 -0700 | [diff] [blame^] | 1 | package org.onosproject.driver.optical.extensions; |
| 2 | |
| 3 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 4 | import org.onlab.osgi.DefaultServiceDirectory; |
| 5 | import org.onlab.osgi.ServiceDirectory; |
| 6 | import org.onosproject.codec.CodecContext; |
| 7 | import org.onosproject.codec.CodecService; |
| 8 | import org.onosproject.codec.ExtensionTreatmentCodec; |
| 9 | import org.onosproject.driver.extensions.OplinkAttenuation; |
| 10 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 11 | import org.onosproject.net.flow.instructions.ExtensionTreatment; |
| 12 | import org.onosproject.net.flow.instructions.ExtensionTreatmentType; |
| 13 | |
| 14 | import static org.onlab.util.Tools.nullIsIllegal; |
| 15 | |
| 16 | /** |
| 17 | * Codec for Oplink extensions. |
| 18 | */ |
| 19 | public class OplinkExtensionTreatmentCodec extends AbstractHandlerBehaviour |
| 20 | implements ExtensionTreatmentCodec { |
| 21 | |
| 22 | private static final String TYPE = "type"; |
| 23 | private static final String MISSING_TYPE = "Missing extension type"; |
| 24 | private static final String UNSUPPORTED_TYPE = "Extension type is not supported: "; |
| 25 | |
| 26 | @Override |
| 27 | public ExtensionTreatment decode(ObjectNode objectNode, CodecContext context) { |
| 28 | if (objectNode == null || !objectNode.isObject()) { |
| 29 | return null; |
| 30 | } |
| 31 | |
| 32 | int typeInt = nullIsIllegal(objectNode.get(TYPE), MISSING_TYPE).asInt(); |
| 33 | ExtensionTreatmentType type = new ExtensionTreatmentType(typeInt); |
| 34 | |
| 35 | if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.OPLINK_ATTENUATION.type())) { |
| 36 | return decodeTreatment(objectNode, context, OplinkAttenuation.class); |
| 37 | } else { |
| 38 | throw new UnsupportedOperationException(UNSUPPORTED_TYPE + type.toString()); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | private <T extends ExtensionTreatment> ExtensionTreatment decodeTreatment( |
| 43 | ObjectNode objectNode, CodecContext context, Class<T> entityClass) { |
| 44 | if (context == null) { |
| 45 | ServiceDirectory serviceDirectory = new DefaultServiceDirectory(); |
| 46 | return serviceDirectory.get(CodecService.class).getCodec(entityClass).decode(objectNode, null); |
| 47 | } else { |
| 48 | return context.codec(entityClass).decode(objectNode, context); |
| 49 | } |
| 50 | } |
| 51 | } |