blob: d40073a93dcc519a63c206828dd5e12860a88dc1 [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
46 /**
47 * Contains information of the java file being generated.
48 */
49 private JavaFileInfo javaFileInfo;
50
51 /**
Vidyashree Rama506cbe12016-03-28 11:59:27 +053052 * File handle to maintain temporary java code fragments as per the code
53 * snippet types.
54 */
55 private TempJavaCodeFragmentFiles tempFileHandle;
56
57 /**
58 * Creates an instance of java Notification.
59 */
60 public YangJavaNotification() {
61 super();
62 setJavaFileInfo(new JavaFileInfo());
Bharat saraswal33dfa012016-05-17 19:59:16 +053063 getJavaFileInfo().setGeneratedFileTypes(GENERATE_INTERFACE_WITH_BUILDER);
Vidyashree Rama506cbe12016-03-28 11:59:27 +053064 }
65
66 /**
67 * Returns the generated java file information.
68 *
69 * @return generated java file information
70 */
71 @Override
72 public JavaFileInfo getJavaFileInfo() {
73
74 if (javaFileInfo == null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +053075 throw new TranslatorException("Missing java info in java datamodel node");
Vidyashree Rama506cbe12016-03-28 11:59:27 +053076 }
77 return javaFileInfo;
78 }
79
80 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053081 * Sets the java file info object.
Vidyashree Rama506cbe12016-03-28 11:59:27 +053082 *
83 * @param javaInfo java file info object
84 */
85 @Override
86 public void setJavaFileInfo(JavaFileInfo javaInfo) {
Vidyashree Rama506cbe12016-03-28 11:59:27 +053087 javaFileInfo = javaInfo;
88 }
89
90 /**
Vidyashree Rama506cbe12016-03-28 11:59:27 +053091 * Returns the temporary file handle.
92 *
93 * @return temporary file handle
94 */
95 @Override
96 public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() {
Vidyashree Rama506cbe12016-03-28 11:59:27 +053097 return tempFileHandle;
98 }
99
100 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530101 * Sets temporary file handle.
Vidyashree Rama506cbe12016-03-28 11:59:27 +0530102 *
103 * @param fileHandle temporary file handle
104 */
105 @Override
106 public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) {
Vidyashree Rama506cbe12016-03-28 11:59:27 +0530107 tempFileHandle = fileHandle;
108 }
109
110 /**
111 * Prepare the information for java code generation corresponding to YANG
112 * notification info.
113 *
janani bde4ffab2016-04-15 16:18:30 +0530114 * @param yangPlugin YANG plugin config
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530115 * @throws TranslatorException translator operation fail
Vidyashree Rama506cbe12016-03-28 11:59:27 +0530116 */
117 @Override
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530118 public void generateCodeEntry(YangPluginConfig yangPlugin) throws TranslatorException {
Vidyashree Rama506cbe12016-03-28 11:59:27 +0530119
Ray Milkey994c4982016-05-11 18:39:39 +0000120 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530121 * As part of the notification support the following files needs to be generated.
122 * 1) Subject of the notification(event), this is simple interface with builder class.
123 * 2) Event class extending "AbstractEvent" and defining event type enum.
124 * 3) Event listener interface extending "EventListener".
125 *
126 * The manager class needs to extend the ListenerRegistry.
127 */
Ray Milkey994c4982016-05-11 18:39:39 +0000128
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530129 // Generate subject of the notification(event), this is simple interface
130 // with builder class.
131 try {
132 generateCodeOfAugmentableNode(this, yangPlugin);
133 addNotificationToExtendsList();
134 } catch (IOException e) {
135 throw new TranslatorException(
136 "Failed to prepare generate code entry for notification node " + this.getName());
137 }
138 }
Ray Milkey994c4982016-05-11 18:39:39 +0000139
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530140 /*Adds current notification info to the extends list so its parents service*/
141 private void addNotificationToExtendsList() {
142 YangNode parent = this.getParent();
143 JavaExtendsListHolder holder = ((TempJavaCodeFragmentFilesContainer) parent)
144 .getTempJavaCodeFragmentFiles()
145 .getServiceTempFiles().getJavaExtendsListHolder();
146 JavaQualifiedTypeInfo event = new JavaQualifiedTypeInfo();
147
148 String parentInfo = getCapitalCase(((JavaFileInfoContainer) parent)
149 .getJavaFileInfo().getJavaName());
150 event.setClassInfo(parentInfo + EVENT_STRING);
151 event.setPkgInfo(getJavaFileInfo().getPackage());
152 holder.addToExtendsList(event, parent);
153
154 JavaQualifiedTypeInfo eventListener = new JavaQualifiedTypeInfo();
155
156 eventListener.setClassInfo(parentInfo + EVENT_LISTENER_STRING);
157 eventListener.setPkgInfo(getJavaFileInfo().getPackage());
158 holder.addToExtendsList(eventListener, parent);
159
Vidyashree Rama506cbe12016-03-28 11:59:27 +0530160 }
161
162 /**
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530163 * Creates a java file using the YANG notification info.
Vidyashree Rama506cbe12016-03-28 11:59:27 +0530164 */
165 @Override
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530166 public void generateCodeExit() throws TranslatorException {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530167 try {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530168 getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_INTERFACE_WITH_BUILDER, this);
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530169 } catch (IOException e) {
170 throw new TranslatorException("Failed to generate code for notification node " + this.getName());
171 }
Vidyashree Rama506cbe12016-03-28 11:59:27 +0530172
173 }
174}