janani b
Committed by Gerrit Code Review

Defect fix for YANG revision

Change-Id: I6af5e82122629ea69c40b4fc3159bc7712d8293d
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
17 package org.onosproject.yangutils.parser.impl.listeners; 17 package org.onosproject.yangutils.parser.impl.listeners;
18 18
19 import org.onosproject.yangutils.datamodel.YangModule; 19 import org.onosproject.yangutils.datamodel.YangModule;
20 +import org.onosproject.yangutils.datamodel.YangRevision;
20 import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; 21 import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
21 import org.onosproject.yangutils.parser.exceptions.ParserException; 22 import org.onosproject.yangutils.parser.exceptions.ParserException;
22 import org.onosproject.yangutils.parser.impl.TreeWalkListener; 23 import org.onosproject.yangutils.parser.impl.TreeWalkListener;
...@@ -30,6 +31,7 @@ import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorTyp ...@@ -30,6 +31,7 @@ import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorTyp
30 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER; 31 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
31 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; 32 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
32 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier; 33 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
34 +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.setCurrentDateForRevision;
33 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsEmpty; 35 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsEmpty;
34 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; 36 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
35 import static org.onosproject.yangutils.utils.YangConstructType.MODULE_DATA; 37 import static org.onosproject.yangutils.utils.YangConstructType.MODULE_DATA;
...@@ -85,6 +87,13 @@ public final class ModuleListener { ...@@ -85,6 +87,13 @@ public final class ModuleListener {
85 yangModule.setVersion((byte) 1); 87 yangModule.setVersion((byte) 1);
86 } 88 }
87 89
90 + if (ctx.moduleBody().revisionStatements().revisionStatement().isEmpty()) {
91 + String currentDate = setCurrentDateForRevision();
92 + YangRevision currentRevision = new YangRevision();
93 + currentRevision.setRevDate(currentDate);
94 + yangModule.setRevision(currentRevision);
95 + }
96 +
88 listener.getParsedDataStack().push(yangModule); 97 listener.getParsedDataStack().push(yangModule);
89 } 98 }
90 99
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
16 16
17 package org.onosproject.yangutils.parser.impl.listeners; 17 package org.onosproject.yangutils.parser.impl.listeners;
18 18
19 +import org.onosproject.yangutils.datamodel.YangRevision;
19 import org.onosproject.yangutils.datamodel.YangSubModule; 20 import org.onosproject.yangutils.datamodel.YangSubModule;
20 import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; 21 import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
21 import org.onosproject.yangutils.parser.exceptions.ParserException; 22 import org.onosproject.yangutils.parser.exceptions.ParserException;
...@@ -30,6 +31,7 @@ import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorTyp ...@@ -30,6 +31,7 @@ import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorTyp
30 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER; 31 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
31 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; 32 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
32 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier; 33 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
34 +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.setCurrentDateForRevision;
33 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsEmpty; 35 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsEmpty;
34 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; 36 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
35 import static org.onosproject.yangutils.utils.YangConstructType.SUB_MODULE_DATA; 37 import static org.onosproject.yangutils.utils.YangConstructType.SUB_MODULE_DATA;
...@@ -88,6 +90,13 @@ public final class SubModuleListener { ...@@ -88,6 +90,13 @@ public final class SubModuleListener {
88 yangSubModule.setVersion((byte) 1); 90 yangSubModule.setVersion((byte) 1);
89 } 91 }
90 92
93 + if (ctx.submoduleBody().revisionStatements().revisionStatement().isEmpty()) {
94 + String currentDate = setCurrentDateForRevision();
95 + YangRevision currentRevision = new YangRevision();
96 + currentRevision.setRevDate(currentDate);
97 + yangSubModule.setRevision(currentRevision);
98 + }
99 +
91 listener.getParsedDataStack().push(yangSubModule); 100 listener.getParsedDataStack().push(yangSubModule);
92 } 101 }
93 102
......
...@@ -20,6 +20,7 @@ import org.antlr.v4.runtime.ParserRuleContext; ...@@ -20,6 +20,7 @@ import org.antlr.v4.runtime.ParserRuleContext;
20 20
21 import java.text.ParseException; 21 import java.text.ParseException;
22 import java.text.SimpleDateFormat; 22 import java.text.SimpleDateFormat;
23 +import java.util.Calendar;
23 import java.util.regex.Pattern; 24 import java.util.regex.Pattern;
24 25
25 import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; 26 import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
...@@ -37,6 +38,11 @@ public final class ListenerUtil { ...@@ -37,6 +38,11 @@ public final class ListenerUtil {
37 private static final String TRUE_KEYWORD = "true"; 38 private static final String TRUE_KEYWORD = "true";
38 private static final String FALSE_KEYWORD = "false"; 39 private static final String FALSE_KEYWORD = "false";
39 private static final int IDENTIFIER_LENGTH = 64; 40 private static final int IDENTIFIER_LENGTH = 64;
41 + private static final String DATE_FORMAT = "yyyy-MM-dd";
42 + private static final String EMPTY_STRING = "";
43 + private static final String HYPHEN = "-";
44 + private static final String SLASH = "/";
45 + private static final String SPACE = " ";
40 46
41 /** 47 /**
42 * Creates a new listener util. 48 * Creates a new listener util.
...@@ -52,7 +58,7 @@ public final class ListenerUtil { ...@@ -52,7 +58,7 @@ public final class ListenerUtil {
52 */ 58 */
53 public static String removeQuotesAndHandleConcat(String yangStringData) { 59 public static String removeQuotesAndHandleConcat(String yangStringData) {
54 60
55 - yangStringData = yangStringData.replace("\"", ""); 61 + yangStringData = yangStringData.replace("\"", EMPTY_STRING);
56 String[] tmpData = yangStringData.split(Pattern.quote(PLUS)); 62 String[] tmpData = yangStringData.split(Pattern.quote(PLUS));
57 StringBuilder builder = new StringBuilder(); 63 StringBuilder builder = new StringBuilder();
58 for (String yangString : tmpData) { 64 for (String yangString : tmpData) {
...@@ -103,7 +109,7 @@ public final class ListenerUtil { ...@@ -103,7 +109,7 @@ public final class ListenerUtil {
103 return false; 109 return false;
104 } 110 }
105 111
106 - SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 112 + SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
107 sdf.setLenient(false); 113 sdf.setLenient(false);
108 114
109 try { 115 try {
...@@ -184,4 +190,18 @@ public final class ListenerUtil { ...@@ -184,4 +190,18 @@ public final class ListenerUtil {
184 throw parserException; 190 throw parserException;
185 } 191 }
186 } 192 }
193 +
194 + /**
195 + * Sets current date and makes it in usable format for revision.
196 + *
197 + * @return usable current date format for revision
198 + */
199 + public static String setCurrentDateForRevision() {
200 +
201 + Calendar date = Calendar.getInstance();
202 + SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
203 + String dateForRevision = ((dateFormat.format(date.getTime())).replaceAll(SLASH, HYPHEN)).replaceAll(SPACE,
204 + EMPTY_STRING);
205 + return dateForRevision;
206 + }
187 } 207 }
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -37,8 +37,8 @@ import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getAbsolutePac ...@@ -37,8 +37,8 @@ import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getAbsolutePac
37 * Module information extended to support java code generation. 37 * Module information extended to support java code generation.
38 */ 38 */
39 public class YangJavaModule extends YangModule 39 public class YangJavaModule extends YangModule
40 - implements JavaCodeGenerator, HasJavaFileInfo, 40 +implements JavaCodeGenerator, HasJavaFileInfo,
41 - HasJavaImportData, HasTempJavaCodeFragmentFiles { 41 +HasJavaImportData, HasTempJavaCodeFragmentFiles {
42 42
43 /** 43 /**
44 * Contains the information of the java file being generated. 44 * Contains the information of the java file being generated.
...@@ -76,7 +76,7 @@ public class YangJavaModule extends YangModule ...@@ -76,7 +76,7 @@ public class YangJavaModule extends YangModule
76 public JavaFileInfo getJavaFileInfo() { 76 public JavaFileInfo getJavaFileInfo() {
77 77
78 if (javaFileInfo == null) { 78 if (javaFileInfo == null) {
79 - throw new RuntimeException("Missing java info in java datamodel node"); 79 + throw new RuntimeException("Missing java info in java datamodel node.");
80 } 80 }
81 return javaFileInfo; 81 return javaFileInfo;
82 } 82 }
......
...@@ -25,6 +25,7 @@ import org.onosproject.yangutils.parser.impl.YangUtilsParserManager; ...@@ -25,6 +25,7 @@ import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
25 import java.io.IOException; 25 import java.io.IOException;
26 26
27 import static org.hamcrest.core.Is.is; 27 import static org.hamcrest.core.Is.is;
28 +import static org.hamcrest.core.IsNull.notNullValue;
28 import static org.junit.Assert.assertThat; 29 import static org.junit.Assert.assertThat;
29 30
30 /** 31 /**
...@@ -64,4 +65,14 @@ public class RevisionListenerTest { ...@@ -64,4 +65,14 @@ public class RevisionListenerTest {
64 65
65 YangNode node = manager.getDataModel("src/test/resources/RevisionInValidOrder.yang"); 66 YangNode node = manager.getDataModel("src/test/resources/RevisionInValidOrder.yang");
66 } 67 }
68 +
69 + /**
70 + * Checks the revision with current date is created for empty revision statement.
71 + */
72 + @Test
73 + public void processWithoutRevision() throws IOException, ParserException {
74 +
75 + YangNode node = manager.getDataModel("src/test/resources/RevisionAbsence.yang");
76 + assertThat(((YangModule) node).getRevision().getRevDate(), notNullValue());
77 + }
67 } 78 }
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -26,6 +26,7 @@ import org.onosproject.yangutils.parser.impl.YangUtilsParserManager; ...@@ -26,6 +26,7 @@ import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
26 import java.io.IOException; 26 import java.io.IOException;
27 27
28 import static org.hamcrest.core.Is.is; 28 import static org.hamcrest.core.Is.is;
29 +import static org.hamcrest.core.IsNull.notNullValue;
29 import static org.junit.Assert.assertThat; 30 import static org.junit.Assert.assertThat;
30 31
31 /** 32 /**
...@@ -107,6 +108,8 @@ public class SubModuleListenerTest { ...@@ -107,6 +108,8 @@ public class SubModuleListenerTest {
107 assertThat(yangNode.getBelongsTo().getBelongsToModuleName(), is("ONOS")); 108 assertThat(yangNode.getBelongsTo().getBelongsToModuleName(), is("ONOS"));
108 // Checks for the version value in data model tree. 109 // Checks for the version value in data model tree.
109 assertThat(yangNode.getBelongsTo().getPrefix(), is("On1")); 110 assertThat(yangNode.getBelongsTo().getPrefix(), is("On1"));
111 + //Checks the revision with current date is created for empty revision statement.
112 + assertThat(((YangSubModule) node).getRevision().getRevDate(), notNullValue());
110 } 113 }
111 114
112 /** 115 /**
......
1 +module Test {
2 +yang-version 1;
3 +namespace urn:ietf:params:xml:ns:yang:ietf-ospf;
4 +prefix test;
5 +}