Vidyashree Rama | 506cbe1 | 2016-03-28 11:59:27 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Vidyashree Rama | 506cbe1 | 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 | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 20 | |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 21 | import org.onosproject.yangutils.datamodel.YangNode; |
Vidyashree Rama | 506cbe1 | 2016-03-28 11:59:27 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.datamodel.YangNotification; |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.translator.exception.TranslatorException; |
Vidyashree Rama | 506cbe1 | 2016-03-28 11:59:27 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator; |
| 25 | import org.onosproject.yangutils.translator.tojava.JavaFileInfo; |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.translator.tojava.JavaFileInfoContainer; |
| 27 | import org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo; |
Vidyashree Rama | 506cbe1 | 2016-03-28 11:59:27 +0530 | [diff] [blame] | 28 | import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles; |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 29 | import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFilesContainer; |
| 30 | import org.onosproject.yangutils.translator.tojava.utils.JavaExtendsListHolder; |
janani b | de4ffab | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 31 | import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig; |
Vidyashree Rama | 506cbe1 | 2016-03-28 11:59:27 +0530 | [diff] [blame] | 32 | |
| 33 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_INTERFACE_WITH_BUILDER; |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 34 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCapitalCase; |
| 35 | import static org.onosproject.yangutils.translator.tojava.utils.YangJavaModelUtils.generateCodeOfAugmentableNode; |
| 36 | import static org.onosproject.yangutils.utils.UtilConstants.EVENT_LISTENER_STRING; |
| 37 | import static org.onosproject.yangutils.utils.UtilConstants.EVENT_STRING; |
Vidyashree Rama | 506cbe1 | 2016-03-28 11:59:27 +0530 | [diff] [blame] | 38 | |
| 39 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 40 | * Represents notification information extended to support java code generation. |
Vidyashree Rama | 506cbe1 | 2016-03-28 11:59:27 +0530 | [diff] [blame] | 41 | */ |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 42 | public class YangJavaNotification |
| 43 | extends YangNotification |
| 44 | implements JavaCodeGenerator, JavaCodeGeneratorInfo { |
Vidyashree Rama | 506cbe1 | 2016-03-28 11:59:27 +0530 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * Contains information of the java file being generated. |
| 48 | */ |
| 49 | private JavaFileInfo javaFileInfo; |
| 50 | |
| 51 | /** |
Vidyashree Rama | 506cbe1 | 2016-03-28 11:59:27 +0530 | [diff] [blame] | 52 | * File handle to maintain temporary java code fragments as per the code |
| 53 | * snippet types. |
| 54 | */ |
| 55 | private TempJavaCodeFragmentFiles tempFileHandle; |
| 56 | |
| 57 | /** |
| 58 | * Creates an instance of java Notification. |
| 59 | */ |
| 60 | public YangJavaNotification() { |
| 61 | super(); |
| 62 | setJavaFileInfo(new JavaFileInfo()); |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 63 | getJavaFileInfo().setGeneratedFileTypes(GENERATE_INTERFACE_WITH_BUILDER); |
Vidyashree Rama | 506cbe1 | 2016-03-28 11:59:27 +0530 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Returns the generated java file information. |
| 68 | * |
| 69 | * @return generated java file information |
| 70 | */ |
| 71 | @Override |
| 72 | public JavaFileInfo getJavaFileInfo() { |
| 73 | |
| 74 | if (javaFileInfo == null) { |
Bharat saraswal | 6ef0b76 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 75 | throw new TranslatorException("Missing java info in java datamodel node"); |
Vidyashree Rama | 506cbe1 | 2016-03-28 11:59:27 +0530 | [diff] [blame] | 76 | } |
| 77 | return javaFileInfo; |
| 78 | } |
| 79 | |
| 80 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 81 | * Sets the java file info object. |
Vidyashree Rama | 506cbe1 | 2016-03-28 11:59:27 +0530 | [diff] [blame] | 82 | * |
| 83 | * @param javaInfo java file info object |
| 84 | */ |
| 85 | @Override |
| 86 | public void setJavaFileInfo(JavaFileInfo javaInfo) { |
Vidyashree Rama | 506cbe1 | 2016-03-28 11:59:27 +0530 | [diff] [blame] | 87 | javaFileInfo = javaInfo; |
| 88 | } |
| 89 | |
| 90 | /** |
Vidyashree Rama | 506cbe1 | 2016-03-28 11:59:27 +0530 | [diff] [blame] | 91 | * Returns the temporary file handle. |
| 92 | * |
| 93 | * @return temporary file handle |
| 94 | */ |
| 95 | @Override |
| 96 | public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() { |
Vidyashree Rama | 506cbe1 | 2016-03-28 11:59:27 +0530 | [diff] [blame] | 97 | return tempFileHandle; |
| 98 | } |
| 99 | |
| 100 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 101 | * Sets temporary file handle. |
Vidyashree Rama | 506cbe1 | 2016-03-28 11:59:27 +0530 | [diff] [blame] | 102 | * |
| 103 | * @param fileHandle temporary file handle |
| 104 | */ |
| 105 | @Override |
| 106 | public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) { |
Vidyashree Rama | 506cbe1 | 2016-03-28 11:59:27 +0530 | [diff] [blame] | 107 | tempFileHandle = fileHandle; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Prepare the information for java code generation corresponding to YANG |
| 112 | * notification info. |
| 113 | * |
janani b | de4ffab | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 114 | * @param yangPlugin YANG plugin config |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 115 | * @throws TranslatorException translator operation fail |
Vidyashree Rama | 506cbe1 | 2016-03-28 11:59:27 +0530 | [diff] [blame] | 116 | */ |
| 117 | @Override |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 118 | public void generateCodeEntry(YangPluginConfig yangPlugin) throws TranslatorException { |
Vidyashree Rama | 506cbe1 | 2016-03-28 11:59:27 +0530 | [diff] [blame] | 119 | |
Ray Milkey | 994c498 | 2016-05-11 18:39:39 +0000 | [diff] [blame] | 120 | /** |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 121 | * As part of the notification support the following files needs to be generated. |
| 122 | * 1) Subject of the notification(event), this is simple interface with builder class. |
| 123 | * 2) Event class extending "AbstractEvent" and defining event type enum. |
| 124 | * 3) Event listener interface extending "EventListener". |
| 125 | * |
| 126 | * The manager class needs to extend the ListenerRegistry. |
| 127 | */ |
Ray Milkey | 994c498 | 2016-05-11 18:39:39 +0000 | [diff] [blame] | 128 | |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 129 | // Generate subject of the notification(event), this is simple interface |
| 130 | // with builder class. |
| 131 | try { |
| 132 | generateCodeOfAugmentableNode(this, yangPlugin); |
| 133 | addNotificationToExtendsList(); |
| 134 | } catch (IOException e) { |
| 135 | throw new TranslatorException( |
| 136 | "Failed to prepare generate code entry for notification node " + this.getName()); |
| 137 | } |
| 138 | } |
Ray Milkey | 994c498 | 2016-05-11 18:39:39 +0000 | [diff] [blame] | 139 | |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 140 | /*Adds current notification info to the extends list so its parents service*/ |
| 141 | private void addNotificationToExtendsList() { |
| 142 | YangNode parent = this.getParent(); |
| 143 | JavaExtendsListHolder holder = ((TempJavaCodeFragmentFilesContainer) parent) |
| 144 | .getTempJavaCodeFragmentFiles() |
| 145 | .getServiceTempFiles().getJavaExtendsListHolder(); |
| 146 | JavaQualifiedTypeInfo event = new JavaQualifiedTypeInfo(); |
| 147 | |
| 148 | String parentInfo = getCapitalCase(((JavaFileInfoContainer) parent) |
| 149 | .getJavaFileInfo().getJavaName()); |
| 150 | event.setClassInfo(parentInfo + EVENT_STRING); |
| 151 | event.setPkgInfo(getJavaFileInfo().getPackage()); |
| 152 | holder.addToExtendsList(event, parent); |
| 153 | |
| 154 | JavaQualifiedTypeInfo eventListener = new JavaQualifiedTypeInfo(); |
| 155 | |
| 156 | eventListener.setClassInfo(parentInfo + EVENT_LISTENER_STRING); |
| 157 | eventListener.setPkgInfo(getJavaFileInfo().getPackage()); |
| 158 | holder.addToExtendsList(eventListener, parent); |
| 159 | |
Vidyashree Rama | 506cbe1 | 2016-03-28 11:59:27 +0530 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /** |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 163 | * Creates a java file using the YANG notification info. |
Vidyashree Rama | 506cbe1 | 2016-03-28 11:59:27 +0530 | [diff] [blame] | 164 | */ |
| 165 | @Override |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 166 | public void generateCodeExit() throws TranslatorException { |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 167 | try { |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 168 | getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_INTERFACE_WITH_BUILDER, this); |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 169 | } catch (IOException e) { |
| 170 | throw new TranslatorException("Failed to generate code for notification node " + this.getName()); |
| 171 | } |
Vidyashree Rama | 506cbe1 | 2016-03-28 11:59:27 +0530 | [diff] [blame] | 172 | |
| 173 | } |
| 174 | } |