blob: c17d3fa5107ce7ff337424de14b1610dfd00eab3 [file] [log] [blame]
Shankara-Huaweid5823ab2016-11-22 10:14:52 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Shankara-Huaweid5823ab2016-11-22 10:14:52 +05303 *
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
17package org.onosproject.yms.app.ych.defaultcodecs;
18
19
20import org.onosproject.yms.app.ych.YchException;
21import org.onosproject.yms.app.ych.defaultcodecs.xml.XmlCodecHandler;
22import org.onosproject.yms.app.ych.defaultcodecs.xml.XmlCodecMultiInstanceHandler;
23import org.onosproject.yms.app.ych.defaultcodecs.xml.XmlCodecMultiInstanceLeafHandler;
24import org.onosproject.yms.app.ych.defaultcodecs.xml.XmlCodecSingleInstanceHandler;
25import org.onosproject.yms.app.ych.defaultcodecs.xml.XmlCodecSingleInstanceLeafHandler;
26import org.onosproject.yms.ych.YangProtocolEncodingFormat;
27import org.onosproject.yms.ydt.YdtContext;
28import org.onosproject.yms.ydt.YdtType;
29import org.slf4j.Logger;
30import org.slf4j.LoggerFactory;
31
32import java.util.HashMap;
33import java.util.Map;
34
35import static org.onosproject.yms.ych.YangProtocolEncodingFormat.XML;
36import static org.onosproject.yms.ydt.YdtType.MULTI_INSTANCE_LEAF_VALUE_NODE;
37import static org.onosproject.yms.ydt.YdtType.MULTI_INSTANCE_NODE;
38import static org.onosproject.yms.ydt.YdtType.SINGLE_INSTANCE_LEAF_VALUE_NODE;
39import 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 */
45public 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}