Skip to content
This repository was archived by the owner on Nov 5, 2024. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.slf4j.Logger;

import java.io.Serializable;
import java.util.Date;
import java.time.LocalDate;
import java.util.Set;

import static org.onosproject.yang.compiler.datamodel.utils.DataModelUtils.findReferredNode;
Expand Down Expand Up @@ -96,7 +96,7 @@ public class YangImport extends DefaultLocationInfo
* the most recent "revision" statement in the imported module. organization
* which defined the YANG module.
*/
private Date revision;
private LocalDate revision;

/**
* Reference to node which is imported.
Expand Down Expand Up @@ -151,7 +151,7 @@ public void setPrefixId(String prefixId) {
*
* @return the revision of the imported module
*/
public Date getRevision() {
public LocalDate getRevision() {
return revision;
}

Expand All @@ -160,7 +160,7 @@ public Date getRevision() {
*
* @param rev set the revision of the imported module
*/
public void setRevision(Date rev) {
public void setRevision(LocalDate rev) {
revision = rev;
}

Expand Down Expand Up @@ -224,7 +224,7 @@ public void setImportedNode(YangNode importedNode) {
*/
public void addReferenceToImport(Set<YangNode> yangNodeSet) throws DataModelException {
String importedModuleName = getModuleName();
Date importedModuleRevision = getRevision();
LocalDate importedModuleRevision = getRevision();
YangNode moduleNode = null;
/*
* Find the imported module node for a given module name with a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import org.onosproject.yang.compiler.datamodel.utils.YangConstructType;

import java.io.Serializable;
import java.util.Date;
import java.time.LocalDate;
import java.util.Set;

import static org.onosproject.yang.compiler.datamodel.utils.DataModelUtils.findReferredNode;
Expand Down Expand Up @@ -57,7 +57,7 @@ public class YangInclude extends DefaultLocationInfo
* The include's "revision-date" statement is used to specify the exact
* version of the submodule to import.
*/
private Date revision;
private LocalDate revision;

/**
* Reference to node which is included.
Expand Down Expand Up @@ -93,7 +93,7 @@ public void setSubModuleName(String subModuleName) {
*
* @return the revision
*/
public Date getRevision() {
public LocalDate getRevision() {
return revision;
}

Expand All @@ -102,7 +102,7 @@ public Date getRevision() {
*
* @param revision the revision to set
*/
public void setRevision(Date revision) {
public void setRevision(LocalDate revision) {
this.revision = revision;
}

Expand Down Expand Up @@ -157,7 +157,7 @@ public void setIncludedNode(YangNode includedNode) {
*/
public YangSubModule addReferenceToInclude(Set<YangNode> yangNodeSet) throws DataModelException {
String includedSubModuleName = getSubModuleName();
Date includedSubModuleRevision = getRevision();
LocalDate includedSubModuleRevision = getRevision();
YangNode subModuleNode = null;

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import org.onosproject.yang.compiler.datamodel.utils.YangConstructType;

import java.io.Serializable;
import java.util.Date;
import java.time.LocalDate;

/*
* Reference:RFC 6020.
Expand Down Expand Up @@ -55,7 +55,7 @@ public class YangRevision
/**
* Revision date. Date string in the format "YYYY-MM-DD"
*/
private Date revDate;
private LocalDate revDate;

/**
* Description of revision.
Expand All @@ -78,7 +78,7 @@ public YangRevision() {
*
* @return the revision date
*/
public Date getRevDate() {
public LocalDate getRevDate() {
return revDate;
}

Expand All @@ -87,7 +87,7 @@ public Date getRevDate() {
*
* @param revDate the revision date to set
*/
public void setRevDate(Date revDate) {
public void setRevDate(LocalDate revDate) {
this.revDate = revDate;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
Expand Down Expand Up @@ -1476,8 +1475,7 @@ private static YangNode getNode(YangNodeIdentifier nodeId,
public static String getDateInStringFormat(YangNode schemaNode) {
if (schemaNode != null) {
if (schemaNode.getRevision() != null) {
return new SimpleDateFormat(DATE_FORMAT)
.format(schemaNode.getRevision().getRevDate());
return schemaNode.getRevision().getRevDate().toString();
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.onosproject.yang.compiler.parser.exceptions.ParserException;
import org.onosproject.yang.compiler.parser.impl.TreeWalkListener;

import java.util.Date;
import java.time.LocalDate;

import static org.onosproject.yang.compiler.datamodel.utils.YangConstructType.REVISION_DATE_DATA;
import static org.onosproject.yang.compiler.parser.antlrgencode.GeneratedYangParser.RevisionDateStatementContext;
Expand Down Expand Up @@ -88,7 +88,7 @@ public static void processRevisionDateEntry(TreeWalkListener listener,
checkStackIsNotEmpty(listener, MISSING_HOLDER, REVISION_DATE_DATA, ctx.dateArgumentString().getText(),
ENTRY);

Date date = getValidDateFromString(ctx.dateArgumentString().getText(), ctx);
LocalDate date = getValidDateFromString(ctx.dateArgumentString().getText(), ctx);

// Obtain the node of the stack.
Parsable tmpNode = listener.getParsedDataStack().peek();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.onosproject.yang.compiler.parser.exceptions.ParserException;
import org.onosproject.yang.compiler.parser.impl.TreeWalkListener;

import java.util.Date;
import java.time.LocalDate;

import static org.onosproject.yang.compiler.datamodel.utils.YangConstructType.REVISION_DATA;
import static org.onosproject.yang.compiler.parser.antlrgencode.GeneratedYangParser.RevisionStatementContext;
Expand Down Expand Up @@ -89,7 +89,7 @@ public static void processRevisionEntry(TreeWalkListener listener,
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, REVISION_DATA, ctx.dateArgumentString().getText(), ENTRY);

Date date = getValidDateFromString(ctx.dateArgumentString().getText(), ctx);
LocalDate date = getValidDateFromString(ctx.dateArgumentString().getText(), ctx);

YangRevision revisionNode = new YangRevision();
revisionNode.setRevDate(date);
Expand Down Expand Up @@ -123,8 +123,8 @@ public static void processRevisionExit(TreeWalkListener listener, RevisionStatem
case MODULE_DATA: {
YangModule module = (YangModule) tmpNode;
if (module.getRevision() != null) {
Date curRevisionDate = module.getRevision().getRevDate();
if (curRevisionDate.before(((YangRevision) tmpRevisionNode).getRevDate())) {
LocalDate curRevisionDate = module.getRevision().getRevDate();
if (curRevisionDate.isBefore(((YangRevision) tmpRevisionNode).getRevDate())) {
module.setRevision((YangRevision) tmpRevisionNode);
}
} else {
Expand All @@ -135,8 +135,8 @@ public static void processRevisionExit(TreeWalkListener listener, RevisionStatem
case SUB_MODULE_DATA: {
YangSubModule subModule = (YangSubModule) tmpNode;
if (subModule.getRevision() != null) {
Date curRevisionDate = subModule.getRevision().getRevDate();
if (curRevisionDate.before(((YangRevision) tmpRevisionNode).getRevDate())) {
LocalDate curRevisionDate = subModule.getRevision().getRevDate();
if (curRevisionDate.isBefore(((YangRevision) tmpRevisionNode).getRevDate())) {
subModule.setRevision((YangRevision) tmpRevisionNode);
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,8 @@
import org.onosproject.yang.compiler.translator.tojava.javamodel.YangJavaAugmentTranslator;
import org.slf4j.Logger;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
Expand Down Expand Up @@ -70,6 +68,7 @@
import static org.onosproject.yang.compiler.utils.UtilConstants.CURRENT;
import static org.onosproject.yang.compiler.utils.UtilConstants.EMPTY_STRING;
import static org.onosproject.yang.compiler.utils.UtilConstants.FALSE;
import static org.onosproject.yang.compiler.utils.UtilConstants.HYPHEN;
import static org.onosproject.yang.compiler.utils.UtilConstants.IN;
import static org.onosproject.yang.compiler.utils.UtilConstants.INVALID_TREE;
import static org.onosproject.yang.compiler.utils.UtilConstants.ONE;
Expand Down Expand Up @@ -103,6 +102,8 @@ public final class ListenerUtil {
Pattern.compile("\\[(.*?)\\]");
private static final String XML = "xml";
private static final int IDENTIFIER_LENGTH = 64;
private static final int VALUE_CHECK = 10;
private static final int ZERO = 0;
private static final String DATE_FORMAT = "yyyy-MM-dd";
private static final String REGEX_EQUAL = "[=]";
private static final String REGEX_OPEN_BRACE = "[(]";
Expand Down Expand Up @@ -172,30 +173,6 @@ public static String getValidIdentifier(String identifier,
throw parserException;
}

/**
* Validates the revision date.
*
* @param dateToValidate input revision date
* @return validation result, true for success, false for failure
*/
public static boolean isDateValid(String dateToValidate) {
if (dateToValidate == null || !dateToValidate.matches(DATE_PATTERN)) {
return false;
}

SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
sdf.setLenient(false);

try {
//if not valid, it will throw ParseException
sdf.parse(dateToValidate);
} catch (ParseException e) {
return false;
}

return true;
}

/**
* Validates YANG version.
*
Expand Down Expand Up @@ -310,27 +287,6 @@ public static boolean getValidBooleanValue(String booleanValue, YangConstructTyp
}
}

/**
* Returns current date and makes it in usable format for revision.
*
* @return usable current date format for revision
*/
public static Date getCurrentDateForRevision() {

SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);

Date date = new Date();
String dateInString = dateFormat.format(date);
try {
//if not valid, it will throw ParseException
Date now = dateFormat.parse(dateInString);
return date;
} catch (ParseException e) {
ParserException parserException = new ParserException("YANG file error: Input date is not correct");
throw parserException;
}
}

/**
* Checks and return valid node identifier.
*
Expand Down Expand Up @@ -630,7 +586,7 @@ public static void handleUnsupportedYangConstruct(YangConstructType type,
* @param ctx yang construct's context to get the line number and character position
* @return date format for revision
*/
public static Date getValidDateFromString(String dateInString, ParserRuleContext ctx) {
public static LocalDate getValidDateFromString(String dateInString, ParserRuleContext ctx) {
String dateArgument = removeQuotesAndHandleConcat(dateInString);
if (!dateArgument.matches(DATE_PATTERN)) {
ParserException parserException = new ParserException("YANG file error: Input date is not correct");
Expand All @@ -639,18 +595,20 @@ public static Date getValidDateFromString(String dateInString, ParserRuleContext
throw parserException;
}

SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
sdf.setLenient(false);
String[] revisionArr = dateArgument.toString().split(HYPHEN);

try {
//if not valid, it will throw ParseException
return sdf.parse(dateArgument);
} catch (ParseException e) {
ParserException parserException = new ParserException("YANG file error: Input date is not correct");
parserException.setLine(ctx.getStart().getLine());
parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
throw parserException;
StringBuilder rev = new StringBuilder(revisionArr[0]);
for (int i = 1; i < revisionArr.length; i++) {
rev.append(HYPHEN);
Integer val = Integer.parseInt(revisionArr[i]);
if (val < VALUE_CHECK) {
rev.append(ZERO);
}
rev.append(val);
}

//if not valid, it will throw ParseException
return LocalDate.parse(rev);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
Expand All @@ -35,8 +35,6 @@
public class ImportListenerTest {

private final YangUtilsParserManager manager = new YangUtilsParserManager();
private static final String DATE_FORMAT = "yyyy-MM-dd";
private final SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_FORMAT);

/**
* Checks if mandatory parameter prefix is present in import.
Expand Down Expand Up @@ -74,7 +72,7 @@ public void processImportValidEntry() throws IOException, ParserException, Parse
YangNode node = manager.getDataModel("src/test/resources/ImportValidEntry.yang");

// Checks for the revision value in data model tree.
assertThat(((YangModule) node).getImportList().get(0).getRevision(), is(simpleDateFormat.parse("2015-02-03")));
assertThat(((YangModule) node).getImportList().get(0).getRevision(), is(LocalDate.parse("2015-02-03")));
// Checks for the prefix id in data model tree.
assertThat(((YangModule) node).getImportList().get(0).getPrefixId(), is("On2"));
// Checks for the module name in data model tree.
Expand Down
Loading