blob: 8624a44e6b6e9c09f7002b29996532543c22ef28 [file] [log] [blame]
Vidyashree Rama506cbe12016-03-28 11:59:27 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Vidyashree Rama506cbe12016-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;
Bharat saraswald9822e92016-04-05 15:13:44 +053020
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053021import org.onosproject.yangutils.datamodel.YangNode;
Vidyashree Rama506cbe12016-03-28 11:59:27 +053022import org.onosproject.yangutils.datamodel.YangNotification;
Bharat saraswald9822e92016-04-05 15:13:44 +053023import org.onosproject.yangutils.translator.exception.TranslatorException;
Vidyashree Rama506cbe12016-03-28 11:59:27 +053024import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator;
25import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053026import org.onosproject.yangutils.translator.tojava.JavaFileInfoContainer;
27import org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo;
Vidyashree Rama506cbe12016-03-28 11:59:27 +053028import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053029import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFilesContainer;
30import org.onosproject.yangutils.translator.tojava.utils.JavaExtendsListHolder;
janani bde4ffab2016-04-15 16:18:30 +053031import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig;
Vidyashree Rama506cbe12016-03-28 11:59:27 +053032
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053033import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_CLASS;
34import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_LISTENER_INTERFACE;
Vidyashree Rama506cbe12016-03-28 11:59:27 +053035import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_INTERFACE_WITH_BUILDER;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053036import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCapitalCase;
37import static org.onosproject.yangutils.translator.tojava.utils.YangJavaModelUtils.generateCodeOfAugmentableNode;
38import static org.onosproject.yangutils.utils.UtilConstants.EVENT_LISTENER_STRING;
39import static org.onosproject.yangutils.utils.UtilConstants.EVENT_STRING;
Vidyashree Rama506cbe12016-03-28 11:59:27 +053040
41/**
Bharat saraswald9822e92016-04-05 15:13:44 +053042 * Represents notification information extended to support java code generation.
Vidyashree Rama506cbe12016-03-28 11:59:27 +053043 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053044public class YangJavaNotification
45 extends YangNotification
46 implements JavaCodeGenerator, JavaCodeGeneratorInfo {
Vidyashree Rama506cbe12016-03-28 11:59:27 +053047
48 /**
49 * Contains information of the java file being generated.
50 */
51 private JavaFileInfo javaFileInfo;
52
53 /**
Vidyashree Rama506cbe12016-03-28 11:59:27 +053054 * File handle to maintain temporary java code fragments as per the code
55 * snippet types.
56 */
57 private TempJavaCodeFragmentFiles tempFileHandle;
58
59 /**
60 * Creates an instance of java Notification.
61 */
62 public YangJavaNotification() {
63 super();
64 setJavaFileInfo(new JavaFileInfo());
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053065 getJavaFileInfo().setGeneratedFileTypes(GENERATE_INTERFACE_WITH_BUILDER
66 | GENERATE_EVENT_CLASS | GENERATE_EVENT_LISTENER_INTERFACE);
Vidyashree Rama506cbe12016-03-28 11:59:27 +053067 }
68
69 /**
70 * Returns the generated java file information.
71 *
72 * @return generated java file information
73 */
74 @Override
75 public JavaFileInfo getJavaFileInfo() {
76
77 if (javaFileInfo == null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +053078 throw new TranslatorException("Missing java info in java datamodel node");
Vidyashree Rama506cbe12016-03-28 11:59:27 +053079 }
80 return javaFileInfo;
81 }
82
83 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053084 * Sets the java file info object.
Vidyashree Rama506cbe12016-03-28 11:59:27 +053085 *
86 * @param javaInfo java file info object
87 */
88 @Override
89 public void setJavaFileInfo(JavaFileInfo javaInfo) {
Vidyashree Rama506cbe12016-03-28 11:59:27 +053090 javaFileInfo = javaInfo;
91 }
92
93 /**
Vidyashree Rama506cbe12016-03-28 11:59:27 +053094 * Returns the temporary file handle.
95 *
96 * @return temporary file handle
97 */
98 @Override
99 public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() {
Vidyashree Rama506cbe12016-03-28 11:59:27 +0530100 return tempFileHandle;
101 }
102
103 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530104 * Sets temporary file handle.
Vidyashree Rama506cbe12016-03-28 11:59:27 +0530105 *
106 * @param fileHandle temporary file handle
107 */
108 @Override
109 public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) {
Vidyashree Rama506cbe12016-03-28 11:59:27 +0530110 tempFileHandle = fileHandle;
111 }
112
113 /**
114 * Prepare the information for java code generation corresponding to YANG
115 * notification info.
116 *
janani bde4ffab2016-04-15 16:18:30 +0530117 * @param yangPlugin YANG plugin config
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530118 * @throws TranslatorException translator operation fail
Vidyashree Rama506cbe12016-03-28 11:59:27 +0530119 */
120 @Override
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530121 public void generateCodeEntry(YangPluginConfig yangPlugin) throws TranslatorException {
Vidyashree Rama506cbe12016-03-28 11:59:27 +0530122
Ray Milkey994c4982016-05-11 18:39:39 +0000123 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530124 * As part of the notification support the following files needs to be generated.
125 * 1) Subject of the notification(event), this is simple interface with builder class.
126 * 2) Event class extending "AbstractEvent" and defining event type enum.
127 * 3) Event listener interface extending "EventListener".
128 *
129 * The manager class needs to extend the ListenerRegistry.
130 */
Ray Milkey994c4982016-05-11 18:39:39 +0000131
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530132 // Generate subject of the notification(event), this is simple interface
133 // with builder class.
134 try {
135 generateCodeOfAugmentableNode(this, yangPlugin);
136 addNotificationToExtendsList();
137 } catch (IOException e) {
138 throw new TranslatorException(
139 "Failed to prepare generate code entry for notification node " + this.getName());
140 }
141 }
Ray Milkey994c4982016-05-11 18:39:39 +0000142
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530143 /*Adds current notification info to the extends list so its parents service*/
144 private void addNotificationToExtendsList() {
145 YangNode parent = this.getParent();
146 JavaExtendsListHolder holder = ((TempJavaCodeFragmentFilesContainer) parent)
147 .getTempJavaCodeFragmentFiles()
148 .getServiceTempFiles().getJavaExtendsListHolder();
149 JavaQualifiedTypeInfo event = new JavaQualifiedTypeInfo();
150
151 String parentInfo = getCapitalCase(((JavaFileInfoContainer) parent)
152 .getJavaFileInfo().getJavaName());
153 event.setClassInfo(parentInfo + EVENT_STRING);
154 event.setPkgInfo(getJavaFileInfo().getPackage());
155 holder.addToExtendsList(event, parent);
156
157 JavaQualifiedTypeInfo eventListener = new JavaQualifiedTypeInfo();
158
159 eventListener.setClassInfo(parentInfo + EVENT_LISTENER_STRING);
160 eventListener.setPkgInfo(getJavaFileInfo().getPackage());
161 holder.addToExtendsList(eventListener, parent);
162
Vidyashree Rama506cbe12016-03-28 11:59:27 +0530163 }
164
165 /**
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530166 * Creates a java file using the YANG notification info.
Vidyashree Rama506cbe12016-03-28 11:59:27 +0530167 */
168 @Override
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530169 public void generateCodeExit() throws TranslatorException {
Ray Milkey994c4982016-05-11 18:39:39 +0000170 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530171 * As part of the notification support the following files needs to be generated.
172 * 1) Subject of the notification(event), this is simple interface with builder class.
173 * 2) Event class extending "AbstractEvent" and defining event type enum.
174 * 3) Event listener interface extending "EventListener".
175 *
176 * The manager class needs to extend the "ListenerRegistry".
177 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530178 try {
179 getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_INTERFACE_WITH_BUILDER
180 | GENERATE_EVENT_CLASS | GENERATE_EVENT_LISTENER_INTERFACE, this);
181 } catch (IOException e) {
182 throw new TranslatorException("Failed to generate code for notification node " + this.getName());
183 }
Vidyashree Rama506cbe12016-03-28 11:59:27 +0530184
185 }
186}