Skip to content
Merged
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
4 changes: 2 additions & 2 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ jobs:

steps:
- uses: actions/checkout@v3
- name: Set up JDK 8
- name: Set up JDK 11
uses: actions/setup-java@v3
with:
java-version: '8'
java-version: '11'
distribution: 'temurin'
cache: maven
- name: Build with Maven
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
<xerces.xercesImpl.version>2.12.2</xerces.xercesImpl.version>
<junit.jupyter.version>5.9.3</junit.jupyter.version>
<com.github.albfernandez.juniversalchardet.version>2.4.0</com.github.albfernandez.juniversalchardet.version>
<org.antlr.antlr4-runtime.version>4.9.3</org.antlr.antlr4-runtime.version>
<org.antlr.antlr4-runtime.version>4.11.1</org.antlr.antlr4-runtime.version>
<com.google.code.gson.version>2.10.1</com.google.code.gson.version>
<com.google.guava.version>33.5.0-jre</com.google.guava.version>
<com.google.protobuf.version>3.25.1</com.google.protobuf.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import org.yangcentral.yangkit.model.api.schema.YangSchemaContext;
import org.yangcentral.yangkit.parser.YangParserException;
import org.yangcentral.yangkit.parser.YangYinParser;

import java.util.Arrays;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
Expand Down Expand Up @@ -150,9 +150,12 @@ public void getMultipleYangModules() throws DocumentException, IOException, Yang
YangDataDocument yangDataDocument = new YangDataDocumentJsonParser(schemaContext).parse(jsonNode,validatorResultBuilder);

String[] result = yangDataDocument.getModulesStrings();
assertEquals(result.length, 2);
assertArrayEquals(result, new String[]{readFile(yangComplexFile), readFile(yangSimpleFile)});
String[] expected = new String[]{readFile(yangSimpleFile), readFile(yangComplexFile)};
Arrays.sort(result);
Arrays.sort(expected);

assertEquals(result.length, 2);
assertArrayEquals(result, expected);
}

public String readFile(String path){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
*/
public class App3 {
public static void main(String[] args) throws IOException, YangParserException, DocumentException {
InputStream inputStream = App3.class.getClassLoader().getResourceAsStream("insa-test.yang");
InputStream inputStream = App3.class.getClassLoader().getResourceAsStream("App3/yang/insa-test.yang");

// Parsing module
YangSchemaContext schemaContext = YangYinParser.parse(inputStream, "insa-custom", null);
Expand All @@ -49,7 +49,7 @@ public static void main(String[] args) throws IOException, YangParserException,

// Parsing from file with a yang module dependence
System.out.println("-------------------");
URL dependentYang = App3.class.getClassLoader().getResource("insa-test-dependence.yang");
URL dependentYang = App3.class.getClassLoader().getResource("App3/yang/insa-test-dependence.yang");
schemaContext = YangYinParser.parse(dependentYang.getFile(), schemaContext);
ValidatorResult result2 = schemaContext.validate();
System.out.println("Second yang is valid ? " + result2.isOk());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright 2023 INSA Lyon.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.yangcentral.yangkit.examples;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.dom4j.DocumentException;
import org.yangcentral.yangkit.common.api.validate.ValidatorRecord;
import org.yangcentral.yangkit.common.api.validate.ValidatorResult;
import org.yangcentral.yangkit.common.api.validate.ValidatorResultBuilder;
import org.yangcentral.yangkit.data.api.model.YangDataDocument;
import org.yangcentral.yangkit.data.codec.json.YangDataDocumentJsonParser;
import org.yangcentral.yangkit.model.api.schema.YangSchemaContext;
import org.yangcentral.yangkit.model.api.stmt.Module;
import org.yangcentral.yangkit.parser.YangParserException;
import org.yangcentral.yangkit.parser.YangYinParser;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

/**
* Usecase on how to use yang to validate YANG-push notification
*
*/
public class App5 {
public static void main(String[] args) throws IOException, YangParserException, DocumentException {

URL yangUrl = App5.class.getClassLoader().getResource("App5/interfaces");
String yangDir = yangUrl.getFile();

// Parsing module
YangSchemaContext schemaContext = YangYinParser.parse(yangDir);
ValidatorResult result = schemaContext.validate();
System.out.println("Valid? " + result.isOk());
System.out.println("Size modules = " + schemaContext.getModules().size());

int count = 0;
for (Module module : schemaContext.getModules()) {
System.out.println(count + "->Module["+ module.getModuleId().getModuleName()+"] prefix[" + module.getSelfPrefix()+"] | revision[" + module.getCurRevision().get() + "] ; root? [" + module.isSchemaTreeRoot() + "]");
count++;
}

if (!result.isOk()) {
for (ValidatorRecord<?, ?> record : result.getRecords()) {
System.out.println("Error: "+ record.getBadElement() + ':' + record.getErrorMsg().getMessage());

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for those printed info, how about using record.getSeverity() instead of all as Error?

(so that other severities such as Warning etc. can be distinguished from here)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only ‘Error’ means result is not ok,:)

}
} else {
System.out.println("YANGs valid");
for (ValidatorRecord<?, ?> record : result.getRecords()) {
System.out.println("valid: "+ record.getBadElement() + ':' + record.getErrorMsg().getMessage() + "; Severity:" + record.getSeverity().getFieldName());
}
}
System.out.println("------------ Validating valid message ------------");
InputStream jsonInputStream = App5.class.getClassLoader().getResourceAsStream("App5/json/interface.json");
JsonNode jsonElement = null;
ObjectMapper objectMapper = new ObjectMapper();
jsonElement = objectMapper.readTree(jsonInputStream);

System.out.println("Valid Message: " + jsonElement);

// Validating
ValidatorResultBuilder validatorResultBuilder = new ValidatorResultBuilder();
YangDataDocument doc = new YangDataDocumentJsonParser(schemaContext).parse(jsonElement, validatorResultBuilder);
doc.update();
ValidatorResult validatorResult = validatorResultBuilder.build();

System.out.println("Is deserialization ok (should be true)? " + validatorResult.isOk());

ValidatorResult validationResult = doc.validate();
System.out.println("second validation: " + validationResult.isOk());
if (!validationResult.isOk()) System.out.println(validationResult);

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright 2023 INSA Lyon.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.yangcentral.yangkit.examples;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.dom4j.DocumentException;
import org.yangcentral.yangkit.common.api.validate.ValidatorRecord;
import org.yangcentral.yangkit.common.api.validate.ValidatorResult;
import org.yangcentral.yangkit.common.api.validate.ValidatorResultBuilder;
import org.yangcentral.yangkit.data.api.model.YangDataDocument;
import org.yangcentral.yangkit.data.codec.json.YangDataDocumentJsonParser;
import org.yangcentral.yangkit.model.api.schema.YangSchemaContext;
import org.yangcentral.yangkit.model.api.stmt.Module;
import org.yangcentral.yangkit.parser.YangParserException;
import org.yangcentral.yangkit.parser.YangYinParser;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

/**
* Usecase on how to use yang to validate YANG-push notification
*
*/
public class App6 {
public static void main(String[] args) throws IOException, YangParserException, DocumentException {

URL yangUrl = App6.class.getClassLoader().getResource("App6/yangs");
String yangDir = yangUrl.getFile();

// Parsing module
YangSchemaContext schemaContext = YangYinParser.parse(yangDir);
ValidatorResult result = schemaContext.validate();
System.out.println("Valid? " + result.isOk());
System.out.println("Size modules = " + schemaContext.getModules().size());

int count = 0;
for (Module module : schemaContext.getModules()) {
System.out.println(count + "->Module["+ module.getModuleId().getModuleName()+"] prefix[" + module.getSelfPrefix()+"] | revision[" + module.getCurRevision().get() + "] ; root? [" + module.isSchemaTreeRoot() + "]");
count++;
}

if (!result.isOk()) {
for (ValidatorRecord<?, ?> record : result.getRecords()) {
System.out.println("Error: "+ record.getBadElement() + ':' + record.getErrorMsg().getMessage());
}
} else {
System.out.println("YANGs valid");
for (ValidatorRecord<?, ?> record : result.getRecords()) {
System.out.println("valid: "+ record.getBadElement() + ':' + record.getErrorMsg().getMessage() + "; Severity:" + record.getSeverity().getFieldName());
}
}
System.out.println("------------ Validating valid message ------------");
InputStream jsonInputStream = App6.class.getClassLoader().getResourceAsStream("App6/json/telemetry-msg.json");
JsonNode jsonElement = null;
ObjectMapper objectMapper = new ObjectMapper();
jsonElement = objectMapper.readTree(jsonInputStream);

System.out.println("Valid Message: " + jsonElement);

// Validating
ValidatorResultBuilder validatorResultBuilder = new ValidatorResultBuilder();
YangDataDocument doc = new YangDataDocumentJsonParser(schemaContext).parse(jsonElement, validatorResultBuilder);
doc.update();
ValidatorResult validatorResult = validatorResultBuilder.build();

System.out.println("Is deserialization ok (should be true)? " + validatorResult.isOk());

ValidatorResult validationResult = doc.validate();
System.out.println("second validation: " + validationResult.isOk());
if (!validationResult.isOk()) System.out.println(validationResult);

}
}
Loading
Loading