blob: a56fefa9791cb8e87435873f94c42566a4ec21bc [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;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053025import org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorInfo;
Vidyashree Rama506cbe12016-03-28 11:59:27 +053026import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053027import org.onosproject.yangutils.translator.tojava.JavaFileInfoContainer;
28import org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo;
Vidyashree Rama506cbe12016-03-28 11:59:27 +053029import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053030import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFilesContainer;
Bharat saraswalb551aae2016-07-14 15:18:20 +053031import org.onosproject.yangutils.translator.tojava.TempJavaServiceFragmentFiles;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053032import org.onosproject.yangutils.translator.tojava.utils.JavaExtendsListHolder;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053033import org.onosproject.yangutils.utils.io.impl.YangPluginConfig;
Vidyashree Rama506cbe12016-03-28 11:59:27 +053034
35import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_INTERFACE_WITH_BUILDER;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053036import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCapitalCase;
Bharat saraswalb551aae2016-07-14 15:18:20 +053037import static org.onosproject.yangutils.translator.tojava.YangJavaModelUtils.generateCodeOfAugmentableNode;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053038import 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
Bharat saraswal96dfef02016-06-16 00:29:12 +053048 private static final long serialVersionUID = 806201624L;
49
Vidyashree Rama506cbe12016-03-28 11:59:27 +053050 /**
51 * Contains information of the java file being generated.
52 */
53 private JavaFileInfo javaFileInfo;
54
55 /**
Vidyashree Rama506cbe12016-03-28 11:59:27 +053056 * File handle to maintain temporary java code fragments as per the code
57 * snippet types.
58 */
Bharat saraswal96dfef02016-06-16 00:29:12 +053059 private transient TempJavaCodeFragmentFiles tempFileHandle;
Vidyashree Rama506cbe12016-03-28 11:59:27 +053060
61 /**
62 * Creates an instance of java Notification.
63 */
64 public YangJavaNotification() {
65 super();
66 setJavaFileInfo(new JavaFileInfo());
Bharat saraswal33dfa012016-05-17 19:59:16 +053067 getJavaFileInfo().setGeneratedFileTypes(GENERATE_INTERFACE_WITH_BUILDER);
Vidyashree Rama506cbe12016-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
76 public JavaFileInfo getJavaFileInfo() {
77
78 if (javaFileInfo == null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +053079 throw new TranslatorException("Missing java info in java datamodel node");
Vidyashree Rama506cbe12016-03-28 11:59:27 +053080 }
81 return javaFileInfo;
82 }
83
84 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053085 * Sets the java file info object.
Vidyashree Rama506cbe12016-03-28 11:59:27 +053086 *
87 * @param javaInfo java file info object
88 */
89 @Override
90 public void setJavaFileInfo(JavaFileInfo javaInfo) {
Vidyashree Rama506cbe12016-03-28 11:59:27 +053091 javaFileInfo = javaInfo;
92 }
93
94 /**
Vidyashree Rama506cbe12016-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 Rama506cbe12016-03-28 11:59:27 +0530101 return tempFileHandle;
102 }
103
104 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530105 * Sets temporary file handle.
Vidyashree Rama506cbe12016-03-28 11:59:27 +0530106 *
107 * @param fileHandle temporary file handle
108 */
109 @Override
110 public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) {
Vidyashree Rama506cbe12016-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 bde4ffab2016-04-15 16:18:30 +0530118 * @param yangPlugin YANG plugin config
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530119 * @throws TranslatorException translator operation fail
Vidyashree Rama506cbe12016-03-28 11:59:27 +0530120 */
121 @Override
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530122 public void generateCodeEntry(YangPluginConfig yangPlugin) throws TranslatorException {
Vidyashree Rama506cbe12016-03-28 11:59:27 +0530123
Ray Milkey994c4982016-05-11 18:39:39 +0000124 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530125 * As part of the notification support the following files needs to be generated.
126 * 1) Subject of the notification(event), this is simple interface with builder class.
127 * 2) Event class extending "AbstractEvent" and defining event type enum.
128 * 3) Event listener interface extending "EventListener".
129 *
130 * The manager class needs to extend the ListenerRegistry.
131 */
Ray Milkey994c4982016-05-11 18:39:39 +0000132
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530133 // Generate subject of the notification(event), this is simple interface
134 // with builder class.
135 try {
136 generateCodeOfAugmentableNode(this, yangPlugin);
137 addNotificationToExtendsList();
138 } catch (IOException e) {
139 throw new TranslatorException(
Bharat saraswal96dfef02016-06-16 00:29:12 +0530140 "Failed to prepare generate code entry for notification node " + getName());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530141 }
142 }
Ray Milkey994c4982016-05-11 18:39:39 +0000143
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530144 /*Adds current notification info to the extends list so its parents service*/
145 private void addNotificationToExtendsList() {
Bharat saraswal96dfef02016-06-16 00:29:12 +0530146 YangNode parent = getParent();
Bharat saraswalb551aae2016-07-14 15:18:20 +0530147 TempJavaServiceFragmentFiles tempJavaServiceFragmentFiles = ((TempJavaCodeFragmentFilesContainer) parent)
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530148 .getTempJavaCodeFragmentFiles()
Bharat saraswalb551aae2016-07-14 15:18:20 +0530149 .getServiceTempFiles();
150 JavaExtendsListHolder holder = tempJavaServiceFragmentFiles.getJavaExtendsListHolder();
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530151 JavaQualifiedTypeInfo event = new JavaQualifiedTypeInfo();
152
153 String parentInfo = getCapitalCase(((JavaFileInfoContainer) parent)
154 .getJavaFileInfo().getJavaName());
155 event.setClassInfo(parentInfo + EVENT_STRING);
156 event.setPkgInfo(getJavaFileInfo().getPackage());
Bharat saraswalb551aae2016-07-14 15:18:20 +0530157 holder.addToExtendsList(event, parent, tempJavaServiceFragmentFiles);
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530158
159 JavaQualifiedTypeInfo eventListener = new JavaQualifiedTypeInfo();
160
161 eventListener.setClassInfo(parentInfo + EVENT_LISTENER_STRING);
162 eventListener.setPkgInfo(getJavaFileInfo().getPackage());
Bharat saraswalb551aae2016-07-14 15:18:20 +0530163 holder.addToExtendsList(eventListener, parent, tempJavaServiceFragmentFiles);
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530164
Vidyashree Rama506cbe12016-03-28 11:59:27 +0530165 }
166
167 /**
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530168 * Creates a java file using the YANG notification info.
Vidyashree Rama506cbe12016-03-28 11:59:27 +0530169 */
170 @Override
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530171 public void generateCodeExit() throws TranslatorException {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530172 try {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530173 getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_INTERFACE_WITH_BUILDER, this);
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530174 } catch (IOException e) {
Bharat saraswal96dfef02016-06-16 00:29:12 +0530175 throw new TranslatorException("Failed to generate code for notification node " + getName());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530176 }
Vidyashree Rama506cbe12016-03-28 11:59:27 +0530177
178 }
179}