blob: e91d0cf2697e4432ddc72eefe6cb4777640f6fa3 [file] [log] [blame]
Rama-Huaweib711e5c2016-08-31 07:55:46 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Rama-Huaweib711e5c2016-08-31 07:55:46 +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.yob;
18
19import org.onosproject.yms.app.ydt.YdtExtendedContext;
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053020import org.onosproject.yms.app.yob.exception.YobException;
Rama-Huaweib711e5c2016-08-31 07:55:46 +053021import org.onosproject.yms.ydt.YdtType;
22import org.slf4j.Logger;
23import org.slf4j.LoggerFactory;
24
25import java.util.HashMap;
26import java.util.Map;
27
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053028import static org.onosproject.yms.app.yob.YobConstants.E_YDT_TYPE_IS_NOT_SUPPORT;
Rama-Huaweib711e5c2016-08-31 07:55:46 +053029import static org.onosproject.yms.ydt.YdtType.MULTI_INSTANCE_LEAF_VALUE_NODE;
30import static org.onosproject.yms.ydt.YdtType.MULTI_INSTANCE_NODE;
31import static org.onosproject.yms.ydt.YdtType.SINGLE_INSTANCE_LEAF_VALUE_NODE;
32import static org.onosproject.yms.ydt.YdtType.SINGLE_INSTANCE_NODE;
33
34/**
35 * Represents an YANG object builder factory to create different types
36 * of YANG data tree node.
37 */
38final class YobHandlerFactory {
39
40 private static final Logger log =
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053041 LoggerFactory.getLogger(YobHandlerFactory.class);
Rama-Huaweib711e5c2016-08-31 07:55:46 +053042
43 /**
44 * Map of YANG object builder handler.
45 */
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053046 private static final Map<YdtType, YobHandler> HANDLER_MAP = new HashMap<>();
Rama-Huaweib711e5c2016-08-31 07:55:46 +053047
48 /**
49 * Create instance of YobHandlerFactory.
50 */
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053051 private YobHandlerFactory() {
52 HANDLER_MAP.put(SINGLE_INSTANCE_NODE, new YobSingleInstanceHandler());
53 HANDLER_MAP.put(MULTI_INSTANCE_NODE, new YobMultiInstanceHandler());
54 HANDLER_MAP.put(SINGLE_INSTANCE_LEAF_VALUE_NODE,
55 new YobSingleInstanceLeafHandler());
56 HANDLER_MAP.put(MULTI_INSTANCE_LEAF_VALUE_NODE,
57 new YobMultiInstanceLeafHandler());
Rama-Huaweib711e5c2016-08-31 07:55:46 +053058 }
59
60 /**
61 * Returns the corresponding YOB handler for current context.
62 *
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053063 * @param currentNode current YDT node for which object needs to be created
64 * @return handler to create the object
65 * @throws YobException if the YDT node type is not supported in YOB
Rama-Huaweib711e5c2016-08-31 07:55:46 +053066 */
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053067 YobHandler getYobHandlerForContext(YdtExtendedContext currentNode) {
68 YobHandler yobHandler = HANDLER_MAP.get(currentNode.getYdtType());
Rama-Huaweib711e5c2016-08-31 07:55:46 +053069 if (yobHandler == null) {
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053070 log.error(E_YDT_TYPE_IS_NOT_SUPPORT);
71 throw new YobException(E_YDT_TYPE_IS_NOT_SUPPORT);
Rama-Huaweib711e5c2016-08-31 07:55:46 +053072 }
73 return yobHandler;
74 }
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053075
76 /**
77 * Returns the YANG object builder factory instance.
78 *
79 * @return YANG object builder factory instance
80 */
81 public static YobHandlerFactory instance() {
82 return LazyHolder.INSTANCE;
83 }
84
85 /*
86 * Bill Pugh Singleton pattern. INSTANCE won't be instantiated until the
87 * LazyHolder class is loaded via a call to the instance() method below.
88 */
89 private static class LazyHolder {
90 private static final YobHandlerFactory INSTANCE =
91 new YobHandlerFactory();
92 }
Rama-Huaweib711e5c2016-08-31 07:55:46 +053093}