blob: a693820618ea17a0c0d01439a084cebe790b37df [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;
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +053022import org.onosproject.yangutils.datamodel.javadatamodel.JavaFileInfo;
23import org.onosproject.yangutils.datamodel.javadatamodel.YangJavaNotification;
24import org.onosproject.yangutils.datamodel.javadatamodel.YangPluginConfig;
Bharat saraswald9822e92016-04-05 15:13:44 +053025import org.onosproject.yangutils.translator.exception.TranslatorException;
Vidyashree Rama506cbe12016-03-28 11:59:27 +053026import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053027import org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorInfo;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053028import org.onosproject.yangutils.translator.tojava.JavaFileInfoContainer;
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +053029import org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfoTranslator;
Vidyashree Rama506cbe12016-03-28 11:59:27 +053030import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053031import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFilesContainer;
Bharat saraswalb551aae2016-07-14 15:18:20 +053032import org.onosproject.yangutils.translator.tojava.TempJavaServiceFragmentFiles;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053033import org.onosproject.yangutils.translator.tojava.utils.JavaExtendsListHolder;
Vidyashree Rama506cbe12016-03-28 11:59:27 +053034
35import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_INTERFACE_WITH_BUILDER;
Bharat saraswalb551aae2016-07-14 15:18:20 +053036import static org.onosproject.yangutils.translator.tojava.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;
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +053039import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCapitalCase;
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 */
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +053044public class YangJavaNotificationTranslator
45 extends YangJavaNotification
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053046 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 /**
Vidyashree Rama506cbe12016-03-28 11:59:27 +053051 * File handle to maintain temporary java code fragments as per the code
52 * snippet types.
53 */
Bharat saraswal96dfef02016-06-16 00:29:12 +053054 private transient TempJavaCodeFragmentFiles tempFileHandle;
Vidyashree Rama506cbe12016-03-28 11:59:27 +053055
56 /**
57 * Creates an instance of java Notification.
58 */
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +053059 public YangJavaNotificationTranslator() {
Vidyashree Rama506cbe12016-03-28 11:59:27 +053060 super();
61 setJavaFileInfo(new JavaFileInfo());
Bharat saraswal33dfa012016-05-17 19:59:16 +053062 getJavaFileInfo().setGeneratedFileTypes(GENERATE_INTERFACE_WITH_BUILDER);
Vidyashree Rama506cbe12016-03-28 11:59:27 +053063 }
64
65 /**
66 * Returns the generated java file information.
67 *
68 * @return generated java file information
69 */
70 @Override
71 public JavaFileInfo getJavaFileInfo() {
72
73 if (javaFileInfo == null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +053074 throw new TranslatorException("Missing java info in java datamodel node");
Vidyashree Rama506cbe12016-03-28 11:59:27 +053075 }
76 return javaFileInfo;
77 }
78
79 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053080 * Sets the java file info object.
Vidyashree Rama506cbe12016-03-28 11:59:27 +053081 *
82 * @param javaInfo java file info object
83 */
84 @Override
85 public void setJavaFileInfo(JavaFileInfo javaInfo) {
Vidyashree Rama506cbe12016-03-28 11:59:27 +053086 javaFileInfo = javaInfo;
87 }
88
89 /**
Vidyashree Rama506cbe12016-03-28 11:59:27 +053090 * Returns the temporary file handle.
91 *
92 * @return temporary file handle
93 */
94 @Override
95 public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() {
Vidyashree Rama506cbe12016-03-28 11:59:27 +053096 return tempFileHandle;
97 }
98
99 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530100 * Sets temporary file handle.
Vidyashree Rama506cbe12016-03-28 11:59:27 +0530101 *
102 * @param fileHandle temporary file handle
103 */
104 @Override
105 public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) {
Vidyashree Rama506cbe12016-03-28 11:59:27 +0530106 tempFileHandle = fileHandle;
107 }
108
109 /**
110 * Prepare the information for java code generation corresponding to YANG
111 * notification info.
112 *
janani bde4ffab2016-04-15 16:18:30 +0530113 * @param yangPlugin YANG plugin config
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530114 * @throws TranslatorException translator operation fail
Vidyashree Rama506cbe12016-03-28 11:59:27 +0530115 */
116 @Override
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530117 public void generateCodeEntry(YangPluginConfig yangPlugin) throws TranslatorException {
Vidyashree Rama506cbe12016-03-28 11:59:27 +0530118
Ray Milkey994c4982016-05-11 18:39:39 +0000119 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530120 * As part of the notification support the following files needs to be generated.
121 * 1) Subject of the notification(event), this is simple interface with builder class.
122 * 2) Event class extending "AbstractEvent" and defining event type enum.
123 * 3) Event listener interface extending "EventListener".
124 *
125 * The manager class needs to extend the ListenerRegistry.
126 */
Ray Milkey994c4982016-05-11 18:39:39 +0000127
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530128 // Generate subject of the notification(event), this is simple interface
129 // with builder class.
130 try {
131 generateCodeOfAugmentableNode(this, yangPlugin);
132 addNotificationToExtendsList();
133 } catch (IOException e) {
134 throw new TranslatorException(
Bharat saraswal96dfef02016-06-16 00:29:12 +0530135 "Failed to prepare generate code entry for notification node " + getName());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530136 }
137 }
Ray Milkey994c4982016-05-11 18:39:39 +0000138
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530139 /*Adds current notification info to the extends list so its parents service*/
140 private void addNotificationToExtendsList() {
Bharat saraswal96dfef02016-06-16 00:29:12 +0530141 YangNode parent = getParent();
Bharat saraswalb551aae2016-07-14 15:18:20 +0530142 TempJavaServiceFragmentFiles tempJavaServiceFragmentFiles = ((TempJavaCodeFragmentFilesContainer) parent)
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530143 .getTempJavaCodeFragmentFiles()
Bharat saraswalb551aae2016-07-14 15:18:20 +0530144 .getServiceTempFiles();
145 JavaExtendsListHolder holder = tempJavaServiceFragmentFiles.getJavaExtendsListHolder();
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530146 JavaQualifiedTypeInfoTranslator event = new JavaQualifiedTypeInfoTranslator();
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530147
148 String parentInfo = getCapitalCase(((JavaFileInfoContainer) parent)
149 .getJavaFileInfo().getJavaName());
150 event.setClassInfo(parentInfo + EVENT_STRING);
151 event.setPkgInfo(getJavaFileInfo().getPackage());
Bharat saraswalb551aae2016-07-14 15:18:20 +0530152 holder.addToExtendsList(event, parent, tempJavaServiceFragmentFiles);
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530153
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530154 JavaQualifiedTypeInfoTranslator eventListener = new JavaQualifiedTypeInfoTranslator();
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530155
156 eventListener.setClassInfo(parentInfo + EVENT_LISTENER_STRING);
157 eventListener.setPkgInfo(getJavaFileInfo().getPackage());
Bharat saraswalb551aae2016-07-14 15:18:20 +0530158 holder.addToExtendsList(eventListener, parent, tempJavaServiceFragmentFiles);
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530159
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) {
Bharat saraswal96dfef02016-06-16 00:29:12 +0530170 throw new TranslatorException("Failed to generate code for notification node " + getName());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530171 }
Vidyashree Rama506cbe12016-03-28 11:59:27 +0530172
173 }
174}