blob: c62f85710a371546c85df1139a548d21e33dda05 [file] [log] [blame]
Ray Milkey85267002016-11-16 11:06:35 -08001/*
2 * Copyright 2016-present 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 */
yjimmyycfcb0532016-07-11 16:03:48 -070016package org.onosproject.driver.optical.extensions;
17
18import com.fasterxml.jackson.databind.node.ObjectNode;
19import org.onlab.osgi.DefaultServiceDirectory;
20import org.onlab.osgi.ServiceDirectory;
21import org.onosproject.codec.CodecContext;
22import org.onosproject.codec.CodecService;
23import org.onosproject.codec.ExtensionTreatmentCodec;
24import org.onosproject.driver.extensions.OplinkAttenuation;
25import org.onosproject.net.driver.AbstractHandlerBehaviour;
26import org.onosproject.net.flow.instructions.ExtensionTreatment;
27import org.onosproject.net.flow.instructions.ExtensionTreatmentType;
28
29import static org.onlab.util.Tools.nullIsIllegal;
30
31/**
32 * Codec for Oplink extensions.
33 */
34public class OplinkExtensionTreatmentCodec extends AbstractHandlerBehaviour
35 implements ExtensionTreatmentCodec {
36
37 private static final String TYPE = "type";
38 private static final String MISSING_TYPE = "Missing extension type";
39 private static final String UNSUPPORTED_TYPE = "Extension type is not supported: ";
40
41 @Override
42 public ExtensionTreatment decode(ObjectNode objectNode, CodecContext context) {
43 if (objectNode == null || !objectNode.isObject()) {
44 return null;
45 }
46
47 int typeInt = nullIsIllegal(objectNode.get(TYPE), MISSING_TYPE).asInt();
48 ExtensionTreatmentType type = new ExtensionTreatmentType(typeInt);
49
50 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.OPLINK_ATTENUATION.type())) {
51 return decodeTreatment(objectNode, context, OplinkAttenuation.class);
52 } else {
53 throw new UnsupportedOperationException(UNSUPPORTED_TYPE + type.toString());
54 }
55 }
56
57 private <T extends ExtensionTreatment> ExtensionTreatment decodeTreatment(
58 ObjectNode objectNode, CodecContext context, Class<T> entityClass) {
59 if (context == null) {
60 ServiceDirectory serviceDirectory = new DefaultServiceDirectory();
61 return serviceDirectory.get(CodecService.class).getCodec(entityClass).decode(objectNode, null);
62 } else {
63 return context.codec(entityClass).decode(objectNode, context);
64 }
65 }
66}