Skip to content
This repository was archived by the owner on Nov 5, 2024. It is now read-only.

Commit 7b9726e

Browse files
[ONOS-8031]Yang revision date are stored with default timezone
Change-Id: I14265b2e6c2f2b82f183f6317f79ec675dbc085c
1 parent 65b9d17 commit 7b9726e

File tree

16 files changed

+147
-128
lines changed

16 files changed

+147
-128
lines changed

compiler/base/datamodel/src/main/java/org/onosproject/yang/compiler/datamodel/YangImport.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import org.slf4j.Logger;
2222

2323
import java.io.Serializable;
24-
import java.util.Date;
24+
import java.time.LocalDate;
2525
import java.util.Set;
2626

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

101101
/**
102102
* Reference to node which is imported.
@@ -151,7 +151,7 @@ public void setPrefixId(String prefixId) {
151151
*
152152
* @return the revision of the imported module
153153
*/
154-
public Date getRevision() {
154+
public LocalDate getRevision() {
155155
return revision;
156156
}
157157

@@ -160,7 +160,7 @@ public Date getRevision() {
160160
*
161161
* @param rev set the revision of the imported module
162162
*/
163-
public void setRevision(Date rev) {
163+
public void setRevision(LocalDate rev) {
164164
revision = rev;
165165
}
166166

@@ -224,7 +224,7 @@ public void setImportedNode(YangNode importedNode) {
224224
*/
225225
public void addReferenceToImport(Set<YangNode> yangNodeSet) throws DataModelException {
226226
String importedModuleName = getModuleName();
227-
Date importedModuleRevision = getRevision();
227+
LocalDate importedModuleRevision = getRevision();
228228
YangNode moduleNode = null;
229229
/*
230230
* Find the imported module node for a given module name with a

compiler/base/datamodel/src/main/java/org/onosproject/yang/compiler/datamodel/YangInclude.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import org.onosproject.yang.compiler.datamodel.utils.YangConstructType;
2121

2222
import java.io.Serializable;
23-
import java.util.Date;
23+
import java.time.LocalDate;
2424
import java.util.Set;
2525

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

6262
/**
6363
* Reference to node which is included.
@@ -93,7 +93,7 @@ public void setSubModuleName(String subModuleName) {
9393
*
9494
* @return the revision
9595
*/
96-
public Date getRevision() {
96+
public LocalDate getRevision() {
9797
return revision;
9898
}
9999

@@ -102,7 +102,7 @@ public Date getRevision() {
102102
*
103103
* @param revision the revision to set
104104
*/
105-
public void setRevision(Date revision) {
105+
public void setRevision(LocalDate revision) {
106106
this.revision = revision;
107107
}
108108

@@ -157,7 +157,7 @@ public void setIncludedNode(YangNode includedNode) {
157157
*/
158158
public YangSubModule addReferenceToInclude(Set<YangNode> yangNodeSet) throws DataModelException {
159159
String includedSubModuleName = getSubModuleName();
160-
Date includedSubModuleRevision = getRevision();
160+
LocalDate includedSubModuleRevision = getRevision();
161161
YangNode subModuleNode = null;
162162

163163
/*

compiler/base/datamodel/src/main/java/org/onosproject/yang/compiler/datamodel/YangRevision.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import org.onosproject.yang.compiler.datamodel.utils.YangConstructType;
2121

2222
import java.io.Serializable;
23-
import java.util.Date;
23+
import java.time.LocalDate;
2424

2525
/*
2626
* Reference:RFC 6020.
@@ -55,7 +55,7 @@ public class YangRevision
5555
/**
5656
* Revision date. Date string in the format "YYYY-MM-DD"
5757
*/
58-
private Date revDate;
58+
private LocalDate revDate;
5959

6060
/**
6161
* Description of revision.
@@ -78,7 +78,7 @@ public YangRevision() {
7878
*
7979
* @return the revision date
8080
*/
81-
public Date getRevDate() {
81+
public LocalDate getRevDate() {
8282
return revDate;
8383
}
8484

@@ -87,7 +87,7 @@ public Date getRevDate() {
8787
*
8888
* @param revDate the revision date to set
8989
*/
90-
public void setRevDate(Date revDate) {
90+
public void setRevDate(LocalDate revDate) {
9191
this.revDate = revDate;
9292
}
9393

compiler/base/datamodel/src/main/java/org/onosproject/yang/compiler/datamodel/utils/DataModelUtils.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@
7676
import java.nio.file.Files;
7777
import java.nio.file.Path;
7878
import java.nio.file.Paths;
79-
import java.text.SimpleDateFormat;
8079
import java.util.ArrayList;
8180
import java.util.Collection;
8281
import java.util.Enumeration;
@@ -1476,8 +1475,7 @@ private static YangNode getNode(YangNodeIdentifier nodeId,
14761475
public static String getDateInStringFormat(YangNode schemaNode) {
14771476
if (schemaNode != null) {
14781477
if (schemaNode.getRevision() != null) {
1479-
return new SimpleDateFormat(DATE_FORMAT)
1480-
.format(schemaNode.getRevision().getRevDate());
1478+
return schemaNode.getRevision().getRevDate().toString();
14811479
}
14821480
}
14831481
return null;

compiler/base/parser/src/main/java/org/onosproject/yang/compiler/parser/impl/listeners/RevisionDateListener.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import org.onosproject.yang.compiler.parser.exceptions.ParserException;
2323
import org.onosproject.yang.compiler.parser.impl.TreeWalkListener;
2424

25-
import java.util.Date;
25+
import java.time.LocalDate;
2626

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

91-
Date date = getValidDateFromString(ctx.dateArgumentString().getText(), ctx);
91+
LocalDate date = getValidDateFromString(ctx.dateArgumentString().getText(), ctx);
9292

9393
// Obtain the node of the stack.
9494
Parsable tmpNode = listener.getParsedDataStack().peek();

compiler/base/parser/src/main/java/org/onosproject/yang/compiler/parser/impl/listeners/RevisionListener.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import org.onosproject.yang.compiler.parser.exceptions.ParserException;
2424
import org.onosproject.yang.compiler.parser.impl.TreeWalkListener;
2525

26-
import java.util.Date;
26+
import java.time.LocalDate;
2727

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

92-
Date date = getValidDateFromString(ctx.dateArgumentString().getText(), ctx);
92+
LocalDate date = getValidDateFromString(ctx.dateArgumentString().getText(), ctx);
9393

9494
YangRevision revisionNode = new YangRevision();
9595
revisionNode.setRevDate(date);
@@ -123,8 +123,8 @@ public static void processRevisionExit(TreeWalkListener listener, RevisionStatem
123123
case MODULE_DATA: {
124124
YangModule module = (YangModule) tmpNode;
125125
if (module.getRevision() != null) {
126-
Date curRevisionDate = module.getRevision().getRevDate();
127-
if (curRevisionDate.before(((YangRevision) tmpRevisionNode).getRevDate())) {
126+
LocalDate curRevisionDate = module.getRevision().getRevDate();
127+
if (curRevisionDate.isBefore(((YangRevision) tmpRevisionNode).getRevDate())) {
128128
module.setRevision((YangRevision) tmpRevisionNode);
129129
}
130130
} else {
@@ -135,8 +135,8 @@ public static void processRevisionExit(TreeWalkListener listener, RevisionStatem
135135
case SUB_MODULE_DATA: {
136136
YangSubModule subModule = (YangSubModule) tmpNode;
137137
if (subModule.getRevision() != null) {
138-
Date curRevisionDate = subModule.getRevision().getRevDate();
139-
if (curRevisionDate.before(((YangRevision) tmpRevisionNode).getRevDate())) {
138+
LocalDate curRevisionDate = subModule.getRevision().getRevDate();
139+
if (curRevisionDate.isBefore(((YangRevision) tmpRevisionNode).getRevDate())) {
140140
subModule.setRevision((YangRevision) tmpRevisionNode);
141141
}
142142
} else {

compiler/base/parser/src/main/java/org/onosproject/yang/compiler/parser/impl/parserutils/ListenerUtil.java

Lines changed: 17 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,8 @@
3939
import org.onosproject.yang.compiler.translator.tojava.javamodel.YangJavaAugmentTranslator;
4040
import org.slf4j.Logger;
4141

42-
import java.text.ParseException;
43-
import java.text.SimpleDateFormat;
42+
import java.time.LocalDate;
4443
import java.util.ArrayList;
45-
import java.util.Date;
4644
import java.util.HashMap;
4745
import java.util.LinkedList;
4846
import java.util.List;
@@ -70,6 +68,7 @@
7068
import static org.onosproject.yang.compiler.utils.UtilConstants.CURRENT;
7169
import static org.onosproject.yang.compiler.utils.UtilConstants.EMPTY_STRING;
7270
import static org.onosproject.yang.compiler.utils.UtilConstants.FALSE;
71+
import static org.onosproject.yang.compiler.utils.UtilConstants.HYPHEN;
7372
import static org.onosproject.yang.compiler.utils.UtilConstants.IN;
7473
import static org.onosproject.yang.compiler.utils.UtilConstants.INVALID_TREE;
7574
import static org.onosproject.yang.compiler.utils.UtilConstants.ONE;
@@ -103,6 +102,8 @@ public final class ListenerUtil {
103102
Pattern.compile("\\[(.*?)\\]");
104103
private static final String XML = "xml";
105104
private static final int IDENTIFIER_LENGTH = 64;
105+
private static final int VALUE_CHECK = 10;
106+
private static final int ZERO = 0;
106107
private static final String DATE_FORMAT = "yyyy-MM-dd";
107108
private static final String REGEX_EQUAL = "[=]";
108109
private static final String REGEX_OPEN_BRACE = "[(]";
@@ -172,30 +173,6 @@ public static String getValidIdentifier(String identifier,
172173
throw parserException;
173174
}
174175

175-
/**
176-
* Validates the revision date.
177-
*
178-
* @param dateToValidate input revision date
179-
* @return validation result, true for success, false for failure
180-
*/
181-
public static boolean isDateValid(String dateToValidate) {
182-
if (dateToValidate == null || !dateToValidate.matches(DATE_PATTERN)) {
183-
return false;
184-
}
185-
186-
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
187-
sdf.setLenient(false);
188-
189-
try {
190-
//if not valid, it will throw ParseException
191-
sdf.parse(dateToValidate);
192-
} catch (ParseException e) {
193-
return false;
194-
}
195-
196-
return true;
197-
}
198-
199176
/**
200177
* Validates YANG version.
201178
*
@@ -310,27 +287,6 @@ public static boolean getValidBooleanValue(String booleanValue, YangConstructTyp
310287
}
311288
}
312289

313-
/**
314-
* Returns current date and makes it in usable format for revision.
315-
*
316-
* @return usable current date format for revision
317-
*/
318-
public static Date getCurrentDateForRevision() {
319-
320-
SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
321-
322-
Date date = new Date();
323-
String dateInString = dateFormat.format(date);
324-
try {
325-
//if not valid, it will throw ParseException
326-
Date now = dateFormat.parse(dateInString);
327-
return date;
328-
} catch (ParseException e) {
329-
ParserException parserException = new ParserException("YANG file error: Input date is not correct");
330-
throw parserException;
331-
}
332-
}
333-
334290
/**
335291
* Checks and return valid node identifier.
336292
*
@@ -630,7 +586,7 @@ public static void handleUnsupportedYangConstruct(YangConstructType type,
630586
* @param ctx yang construct's context to get the line number and character position
631587
* @return date format for revision
632588
*/
633-
public static Date getValidDateFromString(String dateInString, ParserRuleContext ctx) {
589+
public static LocalDate getValidDateFromString(String dateInString, ParserRuleContext ctx) {
634590
String dateArgument = removeQuotesAndHandleConcat(dateInString);
635591
if (!dateArgument.matches(DATE_PATTERN)) {
636592
ParserException parserException = new ParserException("YANG file error: Input date is not correct");
@@ -639,18 +595,20 @@ public static Date getValidDateFromString(String dateInString, ParserRuleContext
639595
throw parserException;
640596
}
641597

642-
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
643-
sdf.setLenient(false);
598+
String[] revisionArr = dateArgument.toString().split(HYPHEN);
644599

645-
try {
646-
//if not valid, it will throw ParseException
647-
return sdf.parse(dateArgument);
648-
} catch (ParseException e) {
649-
ParserException parserException = new ParserException("YANG file error: Input date is not correct");
650-
parserException.setLine(ctx.getStart().getLine());
651-
parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
652-
throw parserException;
600+
StringBuilder rev = new StringBuilder(revisionArr[0]);
601+
for (int i = 1; i < revisionArr.length; i++) {
602+
rev.append(HYPHEN);
603+
Integer val = Integer.parseInt(revisionArr[i]);
604+
if (val < VALUE_CHECK) {
605+
rev.append(ZERO);
606+
}
607+
rev.append(val);
653608
}
609+
610+
//if not valid, it will throw ParseException
611+
return LocalDate.parse(rev);
654612
}
655613

656614
/**

compiler/base/parser/src/test/java/org/onosproject/yang/compiler/parser/impl/listeners/ImportListenerTest.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
import java.io.IOException;
2626
import java.text.ParseException;
27-
import java.text.SimpleDateFormat;
27+
import java.time.LocalDate;
2828

2929
import static org.hamcrest.core.Is.is;
3030
import static org.junit.Assert.assertThat;
@@ -35,8 +35,6 @@
3535
public class ImportListenerTest {
3636

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

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

7674
// Checks for the revision value in data model tree.
77-
assertThat(((YangModule) node).getImportList().get(0).getRevision(), is(simpleDateFormat.parse("2015-02-03")));
75+
assertThat(((YangModule) node).getImportList().get(0).getRevision(), is(LocalDate.parse("2015-02-03")));
7876
// Checks for the prefix id in data model tree.
7977
assertThat(((YangModule) node).getImportList().get(0).getPrefixId(), is("On2"));
8078
// Checks for the module name in data model tree.

0 commit comments

Comments
 (0)