blob: 6ac0fb70ea4f11f5483bf288418cae32f703d5c4 [file] [log] [blame]
Vinod Kumar S9f26ae52016-03-23 15:30:27 +05301/*
Brian O'Connor0f7908b2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Vinod Kumar S9f26ae52016-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
18import java.io.IOException;
Bharat saraswale2bc60d2016-04-16 02:28:25 +053019
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053020import org.onosproject.yangutils.datamodel.YangAugment;
Bharat saraswal780eca32016-04-05 12:45:45 +053021import org.onosproject.yangutils.translator.exception.TranslatorException;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053022import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator;
23import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053024import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
janani b1c6acc42016-04-15 16:18:30 +053025import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053026
27import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_INTERFACE_WITH_BUILDER;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053028import static org.onosproject.yangutils.translator.tojava.utils.YangJavaModelUtils.generateCodeOfAugmentableNode;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053029
30/**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053031 * Represents augment information extended to support java code generation.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053032 */
Vinod Kumar S79a374b2016-04-30 21:09:15 +053033public class YangJavaAugment
34 extends YangAugment
35 implements JavaCodeGeneratorInfo, JavaCodeGenerator {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053036
Bharat saraswalc2d3be12016-06-16 00:29:12 +053037 private static final long serialVersionUID = 806201632L;
38
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053039 /**
40 * Contains the information of the java file being generated.
41 */
42 private JavaFileInfo javaFileInfo;
43
44 /**
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053045 * File handle to maintain temporary java code fragments as per the code
46 * snippet types.
47 */
Bharat saraswalc2d3be12016-06-16 00:29:12 +053048 private transient TempJavaCodeFragmentFiles tempFileHandle;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053049
50 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053051 * Creates a YANG java augment object.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053052 */
53 public YangJavaAugment() {
54 super();
55 setJavaFileInfo(new JavaFileInfo());
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053056 getJavaFileInfo().setGeneratedFileTypes(GENERATE_INTERFACE_WITH_BUILDER);
57 }
58
59 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053060 * Returns the generated java file information.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053061 *
62 * @return generated java file information
63 */
64 @Override
65 public JavaFileInfo getJavaFileInfo() {
66
67 if (javaFileInfo == null) {
Bharat saraswal780eca32016-04-05 12:45:45 +053068 throw new TranslatorException("Missing java info in java datamodel node");
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053069 }
70 return javaFileInfo;
71 }
72
73 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053074 * Sets the java file info object.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053075 *
76 * @param javaInfo java file info object
77 */
78 @Override
79 public void setJavaFileInfo(JavaFileInfo javaInfo) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053080 javaFileInfo = javaInfo;
81 }
82
83 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053084 * Returns the temporary file handle.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053085 *
86 * @return temporary file handle
87 */
88 @Override
89 public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053090 return tempFileHandle;
91 }
92
93 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053094 * Sets temporary file handle.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053095 *
96 * @param fileHandle temporary file handle
97 */
98 @Override
99 public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530100 tempFileHandle = fileHandle;
101 }
102
103 /**
104 * Prepare the information for java code generation corresponding to YANG
105 * augment info.
106 *
janani b1c6acc42016-04-15 16:18:30 +0530107 * @param yangPlugin YANG plugin config
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530108 * @throws TranslatorException translator operation fail
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530109 */
110 @Override
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530111 public void generateCodeEntry(YangPluginConfig yangPlugin) throws TranslatorException {
112 try {
113 generateCodeOfAugmentableNode(this, yangPlugin);
114 } catch (IOException e) {
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530115 throw new TranslatorException("Failed to generate code for augmentable node " + getName());
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530116 }
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530117 }
118
119 /**
Bharat saraswale2bc60d2016-04-16 02:28:25 +0530120 * Create a java file using the YANG augment info.
121 *
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530122 * @throws TranslatorException when failed to do translator operations
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530123 */
124 @Override
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530125 public void generateCodeExit() throws TranslatorException {
126 try {
127 getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_INTERFACE_WITH_BUILDER, this);
128 } catch (IOException e) {
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530129 throw new TranslatorException("Failed to generate code for augmentable node " + getName());
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530130 }
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530131 }
132}