Ray Milkey | 8526700 | 2016-11-16 11:06:35 -0800 | [diff] [blame^] | 1 | /* |
| 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 | */ |
yjimmyy | cfcb053 | 2016-07-11 16:03:48 -0700 | [diff] [blame] | 16 | package org.onosproject.driver.optical.extensions; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 19 | import org.onlab.osgi.DefaultServiceDirectory; |
| 20 | import org.onlab.osgi.ServiceDirectory; |
| 21 | import org.onosproject.codec.CodecContext; |
| 22 | import org.onosproject.codec.CodecService; |
| 23 | import org.onosproject.codec.ExtensionTreatmentCodec; |
| 24 | import org.onosproject.driver.extensions.OplinkAttenuation; |
| 25 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 26 | import org.onosproject.net.flow.instructions.ExtensionTreatment; |
| 27 | import org.onosproject.net.flow.instructions.ExtensionTreatmentType; |
| 28 | |
| 29 | import static org.onlab.util.Tools.nullIsIllegal; |
| 30 | |
| 31 | /** |
| 32 | * Codec for Oplink extensions. |
| 33 | */ |
| 34 | public 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 | } |