blob: d8fe5174743b5a93dc260c5f44fa03495be02045 [file] [log] [blame]
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +05301/*
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 */
16package org.onosproject.yangutils.translator.tojava.javamodel;
17
18import org.onosproject.yangutils.datamodel.YangType;
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +053019import org.onosproject.yangutils.datamodel.javadatamodel.JavaQualifiedTypeInfo;
20import org.onosproject.yangutils.datamodel.javadatamodel.YangToJavaNamingConflictUtil;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053021import org.onosproject.yangutils.translator.exception.TranslatorException;
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +053022import org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfoTranslator;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053023
24/**
25 * Represents java information corresponding to the YANG type.
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053026 *
27 * @param <T> generic parameter for YANG java type
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053028 */
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +053029public class YangJavaTypeTranslator<T>
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053030 extends YangType<T>
31 implements JavaQualifiedTypeResolver {
32
33 private JavaQualifiedTypeInfo javaQualifiedAccess;
34
35 /**
36 * Create a YANG leaf object with java qualified access details.
37 */
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +053038 public YangJavaTypeTranslator() {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053039 super();
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +053040 setJavaQualifiedInfo(new JavaQualifiedTypeInfoTranslator());
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053041 }
42
43 @Override
janani bdd1314f2016-05-19 17:39:50 +053044 public void updateJavaQualifiedInfo(YangToJavaNamingConflictUtil conflictResolver) {
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +053045 JavaQualifiedTypeInfoTranslator importInfo = getJavaQualifiedInfo();
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053046
47 /*
48 * Type is added as an attribute in the class.
49 */
janani bdd1314f2016-05-19 17:39:50 +053050 String className = AttributesJavaDataType.getJavaImportClass(this, false, conflictResolver);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053051 if (className != null) {
52 /*
53 * Corresponding to the attribute type a class needs to be imported,
54 * since it can be a derived type or a usage of wrapper classes.
55 */
56 importInfo.setClassInfo(className);
57 String classPkg = AttributesJavaDataType.getJavaImportPackage(this,
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +053058 false, conflictResolver);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053059 if (classPkg == null) {
60 throw new TranslatorException("import package cannot be null when the class is used");
61 }
62 importInfo.setPkgInfo(classPkg);
63 } else {
64 /*
65 * The attribute does not need a class to be imported, for example
66 * built in java types.
67 */
68 String dataTypeName = AttributesJavaDataType.getJavaDataType(this);
69 if (dataTypeName == null) {
70 throw new TranslatorException("not supported data type");
71 }
72 importInfo.setClassInfo(dataTypeName);
73 }
Bharat saraswalc0e04842016-05-12 13:16:57 +053074 setJavaQualifiedInfo(importInfo);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053075 }
76
77 @Override
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +053078 public JavaQualifiedTypeInfoTranslator getJavaQualifiedInfo() {
79 return (JavaQualifiedTypeInfoTranslator) javaQualifiedAccess;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053080 }
81
82 @Override
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +053083 public void setJavaQualifiedInfo(JavaQualifiedTypeInfoTranslator typeInfo) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053084 javaQualifiedAccess = typeInfo;
85 }
86}