blob: 71900c232c67eb47cdb479cdd86781aa9bc84521 [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
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053019import org.onosproject.yms.app.yob.exception.YobException;
Rama-Huaweib711e5c2016-08-31 07:55:46 +053020import org.slf4j.Logger;
21import org.slf4j.LoggerFactory;
22
Vidyashree Rama6160be12016-11-24 13:43:31 +053023import static org.onosproject.yms.app.yob.YobConstants.E_BUILDER_IS_NOT_ALREADY_SET;
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053024import static org.onosproject.yms.app.yob.YobConstants.E_BUILDER_IS_NOT_SET;
25import static org.onosproject.yms.app.yob.YobConstants.E_BUILT_OBJ_IS_NOT_SET;
26import static org.onosproject.yms.app.yob.YobConstants.E_FAIL_TO_CREATE_OBJ;
27import static org.onosproject.yms.app.yob.YobConstants.E_FAIL_TO_LOAD_CLASS;
28import static org.onosproject.yms.app.yob.YobConstants.E_OBJ_BUILDING_WITHOUT_BUILDER;
29import static org.onosproject.yms.app.yob.YobConstants.E_OBJ_IS_ALREADY_BUILT_NOT_BUILD;
30import static org.onosproject.yms.app.yob.YobConstants.E_OBJ_IS_ALREADY_BUILT_NOT_FETCH;
Vidyashree Rama6160be12016-11-24 13:43:31 +053031import static org.onosproject.yms.app.yob.YobConstants.E_OBJ_IS_ALREADY_BUILT_NOT_SET;
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053032import static org.onosproject.yms.app.yob.YobConstants.E_OBJ_IS_NOT_SET_NOT_FETCH;
33import static org.onosproject.yms.app.yob.YobConstants.E_REFLECTION_FAIL_TO_CREATE_OBJ;
34import static org.onosproject.yms.app.yob.YobConstants.L_FAIL_TO_CREATE_OBJ;
35import static org.onosproject.yms.app.yob.YobConstants.L_FAIL_TO_LOAD_CLASS;
36import static org.onosproject.yms.app.yob.YobConstants.L_REFLECTION_FAIL_TO_CREATE_OBJ;
Rama-Huaweib711e5c2016-08-31 07:55:46 +053037
38/**
39 * Represents the container of YANG object being built or the builder.
40 */
41class YobBuilderOrBuiltObject {
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053042 private static final Logger log =
43 LoggerFactory.getLogger(YobWorkBench.class);
Rama-Huaweib711e5c2016-08-31 07:55:46 +053044
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-Huawei7b1733c2016-10-25 13:44:26 +053065 /**
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-Huaweib711e5c2016-08-31 07:55:46 +053072 YobBuilderOrBuiltObject(String qualifiedClassName,
73 ClassLoader registeredAppClassLoader) {
74 try {
75 yangDefaultClass =
76 registeredAppClassLoader.loadClass(qualifiedClassName);
77 yangBuilderClass = yangDefaultClass.getDeclaredClasses()[0];
Vidyashree Rama6160be12016-11-24 13:43:31 +053078 setBuilderObject(yangBuilderClass.newInstance());
Rama-Huaweib711e5c2016-08-31 07:55:46 +053079 } catch (ClassNotFoundException e) {
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053080 log.error(L_FAIL_TO_LOAD_CLASS, qualifiedClassName);
81 throw new YobException(E_FAIL_TO_LOAD_CLASS + qualifiedClassName);
Rama-Huaweib711e5c2016-08-31 07:55:46 +053082 } catch (InstantiationException | IllegalAccessException e) {
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053083 log.error(L_FAIL_TO_CREATE_OBJ, qualifiedClassName);
84 throw new YobException(E_FAIL_TO_CREATE_OBJ + qualifiedClassName);
Rama-Huaweib711e5c2016-08-31 07:55:46 +053085 } catch (NullPointerException e) {
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +053086 log.error(L_REFLECTION_FAIL_TO_CREATE_OBJ, qualifiedClassName);
87 throw new YobException(E_REFLECTION_FAIL_TO_CREATE_OBJ +
88 qualifiedClassName);
Rama-Huaweib711e5c2016-08-31 07:55:46 +053089 }
90 }
91
92 /**
93 * Returns the builder object if it is set.
94 *
95 * @return builder object
Vidyashree Rama6160be12016-11-24 13:43:31 +053096 * @throws YobException if builder is not available
Rama-Huaweib711e5c2016-08-31 07:55:46 +053097 */
98 Object getBuilderObject() {
99 if (isBuilt) {
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +0530100 throw new YobException(E_OBJ_IS_ALREADY_BUILT_NOT_FETCH);
Rama-Huaweib711e5c2016-08-31 07:55:46 +0530101 }
102
103 if (builderOrBuiltObject == null) {
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +0530104 throw new YobException(E_BUILDER_IS_NOT_SET);
Rama-Huaweib711e5c2016-08-31 07:55:46 +0530105 }
106
107 return builderOrBuiltObject;
108 }
109
110 /**
Vidyashree Rama6160be12016-11-24 13:43:31 +0530111 * 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-Huaweib711e5c2016-08-31 07:55:46 +0530130 * Returns the built object.
131 *
132 * @return built object
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +0530133 * @throws YobException if built object is not available or if it is not
134 * built
Rama-Huaweib711e5c2016-08-31 07:55:46 +0530135 */
136 Object getBuiltObject() {
137 if (!isBuilt) {
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +0530138 throw new YobException(E_OBJ_IS_NOT_SET_NOT_FETCH);
Rama-Huaweib711e5c2016-08-31 07:55:46 +0530139 }
140
141 if (builderOrBuiltObject == null) {
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +0530142 throw new YobException(E_BUILT_OBJ_IS_NOT_SET);
Rama-Huaweib711e5c2016-08-31 07:55:46 +0530143 }
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-Huawei7b1733c2016-10-25 13:44:26 +0530153 * @throws YobException if builder object is not available or if it is
154 * already built
Rama-Huaweib711e5c2016-08-31 07:55:46 +0530155 */
156 void setBuiltObject(Object builtObject) {
157 if (isBuilt) {
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +0530158 throw new YobException(E_OBJ_IS_ALREADY_BUILT_NOT_BUILD);
Rama-Huaweib711e5c2016-08-31 07:55:46 +0530159 }
160
161 if (builderOrBuiltObject == null) {
VinodKumarS-Huawei7b1733c2016-10-25 13:44:26 +0530162 throw new YobException(E_OBJ_BUILDING_WITHOUT_BUILDER);
Rama-Huaweib711e5c2016-08-31 07:55:46 +0530163 }
164
165 isBuilt = true;
166 builderOrBuiltObject = builtObject;
167 }
168}