blob: 68f8164d589634832daadceaac7008485953b5dd [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
33import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_INTERFACE_WITH_BUILDER;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053034import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCapitalCase;
35import static org.onosproject.yangutils.translator.tojava.utils.YangJavaModelUtils.generateCodeOfAugmentableNode;
36import static org.onosproject.yangutils.utils.UtilConstants.EVENT_LISTENER_STRING;
37import static org.onosproject.yangutils.utils.UtilConstants.EVENT_STRING;
Vidyashree Rama506cbe12016-03-28 11:59:27 +053038
39/**
Bharat saraswald9822e92016-04-05 15:13:44 +053040 * Represents notification information extended to support java code generation.
Vidyashree Rama506cbe12016-03-28 11:59:27 +053041 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053042public class YangJavaNotification
43 extends YangNotification
44 implements JavaCodeGenerator, JavaCodeGeneratorInfo {
Vidyashree Rama506cbe12016-03-28 11:59:27 +053045
Bharat saraswal96dfef02016-06-16 00:29:12 +053046 private static final long serialVersionUID = 806201624L;
47
Vidyashree Rama506cbe12016-03-28 11:59:27 +053048 /**
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 */
Bharat saraswal96dfef02016-06-16 00:29:12 +053057 private transient TempJavaCodeFragmentFiles tempFileHandle;
Vidyashree Rama506cbe12016-03-28 11:59:27 +053058
59 /**
60 * Creates an instance of java Notification.
61 */
62 public YangJavaNotification() {
63 super();
64 setJavaFileInfo(new JavaFileInfo());
Bharat saraswal33dfa012016-05-17 19:59:16 +053065 getJavaFileInfo().setGeneratedFileTypes(GENERATE_INTERFACE_WITH_BUILDER);
Vidyashree Rama506cbe12016-03-28 11:59:27 +053066 }
67
68 /**
69 * Returns the generated java file information.
70 *
71 * @return generated java file information
72 */
73 @Override
74 public JavaFileInfo getJavaFileInfo() {
75
76 if (javaFileInfo == null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +053077 throw new TranslatorException("Missing java info in java datamodel node");
Vidyashree Rama506cbe12016-03-28 11:59:27 +053078 }
79 return javaFileInfo;
80 }
81
82 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053083 * Sets the java file info object.
Vidyashree Rama506cbe12016-03-28 11:59:27 +053084 *
85 * @param javaInfo java file info object
86 */
87 @Override
88 public void setJavaFileInfo(JavaFileInfo javaInfo) {
Vidyashree Rama506cbe12016-03-28 11:59:27 +053089 javaFileInfo = javaInfo;
90 }
91
92 /**
Vidyashree Rama506cbe12016-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 Rama506cbe12016-03-28 11:59:27 +053099 return tempFileHandle;
100 }
101
102 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530103 * Sets temporary file handle.
Vidyashree Rama506cbe12016-03-28 11:59:27 +0530104 *
105 * @param fileHandle temporary file handle
106 */
107 @Override
108 public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) {
Vidyashree Rama506cbe12016-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 bde4ffab2016-04-15 16:18:30 +0530116 * @param yangPlugin YANG plugin config
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530117 * @throws TranslatorException translator operation fail
Vidyashree Rama506cbe12016-03-28 11:59:27 +0530118 */
119 @Override
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530120 public void generateCodeEntry(YangPluginConfig yangPlugin) throws TranslatorException {
Vidyashree Rama506cbe12016-03-28 11:59:27 +0530121
Ray Milkey994c4982016-05-11 18:39:39 +0000122 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530123 * As part of the notification support the following files needs to be generated.
124 * 1) Subject of the notification(event), this is simple interface with builder class.
125 * 2) Event class extending "AbstractEvent" and defining event type enum.
126 * 3) Event listener interface extending "EventListener".
127 *
128 * The manager class needs to extend the ListenerRegistry.
129 */
Ray Milkey994c4982016-05-11 18:39:39 +0000130
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530131 // Generate subject of the notification(event), this is simple interface
132 // with builder class.
133 try {
134 generateCodeOfAugmentableNode(this, yangPlugin);
135 addNotificationToExtendsList();
136 } catch (IOException e) {
137 throw new TranslatorException(
Bharat saraswal96dfef02016-06-16 00:29:12 +0530138 "Failed to prepare generate code entry for notification node " + getName());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530139 }
140 }
Ray Milkey994c4982016-05-11 18:39:39 +0000141
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530142 /*Adds current notification info to the extends list so its parents service*/
143 private void addNotificationToExtendsList() {
Bharat saraswal96dfef02016-06-16 00:29:12 +0530144 YangNode parent = getParent();
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530145 JavaExtendsListHolder holder = ((TempJavaCodeFragmentFilesContainer) parent)
146 .getTempJavaCodeFragmentFiles()
147 .getServiceTempFiles().getJavaExtendsListHolder();
148 JavaQualifiedTypeInfo event = new JavaQualifiedTypeInfo();
149
150 String parentInfo = getCapitalCase(((JavaFileInfoContainer) parent)
151 .getJavaFileInfo().getJavaName());
152 event.setClassInfo(parentInfo + EVENT_STRING);
153 event.setPkgInfo(getJavaFileInfo().getPackage());
154 holder.addToExtendsList(event, parent);
155
156 JavaQualifiedTypeInfo eventListener = new JavaQualifiedTypeInfo();
157
158 eventListener.setClassInfo(parentInfo + EVENT_LISTENER_STRING);
159 eventListener.setPkgInfo(getJavaFileInfo().getPackage());
160 holder.addToExtendsList(eventListener, parent);
161
Vidyashree Rama506cbe12016-03-28 11:59:27 +0530162 }
163
164 /**
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530165 * Creates a java file using the YANG notification info.
Vidyashree Rama506cbe12016-03-28 11:59:27 +0530166 */
167 @Override
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530168 public void generateCodeExit() throws TranslatorException {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530169 try {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530170 getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_INTERFACE_WITH_BUILDER, this);
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530171 } catch (IOException e) {
Bharat saraswal96dfef02016-06-16 00:29:12 +0530172 throw new TranslatorException("Failed to generate code for notification node " + getName());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530173 }
Vidyashree Rama506cbe12016-03-28 11:59:27 +0530174
175 }
176}