blob: 61eebfb6fcc855dc7743b5f3200484fe2bcdcc6b [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;
31import org.onosproject.yangutils.translator.tojava.utils.JavaExtendsListHolder;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053032import org.onosproject.yangutils.utils.io.impl.YangPluginConfig;
Vidyashree Rama506cbe12016-03-28 11:59:27 +053033
34import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_INTERFACE_WITH_BUILDER;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053035import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCapitalCase;
36import static org.onosproject.yangutils.translator.tojava.javamodel.YangJavaModelUtils.generateCodeOfAugmentableNode;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053037import static org.onosproject.yangutils.utils.UtilConstants.EVENT_LISTENER_STRING;
38import static org.onosproject.yangutils.utils.UtilConstants.EVENT_STRING;
Vidyashree Rama506cbe12016-03-28 11:59:27 +053039
40/**
Bharat saraswald9822e92016-04-05 15:13:44 +053041 * Represents notification information extended to support java code generation.
Vidyashree Rama506cbe12016-03-28 11:59:27 +053042 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053043public class YangJavaNotification
44 extends YangNotification
45 implements JavaCodeGenerator, JavaCodeGeneratorInfo {
Vidyashree Rama506cbe12016-03-28 11:59:27 +053046
Bharat saraswal96dfef02016-06-16 00:29:12 +053047 private static final long serialVersionUID = 806201624L;
48
Vidyashree Rama506cbe12016-03-28 11:59:27 +053049 /**
50 * Contains information of the java file being generated.
51 */
52 private JavaFileInfo javaFileInfo;
53
54 /**
Vidyashree Rama506cbe12016-03-28 11:59:27 +053055 * File handle to maintain temporary java code fragments as per the code
56 * snippet types.
57 */
Bharat saraswal96dfef02016-06-16 00:29:12 +053058 private transient TempJavaCodeFragmentFiles tempFileHandle;
Vidyashree Rama506cbe12016-03-28 11:59:27 +053059
60 /**
61 * Creates an instance of java Notification.
62 */
63 public YangJavaNotification() {
64 super();
65 setJavaFileInfo(new JavaFileInfo());
Bharat saraswal33dfa012016-05-17 19:59:16 +053066 getJavaFileInfo().setGeneratedFileTypes(GENERATE_INTERFACE_WITH_BUILDER);
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(
Bharat saraswal96dfef02016-06-16 00:29:12 +0530139 "Failed to prepare generate code entry for notification node " + getName());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530140 }
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() {
Bharat saraswal96dfef02016-06-16 00:29:12 +0530145 YangNode parent = getParent();
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530146 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 {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530170 try {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530171 getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_INTERFACE_WITH_BUILDER, this);
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530172 } catch (IOException e) {
Bharat saraswal96dfef02016-06-16 00:29:12 +0530173 throw new TranslatorException("Failed to generate code for notification node " + getName());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530174 }
Vidyashree Rama506cbe12016-03-28 11:59:27 +0530175
176 }
177}