1111import com .fasterxml .jackson .core .JsonProcessingException ;
1212import com .fasterxml .jackson .databind .JsonNode ;
1313import 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 ;
1614import io .lighty .modules .gnmi .simulatordevice .config .GnmiSimulatorConfiguration ;
1715import java .io .IOException ;
1816import java .io .InputStream ;
17+ import java .util .Collection ;
18+ import java .util .Collections ;
19+ import java .util .HashMap ;
1920import java .util .HashSet ;
21+ import java .util .Map ;
22+ import java .util .ServiceLoader ;
2023import java .util .Set ;
24+ import java .util .stream .Collectors ;
25+ import org .opendaylight .yangtools .binding .meta .YangModelBindingProvider ;
2126import 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 ;
2230import org .slf4j .Logger ;
2331import 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}
0 commit comments