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