Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +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.yob; |
| 18 | |
| 19 | import org.onosproject.yms.app.ydt.YdtExtendedContext; |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 20 | import org.onosproject.yms.app.yob.exception.YobException; |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 21 | import org.onosproject.yms.ydt.YdtType; |
| 22 | import org.slf4j.Logger; |
| 23 | import org.slf4j.LoggerFactory; |
| 24 | |
| 25 | import java.util.HashMap; |
| 26 | import java.util.Map; |
| 27 | |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 28 | import static org.onosproject.yms.app.yob.YobConstants.E_YDT_TYPE_IS_NOT_SUPPORT; |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 29 | import static org.onosproject.yms.ydt.YdtType.MULTI_INSTANCE_LEAF_VALUE_NODE; |
| 30 | import static org.onosproject.yms.ydt.YdtType.MULTI_INSTANCE_NODE; |
| 31 | import static org.onosproject.yms.ydt.YdtType.SINGLE_INSTANCE_LEAF_VALUE_NODE; |
| 32 | import 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 | */ |
| 38 | final class YobHandlerFactory { |
| 39 | |
| 40 | private static final Logger log = |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 41 | LoggerFactory.getLogger(YobHandlerFactory.class); |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 42 | |
| 43 | /** |
| 44 | * Map of YANG object builder handler. |
| 45 | */ |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 46 | private static final Map<YdtType, YobHandler> HANDLER_MAP = new HashMap<>(); |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 47 | |
| 48 | /** |
| 49 | * Create instance of YobHandlerFactory. |
| 50 | */ |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 51 | 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-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Returns the corresponding YOB handler for current context. |
| 62 | * |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 63 | * @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-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 66 | */ |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 67 | YobHandler getYobHandlerForContext(YdtExtendedContext currentNode) { |
| 68 | YobHandler yobHandler = HANDLER_MAP.get(currentNode.getYdtType()); |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 69 | if (yobHandler == null) { |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 70 | log.error(E_YDT_TYPE_IS_NOT_SUPPORT); |
| 71 | throw new YobException(E_YDT_TYPE_IS_NOT_SUPPORT); |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 72 | } |
| 73 | return yobHandler; |
| 74 | } |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 75 | |
| 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-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 93 | } |