blob: fbe6edf1f27832513997a14fa4d61225fe811719 [file] [log] [blame]
yjimmyycfcb0532016-07-11 16:03:48 -07001package org.onosproject.driver.optical.extensions;
2
3import com.fasterxml.jackson.databind.node.ObjectNode;
4import org.onlab.osgi.DefaultServiceDirectory;
5import org.onlab.osgi.ServiceDirectory;
6import org.onosproject.codec.CodecContext;
7import org.onosproject.codec.CodecService;
8import org.onosproject.codec.ExtensionTreatmentCodec;
9import org.onosproject.driver.extensions.OplinkAttenuation;
10import org.onosproject.net.driver.AbstractHandlerBehaviour;
11import org.onosproject.net.flow.instructions.ExtensionTreatment;
12import org.onosproject.net.flow.instructions.ExtensionTreatmentType;
13
14import static org.onlab.util.Tools.nullIsIllegal;
15
16/**
17 * Codec for Oplink extensions.
18 */
19public 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}