blob: 5d6f45dcbd3279d39d771d91486c836c4547e390 [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
Gaurav Agrawal61edaa02016-08-31 16:52:56 +053019import org.onosproject.yangutils.datamodel.RpcNotificationContainer;
Bharat saraswal4aaab4d2016-05-17 14:19:38 +053020import org.onosproject.yangutils.datamodel.YangNode;
Shankara-Huaweib7564772016-08-02 18:13:13 +053021import org.onosproject.yangutils.datamodel.javadatamodel.YangJavaNotification;
Bharat saraswal63f26fb2016-04-05 15:13:44 +053022import org.onosproject.yangutils.translator.exception.TranslatorException;
Vidyashree Rama620b6e92016-03-28 11:59:27 +053023import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator;
Gaurav Agrawalbfce9342016-06-15 13:58:01 +053024import org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorInfo;
Bharat saraswal4aaab4d2016-05-17 14:19:38 +053025import org.onosproject.yangutils.translator.tojava.JavaFileInfoContainer;
Gaurav Agrawal61edaa02016-08-31 16:52:56 +053026import org.onosproject.yangutils.translator.tojava.JavaFileInfoTranslator;
Shankara-Huaweib7564772016-08-02 18:13:13 +053027import org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfoTranslator;
Vidyashree Rama620b6e92016-03-28 11:59:27 +053028import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
Bharat saraswal4aaab4d2016-05-17 14:19:38 +053029import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFilesContainer;
Bharat saraswalaf413b82016-07-14 15:18:20 +053030import org.onosproject.yangutils.translator.tojava.TempJavaServiceFragmentFiles;
Bharat saraswal4aaab4d2016-05-17 14:19:38 +053031import org.onosproject.yangutils.translator.tojava.utils.JavaExtendsListHolder;
Gaurav Agrawal61edaa02016-08-31 16:52:56 +053032import org.onosproject.yangutils.utils.io.YangPluginConfig;
Vidyashree Rama620b6e92016-03-28 11:59:27 +053033
Gaurav Agrawal3b57f362016-09-07 13:16:35 +053034import java.io.IOException;
35
Vidyashree Rama620b6e92016-03-28 11:59:27 +053036import 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;
Gaurav Agrawal3b57f362016-09-07 13:16:35 +053039import static org.onosproject.yangutils.translator.tojava.utils.TranslatorErrorType.FAIL_AT_ENTRY;
40import static org.onosproject.yangutils.translator.tojava.utils.TranslatorErrorType.FAIL_AT_EXIT;
41import static org.onosproject.yangutils.translator.tojava.utils.TranslatorErrorType.INVALID_NODE;
42import static org.onosproject.yangutils.translator.tojava.utils.TranslatorUtils.getErrorMsg;
Bharat saraswal4aaab4d2016-05-17 14:19:38 +053043import static org.onosproject.yangutils.utils.UtilConstants.EVENT_LISTENER_STRING;
44import static org.onosproject.yangutils.utils.UtilConstants.EVENT_STRING;
Shankara-Huaweib7564772016-08-02 18:13:13 +053045import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCapitalCase;
Vidyashree Rama620b6e92016-03-28 11:59:27 +053046
47/**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053048 * Represents notification information extended to support java code generation.
Vidyashree Rama620b6e92016-03-28 11:59:27 +053049 */
Shankara-Huaweib7564772016-08-02 18:13:13 +053050public class YangJavaNotificationTranslator
51 extends YangJavaNotification
Vinod Kumar S79a374b2016-04-30 21:09:15 +053052 implements JavaCodeGenerator, JavaCodeGeneratorInfo {
Vidyashree Rama620b6e92016-03-28 11:59:27 +053053
Bharat saraswalc2d3be12016-06-16 00:29:12 +053054 private static final long serialVersionUID = 806201624L;
55
Vidyashree Rama620b6e92016-03-28 11:59:27 +053056 /**
Vidyashree Rama620b6e92016-03-28 11:59:27 +053057 * File handle to maintain temporary java code fragments as per the code
58 * snippet types.
59 */
Bharat saraswalc2d3be12016-06-16 00:29:12 +053060 private transient TempJavaCodeFragmentFiles tempFileHandle;
Vidyashree Rama620b6e92016-03-28 11:59:27 +053061
62 /**
63 * Creates an instance of java Notification.
64 */
Shankara-Huaweib7564772016-08-02 18:13:13 +053065 public YangJavaNotificationTranslator() {
Bharat saraswale50edca2016-08-05 01:58:25 +053066 setJavaFileInfo(new JavaFileInfoTranslator());
Bharat saraswal715d3fc2016-05-17 19:59:16 +053067 getJavaFileInfo().setGeneratedFileTypes(GENERATE_INTERFACE_WITH_BUILDER);
Vidyashree Rama620b6e92016-03-28 11:59:27 +053068 }
69
70 /**
71 * Returns the generated java file information.
72 *
73 * @return generated java file information
74 */
75 @Override
Bharat saraswale50edca2016-08-05 01:58:25 +053076 public JavaFileInfoTranslator getJavaFileInfo() {
Vidyashree Rama620b6e92016-03-28 11:59:27 +053077
78 if (javaFileInfo == null) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +053079 throw new TranslatorException(getErrorMsg(INVALID_NODE, this));
Vidyashree Rama620b6e92016-03-28 11:59:27 +053080 }
Bharat saraswale50edca2016-08-05 01:58:25 +053081 return (JavaFileInfoTranslator) javaFileInfo;
Vidyashree Rama620b6e92016-03-28 11:59:27 +053082 }
83
84 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053085 * Sets the java file info object.
Vidyashree Rama620b6e92016-03-28 11:59:27 +053086 *
87 * @param javaInfo java file info object
88 */
89 @Override
Bharat saraswale50edca2016-08-05 01:58:25 +053090 public void setJavaFileInfo(JavaFileInfoTranslator javaInfo) {
Vidyashree Rama620b6e92016-03-28 11:59:27 +053091 javaFileInfo = javaInfo;
92 }
93
94 /**
Vidyashree Rama620b6e92016-03-28 11:59:27 +053095 * Returns the temporary file handle.
96 *
97 * @return temporary file handle
98 */
99 @Override
100 public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() {
Vidyashree Rama620b6e92016-03-28 11:59:27 +0530101 return tempFileHandle;
102 }
103
104 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530105 * Sets temporary file handle.
Vidyashree Rama620b6e92016-03-28 11:59:27 +0530106 *
107 * @param fileHandle temporary file handle
108 */
109 @Override
110 public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) {
Vidyashree Rama620b6e92016-03-28 11:59:27 +0530111 tempFileHandle = fileHandle;
112 }
113
114 /**
115 * Prepare the information for java code generation corresponding to YANG
116 * notification info.
117 *
janani b1c6acc42016-04-15 16:18:30 +0530118 * @param yangPlugin YANG plugin config
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530119 * @throws TranslatorException translator operation fail
Vidyashree Rama620b6e92016-03-28 11:59:27 +0530120 */
121 @Override
Bharat saraswal748fc3c2016-09-06 16:38:20 +0530122 public void generateCodeEntry(YangPluginConfig yangPlugin)
123 throws TranslatorException {
Vidyashree Rama620b6e92016-03-28 11:59:27 +0530124
Gaurav Agrawal493e0a82016-09-28 18:30:08 +0530125 // Obtain the notification name as per enum in notification.
126 String enumName = getEnumJavaAttribute(getName().toUpperCase());
127 ((RpcNotificationContainer) getParent())
128 .addToNotificationEnumMap(enumName, this);
129
Bharat saraswal8beac342016-08-04 02:00:03 +0530130 /*
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530131 * As part of the notification support the following files needs to be
132 * generated.
133 * 1) Subject of the notification(event), this is simple interface with
134 * builder class.
135 * 2) Event class extending "AbstractEvent" and defining event type
136 * enum.
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530137 * 3) Event listener interface extending "EventListener".
138 *
139 * The manager class needs to extend the ListenerRegistry.
140 */
Ray Milkey85971c62016-05-11 18:39:39 +0000141
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530142 // Generate subject of the notification(event), this is simple interface
143 // with builder class.
144 try {
145 generateCodeOfAugmentableNode(this, yangPlugin);
146 addNotificationToExtendsList();
147 } catch (IOException e) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530148 throw new TranslatorException(getErrorMsg(FAIL_AT_ENTRY, this,
149 e.getLocalizedMessage()));
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530150 }
151 }
Ray Milkey85971c62016-05-11 18:39:39 +0000152
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530153 /*Adds current notification info to the extends list so its parents service*/
154 private void addNotificationToExtendsList() {
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530155 YangNode parent = getParent();
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530156 TempJavaServiceFragmentFiles tempFiles =
157 ((TempJavaCodeFragmentFilesContainer) parent).getTempJavaCodeFragmentFiles()
158 .getServiceTempFiles();
159 JavaExtendsListHolder holder = tempFiles.getJavaExtendsListHolder();
160 JavaQualifiedTypeInfoTranslator event =
161 new JavaQualifiedTypeInfoTranslator();
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530162
163 String parentInfo = getCapitalCase(((JavaFileInfoContainer) parent)
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530164 .getJavaFileInfo()
165 .getJavaName());
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530166 event.setClassInfo(parentInfo + EVENT_STRING);
167 event.setPkgInfo(getJavaFileInfo().getPackage());
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530168 holder.addToExtendsList(event, parent, tempFiles);
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530169
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530170 JavaQualifiedTypeInfoTranslator eventListener =
171 new JavaQualifiedTypeInfoTranslator();
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530172
173 eventListener.setClassInfo(parentInfo + EVENT_LISTENER_STRING);
174 eventListener.setPkgInfo(getJavaFileInfo().getPackage());
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530175 holder.addToExtendsList(eventListener, parent, tempFiles);
Vidyashree Rama620b6e92016-03-28 11:59:27 +0530176 }
177
178 /**
Bharat saraswale2bc60d2016-04-16 02:28:25 +0530179 * Creates a java file using the YANG notification info.
Vidyashree Rama620b6e92016-03-28 11:59:27 +0530180 */
181 @Override
Bharat saraswal748fc3c2016-09-06 16:38:20 +0530182 public void generateCodeExit()
183 throws TranslatorException {
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530184 try {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530185 getTempJavaCodeFragmentFiles().generateJavaFile(
186 GENERATE_INTERFACE_WITH_BUILDER, this);
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530187 } catch (IOException e) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530188 throw new TranslatorException(getErrorMsg(FAIL_AT_EXIT, this,
189 e.getLocalizedMessage()));
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530190 }
Vidyashree Rama620b6e92016-03-28 11:59:27 +0530191 }
Vidyashree Rama620b6e92016-03-28 11:59:27 +0530192}