Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +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 | package org.onosproject.yangutils.translator.tojava.javamodel; |
| 17 | |
| 18 | import org.onosproject.yangutils.datamodel.YangType; |
| 19 | import org.onosproject.yangutils.translator.exception.TranslatorException; |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 20 | import org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfoTranslator; |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame^] | 21 | import org.onosproject.yangutils.datamodel.javadatamodel.YangToJavaNamingConflictUtil; |
| 22 | |
| 23 | import static org.onosproject.yangutils.translator.tojava.javamodel.AttributesJavaDataType.getJavaDataType; |
| 24 | import static org.onosproject.yangutils.translator.tojava.javamodel.AttributesJavaDataType.getJavaImportClass; |
| 25 | import static org.onosproject.yangutils.translator.tojava.javamodel.AttributesJavaDataType.getJavaImportPackage; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 26 | |
| 27 | /** |
| 28 | * Represents java information corresponding to the YANG type. |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 29 | * |
| 30 | * @param <T> generic parameter for YANG java type |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 31 | */ |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 32 | public class YangJavaTypeTranslator<T> |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 33 | extends YangType<T> |
| 34 | implements JavaQualifiedTypeResolver { |
| 35 | |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame^] | 36 | private JavaQualifiedTypeInfoTranslator javaQualifiedAccess; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * Create a YANG leaf object with java qualified access details. |
| 40 | */ |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 41 | public YangJavaTypeTranslator() { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 42 | super(); |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 43 | setJavaQualifiedInfo(new JavaQualifiedTypeInfoTranslator()); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | @Override |
janani b | 3e357f6 | 2016-05-19 17:39:50 +0530 | [diff] [blame] | 47 | public void updateJavaQualifiedInfo(YangToJavaNamingConflictUtil conflictResolver) { |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 48 | JavaQualifiedTypeInfoTranslator importInfo = getJavaQualifiedInfo(); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 49 | |
| 50 | /* |
| 51 | * Type is added as an attribute in the class. |
| 52 | */ |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame^] | 53 | String className = getJavaImportClass(this, false, conflictResolver); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 54 | if (className != null) { |
| 55 | /* |
| 56 | * Corresponding to the attribute type a class needs to be imported, |
| 57 | * since it can be a derived type or a usage of wrapper classes. |
| 58 | */ |
| 59 | importInfo.setClassInfo(className); |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame^] | 60 | String classPkg = getJavaImportPackage(this, |
| 61 | false, conflictResolver); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 62 | if (classPkg == null) { |
| 63 | throw new TranslatorException("import package cannot be null when the class is used"); |
| 64 | } |
| 65 | importInfo.setPkgInfo(classPkg); |
| 66 | } else { |
| 67 | /* |
| 68 | * The attribute does not need a class to be imported, for example |
| 69 | * built in java types. |
| 70 | */ |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame^] | 71 | String dataTypeName = getJavaDataType(this); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 72 | if (dataTypeName == null) { |
| 73 | throw new TranslatorException("not supported data type"); |
| 74 | } |
| 75 | importInfo.setClassInfo(dataTypeName); |
| 76 | } |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 77 | setJavaQualifiedInfo(importInfo); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | @Override |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 81 | public JavaQualifiedTypeInfoTranslator getJavaQualifiedInfo() { |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame^] | 82 | return javaQualifiedAccess; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | @Override |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 86 | public void setJavaQualifiedInfo(JavaQualifiedTypeInfoTranslator typeInfo) { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 87 | javaQualifiedAccess = typeInfo; |
| 88 | } |
| 89 | } |