Rama-Huawei | b711e5c | 2016-08-31 07:55:46 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 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.yob.exception.YobExceptions; |
| 20 | import org.slf4j.Logger; |
| 21 | import org.slf4j.LoggerFactory; |
| 22 | |
| 23 | import static org.onosproject.yms.app.yob.YobConstants.BUILDER_IS_NOT_SET; |
| 24 | import static org.onosproject.yms.app.yob.YobConstants.BUILT_OBJ_IS_NOT_SET; |
| 25 | import static org.onosproject.yms.app.yob.YobConstants.FAIL_TO_CREATE_OBJ; |
| 26 | import static org.onosproject.yms.app.yob.YobConstants.FAIL_TO_LOAD_CLASS; |
| 27 | import static org.onosproject.yms.app.yob.YobConstants.OBJ_BUILDING_WITHOUT_BUILDER; |
| 28 | import static org.onosproject.yms.app.yob.YobConstants.OBJ_IS_ALREADY_BUILT_NOT_BUILD; |
| 29 | import static org.onosproject.yms.app.yob.YobConstants.OBJ_IS_ALREADY_BUILT_NOT_FETCH; |
| 30 | import static org.onosproject.yms.app.yob.YobConstants.OBJ_IS_NOT_SET_NOT_FETCH; |
| 31 | import static org.onosproject.yms.app.yob.YobConstants.REFLECTION_FAIL_TO_CREATE_OBJ; |
| 32 | |
| 33 | /** |
| 34 | * Represents the container of YANG object being built or the builder. |
| 35 | */ |
| 36 | class YobBuilderOrBuiltObject { |
| 37 | private static final Logger log = LoggerFactory.getLogger(YobWorkBench.class); |
| 38 | |
| 39 | /** |
| 40 | * Is the contained object a built object. |
| 41 | */ |
| 42 | private boolean isBuilt; |
| 43 | |
| 44 | /** |
| 45 | * Builder or built object. |
| 46 | */ |
| 47 | private Object builderOrBuiltObject; |
| 48 | |
| 49 | /** |
| 50 | * Default / op param builder class. |
| 51 | */ |
| 52 | Class<?> yangBuilderClass; |
| 53 | |
| 54 | /** |
| 55 | * Default Class. |
| 56 | */ |
| 57 | Class<?> yangDefaultClass; |
| 58 | |
| 59 | YobBuilderOrBuiltObject(String qualifiedClassName, |
| 60 | ClassLoader registeredAppClassLoader) { |
| 61 | try { |
| 62 | yangDefaultClass = |
| 63 | registeredAppClassLoader.loadClass(qualifiedClassName); |
| 64 | yangBuilderClass = yangDefaultClass.getDeclaredClasses()[0]; |
| 65 | builderOrBuiltObject = yangBuilderClass.newInstance(); |
| 66 | } catch (ClassNotFoundException e) { |
| 67 | log.error(FAIL_TO_LOAD_CLASS + qualifiedClassName); |
| 68 | } catch (InstantiationException | IllegalAccessException e) { |
| 69 | log.error(FAIL_TO_CREATE_OBJ + qualifiedClassName); |
| 70 | } catch (NullPointerException e) { |
| 71 | log.error(REFLECTION_FAIL_TO_CREATE_OBJ + qualifiedClassName); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Returns the builder object if it is set. |
| 77 | * |
| 78 | * @return builder object |
| 79 | * @throws YobExceptions if builder object is not available |
| 80 | */ |
| 81 | Object getBuilderObject() { |
| 82 | if (isBuilt) { |
| 83 | throw new YobExceptions(OBJ_IS_ALREADY_BUILT_NOT_FETCH); |
| 84 | } |
| 85 | |
| 86 | if (builderOrBuiltObject == null) { |
| 87 | throw new YobExceptions(BUILDER_IS_NOT_SET); |
| 88 | } |
| 89 | |
| 90 | return builderOrBuiltObject; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Returns the built object. |
| 95 | * |
| 96 | * @return built object |
| 97 | * @throws YobExceptions if built object is not available |
| 98 | */ |
| 99 | Object getBuiltObject() { |
| 100 | if (!isBuilt) { |
| 101 | throw new YobExceptions(OBJ_IS_NOT_SET_NOT_FETCH); |
| 102 | } |
| 103 | |
| 104 | if (builderOrBuiltObject == null) { |
| 105 | throw new YobExceptions(BUILT_OBJ_IS_NOT_SET); |
| 106 | } |
| 107 | |
| 108 | return builderOrBuiltObject; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Check if the built object is being initialized for the 1st time and |
| 113 | * set it. |
| 114 | * |
| 115 | * @param builtObject new built object |
| 116 | * @throws YobExceptions if builder or built object is not available |
| 117 | */ |
| 118 | void setBuiltObject(Object builtObject) { |
| 119 | if (isBuilt) { |
| 120 | throw new YobExceptions(OBJ_IS_ALREADY_BUILT_NOT_BUILD); |
| 121 | } |
| 122 | |
| 123 | if (builderOrBuiltObject == null) { |
| 124 | throw new YobExceptions(OBJ_BUILDING_WITHOUT_BUILDER); |
| 125 | } |
| 126 | |
| 127 | isBuilt = true; |
| 128 | builderOrBuiltObject = builtObject; |
| 129 | } |
| 130 | } |