blob: 93e48587e5b672325c51c7f168ee10edf16f2ffa [file] [log] [blame]
Vinod Kumar S79a374b2016-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
Bharat saraswal2da23bf2016-08-25 15:28:39 +053018import org.onosproject.yangutils.datamodel.javadatamodel.YangJavaType;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053019import org.onosproject.yangutils.translator.exception.TranslatorException;
Shankara-Huaweib7564772016-08-02 18:13:13 +053020import org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfoTranslator;
Bharat saraswale50edca2016-08-05 01:58:25 +053021import org.onosproject.yangutils.utils.io.YangToJavaNamingConflictUtil;
Bharat saraswal8beac342016-08-04 02:00:03 +053022
23import static org.onosproject.yangutils.translator.tojava.javamodel.AttributesJavaDataType.getJavaDataType;
24import static org.onosproject.yangutils.translator.tojava.javamodel.AttributesJavaDataType.getJavaImportClass;
25import static org.onosproject.yangutils.translator.tojava.javamodel.AttributesJavaDataType.getJavaImportPackage;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053026
27/**
28 * Represents java information corresponding to the YANG type.
29 */
Bharat saraswal2da23bf2016-08-25 15:28:39 +053030public class YangJavaTypeTranslator
31 extends YangJavaType
Vinod Kumar S79a374b2016-04-30 21:09:15 +053032 implements JavaQualifiedTypeResolver {
33
Vinod Kumar S79a374b2016-04-30 21:09:15 +053034 /**
35 * Create a YANG leaf object with java qualified access details.
36 */
Shankara-Huaweib7564772016-08-02 18:13:13 +053037 public YangJavaTypeTranslator() {
Vinod Kumar S79a374b2016-04-30 21:09:15 +053038 super();
Shankara-Huaweib7564772016-08-02 18:13:13 +053039 setJavaQualifiedInfo(new JavaQualifiedTypeInfoTranslator());
Vinod Kumar S79a374b2016-04-30 21:09:15 +053040 }
41
42 @Override
janani b3e357f62016-05-19 17:39:50 +053043 public void updateJavaQualifiedInfo(YangToJavaNamingConflictUtil conflictResolver) {
Bharat saraswal2da23bf2016-08-25 15:28:39 +053044 JavaQualifiedTypeInfoTranslator importInfo = (JavaQualifiedTypeInfoTranslator) getJavaQualifiedInfo();
Vinod Kumar S79a374b2016-04-30 21:09:15 +053045
46 /*
47 * Type is added as an attribute in the class.
48 */
Bharat saraswal8beac342016-08-04 02:00:03 +053049 String className = getJavaImportClass(this, false, conflictResolver);
Vinod Kumar S79a374b2016-04-30 21:09:15 +053050 if (className != null) {
51 /*
52 * Corresponding to the attribute type a class needs to be imported,
53 * since it can be a derived type or a usage of wrapper classes.
54 */
55 importInfo.setClassInfo(className);
Bharat saraswal8beac342016-08-04 02:00:03 +053056 String classPkg = getJavaImportPackage(this,
Bharat saraswal2da23bf2016-08-25 15:28:39 +053057 false, conflictResolver);
Vinod Kumar S79a374b2016-04-30 21:09:15 +053058 if (classPkg == null) {
Bharat saraswale3175d32016-08-31 17:50:11 +053059 throw new TranslatorException("import package cannot be null when the class is used " +
60 getDataTypeName() + " in " +
61 getLineNumber() + " at " +
62 getCharPosition()
63 + " in " + getFileName());
Vinod Kumar S79a374b2016-04-30 21:09:15 +053064 }
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 saraswal8beac342016-08-04 02:00:03 +053071 String dataTypeName = getJavaDataType(this);
Vinod Kumar S79a374b2016-04-30 21:09:15 +053072 if (dataTypeName == null) {
Bharat saraswale3175d32016-08-31 17:50:11 +053073 throw new TranslatorException("not supported data type " +
74 getDataTypeName() + " in " +
75 getLineNumber() + " at " +
76 getCharPosition()
77 + " in " + getFileName());
Vinod Kumar S79a374b2016-04-30 21:09:15 +053078 }
79 importInfo.setClassInfo(dataTypeName);
80 }
Bharat saraswal250a7472016-05-12 13:16:57 +053081 setJavaQualifiedInfo(importInfo);
Vinod Kumar S79a374b2016-04-30 21:09:15 +053082 }
83
Vinod Kumar S79a374b2016-04-30 21:09:15 +053084}