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 | |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 19 | import org.onosproject.yms.app.yob.exception.YobException; |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 20 | import org.slf4j.Logger; |
| 21 | import org.slf4j.LoggerFactory; |
| 22 | |
Vidyashree Rama | 6160be1 | 2016-11-24 13:43:31 +0530 | [diff] [blame] | 23 | import static org.onosproject.yms.app.yob.YobConstants.E_BUILDER_IS_NOT_ALREADY_SET; |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 24 | import static org.onosproject.yms.app.yob.YobConstants.E_BUILDER_IS_NOT_SET; |
| 25 | import static org.onosproject.yms.app.yob.YobConstants.E_BUILT_OBJ_IS_NOT_SET; |
| 26 | import static org.onosproject.yms.app.yob.YobConstants.E_FAIL_TO_CREATE_OBJ; |
| 27 | import static org.onosproject.yms.app.yob.YobConstants.E_FAIL_TO_LOAD_CLASS; |
| 28 | import static org.onosproject.yms.app.yob.YobConstants.E_OBJ_BUILDING_WITHOUT_BUILDER; |
| 29 | import static org.onosproject.yms.app.yob.YobConstants.E_OBJ_IS_ALREADY_BUILT_NOT_BUILD; |
| 30 | import static org.onosproject.yms.app.yob.YobConstants.E_OBJ_IS_ALREADY_BUILT_NOT_FETCH; |
Vidyashree Rama | 6160be1 | 2016-11-24 13:43:31 +0530 | [diff] [blame] | 31 | import static org.onosproject.yms.app.yob.YobConstants.E_OBJ_IS_ALREADY_BUILT_NOT_SET; |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 32 | import static org.onosproject.yms.app.yob.YobConstants.E_OBJ_IS_NOT_SET_NOT_FETCH; |
| 33 | import static org.onosproject.yms.app.yob.YobConstants.E_REFLECTION_FAIL_TO_CREATE_OBJ; |
| 34 | import static org.onosproject.yms.app.yob.YobConstants.L_FAIL_TO_CREATE_OBJ; |
| 35 | import static org.onosproject.yms.app.yob.YobConstants.L_FAIL_TO_LOAD_CLASS; |
| 36 | import static org.onosproject.yms.app.yob.YobConstants.L_REFLECTION_FAIL_TO_CREATE_OBJ; |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * Represents the container of YANG object being built or the builder. |
| 40 | */ |
| 41 | class YobBuilderOrBuiltObject { |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 42 | private static final Logger log = |
| 43 | LoggerFactory.getLogger(YobWorkBench.class); |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * Is the contained object a built object. |
| 47 | */ |
| 48 | private boolean isBuilt; |
| 49 | |
| 50 | /** |
| 51 | * Builder or built object. |
| 52 | */ |
| 53 | private Object builderOrBuiltObject; |
| 54 | |
| 55 | /** |
| 56 | * Default / op param builder class. |
| 57 | */ |
| 58 | Class<?> yangBuilderClass; |
| 59 | |
| 60 | /** |
| 61 | * Default Class. |
| 62 | */ |
| 63 | Class<?> yangDefaultClass; |
| 64 | |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 65 | /** |
| 66 | * Create Node Object holder. |
| 67 | * |
| 68 | * @param qualifiedClassName name of the class |
| 69 | * @param registeredAppClassLoader class loader to be used |
| 70 | * @throws YobException if failed to create the node object |
| 71 | */ |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 72 | YobBuilderOrBuiltObject(String qualifiedClassName, |
| 73 | ClassLoader registeredAppClassLoader) { |
| 74 | try { |
| 75 | yangDefaultClass = |
| 76 | registeredAppClassLoader.loadClass(qualifiedClassName); |
| 77 | yangBuilderClass = yangDefaultClass.getDeclaredClasses()[0]; |
Vidyashree Rama | 6160be1 | 2016-11-24 13:43:31 +0530 | [diff] [blame] | 78 | setBuilderObject(yangBuilderClass.newInstance()); |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 79 | } catch (ClassNotFoundException e) { |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 80 | log.error(L_FAIL_TO_LOAD_CLASS, qualifiedClassName); |
| 81 | throw new YobException(E_FAIL_TO_LOAD_CLASS + qualifiedClassName); |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 82 | } catch (InstantiationException | IllegalAccessException e) { |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 83 | log.error(L_FAIL_TO_CREATE_OBJ, qualifiedClassName); |
| 84 | throw new YobException(E_FAIL_TO_CREATE_OBJ + qualifiedClassName); |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 85 | } catch (NullPointerException e) { |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 86 | log.error(L_REFLECTION_FAIL_TO_CREATE_OBJ, qualifiedClassName); |
| 87 | throw new YobException(E_REFLECTION_FAIL_TO_CREATE_OBJ + |
| 88 | qualifiedClassName); |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Returns the builder object if it is set. |
| 94 | * |
| 95 | * @return builder object |
Vidyashree Rama | 6160be1 | 2016-11-24 13:43:31 +0530 | [diff] [blame] | 96 | * @throws YobException if builder is not available |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 97 | */ |
| 98 | Object getBuilderObject() { |
| 99 | if (isBuilt) { |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 100 | throw new YobException(E_OBJ_IS_ALREADY_BUILT_NOT_FETCH); |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | if (builderOrBuiltObject == null) { |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 104 | throw new YobException(E_BUILDER_IS_NOT_SET); |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | return builderOrBuiltObject; |
| 108 | } |
| 109 | |
| 110 | /** |
Vidyashree Rama | 6160be1 | 2016-11-24 13:43:31 +0530 | [diff] [blame] | 111 | * Check if the builder object is being initialized for the first time and |
| 112 | * set it. |
| 113 | * |
| 114 | * @param builderObject new builder object |
| 115 | * @throws YobException if built object is not available |
| 116 | */ |
| 117 | private void setBuilderObject(Object builderObject) { |
| 118 | if (isBuilt) { |
| 119 | throw new YobException(E_OBJ_IS_ALREADY_BUILT_NOT_SET); |
| 120 | } |
| 121 | |
| 122 | if (builderOrBuiltObject != null) { |
| 123 | throw new YobException(E_BUILDER_IS_NOT_ALREADY_SET); |
| 124 | } |
| 125 | |
| 126 | builderOrBuiltObject = builderObject; |
| 127 | } |
| 128 | |
| 129 | /** |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 130 | * Returns the built object. |
| 131 | * |
| 132 | * @return built object |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 133 | * @throws YobException if built object is not available or if it is not |
| 134 | * built |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 135 | */ |
| 136 | Object getBuiltObject() { |
| 137 | if (!isBuilt) { |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 138 | throw new YobException(E_OBJ_IS_NOT_SET_NOT_FETCH); |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | if (builderOrBuiltObject == null) { |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 142 | throw new YobException(E_BUILT_OBJ_IS_NOT_SET); |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | return builderOrBuiltObject; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Check if the built object is being initialized for the 1st time and |
| 150 | * set it. |
| 151 | * |
| 152 | * @param builtObject new built object |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 153 | * @throws YobException if builder object is not available or if it is |
| 154 | * already built |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 155 | */ |
| 156 | void setBuiltObject(Object builtObject) { |
| 157 | if (isBuilt) { |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 158 | throw new YobException(E_OBJ_IS_ALREADY_BUILT_NOT_BUILD); |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | if (builderOrBuiltObject == null) { |
VinodKumarS-Huawei | 7b1733c | 2016-10-25 13:44:26 +0530 | [diff] [blame] | 162 | throw new YobException(E_OBJ_BUILDING_WITHOUT_BUILDER); |
Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | isBuilt = true; |
| 166 | builderOrBuiltObject = builtObject; |
| 167 | } |
| 168 | } |