Shankara-Huawei | d5823ab | 2016-11-22 10:14:52 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame^] | 2 | * Copyright 2016-present Open Networking Foundation |
Shankara-Huawei | d5823ab | 2016-11-22 10:14:52 +0530 | [diff] [blame] | 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 | */ |
| 16 | |
| 17 | package org.onosproject.yms.app.ych.defaultcodecs; |
| 18 | |
| 19 | |
| 20 | import org.onosproject.yms.app.ych.YchException; |
| 21 | import org.onosproject.yms.app.ych.defaultcodecs.xml.XmlCodecHandler; |
| 22 | import org.onosproject.yms.app.ych.defaultcodecs.xml.XmlCodecMultiInstanceHandler; |
| 23 | import org.onosproject.yms.app.ych.defaultcodecs.xml.XmlCodecMultiInstanceLeafHandler; |
| 24 | import org.onosproject.yms.app.ych.defaultcodecs.xml.XmlCodecSingleInstanceHandler; |
| 25 | import org.onosproject.yms.app.ych.defaultcodecs.xml.XmlCodecSingleInstanceLeafHandler; |
| 26 | import org.onosproject.yms.ych.YangProtocolEncodingFormat; |
| 27 | import org.onosproject.yms.ydt.YdtContext; |
| 28 | import org.onosproject.yms.ydt.YdtType; |
| 29 | import org.slf4j.Logger; |
| 30 | import org.slf4j.LoggerFactory; |
| 31 | |
| 32 | import java.util.HashMap; |
| 33 | import java.util.Map; |
| 34 | |
| 35 | import static org.onosproject.yms.ych.YangProtocolEncodingFormat.XML; |
| 36 | import static org.onosproject.yms.ydt.YdtType.MULTI_INSTANCE_LEAF_VALUE_NODE; |
| 37 | import static org.onosproject.yms.ydt.YdtType.MULTI_INSTANCE_NODE; |
| 38 | import static org.onosproject.yms.ydt.YdtType.SINGLE_INSTANCE_LEAF_VALUE_NODE; |
| 39 | import static org.onosproject.yms.ydt.YdtType.SINGLE_INSTANCE_NODE; |
| 40 | |
| 41 | /** |
| 42 | * Represents an YCH handle factory to create different types of YANG data tree |
| 43 | * node. |
| 44 | */ |
| 45 | public final class CodecHandlerFactory { |
| 46 | |
| 47 | private static final Logger log = |
| 48 | LoggerFactory.getLogger(CodecHandlerFactory.class); |
| 49 | private static final String YDT_TYPE_ERROR = "YDT type is not supported."; |
| 50 | |
| 51 | /** |
| 52 | * Map of xml codec handler. |
| 53 | */ |
| 54 | private final Map<YdtType, XmlCodecHandler> handlerMap; |
| 55 | |
| 56 | /** |
| 57 | * Creates a new codec handler factory. |
| 58 | */ |
| 59 | private CodecHandlerFactory() { |
| 60 | handlerMap = new HashMap<>(); |
| 61 | handlerMap.put(SINGLE_INSTANCE_NODE, |
| 62 | new XmlCodecSingleInstanceHandler()); |
| 63 | handlerMap.put(MULTI_INSTANCE_NODE, |
| 64 | new XmlCodecMultiInstanceHandler()); |
| 65 | handlerMap.put(SINGLE_INSTANCE_LEAF_VALUE_NODE, |
| 66 | new XmlCodecSingleInstanceLeafHandler()); |
| 67 | handlerMap.put(MULTI_INSTANCE_LEAF_VALUE_NODE, |
| 68 | new XmlCodecMultiInstanceLeafHandler()); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Returns YCH instance handler node instance. |
| 73 | * |
| 74 | * @param node YDT context node |
| 75 | * @param format data format type expected from driver |
| 76 | * @return returns YCH handler node instance |
| 77 | */ |
| 78 | public XmlCodecHandler getCodecHandlerForContext( |
| 79 | YdtContext node, |
| 80 | YangProtocolEncodingFormat format) { |
| 81 | if (format == XML) { |
| 82 | XmlCodecHandler handler = handlerMap.get(node.getYdtType()); |
| 83 | if (handler == null) { |
| 84 | throw new YchException(YDT_TYPE_ERROR + node.getYdtType()); |
| 85 | } |
| 86 | return handler; |
| 87 | } |
| 88 | log.error("{} data format is not supported.", format); |
| 89 | return null; |
| 90 | } |
| 91 | |
| 92 | /* |
| 93 | * Bill Pugh Singleton pattern. INSTANCE won't be instantiated until the |
| 94 | * LazyHolder class is loaded via a call to the instance() method below. |
| 95 | */ |
| 96 | private static class LazyHolder { |
| 97 | private static final CodecHandlerFactory INSTANCE = |
| 98 | new CodecHandlerFactory(); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Returns a reference to the Singleton Codec Handler factory. |
| 103 | * |
| 104 | * @return the singleton codec handler factory |
| 105 | */ |
| 106 | public static CodecHandlerFactory instance() { |
| 107 | return LazyHolder.INSTANCE; |
| 108 | } |
| 109 | } |