blob: 34c42e8b6ec5d33dff32b9691a33b42aef5663d6 [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
Vidyashree Rama506cbe12016-03-28 11:59:27 +053021import org.onosproject.yangutils.datamodel.YangNotification;
Bharat saraswald9822e92016-04-05 15:13:44 +053022import org.onosproject.yangutils.translator.exception.TranslatorException;
Vidyashree Rama506cbe12016-03-28 11:59:27 +053023import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator;
24import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
Vidyashree Rama506cbe12016-03-28 11:59:27 +053025import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
janani bde4ffab2016-04-15 16:18:30 +053026import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig;
Vidyashree Rama506cbe12016-03-28 11:59:27 +053027
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053028import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_CLASS;
29import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_LISTENER_INTERFACE;
Vidyashree Rama506cbe12016-03-28 11:59:27 +053030import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_INTERFACE_WITH_BUILDER;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053031import static org.onosproject.yangutils.translator.tojava.utils.YangJavaModelUtils.generateCodeOfNode;
Vidyashree Rama506cbe12016-03-28 11:59:27 +053032
33/**
Bharat saraswald9822e92016-04-05 15:13:44 +053034 * Represents notification information extended to support java code generation.
Vidyashree Rama506cbe12016-03-28 11:59:27 +053035 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053036public class YangJavaNotification
37 extends YangNotification
38 implements JavaCodeGenerator, JavaCodeGeneratorInfo {
Vidyashree Rama506cbe12016-03-28 11:59:27 +053039
40 /**
41 * Contains information of the java file being generated.
42 */
43 private JavaFileInfo javaFileInfo;
44
45 /**
Vidyashree Rama506cbe12016-03-28 11:59:27 +053046 * File handle to maintain temporary java code fragments as per the code
47 * snippet types.
48 */
49 private TempJavaCodeFragmentFiles tempFileHandle;
50
51 /**
52 * Creates an instance of java Notification.
53 */
54 public YangJavaNotification() {
55 super();
56 setJavaFileInfo(new JavaFileInfo());
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053057 getJavaFileInfo().setGeneratedFileTypes(GENERATE_INTERFACE_WITH_BUILDER
58 | GENERATE_EVENT_CLASS | GENERATE_EVENT_LISTENER_INTERFACE);
Vidyashree Rama506cbe12016-03-28 11:59:27 +053059 }
60
61 /**
62 * Returns the generated java file information.
63 *
64 * @return generated java file information
65 */
66 @Override
67 public JavaFileInfo getJavaFileInfo() {
68
69 if (javaFileInfo == null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +053070 throw new TranslatorException("Missing java info in java datamodel node");
Vidyashree Rama506cbe12016-03-28 11:59:27 +053071 }
72 return javaFileInfo;
73 }
74
75 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053076 * Sets the java file info object.
Vidyashree Rama506cbe12016-03-28 11:59:27 +053077 *
78 * @param javaInfo java file info object
79 */
80 @Override
81 public void setJavaFileInfo(JavaFileInfo javaInfo) {
Vidyashree Rama506cbe12016-03-28 11:59:27 +053082 javaFileInfo = javaInfo;
83 }
84
85 /**
Vidyashree Rama506cbe12016-03-28 11:59:27 +053086 * Returns the temporary file handle.
87 *
88 * @return temporary file handle
89 */
90 @Override
91 public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() {
Vidyashree Rama506cbe12016-03-28 11:59:27 +053092 return tempFileHandle;
93 }
94
95 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053096 * Sets temporary file handle.
Vidyashree Rama506cbe12016-03-28 11:59:27 +053097 *
98 * @param fileHandle temporary file handle
99 */
100 @Override
101 public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) {
Vidyashree Rama506cbe12016-03-28 11:59:27 +0530102 tempFileHandle = fileHandle;
103 }
104
105 /**
106 * Prepare the information for java code generation corresponding to YANG
107 * notification info.
108 *
janani bde4ffab2016-04-15 16:18:30 +0530109 * @param yangPlugin YANG plugin config
Vidyashree Rama506cbe12016-03-28 11:59:27 +0530110 * @throws IOException IO operation fail
111 */
112 @Override
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530113 public void generateCodeEntry(YangPluginConfig yangPlugin)
114 throws IOException {
Vidyashree Rama506cbe12016-03-28 11:59:27 +0530115
Ray Milkey994c4982016-05-11 18:39:39 +0000116 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530117 * As part of the notification support the following files needs to be generated.
118 * 1) Subject of the notification(event), this is simple interface with builder class.
119 * 2) Event class extending "AbstractEvent" and defining event type enum.
120 * 3) Event listener interface extending "EventListener".
121 *
122 * The manager class needs to extend the ListenerRegistry.
123 */
Ray Milkey994c4982016-05-11 18:39:39 +0000124
125
126 // Generate subject of the notification(event), this is simple interface with builder class.
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530127 generateCodeOfNode(this, yangPlugin);
Vidyashree Rama506cbe12016-03-28 11:59:27 +0530128 }
129
130 /**
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530131 * Creates a java file using the YANG notification info.
Vidyashree Rama506cbe12016-03-28 11:59:27 +0530132 */
133 @Override
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530134 public void generateCodeExit()
135 throws IOException {
Ray Milkey994c4982016-05-11 18:39:39 +0000136 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530137 * As part of the notification support the following files needs to be generated.
138 * 1) Subject of the notification(event), this is simple interface with builder class.
139 * 2) Event class extending "AbstractEvent" and defining event type enum.
140 * 3) Event listener interface extending "EventListener".
141 *
142 * The manager class needs to extend the "ListenerRegistry".
143 */
144 getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_INTERFACE_WITH_BUILDER
145 | GENERATE_EVENT_CLASS | GENERATE_EVENT_LISTENER_INTERFACE, this);
Vidyashree Rama506cbe12016-03-28 11:59:27 +0530146
147 }
148}