Skip to content

Commit b2d1994

Browse files
committed
WIP (will be squashed and removed)
1 parent b701898 commit b2d1994

File tree

4 files changed

+62
-264
lines changed

4 files changed

+62
-264
lines changed

lighty-modules/lighty-gnmi/lighty-gnmi-commons/src/main/java/io/lighty/modules/gnmi/commons/util/models/ModuleId.java

Lines changed: 0 additions & 103 deletions
This file was deleted.

lighty-modules/lighty-gnmi/lighty-gnmi-commons/src/main/java/io/lighty/modules/gnmi/commons/util/models/YangModuleUtils.java

Lines changed: 0 additions & 73 deletions
This file was deleted.

lighty-modules/lighty-gnmi/lighty-gnmi-device-simulator/src/main/java/io/lighty/modules/gnmi/simulatordevice/utils/GnmiSimulatorConfUtils.java

Lines changed: 62 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,22 @@
1111
import com.fasterxml.jackson.core.JsonProcessingException;
1212
import com.fasterxml.jackson.databind.JsonNode;
1313
import com.fasterxml.jackson.databind.ObjectMapper;
14-
import io.lighty.modules.gnmi.commons.util.models.ModuleId;
15-
import io.lighty.modules.gnmi.commons.util.models.YangModuleUtils;
1614
import io.lighty.modules.gnmi.simulatordevice.config.GnmiSimulatorConfiguration;
1715
import java.io.IOException;
1816
import java.io.InputStream;
17+
import java.util.Collection;
18+
import java.util.Collections;
19+
import java.util.HashMap;
1920
import java.util.HashSet;
21+
import java.util.Map;
22+
import java.util.ServiceLoader;
2023
import java.util.Set;
24+
import java.util.stream.Collectors;
25+
import org.opendaylight.yangtools.binding.meta.YangModelBindingProvider;
2126
import org.opendaylight.yangtools.binding.meta.YangModuleInfo;
27+
import org.opendaylight.yangtools.yang.common.QName;
28+
import org.opendaylight.yangtools.yang.common.Revision;
29+
import org.opendaylight.yangtools.yang.common.XMLNamespace;
2230
import org.slf4j.Logger;
2331
import org.slf4j.LoggerFactory;
2432

@@ -52,8 +60,8 @@ public static GnmiSimulatorConfiguration loadGnmiSimulatorConfiguration(final In
5260
gnmiSimulatorConfiguration = mapper.treeToValue(simulatorConfNode, GnmiSimulatorConfiguration.class);
5361
if (simulatorConfNode.has(SCHEMA_SERVICE_ELEMENT_NAME)) {
5462
final JsonNode schemaServiceElement = simulatorConfNode.path(SCHEMA_SERVICE_ELEMENT_NAME);
55-
gnmiSimulatorConfiguration = setModelsToSimulatorConfig(mapper, schemaServiceElement,
56-
gnmiSimulatorConfiguration);
63+
gnmiSimulatorConfiguration = setModelsToSimulatorConfig(schemaServiceElement,
64+
gnmiSimulatorConfiguration);
5765
}
5866

5967
} catch (final JsonProcessingException e) {
@@ -63,26 +71,31 @@ public static GnmiSimulatorConfiguration loadGnmiSimulatorConfiguration(final In
6371
return gnmiSimulatorConfiguration;
6472
}
6573

66-
private static GnmiSimulatorConfiguration setModelsToSimulatorConfig(final ObjectMapper mapper,
67-
final JsonNode schemaServiceElement, final GnmiSimulatorConfiguration gnmiSimulatorConfiguration)
68-
throws JsonProcessingException {
69-
if (schemaServiceElement.has(TOP_LEVEL_MODELS_ELEMENT_NAME)) {
70-
JsonNode yangModels = schemaServiceElement.path(TOP_LEVEL_MODELS_ELEMENT_NAME);
71-
if (yangModels.isArray()) {
72-
Set<ModuleId> moduleIds = new HashSet<>();
73-
for (JsonNode moduleIdNode : yangModels) {
74-
ModuleId moduleId = mapper.treeToValue(moduleIdNode, ModuleId.class);
75-
moduleIds.add(moduleId);
74+
private static GnmiSimulatorConfiguration setModelsToSimulatorConfig(final JsonNode schemaServiceElement,
75+
final GnmiSimulatorConfiguration gnmiSimulatorConfiguration) throws JsonProcessingException {
76+
JsonNode yangModels = schemaServiceElement.path(TOP_LEVEL_MODELS_ELEMENT_NAME);
77+
if (yangModels.isArray()) {
78+
Set<QName> moduleIds = new HashSet<>();
79+
for (JsonNode moduleIdNode : yangModels) {
80+
String namespace = moduleIdNode.path("nameSpace").asText(null);
81+
String name = moduleIdNode.path("name").asText(null);
82+
String revisionStr = moduleIdNode.path("revision").asText(null);
83+
84+
if (namespace == null || name == null) {
85+
LOG.warn("Invalid YANG module definition: missing nameSpace or name -> {}", moduleIdNode);
86+
continue;
7687
}
77-
Set<YangModuleInfo> modelsFromClasspath = YangModuleUtils.getModelsFromClasspath(moduleIds);
78-
gnmiSimulatorConfiguration.setYangModulesInfo(modelsFromClasspath);
79-
} else {
80-
LOG.error("Expected JSON array at {}", TOP_LEVEL_MODELS_ELEMENT_NAME);
88+
Revision revision = revisionStr != null ? Revision.ofNullable(revisionStr).orElse(null) : null;
89+
QName moduleId = QName.create(XMLNamespace.of(namespace), revision, name);
90+
moduleIds.add(moduleId);
8191
}
92+
93+
Set<YangModuleInfo> modelsFromClasspath = getModelsFromClasspath(moduleIds);
94+
gnmiSimulatorConfiguration.setYangModulesInfo(modelsFromClasspath);
8295
} else {
83-
LOG.info("Missing [{}] inside element [{}] in gNMI configuration", TOP_LEVEL_MODELS_ELEMENT_NAME,
84-
SCHEMA_SERVICE_ELEMENT_NAME);
96+
LOG.error("Expected JSON array at {}", TOP_LEVEL_MODELS_ELEMENT_NAME);
8597
}
98+
8699
return gnmiSimulatorConfiguration;
87100
}
88101

@@ -92,4 +105,33 @@ public static GnmiSimulatorConfiguration loadDefaultGnmiSimulatorConfiguration()
92105
return gnmiSimulatorConfiguration;
93106
}
94107

108+
public static Set<YangModuleInfo> getModelsFromClasspath(final Set<QName> filter) {
109+
Map<QName, YangModuleInfo> resolvedModules = new HashMap<>();
110+
ServiceLoader<YangModelBindingProvider> yangProviderLoader = ServiceLoader.load(YangModelBindingProvider.class);
111+
112+
for (QName moduleId : filter) {
113+
Set<YangModuleInfo> filteredSet = new HashSet<>();
114+
115+
for (YangModelBindingProvider yangModelBindingProvider : yangProviderLoader) {
116+
if (moduleId.equals(yangModelBindingProvider.getModuleInfo().getName())) {
117+
filteredSet.add(yangModelBindingProvider.getModuleInfo());
118+
}
119+
}
120+
121+
for (YangModuleInfo yangModuleInfo : filteredSet) {
122+
resolvedModules.put(yangModuleInfo.getName(), yangModuleInfo);
123+
addDependencies(resolvedModules, yangModuleInfo.getImportedModules());
124+
}
125+
}
126+
127+
return Collections.unmodifiableSet(new HashSet<>(resolvedModules.values()));
128+
}
129+
130+
private static void addDependencies(final Map<QName, YangModuleInfo> resolvedModules,
131+
final Collection<YangModuleInfo> importedModules) {
132+
for (YangModuleInfo yangModuleInfo : importedModules) {
133+
resolvedModules.put(yangModuleInfo.getName(), yangModuleInfo);
134+
addDependencies(resolvedModules, yangModuleInfo.getImportedModules());
135+
}
136+
}
95137
}

lighty-modules/lighty-gnmi/lighty-gnmi-sb/src/main/java/io/lighty/gnmi/southbound/lightymodule/util/GnmiConfigUtils.java

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,10 @@
88

99
package io.lighty.gnmi.southbound.lightymodule.util;
1010

11-
import com.fasterxml.jackson.core.JsonProcessingException;
12-
import com.fasterxml.jackson.databind.JsonNode;
13-
import com.fasterxml.jackson.databind.ObjectMapper;
14-
import io.lighty.gnmi.southbound.lightymodule.config.GnmiConfiguration;
15-
import io.lighty.modules.gnmi.commons.util.models.ModuleId;
16-
import io.lighty.modules.gnmi.commons.util.models.YangModuleUtils;
17-
import java.io.IOException;
18-
import java.io.InputStream;
19-
import java.util.HashSet;
20-
import java.util.Optional;
2111
import java.util.Set;
22-
import org.apache.logging.log4j.core.config.ConfigurationException;
2312
import org.opendaylight.yangtools.binding.meta.YangModuleInfo;
24-
import org.slf4j.Logger;
25-
import org.slf4j.LoggerFactory;
2613

2714
public final class GnmiConfigUtils {
28-
private static final Logger LOG = LoggerFactory.getLogger(GnmiConfigUtils.class);
29-
private static final String SCHEMA_SERVICE_ELEMENT_NAME = "initialYangModels";
30-
31-
public static final String GNMI_CONFIG_JSON_ROOT_ELEMENT = "gnmi";
3215
public static final Set<YangModuleInfo> YANG_MODELS = Set.of(
3316
org.opendaylight.yang.svc.v1.urn.lighty.gnmi.topology.rev210316
3417
.YangModuleInfoImpl.getInstance(),
@@ -76,55 +59,4 @@ private GnmiConfigUtils() {
7659
//Utility class
7760
}
7861

79-
public static GnmiConfiguration getDefaultGnmiConfiguration() {
80-
return new GnmiConfiguration();
81-
}
82-
83-
public static GnmiConfiguration getGnmiConfiguration(final InputStream jsonConfigInputStream)
84-
throws ConfigurationException {
85-
final ObjectMapper mapper = new ObjectMapper();
86-
final JsonNode configNode;
87-
try {
88-
configNode = mapper.readTree(jsonConfigInputStream);
89-
} catch (final IOException e) {
90-
throw new ConfigurationException("Cannot deserialize Json content to Json tree nodes", e);
91-
}
92-
if (!configNode.has(GNMI_CONFIG_JSON_ROOT_ELEMENT)) {
93-
LOG.warn("Json config does not contain {} element. Using defaults.", GNMI_CONFIG_JSON_ROOT_ELEMENT);
94-
return getDefaultGnmiConfiguration();
95-
}
96-
final GnmiConfiguration gnmiConfiguration;
97-
try {
98-
JsonNode gnmiConfigJsonNode = configNode.path(GNMI_CONFIG_JSON_ROOT_ELEMENT);
99-
gnmiConfiguration
100-
= mapper.treeToValue(gnmiConfigJsonNode, GnmiConfiguration.class);
101-
final Optional<Set<YangModuleInfo>> yangModulesInfo
102-
= getYangModulesInfoFromConfig(mapper, gnmiConfigJsonNode);
103-
yangModulesInfo.ifPresent(gnmiConfiguration::setYangModulesInfo);
104-
} catch (final JsonProcessingException e) {
105-
throw new ConfigurationException(String.format("Cannot bind Json tree to type: %s",
106-
GnmiConfiguration.class), e);
107-
}
108-
return gnmiConfiguration;
109-
}
110-
111-
private static Optional<Set<YangModuleInfo>> getYangModulesInfoFromConfig(final ObjectMapper mapper,
112-
final JsonNode gnmiConfigJsonNode) throws JsonProcessingException {
113-
if (gnmiConfigJsonNode.has(SCHEMA_SERVICE_ELEMENT_NAME)) {
114-
final JsonNode schemaServiceElement = gnmiConfigJsonNode.path(SCHEMA_SERVICE_ELEMENT_NAME);
115-
if (schemaServiceElement.isArray()) {
116-
Set<ModuleId> moduleIds = new HashSet<>();
117-
for (JsonNode moduleIdNode : schemaServiceElement) {
118-
ModuleId moduleId = mapper.treeToValue(moduleIdNode, ModuleId.class);
119-
moduleIds.add(moduleId);
120-
}
121-
return Optional.of(YangModuleUtils.getModelsFromClasspath(moduleIds));
122-
} else {
123-
LOG.error("Expected JSON array at {}", SCHEMA_SERVICE_ELEMENT_NAME);
124-
return Optional.empty();
125-
}
126-
}
127-
return Optional.empty();
128-
}
129-
13062
}

0 commit comments

Comments
 (0)