Vidyashree Rama | 620b6e9 | 2016-03-28 11:59:27 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0f7908b | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Vidyashree Rama | 620b6e9 | 2016-03-28 11:59:27 +0530 | [diff] [blame] | 3 | * |
| 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 | |
| 17 | package org.onosproject.yangutils.translator.tojava.javamodel; |
| 18 | |
| 19 | import java.io.IOException; |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 20 | |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 21 | import org.onosproject.yangutils.datamodel.YangNode; |
Vidyashree Rama | 620b6e9 | 2016-03-28 11:59:27 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.datamodel.YangNotification; |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.translator.exception.TranslatorException; |
Vidyashree Rama | 620b6e9 | 2016-03-28 11:59:27 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator; |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorInfo; |
Vidyashree Rama | 620b6e9 | 2016-03-28 11:59:27 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.translator.tojava.JavaFileInfo; |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 27 | import org.onosproject.yangutils.translator.tojava.JavaFileInfoContainer; |
| 28 | import org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo; |
Vidyashree Rama | 620b6e9 | 2016-03-28 11:59:27 +0530 | [diff] [blame] | 29 | import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles; |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 30 | import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFilesContainer; |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame^] | 31 | import org.onosproject.yangutils.translator.tojava.TempJavaServiceFragmentFiles; |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 32 | import org.onosproject.yangutils.translator.tojava.utils.JavaExtendsListHolder; |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 33 | import org.onosproject.yangutils.utils.io.impl.YangPluginConfig; |
Vidyashree Rama | 620b6e9 | 2016-03-28 11:59:27 +0530 | [diff] [blame] | 34 | |
| 35 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_INTERFACE_WITH_BUILDER; |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 36 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCapitalCase; |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame^] | 37 | import static org.onosproject.yangutils.translator.tojava.YangJavaModelUtils.generateCodeOfAugmentableNode; |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 38 | import static org.onosproject.yangutils.utils.UtilConstants.EVENT_LISTENER_STRING; |
| 39 | import static org.onosproject.yangutils.utils.UtilConstants.EVENT_STRING; |
Vidyashree Rama | 620b6e9 | 2016-03-28 11:59:27 +0530 | [diff] [blame] | 40 | |
| 41 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 42 | * Represents notification information extended to support java code generation. |
Vidyashree Rama | 620b6e9 | 2016-03-28 11:59:27 +0530 | [diff] [blame] | 43 | */ |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 44 | public class YangJavaNotification |
| 45 | extends YangNotification |
| 46 | implements JavaCodeGenerator, JavaCodeGeneratorInfo { |
Vidyashree Rama | 620b6e9 | 2016-03-28 11:59:27 +0530 | [diff] [blame] | 47 | |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 48 | private static final long serialVersionUID = 806201624L; |
| 49 | |
Vidyashree Rama | 620b6e9 | 2016-03-28 11:59:27 +0530 | [diff] [blame] | 50 | /** |
| 51 | * Contains information of the java file being generated. |
| 52 | */ |
| 53 | private JavaFileInfo javaFileInfo; |
| 54 | |
| 55 | /** |
Vidyashree Rama | 620b6e9 | 2016-03-28 11:59:27 +0530 | [diff] [blame] | 56 | * File handle to maintain temporary java code fragments as per the code |
| 57 | * snippet types. |
| 58 | */ |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 59 | private transient TempJavaCodeFragmentFiles tempFileHandle; |
Vidyashree Rama | 620b6e9 | 2016-03-28 11:59:27 +0530 | [diff] [blame] | 60 | |
| 61 | /** |
| 62 | * Creates an instance of java Notification. |
| 63 | */ |
| 64 | public YangJavaNotification() { |
| 65 | super(); |
| 66 | setJavaFileInfo(new JavaFileInfo()); |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 67 | getJavaFileInfo().setGeneratedFileTypes(GENERATE_INTERFACE_WITH_BUILDER); |
Vidyashree Rama | 620b6e9 | 2016-03-28 11:59:27 +0530 | [diff] [blame] | 68 | } |
| 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 saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 79 | throw new TranslatorException("Missing java info in java datamodel node"); |
Vidyashree Rama | 620b6e9 | 2016-03-28 11:59:27 +0530 | [diff] [blame] | 80 | } |
| 81 | return javaFileInfo; |
| 82 | } |
| 83 | |
| 84 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 85 | * Sets the java file info object. |
Vidyashree Rama | 620b6e9 | 2016-03-28 11:59:27 +0530 | [diff] [blame] | 86 | * |
| 87 | * @param javaInfo java file info object |
| 88 | */ |
| 89 | @Override |
| 90 | public void setJavaFileInfo(JavaFileInfo javaInfo) { |
Vidyashree Rama | 620b6e9 | 2016-03-28 11:59:27 +0530 | [diff] [blame] | 91 | javaFileInfo = javaInfo; |
| 92 | } |
| 93 | |
| 94 | /** |
Vidyashree Rama | 620b6e9 | 2016-03-28 11:59:27 +0530 | [diff] [blame] | 95 | * Returns the temporary file handle. |
| 96 | * |
| 97 | * @return temporary file handle |
| 98 | */ |
| 99 | @Override |
| 100 | public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() { |
Vidyashree Rama | 620b6e9 | 2016-03-28 11:59:27 +0530 | [diff] [blame] | 101 | return tempFileHandle; |
| 102 | } |
| 103 | |
| 104 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 105 | * Sets temporary file handle. |
Vidyashree Rama | 620b6e9 | 2016-03-28 11:59:27 +0530 | [diff] [blame] | 106 | * |
| 107 | * @param fileHandle temporary file handle |
| 108 | */ |
| 109 | @Override |
| 110 | public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) { |
Vidyashree Rama | 620b6e9 | 2016-03-28 11:59:27 +0530 | [diff] [blame] | 111 | tempFileHandle = fileHandle; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Prepare the information for java code generation corresponding to YANG |
| 116 | * notification info. |
| 117 | * |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 118 | * @param yangPlugin YANG plugin config |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 119 | * @throws TranslatorException translator operation fail |
Vidyashree Rama | 620b6e9 | 2016-03-28 11:59:27 +0530 | [diff] [blame] | 120 | */ |
| 121 | @Override |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 122 | public void generateCodeEntry(YangPluginConfig yangPlugin) throws TranslatorException { |
Vidyashree Rama | 620b6e9 | 2016-03-28 11:59:27 +0530 | [diff] [blame] | 123 | |
Ray Milkey | 85971c6 | 2016-05-11 18:39:39 +0000 | [diff] [blame] | 124 | /** |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 125 | * 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 Milkey | 85971c6 | 2016-05-11 18:39:39 +0000 | [diff] [blame] | 132 | |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 133 | // 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 saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 140 | "Failed to prepare generate code entry for notification node " + getName()); |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 141 | } |
| 142 | } |
Ray Milkey | 85971c6 | 2016-05-11 18:39:39 +0000 | [diff] [blame] | 143 | |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 144 | /*Adds current notification info to the extends list so its parents service*/ |
| 145 | private void addNotificationToExtendsList() { |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 146 | YangNode parent = getParent(); |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame^] | 147 | TempJavaServiceFragmentFiles tempJavaServiceFragmentFiles = ((TempJavaCodeFragmentFilesContainer) parent) |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 148 | .getTempJavaCodeFragmentFiles() |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame^] | 149 | .getServiceTempFiles(); |
| 150 | JavaExtendsListHolder holder = tempJavaServiceFragmentFiles.getJavaExtendsListHolder(); |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 151 | 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 saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame^] | 157 | holder.addToExtendsList(event, parent, tempJavaServiceFragmentFiles); |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 158 | |
| 159 | JavaQualifiedTypeInfo eventListener = new JavaQualifiedTypeInfo(); |
| 160 | |
| 161 | eventListener.setClassInfo(parentInfo + EVENT_LISTENER_STRING); |
| 162 | eventListener.setPkgInfo(getJavaFileInfo().getPackage()); |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame^] | 163 | holder.addToExtendsList(eventListener, parent, tempJavaServiceFragmentFiles); |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 164 | |
Vidyashree Rama | 620b6e9 | 2016-03-28 11:59:27 +0530 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | /** |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 168 | * Creates a java file using the YANG notification info. |
Vidyashree Rama | 620b6e9 | 2016-03-28 11:59:27 +0530 | [diff] [blame] | 169 | */ |
| 170 | @Override |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 171 | public void generateCodeExit() throws TranslatorException { |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 172 | try { |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 173 | getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_INTERFACE_WITH_BUILDER, this); |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 174 | } catch (IOException e) { |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 175 | throw new TranslatorException("Failed to generate code for notification node " + getName()); |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 176 | } |
Vidyashree Rama | 620b6e9 | 2016-03-28 11:59:27 +0530 | [diff] [blame] | 177 | |
| 178 | } |
| 179 | } |