Skip to content
Open
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
2 changes: 1 addition & 1 deletion src/main/java/io/lighty/yang/validator/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ private static void runLywForeachYangFile(final List<String> yangFiles, final Co
for (final Module module : lyvContext.testedModules()) {
format.emit(module);
}
format.close();
format.close(lyvContext.testedModules());
}

private static void generateHtmlAnalyzeOutput(final List<String> yangFiles, final Configuration config)
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/io/lighty/yang/validator/formats/Emitter.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import io.lighty.yang.validator.config.Configuration;
import io.lighty.yang.validator.simplify.SchemaTree;
import java.util.Collection;
import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
import org.opendaylight.yangtools.yang.model.api.Module;

Expand All @@ -33,5 +34,5 @@ public interface Emitter {
/**
* Close after printing out the module.
*/
void close();
void close(Collection<Module> modules);
}
5 changes: 3 additions & 2 deletions src/main/java/io/lighty/yang/validator/formats/Format.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import io.lighty.yang.validator.config.Configuration;
import io.lighty.yang.validator.simplify.SchemaTree;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
Expand Down Expand Up @@ -56,7 +57,7 @@ public void emit(final Module module) {
}

@Override
public void close() {
this.usedFormat.close();
public void close(Collection<Module> modules) {
this.usedFormat.close(modules);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import io.lighty.yang.validator.simplify.SchemaTree;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.Optional;
import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
import org.opendaylight.yangtools.yang.model.api.Module;
Expand Down Expand Up @@ -48,7 +49,7 @@ void init(final EffectiveModelContext context, final SchemaTree tree, final Conf
/**
* Close after printing out the module.
*/
protected void close() {
protected void close(Collection<Module> modules) {
// no-op by default
}

Expand Down
79 changes: 70 additions & 9 deletions src/main/java/io/lighty/yang/validator/formats/JsTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
import com.google.common.io.Resources;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import io.lighty.yang.validator.GroupArguments;
import io.lighty.yang.validator.config.Configuration;
import io.lighty.yang.validator.formats.utility.LyvNodeData;
import io.lighty.yang.validator.formats.utility.LyvStack;
import io.lighty.yang.validator.simplify.SchemaTree;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
Expand All @@ -33,6 +35,7 @@
import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
import org.opendaylight.yangtools.yang.model.api.Module;
import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
Expand Down Expand Up @@ -72,12 +75,8 @@ public void emitFormat(final Module module) {

// Notifications
printLines(getNotificationsLines(singletonListInitializer, module));

LOG.info("</table>");
LOG.info("</div>");
LOG.info("{}", loadJS());
LOG.info("</body>");
LOG.info("</html>");
} else {
LOG.error(EMPTY_MODULE_EXCEPTION);
}
Expand Down Expand Up @@ -155,7 +154,7 @@ private List<Line> getRpcsLines(final SingletonListInitializer singletonListInit
private List<Line> getChildNodesLines(final SingletonListInitializer singletonListInitializer,
final Module module) {
final List<Line> lines = new ArrayList<>();
final String headerText = prepareHeader(module);
final String headerText = prepareModule(module);
LOG.info("{}", headerText);
for (final Module m : modelContext.getModules()) {
if (!m.getPrefix().equals(module.getPrefix())) {
Expand Down Expand Up @@ -237,30 +236,73 @@ private static RpcInputOutput getRpcInputOutput(final List<QName> qnames,
return inputOutputOther;
}

private static String loadJS() {
private static String loadJS(Collection<Module> modules) {
final URL url = Resources.getResource("js");
String text = "";
try {
text = Resources.toString(url, StandardCharsets.UTF_8);

final StringBuilder moduleCode = new StringBuilder();

for (Module module : modules) {

// Define the multi-line string to add
final String multiLineStringToAdd =
" $('#basic-" + module.getName()
+ "').simpleTreeTable({\n"
+ "expander: $('#expander-" + module.getName() + "'),\n"
+ "collapser: $('#collapser-" + module.getName() + "')\n});\n";
moduleCode.append(multiLineStringToAdd);
}

// Encapsulate module code within $(document).ready(function () {...});
final String encapsulatedCode = "\n$(document).ready(function () {\n" + moduleCode + "});\n";

// Find the index of the placeholder comment
final String placeholderComment = "//AddTableLogic";
final int commentIndex = text.indexOf(placeholderComment);

if (commentIndex != -1) {
// Insert the encapsulated code after the comment
final StringBuilder modifiedText = new StringBuilder(text);
modifiedText.insert(commentIndex + placeholderComment.length(), encapsulatedCode);
text = modifiedText.toString();
}

} catch (final IOException e) {
LOG.error("Can not load text from js file");
}

return text;
}

private static String prepareHeader(final Module module) {
private static String prepareHeader() {
final URL url = Resources.getResource("header");
String text = "";
try {
text = Resources.toString(url, StandardCharsets.UTF_8);
} catch (final IOException e) {
LOG.error("Can not load text from header file");
}

return text;
}

private static String prepareModule(final Module module) {
final StringBuilder nameRevision = new StringBuilder(module.getName());
module.getRevision().ifPresent(value -> nameRevision.append("@").append(value));
final URL url = Resources.getResource("header");
final URL url = Resources.getResource("module");
String text = "";
try {
text = Resources.toString(url, StandardCharsets.UTF_8);
text = text.replace("expander", "expander-" + module.getName());
text = text.replace("collapser", "collapser-" + module.getName());
text = text.replace("basic", "basic-" + module.getName());
text = text.replace("<NAME_REVISION>", nameRevision);
text = text.replace("<NAMESPACE>", module.getNamespace().toString());
text = text.replace("<PREFIX>", module.getPrefix());
} catch (final IOException e) {
LOG.error("Can not load text from header file");
LOG.error("Can not load text from module file");
}

return text;
Expand Down Expand Up @@ -369,6 +411,16 @@ private void resolveDataNodeContainer(final Iterator<? extends DataSchemaNode> c
}
}


@Override
@SuppressFBWarnings(value = "SLF4J_SIGN_ONLY_FORMAT",
justification = "Valid output from LYV is dependent on Logback output")
void init(EffectiveModelContext context, SchemaTree tree, Configuration config) {
super.init(context, tree, config);
final String headerText = prepareHeader();
LOG.info("{}", headerText);
}

@Override
public Help getHelp() {
return new Help(HELP_NAME, HELP_DESCRIPTION);
Expand All @@ -379,6 +431,15 @@ public Optional<GroupArguments> getGroupArguments() {
return Optional.empty();
}

@Override
@SuppressFBWarnings(value = "SLF4J_SIGN_ONLY_FORMAT",
justification = "Valid output from LYV is dependent on Logback output")
public void close(Collection<Module> modules) {
LOG.info("{}", loadJS(modules));
LOG.info("</body>");
LOG.info("</html>");
}

private static class SingletonListInitializer {

private int id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public void emitFormat(final Module module) {
@SuppressFBWarnings(value = "SLF4J_SIGN_ONLY_FORMAT",
justification = "Valid output from LYV is dependent on Logback output")
@Override
public void close() {
public void close(Collection<Module> modules) {
LOG.info("{}", new JSONObject().put("parsed-models", parsedModels).toString(4));
parsedModels.clear();
}
Expand Down
12 changes: 1 addition & 11 deletions src/main/resources/header
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,4 @@ table.simple-tree-table tr.tree-closed .tree-icon:after {
content:"+"
}
</style>
</head><body>
<div> <b> module name <NAME_REVISION>, namespace <NAMESPACE>, prefix <PREFIX> </b></div><div>
<button type="button" id="expander" class="btn btn-danger">Expand All</button>
<button type="button" id="collapser" class="btn btn-info">Collapse All</button>
<table id="basic" class="table table-bordered table-striped simple-tree-table">
<th> element </th>
<th> schema </th>
<th> type </th>
<th> flag </th>
<th> status </th>
<th> path </th>
</head><body>
12 changes: 1 addition & 11 deletions src/main/resources/js
Original file line number Diff line number Diff line change
Expand Up @@ -325,15 +325,5 @@ $.fn.simpleTreeTable = function(options) {

$.SimpleTreeTable = SimpleTreeTable;

$('#basic').simpleTreeTable({
expander: $('#expander'),
collapser: $('#collapser')
});

$('#collapsed').simpleTreeTable({
opened: 'none',
});
$('#collapsed').simpleTreeTable({
margin: 25
});
//AddTableLogic
</script>
10 changes: 10 additions & 0 deletions src/main/resources/module
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<div> <b> module name <NAME_REVISION>, namespace <NAMESPACE>, prefix <PREFIX> </b></div><div>
<button type="button" id="expander" class="btn btn-danger">Expand All</button>
<button type="button" id="collapser" class="btn btn-info">Collapse All</button>
<table id="basic" class="table table-bordered table-striped simple-tree-table">
<th> element </th>
<th> schema </th>
<th> type </th>
<th> flag </th>
<th> status </th>
<th> path </th>
2 changes: 1 addition & 1 deletion src/test/java/io/lighty/yang/validator/MainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void testSimplifyWithYangFormat() throws Exception {
for (final Module module : effectiveModelContext.getModules()) {
format.emit(module);
}
format.close();
format.close((Collection<Module>) effectiveModelContext.getModules());
contextFactory =
new YangContextFactory(List.of(outPath), List.of(), Collections.emptySet(), false);
effectiveModelContext = contextFactory.createContext(true);
Expand Down
30 changes: 30 additions & 0 deletions src/test/java/io/lighty/yang/validator/formats/JsTreeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@

import static io.lighty.yang.validator.Main.startLyv;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;

import io.lighty.yang.validator.FormatTest;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down Expand Up @@ -69,6 +71,34 @@ public void runDeviationTest() throws Exception {
runJsTreeTest("moduleDeviation.html");
}

@Test
public void testMultipleFiles() throws IOException {
final String module1 = Paths.get(yangPath + "/ietf-connection-oriented-oam@2019-04-16.yang").toString();
final String module2 = Paths.get(yangPath + "/ietf-routing@2018-03-13.yang").toString();
builder.setYangModules(List.of(module1, module2));
final List<FormatPlugin> formats = new ArrayList<>();
formats.add(new JsTree());
formatter = new Format(formats);
builder.setFormat("jstree");
final var configuration = builder.build();
startLyv(configuration, formatter);
final Path outLog = Paths.get(outPath).resolve("out.log");
String fileCreated = Files.readString(outLog);
final String compareWith = Files.readString(outLog.resolveSibling("compare").resolve("multipleModules.html"));

//assert that file contains the first module
assertTrue(fileCreated.contains("<b> module name ietf-connection-oriented-oam@2019-04-16, "
+ "namespace urn:ietf:params:xml:ns:yang:ietf-connection-oriented-oam, prefix co-oam </b>"));
//assert that file contains the second module
assertTrue(fileCreated.contains("<b> module name ietf-routing@2018-03-13, "
+ "namespace urn:ietf:params:xml:ns:yang:ietf-routing, prefix rt </b>"));
//assert that there is only one <body> tag
final int bodyTagCount = fileCreated.split("<body>").length - 1;
assertEquals(1, bodyTagCount);
//assert that outputs match
assertEquals(fileCreated.replaceAll("\\s+", ""), compareWith.replaceAll("\\s+", ""));
}

private void runJsTreeTest(final String comapreWithFileName) throws Exception {
final Path outLog = Paths.get(outPath).resolve("out.log");
final String fileCreated = Files.readString(outLog);
Expand Down
32 changes: 32 additions & 0 deletions src/test/java/io/lighty/yang/validator/formats/JsonTreeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@
*/
package io.lighty.yang.validator.formats;

import static io.lighty.yang.validator.Main.startLyv;

import io.lighty.yang.validator.FormatTest;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import org.testng.Assert;
import org.testng.annotations.Test;

public class JsonTreeTest extends FormatTest {

Expand Down Expand Up @@ -55,6 +59,34 @@ public void runDeviationTest() throws Exception {
runJsonTreeTest("ModuleDeviation.json");
}

@Test
public void testMultipleFiles() throws IOException {
final var module1 = Paths.get(yangPath + "/ietf-connection-oriented-oam@2019-04-16.yang").toString();
final var module2 = Paths.get(yangPath + "/ietf-routing@2018-03-13.yang").toString();
builder.setYangModules(List.of(module1, module2));
final List<FormatPlugin> formats = new ArrayList<>();
formats.add(new JsonTree());
formatter = new Format(formats);
builder.setFormat("json-tree");
final var configuration = builder.build();
startLyv(configuration, formatter);
final var outLog = Paths.get(outPath).resolve("out.log");
final String fileCreated = Files.readString(outLog).substring(Files.readString(outLog).indexOf("{"));
final String compareWith = Files.readString(outLog.resolveSibling("compare").resolve("multipleModules.json"));
final String compareModel1 = Files.readString(
outLog.resolveSibling("compare").resolve("connectionOrientedOam.json"))
.replaceFirst("\\{\\s+\"parsed-models\": \\[\\s+\\{\n", "")
.replaceFirst("]\n\\s*}$", "").replaceAll("\\n\\s*", "");
final String compareModel2 = Files.readString(
outLog.resolveSibling("compare").resolve("routing.json"))
.replaceFirst("\\{\\s+\"parsed-models\": \\[\\s+\\{\n", "")
.replaceFirst("]\n\\s*}$", "").replaceAll("\\n\\s*", "");

Assert.assertEquals(fileCreated.replaceAll("\\s+", ""), compareWith.replaceAll("\\s+", ""));
Assert.assertTrue(fileCreated.replaceAll("\\s+", "").contains(compareModel1.replaceAll("\\s+", "")));
Assert.assertTrue(fileCreated.replaceAll("\\s+", "").contains(compareModel2.replaceAll("\\s+", "")));
}

private void runJsonTreeTest(final String comapreWithFileName) throws Exception {
final Path outLog = Paths.get(outPath).resolve("out.log");
final String fileCreated = Files.readString(outLog);
Expand Down
Loading