blob: f647c0dffef1e58aa24e8b79924bb928a328b1d1 [file] [log] [blame]
Vinod Kumar S38046502016-03-23 15:30:27 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Vinod Kumar S38046502016-03-23 15:30:27 +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 */
16package org.onosproject.yangutils.translator.tojava.javamodel;
17
Bharat saraswalcad0e652016-05-26 23:48:38 +053018import java.io.IOException;
19
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053020import org.onosproject.yangutils.datamodel.YangGrouping;
Bharat saraswalcad0e652016-05-26 23:48:38 +053021import org.onosproject.yangutils.translator.exception.TranslatorException;
22import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator;
23import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
24import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
25import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig;
26
27import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_INTERFACE_WITH_BUILDER;
28import static org.onosproject.yangutils.translator.tojava.utils.YangJavaModelUtils.generateCodeOfNode;
Vinod Kumar S38046502016-03-23 15:30:27 +053029
30/**
Bharat saraswald9822e92016-04-05 15:13:44 +053031 * Represents grouping information extended to support java code generation.
Vinod Kumar S38046502016-03-23 15:30:27 +053032 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053033public class YangJavaGrouping
Bharat saraswalcad0e652016-05-26 23:48:38 +053034 extends YangGrouping implements JavaCodeGeneratorInfo, JavaCodeGenerator {
35
36 /**
37 * Contains the information of the java file being generated.
38 */
39 private JavaFileInfo javaFileInfo;
40
41 /**
42 * File handle to maintain temporary java code fragments as per the code
43 * snippet types.
44 */
45 private TempJavaCodeFragmentFiles tempFileHandle;
Vinod Kumar S38046502016-03-23 15:30:27 +053046
47 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053048 * Creates YANG Java grouping object.
Vinod Kumar S38046502016-03-23 15:30:27 +053049 */
50 public YangJavaGrouping() {
51 super();
Bharat saraswalcad0e652016-05-26 23:48:38 +053052 setJavaFileInfo(new JavaFileInfo());
53 getJavaFileInfo().setGeneratedFileTypes(GENERATE_INTERFACE_WITH_BUILDER);
Vinod Kumar S38046502016-03-23 15:30:27 +053054 }
Bharat saraswalcad0e652016-05-26 23:48:38 +053055
56 /**
57 * Returns the generated java file information.
58 *
59 * @return generated java file information
60 */
61 @Override
62 public JavaFileInfo getJavaFileInfo() {
63 if (javaFileInfo == null) {
64 throw new TranslatorException("Missing java info in java datamodel node");
65 }
66 return javaFileInfo;
67 }
68
69 /**
70 * Sets the java file info object.
71 *
72 * @param javaInfo java file info object
73 */
74 @Override
75 public void setJavaFileInfo(JavaFileInfo javaInfo) {
76 javaFileInfo = javaInfo;
77 }
78
79 /**
80 * Returns the temporary file handle.
81 *
82 * @return temporary file handle
83 */
84 @Override
85 public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() {
86 return tempFileHandle;
87 }
88
89 /**
90 * Sets temporary file handle.
91 *
92 * @param fileHandle temporary file handle
93 */
94 @Override
95 public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) {
96 tempFileHandle = fileHandle;
97 }
98
99 /**
100 * Prepare the information for java code generation corresponding to YANG
101 * grouping info.
102 *
103 * @param yangPlugin YANG plugin config
104 * @throws TranslatorException translator operation fail
105 */
106 @Override
107 public void generateCodeEntry(YangPluginConfig yangPlugin) throws TranslatorException {
108 try {
109 generateCodeOfNode(this, yangPlugin);
110 } catch (IOException e) {
111 throw new TranslatorException(
112 "Failed to prepare generate code entry for container node " + this.getName());
113 }
114 }
115
116 /**
117 * Create a java file using the YANG grouping info.
118 *
119 * @throws TranslatorException translator operation fail
120 */
121 @Override
122 public void generateCodeExit() throws TranslatorException {
123 try {
124 getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_INTERFACE_WITH_BUILDER, this);
125 } catch (IOException e) {
126 throw new TranslatorException("Failed to generate code for container node " + this.getName());
127 }
128 }
129
Vinod Kumar S38046502016-03-23 15:30:27 +0530130}