65091669dd3cf966d866f928aa513c4174fc2e74
[smo/teiv.git] /
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2024 Ericsson
4  *  Modifications Copyright (C) 2024 OpenInfra Foundation Europe
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21 package org.oran.smo.yangtools.parser.yanglibrary;
22
23 import java.util.Collections;
24 import java.util.List;
25 import java.util.stream.Collectors;
26
27 import org.oran.smo.yangtools.parser.ParserExecutionContext;
28 import org.oran.smo.yangtools.parser.data.dom.YangDataDomNode;
29 import org.oran.smo.yangtools.parser.findings.Finding;
30 import org.oran.smo.yangtools.parser.findings.ParserFindingType;
31 import org.oran.smo.yangtools.parser.yanglibrary.Module.IetfYangLibraryConformanceType;
32
33 /**
34  * A populater implementation that extracts data in accordance with RFC8525.
35  *
36  * @author Mark Hollmann
37  */
38 public class RFC8525Populator implements YangLibraryPopulator {
39
40     @Override
41     public void populateModulesState(final ParserExecutionContext context, final ModulesState modulesState,
42             final YangDataDomNode modulesStateDomNode) {
43
44         modulesState.setModuleSetId(IetfYangLibraryParser.getValueOfChild(context, modulesStateDomNode, "module-set-id",
45                 "---unspecified value---"));
46     }
47
48     @Override
49     public void populateModuleInModulesState(final ParserExecutionContext context, final Module module,
50             final YangDataDomNode moduleDomNode) {
51
52         module.setName(IetfYangLibraryParser.getValueOfChild(context, moduleDomNode, "name", ""));
53         module.setRevision(IetfYangLibraryParser.getValueOfChild(context, moduleDomNode, "revision", ""));
54         module.setNamespace(IetfYangLibraryParser.getValueOfChild(context, moduleDomNode, "namespace", ""));
55         module.setFeatures(IetfYangLibraryParser.getValueOfChildren(context, moduleDomNode, "feature"));
56         module.setConformanceType(IetfYangLibraryParser.getValueOfChild(context, moduleDomNode, "conformance-type", ""));
57
58         if (module.getName().isEmpty()) {
59             context.getFindingsManager().addFinding(new Finding(moduleDomNode,
60                     ParserFindingType.P082_YANG_LIBRARY_MANDATORY_VALUE_MISSING.toString(), "Missing module name."));
61         }
62         if (module.getNamespace().isEmpty()) {
63             context.getFindingsManager().addFinding(new Finding(moduleDomNode,
64                     ParserFindingType.P082_YANG_LIBRARY_MANDATORY_VALUE_MISSING.toString(), "Missing module namespace."));
65         }
66         if (module.getConformanceType() == IetfYangLibraryConformanceType.UNKNOWN) {
67             context.getFindingsManager().addFinding(new Finding(moduleDomNode,
68                     ParserFindingType.P081_INCORRECT_YANG_LIBRARY_DATA.toString(),
69                     "missing / incorrect module conformance-type."));
70         }
71
72         final String schemaLocation = IetfYangLibraryParser.getValueOfChild(context, moduleDomNode, "schema", "");
73         if (schemaLocation != null && !schemaLocation.isEmpty()) {
74             module.setSchemaLocations(Collections.singletonList(schemaLocation));
75         }
76
77         final List<YangDataDomNode> deviationDomNodes = IetfYangLibraryParser.getDomChildren(moduleDomNode, "deviation");
78         module.setDeviatedByModuleNames(deviationDomNodes.stream().map(dli -> IetfYangLibraryParser.getValueOfChild(context,
79                 dli, "name", "")).collect(Collectors.toList()));
80     }
81
82     @Override
83     public void populateSubmoduleInModulesState(final ParserExecutionContext context, final Submodule submodule,
84             final YangDataDomNode submoduleDomNode) {
85
86         submodule.setName(IetfYangLibraryParser.getValueOfChild(context, submoduleDomNode, "name", ""));
87         submodule.setRevision(IetfYangLibraryParser.getValueOfChild(context, submoduleDomNode, "revision", ""));
88
89         if (submodule.getName().isEmpty()) {
90             context.getFindingsManager().addFinding(new Finding(submoduleDomNode,
91                     ParserFindingType.P082_YANG_LIBRARY_MANDATORY_VALUE_MISSING.toString(), "Missing submodule name."));
92         }
93
94         final String subModuleSchemaLocation = IetfYangLibraryParser.getValueOfChild(context, submoduleDomNode, "schema",
95                 "");
96         if (subModuleSchemaLocation != null && !subModuleSchemaLocation.isEmpty()) {
97             submodule.setSchemaLocations(Collections.singletonList(subModuleSchemaLocation));
98         }
99     }
100
101     @Override
102     public void populateYangLibrary(final ParserExecutionContext context, final YangLibrary yangLibrary,
103             final YangDataDomNode yangLibraryDomNode) {
104
105         yangLibrary.setContentId(IetfYangLibraryParser.getValueOfChild(context, yangLibraryDomNode, "content-id",
106                 "---unspecified value---"));
107     }
108
109     @Override
110     public void populateModuleSet(final ParserExecutionContext context, final ModuleSet moduleSet,
111             final YangDataDomNode moduleSetDomNode) {
112
113         moduleSet.setName(IetfYangLibraryParser.getValueOfChild(context, moduleSetDomNode, "name", ""));
114         if (moduleSet.getName().isEmpty()) {
115             context.getFindingsManager().addFinding(new Finding(moduleSetDomNode,
116                     ParserFindingType.P082_YANG_LIBRARY_MANDATORY_VALUE_MISSING.toString(), "Missing module-set name."));
117         }
118     }
119
120     @Override
121     public void populateModuleInYangLibrary(final ParserExecutionContext context, final Module module,
122             final YangDataDomNode moduleDomNode, final String conformanceType) {
123
124         module.setName(IetfYangLibraryParser.getValueOfChild(context, moduleDomNode, "name", ""));
125         module.setRevision(IetfYangLibraryParser.getValueOfChild(context, moduleDomNode, "revision", ""));
126         module.setNamespace(IetfYangLibraryParser.getValueOfChild(context, moduleDomNode, "namespace", ""));
127         module.setSchemaLocations(IetfYangLibraryParser.getValueOfChildren(context, moduleDomNode, "location"));
128         module.setFeatures(IetfYangLibraryParser.getValueOfChildren(context, moduleDomNode, "feature"));
129         module.setConformanceType(conformanceType);
130
131         if (module.getName().isEmpty()) {
132             context.getFindingsManager().addFinding(new Finding(moduleDomNode,
133                     ParserFindingType.P082_YANG_LIBRARY_MANDATORY_VALUE_MISSING.toString(), "Missing module name."));
134         }
135         if (module.getNamespace().isEmpty()) {
136             context.getFindingsManager().addFinding(new Finding(moduleDomNode,
137                     ParserFindingType.P082_YANG_LIBRARY_MANDATORY_VALUE_MISSING.toString(), "Missing module namespace."));
138         }
139
140         module.setDeviatedByModuleNames(IetfYangLibraryParser.getValueOfChildren(context, moduleDomNode, "deviation"));
141     }
142
143     @Override
144     public void populateSubmoduleInYangLibrary(final ParserExecutionContext context, final Submodule submodule,
145             final YangDataDomNode submoduleDomNode) {
146
147         submodule.setName(IetfYangLibraryParser.getValueOfChild(context, submoduleDomNode, "name", ""));
148         submodule.setRevision(IetfYangLibraryParser.getValueOfChild(context, submoduleDomNode, "revision", ""));
149         submodule.setSchemaLocations(IetfYangLibraryParser.getValueOfChildren(context, submoduleDomNode, "location"));
150
151         if (submodule.getName().isEmpty()) {
152             context.getFindingsManager().addFinding(new Finding(submoduleDomNode,
153                     ParserFindingType.P082_YANG_LIBRARY_MANDATORY_VALUE_MISSING.toString(), "Missing submodule name."));
154         }
155     }
156 }