blob: a44164fd661433e7b7b51884377e6e395702eacf [file] [log] [blame]
Vidyashree Rama620b6e92016-03-28 11:59:27 +05301/*
Brian O'Connor0f7908b2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Vidyashree Rama620b6e92016-03-28 11:59: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 */
16
17package org.onosproject.yangutils.translator.tojava.javamodel;
18
19import java.io.IOException;
Gaurav Agrawal61edaa02016-08-31 16:52:56 +053020import org.onosproject.yangutils.datamodel.RpcNotificationContainer;
21import org.onosproject.yangutils.datamodel.YangLeavesHolder;
Bharat saraswal4aaab4d2016-05-17 14:19:38 +053022import org.onosproject.yangutils.datamodel.YangNode;
Shankara-Huaweib7564772016-08-02 18:13:13 +053023import org.onosproject.yangutils.datamodel.javadatamodel.YangJavaNotification;
Bharat saraswal63f26fb2016-04-05 15:13:44 +053024import org.onosproject.yangutils.translator.exception.TranslatorException;
Vidyashree Rama620b6e92016-03-28 11:59:27 +053025import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator;
Gaurav Agrawalbfce9342016-06-15 13:58:01 +053026import org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorInfo;
Bharat saraswal4aaab4d2016-05-17 14:19:38 +053027import org.onosproject.yangutils.translator.tojava.JavaFileInfoContainer;
Gaurav Agrawal61edaa02016-08-31 16:52:56 +053028import org.onosproject.yangutils.translator.tojava.JavaFileInfoTranslator;
Shankara-Huaweib7564772016-08-02 18:13:13 +053029import org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfoTranslator;
Vidyashree Rama620b6e92016-03-28 11:59:27 +053030import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
Bharat saraswal4aaab4d2016-05-17 14:19:38 +053031import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFilesContainer;
Bharat saraswalaf413b82016-07-14 15:18:20 +053032import org.onosproject.yangutils.translator.tojava.TempJavaServiceFragmentFiles;
Bharat saraswal4aaab4d2016-05-17 14:19:38 +053033import org.onosproject.yangutils.translator.tojava.utils.JavaExtendsListHolder;
Gaurav Agrawal61edaa02016-08-31 16:52:56 +053034import org.onosproject.yangutils.utils.io.YangPluginConfig;
Vidyashree Rama620b6e92016-03-28 11:59:27 +053035
36import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_INTERFACE_WITH_BUILDER;
Bharat saraswalaf413b82016-07-14 15:18:20 +053037import static org.onosproject.yangutils.translator.tojava.YangJavaModelUtils.generateCodeOfAugmentableNode;
Gaurav Agrawal61edaa02016-08-31 16:52:56 +053038import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getEnumJavaAttribute;
Bharat saraswal4aaab4d2016-05-17 14:19:38 +053039import static org.onosproject.yangutils.utils.UtilConstants.EVENT_LISTENER_STRING;
40import static org.onosproject.yangutils.utils.UtilConstants.EVENT_STRING;
Shankara-Huaweib7564772016-08-02 18:13:13 +053041import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCapitalCase;
Vidyashree Rama620b6e92016-03-28 11:59:27 +053042
43/**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053044 * Represents notification information extended to support java code generation.
Vidyashree Rama620b6e92016-03-28 11:59:27 +053045 */
Shankara-Huaweib7564772016-08-02 18:13:13 +053046public class YangJavaNotificationTranslator
47 extends YangJavaNotification
Vinod Kumar S79a374b2016-04-30 21:09:15 +053048 implements JavaCodeGenerator, JavaCodeGeneratorInfo {
Vidyashree Rama620b6e92016-03-28 11:59:27 +053049
Bharat saraswalc2d3be12016-06-16 00:29:12 +053050 private static final long serialVersionUID = 806201624L;
51
Vidyashree Rama620b6e92016-03-28 11:59:27 +053052 /**
Vidyashree Rama620b6e92016-03-28 11:59:27 +053053 * File handle to maintain temporary java code fragments as per the code
54 * snippet types.
55 */
Bharat saraswalc2d3be12016-06-16 00:29:12 +053056 private transient TempJavaCodeFragmentFiles tempFileHandle;
Vidyashree Rama620b6e92016-03-28 11:59:27 +053057
58 /**
59 * Creates an instance of java Notification.
60 */
Shankara-Huaweib7564772016-08-02 18:13:13 +053061 public YangJavaNotificationTranslator() {
Vidyashree Rama620b6e92016-03-28 11:59:27 +053062 super();
Bharat saraswale50edca2016-08-05 01:58:25 +053063 setJavaFileInfo(new JavaFileInfoTranslator());
Bharat saraswal715d3fc2016-05-17 19:59:16 +053064 getJavaFileInfo().setGeneratedFileTypes(GENERATE_INTERFACE_WITH_BUILDER);
Vidyashree Rama620b6e92016-03-28 11:59:27 +053065 }
66
67 /**
68 * Returns the generated java file information.
69 *
70 * @return generated java file information
71 */
72 @Override
Bharat saraswale50edca2016-08-05 01:58:25 +053073 public JavaFileInfoTranslator getJavaFileInfo() {
Vidyashree Rama620b6e92016-03-28 11:59:27 +053074
75 if (javaFileInfo == null) {
Gaurav Agrawal61edaa02016-08-31 16:52:56 +053076 throw new TranslatorException("Missing java info in java " +
77 "datamodel node");
Vidyashree Rama620b6e92016-03-28 11:59:27 +053078 }
Bharat saraswale50edca2016-08-05 01:58:25 +053079 return (JavaFileInfoTranslator) javaFileInfo;
Vidyashree Rama620b6e92016-03-28 11:59:27 +053080 }
81
82 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053083 * Sets the java file info object.
Vidyashree Rama620b6e92016-03-28 11:59:27 +053084 *
85 * @param javaInfo java file info object
86 */
87 @Override
Bharat saraswale50edca2016-08-05 01:58:25 +053088 public void setJavaFileInfo(JavaFileInfoTranslator javaInfo) {
Vidyashree Rama620b6e92016-03-28 11:59:27 +053089 javaFileInfo = javaInfo;
90 }
91
92 /**
Vidyashree Rama620b6e92016-03-28 11:59:27 +053093 * Returns the temporary file handle.
94 *
95 * @return temporary file handle
96 */
97 @Override
98 public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() {
Vidyashree Rama620b6e92016-03-28 11:59:27 +053099 return tempFileHandle;
100 }
101
102 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530103 * Sets temporary file handle.
Vidyashree Rama620b6e92016-03-28 11:59:27 +0530104 *
105 * @param fileHandle temporary file handle
106 */
107 @Override
108 public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) {
Vidyashree Rama620b6e92016-03-28 11:59:27 +0530109 tempFileHandle = fileHandle;
110 }
111
112 /**
113 * Prepare the information for java code generation corresponding to YANG
114 * notification info.
115 *
janani b1c6acc42016-04-15 16:18:30 +0530116 * @param yangPlugin YANG plugin config
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530117 * @throws TranslatorException translator operation fail
Vidyashree Rama620b6e92016-03-28 11:59:27 +0530118 */
119 @Override
Gaurav Agrawal61edaa02016-08-31 16:52:56 +0530120 public void generateCodeEntry(YangPluginConfig yangPlugin)
121 throws TranslatorException {
Vidyashree Rama620b6e92016-03-28 11:59:27 +0530122
Bharat saraswal8beac342016-08-04 02:00:03 +0530123 /*
Gaurav Agrawal61edaa02016-08-31 16:52:56 +0530124 * As part of the notification support the following files needs to
125 * be generated.
126 * 1) Subject of the notification(event), this is simple interface with
127 * builder class.
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530128 * 2) Event class extending "AbstractEvent" and defining event type enum.
129 * 3) Event listener interface extending "EventListener".
130 *
131 * The manager class needs to extend the ListenerRegistry.
132 */
Ray Milkey85971c62016-05-11 18:39:39 +0000133
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530134 // Generate subject of the notification(event), this is simple interface
135 // with builder class.
136 try {
137 generateCodeOfAugmentableNode(this, yangPlugin);
138 addNotificationToExtendsList();
139 } catch (IOException e) {
140 throw new TranslatorException(
Gaurav Agrawal61edaa02016-08-31 16:52:56 +0530141 "Failed to prepare generate code entry for notification " +
142 "node " + getName());
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530143 }
144 }
Ray Milkey85971c62016-05-11 18:39:39 +0000145
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530146 /*Adds current notification info to the extends list so its parents service*/
147 private void addNotificationToExtendsList() {
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530148 YangNode parent = getParent();
Bharat saraswalaf413b82016-07-14 15:18:20 +0530149 TempJavaServiceFragmentFiles tempJavaServiceFragmentFiles = ((TempJavaCodeFragmentFilesContainer) parent)
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530150 .getTempJavaCodeFragmentFiles()
Bharat saraswalaf413b82016-07-14 15:18:20 +0530151 .getServiceTempFiles();
152 JavaExtendsListHolder holder = tempJavaServiceFragmentFiles.getJavaExtendsListHolder();
Shankara-Huaweib7564772016-08-02 18:13:13 +0530153 JavaQualifiedTypeInfoTranslator event = new JavaQualifiedTypeInfoTranslator();
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530154
155 String parentInfo = getCapitalCase(((JavaFileInfoContainer) parent)
156 .getJavaFileInfo().getJavaName());
157 event.setClassInfo(parentInfo + EVENT_STRING);
158 event.setPkgInfo(getJavaFileInfo().getPackage());
Bharat saraswalaf413b82016-07-14 15:18:20 +0530159 holder.addToExtendsList(event, parent, tempJavaServiceFragmentFiles);
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530160
Shankara-Huaweib7564772016-08-02 18:13:13 +0530161 JavaQualifiedTypeInfoTranslator eventListener = new JavaQualifiedTypeInfoTranslator();
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530162
163 eventListener.setClassInfo(parentInfo + EVENT_LISTENER_STRING);
164 eventListener.setPkgInfo(getJavaFileInfo().getPackage());
Bharat saraswalaf413b82016-07-14 15:18:20 +0530165 holder.addToExtendsList(eventListener, parent, tempJavaServiceFragmentFiles);
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530166
Vidyashree Rama620b6e92016-03-28 11:59:27 +0530167 }
168
169 /**
Bharat saraswale2bc60d2016-04-16 02:28:25 +0530170 * Creates a java file using the YANG notification info.
Vidyashree Rama620b6e92016-03-28 11:59:27 +0530171 */
172 @Override
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530173 public void generateCodeExit() throws TranslatorException {
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530174 try {
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530175 getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_INTERFACE_WITH_BUILDER, this);
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530176 } catch (IOException e) {
Gaurav Agrawal61edaa02016-08-31 16:52:56 +0530177 throw new TranslatorException("Failed to generate code for " +
178 "notification node " +
179 getName());
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530180 }
Vidyashree Rama620b6e92016-03-28 11:59:27 +0530181
182 }
Gaurav Agrawal61edaa02016-08-31 16:52:56 +0530183
184 @Override
185 public void setNameSpaceAndAddToParentSchemaMap() {
186 // Get parent namespace.
187 if (this.getParent() != null) {
188 String nameSpace = this.getParent().getNameSpace();
189 // Set namespace for self node.
190 setNameSpace(nameSpace);
191 // Process addition of leaf to the child schema map of parent.
192 processAdditionOfSchemaNodeToParentMap(getName(), getNameSpace());
193 // Obtain the notification name as per enum in notification.
194 String enumName = getEnumJavaAttribute(getJavaFileInfo().getJavaName()).toUpperCase();
195
196 ((RpcNotificationContainer) this.getParent()).addToNotificationEnumMap(enumName, this);
197
198 }
199 /*
200 * Check if node contains leaf/leaf-list, if yes add namespace for leaf
201 * and leaf list.
202 */
203 if (this instanceof YangLeavesHolder) {
204 ((YangLeavesHolder) this).setLeafNameSpaceAndAddToParentSchemaMap();
205 }
206 }
Vidyashree Rama620b6e92016-03-28 11:59:27 +0530207}