[ONOS-7302] Add implementation to obtain valueNamespace from LeafSchemaConext
Change-Id: I1e3aae1bbde32ec378bdc27afb13d15585dff55a
diff --git a/compiler/base/datamodel/src/main/java/org/onosproject/yang/compiler/datamodel/YangLeaf.java b/compiler/base/datamodel/src/main/java/org/onosproject/yang/compiler/datamodel/YangLeaf.java
index 3f7ede6..a82e9ce 100644
--- a/compiler/base/datamodel/src/main/java/org/onosproject/yang/compiler/datamodel/YangLeaf.java
+++ b/compiler/base/datamodel/src/main/java/org/onosproject/yang/compiler/datamodel/YangLeaf.java
@@ -19,7 +19,7 @@
import org.onosproject.yang.compiler.datamodel.exceptions.DataModelException;
import org.onosproject.yang.compiler.datamodel.utils.Parsable;
import org.onosproject.yang.compiler.datamodel.utils.YangConstructType;
-import org.onosproject.yang.compiler.datamodel.utils.builtindatatype.ObjectProvider;
+import org.onosproject.yang.compiler.datamodel.utils.builtindatatype.LeafContextUtil;
import org.onosproject.yang.model.DataNode.Type;
import org.onosproject.yang.model.LeafRestriction;
import org.onosproject.yang.model.LeafSchemaContext;
@@ -672,7 +672,12 @@
@Override
public Object fromString(String value) {
- return ObjectProvider.getObject(dataType, value, dataType.getDataType());
+ return LeafContextUtil.getObject(dataType, value, dataType.getDataType());
+ }
+
+ @Override
+ public String getValueNamespace(String value) {
+ return LeafContextUtil.getValueNamespace(dataType, value, dataType.getDataType());
}
@Override
diff --git a/compiler/base/datamodel/src/main/java/org/onosproject/yang/compiler/datamodel/YangLeafList.java b/compiler/base/datamodel/src/main/java/org/onosproject/yang/compiler/datamodel/YangLeafList.java
index c42a6bf..c44973a 100644
--- a/compiler/base/datamodel/src/main/java/org/onosproject/yang/compiler/datamodel/YangLeafList.java
+++ b/compiler/base/datamodel/src/main/java/org/onosproject/yang/compiler/datamodel/YangLeafList.java
@@ -19,7 +19,7 @@
import org.onosproject.yang.compiler.datamodel.exceptions.DataModelException;
import org.onosproject.yang.compiler.datamodel.utils.Parsable;
import org.onosproject.yang.compiler.datamodel.utils.YangConstructType;
-import org.onosproject.yang.compiler.datamodel.utils.builtindatatype.ObjectProvider;
+import org.onosproject.yang.compiler.datamodel.utils.builtindatatype.LeafContextUtil;
import org.onosproject.yang.model.DataNode;
import org.onosproject.yang.model.LeafRestriction;
import org.onosproject.yang.model.LeafSchemaContext;
@@ -666,7 +666,12 @@
@Override
public Object fromString(String value) {
- return ObjectProvider.getObject(dataType, value, dataType.getDataType());
+ return LeafContextUtil.getObject(dataType, value, dataType.getDataType());
+ }
+
+ @Override
+ public String getValueNamespace(String value) {
+ return LeafContextUtil.getValueNamespace(dataType, value, dataType.getDataType());
}
@Override
diff --git a/compiler/base/datamodel/src/main/java/org/onosproject/yang/compiler/datamodel/utils/builtindatatype/LeafContextUtil.java b/compiler/base/datamodel/src/main/java/org/onosproject/yang/compiler/datamodel/utils/builtindatatype/LeafContextUtil.java
new file mode 100644
index 0000000..71d1ccd
--- /dev/null
+++ b/compiler/base/datamodel/src/main/java/org/onosproject/yang/compiler/datamodel/utils/builtindatatype/LeafContextUtil.java
@@ -0,0 +1,220 @@
+/*
+ * Copyright 2016-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yang.compiler.datamodel.utils.builtindatatype;
+
+import org.onosproject.yang.compiler.datamodel.YangDerivedInfo;
+import org.onosproject.yang.compiler.datamodel.YangIdentity;
+import org.onosproject.yang.compiler.datamodel.YangIdentityRef;
+import org.onosproject.yang.compiler.datamodel.YangLeafRef;
+import org.onosproject.yang.compiler.datamodel.YangType;
+import org.onosproject.yang.compiler.datamodel.YangUnion;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.util.Iterator;
+
+public final class LeafContextUtil {
+
+ // Object provider constant error string.
+ private static final String E_DATATYPE = "Data type not supported.";
+ private static final String T = "true";
+ private static final String F = "false";
+
+ /**
+ * Restricts creation of object providers instance.
+ */
+ private LeafContextUtil() {
+ }
+
+ /**
+ * Returns object from string value.
+ *
+ * @param typeInfo refers to YANG type information
+ * @param v v argument is leaf value used to set the value in method
+ * @param dataType yang data type
+ * @return values object of corresponding given data type
+ * @throws IllegalArgumentException if input is not valid
+ */
+ public static Object getObject(YangType typeInfo, String v,
+ YangDataTypes dataType)
+ throws IllegalArgumentException {
+ YangDataTypes type;
+ if (dataType != null) {
+ type = dataType;
+ } else {
+ type = typeInfo.getDataType();
+ }
+ switch (type) {
+ case INT8:
+ return Byte.parseByte(v);
+ case UINT8:
+ case INT16:
+ return Short.parseShort(v);
+ case UINT16:
+ case INT32:
+ return Integer.parseInt(v);
+ case UINT32:
+ case INT64:
+ return Long.parseLong(v);
+ case UINT64:
+ return new BigInteger(v);
+ case EMPTY:
+ if (v == null || v.equals("")) {
+ return null;
+ } else if (v.equals(T) || v.equals(F)) {
+ return Boolean.parseBoolean(v);
+ }
+ case BOOLEAN:
+ if (v.equals(T) || v.equals(F)) {
+ return Boolean.parseBoolean(v);
+ }
+ throw new IllegalArgumentException(E_DATATYPE);
+ case BINARY:
+ case BITS:
+ case IDENTITYREF:
+ case ENUMERATION:
+ case STRING:
+ case INSTANCE_IDENTIFIER:
+ return v;
+ case DECIMAL64:
+ return new BigDecimal(v);
+ case LEAFREF:
+ YangType refType = ((YangLeafRef) typeInfo
+ .getDataTypeExtendedInfo()).getEffectiveDataType();
+ return getObject(refType, v, refType.getDataType());
+ case DERIVED:
+ // referred typedef's list of type will always has only one type
+ YangType rt = ((YangDerivedInfo) typeInfo
+ .getDataTypeExtendedInfo()).getReferredTypeDef()
+ .getTypeList().get(0);
+ return getObject(rt, v, rt.getDataType());
+ case UNION:
+ return parseUnionTypeInfo(typeInfo, v);
+ default:
+ throw new IllegalArgumentException(E_DATATYPE);
+ }
+ }
+
+ /**
+ * Returns value namespace from string value.
+ *
+ * @param typeInfo refers to YANG type information
+ * @param v v argument is leaf value used to set the value in method
+ * @param dataType yang data type
+ * @return value namespace of corresponding given data type and value
+ * @throws IllegalArgumentException if input is not valid
+ */
+ public static String getValueNamespace(YangType typeInfo, String v,
+ YangDataTypes dataType)
+ throws IllegalArgumentException {
+ YangDataTypes type;
+ if (dataType != null) {
+ type = dataType;
+ } else {
+ type = typeInfo.getDataType();
+ }
+ switch (type) {
+ case INT8:
+ case UINT8:
+ case INT16:
+ case UINT16:
+ case INT32:
+ case UINT32:
+ case INT64:
+ case UINT64:
+ case EMPTY:
+ case BOOLEAN:
+ case BINARY:
+ case BITS:
+ case ENUMERATION:
+ case STRING:
+ case DECIMAL64:
+ case INSTANCE_IDENTIFIER:
+ return null;
+ case IDENTITYREF:
+ YangIdentity refId = ((YangIdentityRef) typeInfo
+ .getDataTypeExtendedInfo()).getReferredIdentity();
+ return getReferIdNamespace(refId, v);
+ case LEAFREF:
+ YangType refType = ((YangLeafRef) typeInfo
+ .getDataTypeExtendedInfo()).getEffectiveDataType();
+ return getValueNamespace(refType, v, refType.getDataType());
+ case DERIVED:
+ // referred typedef's list of type will always has only one type
+ YangType rt = ((YangDerivedInfo) typeInfo
+ .getDataTypeExtendedInfo()).getReferredTypeDef()
+ .getTypeList().get(0);
+ return getValueNamespace(rt, v, rt.getDataType());
+ case UNION:
+ return getUnionValNamespace(typeInfo, v);
+ default:
+ throw new IllegalArgumentException(E_DATATYPE);
+ }
+ }
+
+ private static String getReferIdNamespace(YangIdentity refId, String v) {
+ String baseIdentity = refId.getYangSchemaNodeIdentifier().getName();
+ if (v.equals(baseIdentity)) {
+ return refId.getYangSchemaNodeIdentifier().getNameSpace()
+ .getModuleNamespace();
+ }
+ for (YangIdentity i : refId.getExtendList()) {
+ String refIdentity = i.getYangSchemaNodeIdentifier().getName();
+ if (v.equals(refIdentity)) {
+ return i.getYangSchemaNodeIdentifier().getNameSpace()
+ .getModuleNamespace();
+ }
+ }
+ throw new IllegalArgumentException("Invalid value of data");
+ }
+
+ private static String getUnionValNamespace(YangType type, String leafValue) {
+ Iterator<YangType<?>> it = ((YangUnion) type.getDataTypeExtendedInfo())
+ .getTypeList().listIterator();
+ while (it.hasNext()) {
+ YangType t = it.next();
+ try {
+ getObject(t, leafValue, t.getDataType());
+ return getValueNamespace(t, leafValue, t.getDataType());
+ } catch (IllegalArgumentException e) {
+ continue;
+ }
+ }
+ throw new IllegalArgumentException("Invalid value of data");
+ }
+
+ /**
+ * Returns the object for given data type with respective value.
+ *
+ * @param type data type of value
+ * @param leafValue value
+ * @return object of data type containing the value
+ */
+ private static Object parseUnionTypeInfo(YangType type, String leafValue) {
+ Iterator<YangType<?>> it = ((YangUnion) type.getDataTypeExtendedInfo())
+ .getTypeList().listIterator();
+ while (it.hasNext()) {
+ YangType t = it.next();
+ try {
+ return getObject(t, leafValue, t.getDataType());
+ } catch (IllegalArgumentException e) {
+ continue;
+ }
+ }
+ throw new IllegalArgumentException("Invalid value of data");
+ }
+}
diff --git a/compiler/base/datamodel/src/main/java/org/onosproject/yang/compiler/datamodel/utils/builtindatatype/ObjectProvider.java b/compiler/base/datamodel/src/main/java/org/onosproject/yang/compiler/datamodel/utils/builtindatatype/ObjectProvider.java
deleted file mode 100644
index 72221c4..0000000
--- a/compiler/base/datamodel/src/main/java/org/onosproject/yang/compiler/datamodel/utils/builtindatatype/ObjectProvider.java
+++ /dev/null
@@ -1,131 +0,0 @@
-/*
- * Copyright 2016-present Open Networking Foundation
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.onosproject.yang.compiler.datamodel.utils.builtindatatype;
-
-import org.onosproject.yang.compiler.datamodel.YangDerivedInfo;
-import org.onosproject.yang.compiler.datamodel.YangLeafRef;
-import org.onosproject.yang.compiler.datamodel.YangType;
-import org.onosproject.yang.compiler.datamodel.YangUnion;
-
-import java.math.BigDecimal;
-import java.math.BigInteger;
-import java.util.Iterator;
-
-public final class ObjectProvider {
-
- // Object provider constant error string.
- private static final String E_DATATYPE = "Data type not supported.";
- private static final String T = "true";
- private static final String F = "false";
- private static final String E_NONEMPTY = "Value is of non Empty type";
-
- /**
- * Restricts creation of object providers instance.
- */
- private ObjectProvider() {
- }
-
- /**
- * Returns object from string value.
- *
- * @param typeInfo refers to YANG type information
- * @param v v argument is leaf value used to set the value in method
- * @param dataType yang data type
- * @return values object of corresponding given data type
- * @throws IllegalArgumentException if input is not valid
- */
- public static Object getObject(YangType typeInfo, String v,
- YangDataTypes dataType)
- throws IllegalArgumentException {
- YangDataTypes type;
- if (dataType != null) {
- type = dataType;
- } else {
- type = typeInfo.getDataType();
- }
- switch (type) {
- case INT8:
- return Byte.parseByte(v);
- case UINT8:
- case INT16:
- return Short.parseShort(v);
- case UINT16:
- case INT32:
- return Integer.parseInt(v);
- case UINT32:
- case INT64:
- return Long.parseLong(v);
- case UINT64:
- return new BigInteger(v);
- case EMPTY:
- if (v == null || v.equals("")) {
- return null;
- } else if (v.equals(T) || v.equals(F)) {
- return Boolean.parseBoolean(v);
- }
- case BOOLEAN:
- if (v.equals(T) || v.equals(F)) {
- return Boolean.parseBoolean(v);
- }
- throw new IllegalArgumentException(E_DATATYPE);
- case BINARY:
- case BITS:
- case IDENTITYREF:
- case ENUMERATION:
- case STRING:
- case INSTANCE_IDENTIFIER:
- return v;
- case DECIMAL64:
- return new BigDecimal(v);
- case LEAFREF:
- YangType refType = ((YangLeafRef) typeInfo
- .getDataTypeExtendedInfo()).getEffectiveDataType();
- return getObject(refType, v, refType.getDataType());
- case DERIVED:
- // referred typedef's list of type will always has only one type
- YangType rt = ((YangDerivedInfo) typeInfo
- .getDataTypeExtendedInfo()).getReferredTypeDef()
- .getTypeList().get(0);
- return getObject(rt, v, rt.getDataType());
- case UNION:
- return parseUnionTypeInfo(typeInfo, v);
- default:
- throw new IllegalArgumentException(E_DATATYPE);
- }
- }
-
- /**
- * Returns the object for given data type with respective value.
- *
- * @param type data type of value
- * @param leafValue value
- * @return object of data type containing the value
- */
- private static Object parseUnionTypeInfo(YangType type, String leafValue) {
- Iterator<YangType<?>> it = ((YangUnion) type.getDataTypeExtendedInfo())
- .getTypeList().listIterator();
- while (it.hasNext()) {
- YangType t = it.next();
- try {
- return getObject(t, leafValue, t.getDataType());
- } catch (IllegalArgumentException e) {
- continue;
- }
- }
- throw new IllegalArgumentException("Invalid value of data");
- }
-}
diff --git a/model/src/main/java/org/onosproject/yang/model/LeafSchemaContext.java b/model/src/main/java/org/onosproject/yang/model/LeafSchemaContext.java
index ff3bfab..bf93ad2 100644
--- a/model/src/main/java/org/onosproject/yang/model/LeafSchemaContext.java
+++ b/model/src/main/java/org/onosproject/yang/model/LeafSchemaContext.java
@@ -47,4 +47,13 @@
* schema
*/
Object fromString(String value);
+
+ /**
+ * Returns value namespace.
+ *
+ * @param value leaf value in string
+ * @return value namespace, null indicates that value namespace is same
+ * as leaf namespace
+ */
+ String getValueNamespace(String value);
}