forked from yang-central/yangkit
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFeatureExample.java
More file actions
185 lines (153 loc) · 7.74 KB
/
FeatureExample.java
File metadata and controls
185 lines (153 loc) · 7.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
* 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 com.insa.app;
import org.dom4j.DocumentException;
import org.yangcentral.yangkit.common.api.exception.Severity;
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.ContainerData;
import org.yangcentral.yangkit.data.api.model.YangDataDocument;
import org.yangcentral.yangkit.data.codec.json.ContainerDataJsonCodec;
import org.yangcentral.yangkit.data.codec.json.YangDataDocumentJsonParser;
import org.yangcentral.yangkit.model.api.schema.YangSchema;
import org.yangcentral.yangkit.model.api.schema.YangSchemaContext;
import org.yangcentral.yangkit.model.api.schema.ModuleSet;
import org.yangcentral.yangkit.model.api.schema.YangModuleDescription;
import org.yangcentral.yangkit.model.api.stmt.Container;
import org.yangcentral.yangkit.model.api.stmt.DataDefinition;
import org.yangcentral.yangkit.model.api.stmt.Feature;
import org.yangcentral.yangkit.model.api.stmt.Module;
import org.yangcentral.yangkit.model.api.stmt.SchemaNode;
import org.yangcentral.yangkit.parser.YangParserException;
import org.yangcentral.yangkit.parser.YangYinParser;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Optional;
/**
* Simple example demonstrating YANG feature concepts with Yangkit
*/
public class FeatureExample {
public static void main(String[] args) throws IOException, YangParserException, DocumentException {
System.out.println("=== Simple YANG Feature Example ===\n");
// Parse our example YANG module with features
URL yangUrl = FeatureExample.class.getClassLoader().getResource("FeatureExample/yang");
String yangDir = yangUrl.getFile();
YangSchemaContext schemaContext = YangYinParser.parse(yangDir);
ValidatorResult result = schemaContext.validate();
if (!result.isOk()) {
System.out.println("Schema validation failed:");
for (ValidatorRecord<?, ?> record : result.getRecords()) {
System.out.println(" Error: " + record.getErrorMsg().getMessage());
}
return;
}
// Find our example module
Optional<Module> moduleOpt = schemaContext.getModule("example-features", null);
if (!moduleOpt.isPresent()) {
System.out.println("example-features module not found");
return;
}
Module exampleModule = moduleOpt.get();
boolean valid = false;
// Test 1: Enable no features
System.out.println("1. With no features enabled:");
YangSchema noFeatureSchema = createSchemaWithFeature(exampleModule, null);
schemaContext.setYangSchema(noFeatureSchema);
showElementActivity(exampleModule);
valid = validateJson("basic-config.json", schemaContext);
System.out.println("Validation result: " + (valid ? "valid" : "invalid"));
// Test 2: Enable advanced-config feature
System.out.println("\n2. With 'advanced-config' feature enabled:");
YangSchema advancedSchema = createSchemaWithFeature(exampleModule, "advanced-config");
schemaContext.setYangSchema(advancedSchema);
showElementActivity(exampleModule);
valid = validateJson("advanced-config.json", schemaContext);
System.out.println("Validation result: " + (valid ? "valid" : "invalid"));
// Test 3: Enable monitoring feature
System.out.println("\n3. With 'monitoring' feature enabled:");
YangSchema monitoringSchema = createSchemaWithFeature(exampleModule, "monitoring");
schemaContext.setYangSchema(monitoringSchema);
showElementActivity(exampleModule);
valid = validateJson("monitoring-config.json", schemaContext);
System.out.println("Validation result: " + (valid ? "valid" : "invalid"));
// Test 4: All features enabled
System.out.println("\n4. With all features enabled:");
// Apparently, if no schema is specified, all features are enabled by default.
// This is the same in Libyang.
schemaContext.setYangSchema(null);
showElementActivity(exampleModule);
valid = validateJson("all-features.json", schemaContext);
System.out.println("Validation result: " + (valid ? "valid" : "invalid"));
}
private static void showElementActivity(Module module) {
for (DataDefinition dataDefinition : module.getDataDefChildren()) {
showElementStatus(dataDefinition, " ");
}
}
private static void showElementStatus(DataDefinition element, String indent) {
if (element instanceof SchemaNode) {
SchemaNode schemaNode = (SchemaNode) element;
boolean isActive = schemaNode.isActive();
String status = isActive ? "✅" : "🚫";
System.out.println(indent + element.getArgStr() + " - " + status);
}
// Recursively show child elements
if (element instanceof org.yangcentral.yangkit.model.api.stmt.DataDefContainer) {
var container = (org.yangcentral.yangkit.model.api.stmt.DataDefContainer) element;
for (DataDefinition child : container.getDataDefChildren()) {
showElementStatus(child, indent + " ");
}
}
}
private static YangSchema createSchemaWithFeature(Module module, String featureName) {
return createSchemaWithFeatures(module, featureName);
}
private static YangSchema createSchemaWithFeatures(Module module, String... featureNames) {
YangSchema schema = new YangSchema();
schema.setName("test-schema");
ModuleSet moduleSet = new ModuleSet();
moduleSet.setName("test-module-set");
YangModuleDescription moduleDesc = new YangModuleDescription(module.getModuleId());
for (String featureName : featureNames) {
moduleDesc.addFeature(featureName);
}
moduleSet.addModule(moduleDesc);
schema.addModuleSet(moduleSet);
return schema;
}
private static boolean validateJson(String jsonFile, YangSchemaContext schemaContext) throws IOException {
// My validation
JsonNode jsonNode = new ObjectMapper().readTree(new File(
FeatureExample.class.getClassLoader().getResource("FeatureExample/json/" + jsonFile).getFile()));
ValidatorResultBuilder validatorResultBuilder = new ValidatorResultBuilder();
YangDataDocument doc = new YangDataDocumentJsonParser(schemaContext).parse(jsonNode, validatorResultBuilder);
ValidatorResult validatorResult = validatorResultBuilder.build();
if (!validatorResult.isOk()) {
System.out.println("Built-in print: " + validatorResult.print(Severity.ERROR));
}
doc.update();
ValidatorResult validatorResult1 = doc.validate();
if (!validatorResult1.isOk()) {
System.out.println("Built-in print: " + validatorResult1.print(Severity.ERROR));
}
return validatorResult.isOk() && validatorResult1.isOk();
}
}