janani b
Committed by Gerrit Code Review

Defect fix for YANG revision

Change-Id: I6af5e82122629ea69c40b4fc3159bc7712d8293d
......@@ -17,6 +17,7 @@
package org.onosproject.yangutils.parser.impl.listeners;
import org.onosproject.yangutils.datamodel.YangModule;
import org.onosproject.yangutils.datamodel.YangRevision;
import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
import org.onosproject.yangutils.parser.exceptions.ParserException;
import org.onosproject.yangutils.parser.impl.TreeWalkListener;
......@@ -30,6 +31,7 @@ import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorTyp
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.setCurrentDateForRevision;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsEmpty;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
import static org.onosproject.yangutils.utils.YangConstructType.MODULE_DATA;
......@@ -85,6 +87,13 @@ public final class ModuleListener {
yangModule.setVersion((byte) 1);
}
if (ctx.moduleBody().revisionStatements().revisionStatement().isEmpty()) {
String currentDate = setCurrentDateForRevision();
YangRevision currentRevision = new YangRevision();
currentRevision.setRevDate(currentDate);
yangModule.setRevision(currentRevision);
}
listener.getParsedDataStack().push(yangModule);
}
......
......@@ -16,6 +16,7 @@
package org.onosproject.yangutils.parser.impl.listeners;
import org.onosproject.yangutils.datamodel.YangRevision;
import org.onosproject.yangutils.datamodel.YangSubModule;
import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
import org.onosproject.yangutils.parser.exceptions.ParserException;
......@@ -30,6 +31,7 @@ import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorTyp
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.setCurrentDateForRevision;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsEmpty;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
import static org.onosproject.yangutils.utils.YangConstructType.SUB_MODULE_DATA;
......@@ -88,6 +90,13 @@ public final class SubModuleListener {
yangSubModule.setVersion((byte) 1);
}
if (ctx.submoduleBody().revisionStatements().revisionStatement().isEmpty()) {
String currentDate = setCurrentDateForRevision();
YangRevision currentRevision = new YangRevision();
currentRevision.setRevDate(currentDate);
yangSubModule.setRevision(currentRevision);
}
listener.getParsedDataStack().push(yangSubModule);
}
......
......@@ -20,6 +20,7 @@ import org.antlr.v4.runtime.ParserRuleContext;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.regex.Pattern;
import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
......@@ -37,6 +38,11 @@ public final class ListenerUtil {
private static final String TRUE_KEYWORD = "true";
private static final String FALSE_KEYWORD = "false";
private static final int IDENTIFIER_LENGTH = 64;
private static final String DATE_FORMAT = "yyyy-MM-dd";
private static final String EMPTY_STRING = "";
private static final String HYPHEN = "-";
private static final String SLASH = "/";
private static final String SPACE = " ";
/**
* Creates a new listener util.
......@@ -52,7 +58,7 @@ public final class ListenerUtil {
*/
public static String removeQuotesAndHandleConcat(String yangStringData) {
yangStringData = yangStringData.replace("\"", "");
yangStringData = yangStringData.replace("\"", EMPTY_STRING);
String[] tmpData = yangStringData.split(Pattern.quote(PLUS));
StringBuilder builder = new StringBuilder();
for (String yangString : tmpData) {
......@@ -103,7 +109,7 @@ public final class ListenerUtil {
return false;
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
sdf.setLenient(false);
try {
......@@ -184,4 +190,18 @@ public final class ListenerUtil {
throw parserException;
}
}
/**
* Sets current date and makes it in usable format for revision.
*
* @return usable current date format for revision
*/
public static String setCurrentDateForRevision() {
Calendar date = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
String dateForRevision = ((dateFormat.format(date.getTime())).replaceAll(SLASH, HYPHEN)).replaceAll(SPACE,
EMPTY_STRING);
return dateForRevision;
}
}
\ No newline at end of file
......
......@@ -37,8 +37,8 @@ import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getAbsolutePac
* Module information extended to support java code generation.
*/
public class YangJavaModule extends YangModule
implements JavaCodeGenerator, HasJavaFileInfo,
HasJavaImportData, HasTempJavaCodeFragmentFiles {
implements JavaCodeGenerator, HasJavaFileInfo,
HasJavaImportData, HasTempJavaCodeFragmentFiles {
/**
* Contains the information of the java file being generated.
......@@ -76,7 +76,7 @@ public class YangJavaModule extends YangModule
public JavaFileInfo getJavaFileInfo() {
if (javaFileInfo == null) {
throw new RuntimeException("Missing java info in java datamodel node");
throw new RuntimeException("Missing java info in java datamodel node.");
}
return javaFileInfo;
}
......
......@@ -25,6 +25,7 @@ import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
import java.io.IOException;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNull.notNullValue;
import static org.junit.Assert.assertThat;
/**
......@@ -64,4 +65,14 @@ public class RevisionListenerTest {
YangNode node = manager.getDataModel("src/test/resources/RevisionInValidOrder.yang");
}
/**
* Checks the revision with current date is created for empty revision statement.
*/
@Test
public void processWithoutRevision() throws IOException, ParserException {
YangNode node = manager.getDataModel("src/test/resources/RevisionAbsence.yang");
assertThat(((YangModule) node).getRevision().getRevDate(), notNullValue());
}
}
\ No newline at end of file
......
......@@ -26,6 +26,7 @@ import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
import java.io.IOException;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNull.notNullValue;
import static org.junit.Assert.assertThat;
/**
......@@ -107,6 +108,8 @@ public class SubModuleListenerTest {
assertThat(yangNode.getBelongsTo().getBelongsToModuleName(), is("ONOS"));
// Checks for the version value in data model tree.
assertThat(yangNode.getBelongsTo().getPrefix(), is("On1"));
//Checks the revision with current date is created for empty revision statement.
assertThat(((YangSubModule) node).getRevision().getRevDate(), notNullValue());
}
/**
......
module Test {
yang-version 1;
namespace urn:ietf:params:xml:ns:yang:ietf-ospf;
prefix test;
}