2 * ============LICENSE_START=======================================================
3 * ONAP : ccsdk features
4 * ================================================================================
5 * Copyright (C) 2021 highstreet technologies GmbH Intellectual Property.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.ccsdk.features.sdnr.wt.devicemanager.oran.impl.dom;
24 import com.google.common.base.VerifyException;
25 import com.google.common.collect.ImmutableList;
26 import java.io.IOException;
27 import java.io.InputStream;
29 import java.net.URISyntaxException;
30 import java.util.ArrayList;
31 import java.util.Collection;
32 import java.util.Iterator;
33 import java.util.List;
34 import java.util.NoSuchElementException;
35 import java.util.Optional;
36 import javax.xml.stream.XMLStreamException;
37 import javax.xml.stream.XMLStreamReader;
38 import org.junit.AfterClass;
39 import org.junit.BeforeClass;
40 import org.junit.Test;
41 import org.opendaylight.yangtools.util.xml.UntrustedXML;
42 import org.opendaylight.yangtools.yang.binding.CodeHelpers;
43 import org.opendaylight.yangtools.yang.common.QName;
44 import org.opendaylight.yangtools.yang.common.QNameModule;
45 import org.opendaylight.yangtools.yang.common.Revision;
46 import org.opendaylight.yangtools.yang.common.XMLNamespace;
47 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
48 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
49 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
50 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
51 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
52 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
53 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
54 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
55 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
56 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
57 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
58 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
59 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
60 import org.opendaylight.yangtools.yang.data.codec.xml.XmlParserStream;
61 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
62 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
63 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
64 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
65 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
66 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
67 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack.Inference;
68 //import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack.Inference; //Yangtool 8.0
69 import org.xml.sax.SAXException;
71 public class TestORANReadHardware {
74 * private static final QNameModule IETF_HARDWARE_MODULE =
75 * QNameModule.create(XMLNamespace.of(
76 * "urn:ietf:params:xml:ns:yang:ietf-hardware"), Revision.of("2018-03-13"));
77 */ // Applicable for Yangtools 8.0
78 // private static final URI IETF_HARDWARE_URI =
79 // URI.create("urn:ietf:params:xml:ns:yang:ietf-hardware");
80 private static final QNameModule IETF_HARDWARE_MODULE = QNameModule
81 .create(XMLNamespace.of("urn:ietf:params:xml:ns:yang:ietf-hardware"), Revision.of("2018-03-13"));
82 private static final QName HW_CONTAINER = QName.create(IETF_HARDWARE_MODULE, "hardware");
83 private static final QName HW_COMPONENT_LIST = QName.create(IETF_HARDWARE_MODULE, "component");
84 private static final QName HW_COMPONENT_LIST_KEY = QName.create(IETF_HARDWARE_MODULE, "name");
85 private static final QName HW_COMPONENT_LIST_CLASS = QName.create(IETF_HARDWARE_MODULE, "class");
86 private static final QName HW_COMPONENT_LIST_PHYSICAL_INDEX = QName.create(IETF_HARDWARE_MODULE, "physical-index"); // leaf:int32
87 private static final QName HW_COMPONENT_LIST_DESC = QName.create(IETF_HARDWARE_MODULE, "description"); // leaf:String
88 private static final QName HW_COMPONENT_LIST_PARENT = QName.create(IETF_HARDWARE_MODULE, "parent"); // leaf:leafref
89 private static final QName HW_COMPONENT_LIST_PARENT_REL_POS = QName.create(IETF_HARDWARE_MODULE, "parent-rel-pos"); // leaf:int32
90 private static final QName HW_COMPONENT_LIST_CONTAINS_CHILD = QName.create(IETF_HARDWARE_MODULE, "contains-child"); // leaf-list:leafref
91 private static final QName HW_COMPONENT_LIST_HW_REV = QName.create(IETF_HARDWARE_MODULE, "hardware-rev"); // leaf:String
92 private static final QName HW_COMPONENT_LIST_FW_REV = QName.create(IETF_HARDWARE_MODULE, "firmware-rev"); // leaf:String
93 private static final QName HW_COMPONENT_LIST_SW_REV = QName.create(IETF_HARDWARE_MODULE, "software-rev"); // leaf:String
94 private static final QName HW_COMPONENT_LIST_SER_NUM = QName.create(IETF_HARDWARE_MODULE, "serial-num"); // leaf:String
95 private static final QName HW_COMPONENT_LIST_MFG_NAME = QName.create(IETF_HARDWARE_MODULE, "mfg-name"); // leaf:String
96 private static final QName HW_COMPONENT_LIST_MODEL_NAME = QName.create(IETF_HARDWARE_MODULE, "model-name"); // leaf:String
97 private static final QName HW_COMPONENT_LIST_ALIAS = QName.create(IETF_HARDWARE_MODULE, "alias"); // leaf:String
98 private static final QName HW_COMPONENT_LIST_ASSET_ID = QName.create(IETF_HARDWARE_MODULE, "asset-id"); // leaf:String
99 private static final QName HW_COMPONENT_LIST_IS_FRU = QName.create(IETF_HARDWARE_MODULE, "is-fru"); // leaf:boolean
100 private static final QName HW_COMPONENT_LIST_MFG_DATE = QName.create(IETF_HARDWARE_MODULE, "mfg-date"); // leaf:yang:date-and-time
101 private static final QName HW_COMPONENT_LIST_URI = QName.create(IETF_HARDWARE_MODULE, "uri"); // leaf-list:inet:uri
102 private static final QName HW_COMPONENT_LIST_UUID = QName.create(IETF_HARDWARE_MODULE, "uuid"); // leaf:yang:uuid
103 private static final QName HW_COMPONENT_LIST_STATE = QName.create(IETF_HARDWARE_MODULE, "state"); // leaf:yang:uuid
104 private static final QName HW_COMPONENT_LIST_ADMIN_STATE = QName.create(IETF_HARDWARE_MODULE, "admin-state"); // leaf:yang:uuid
105 private static final QName HW_COMPONENT_LIST_OPER_STATE = QName.create(IETF_HARDWARE_MODULE, "oper-state"); // leaf:yang:uuid
107 private static EffectiveModelContext schemaContext;
108 private static Inference hwContainerSchema;
111 public static void setup() throws IOException {
112 schemaContext = YangParserTestUtils.parseYangResourceDirectory("/");
113 hwContainerSchema = Inference.ofDataTreePath(schemaContext, HW_CONTAINER);
114 System.out.println("URL is - " + TestORANReadHardware.class.getResource("/"));
118 public static void cleanup() {
119 schemaContext = null;
120 hwContainerSchema = null;
124 public void testIetfHardwareFromXML() throws XMLStreamException, URISyntaxException, IOException, SAXException {
126 final InputStream resourceAsStream = TestORANReadHardware.class.getResourceAsStream("/ietf-hardware.xml");
129 * final XMLInputFactory factory = XMLInputFactory.newInstance();
130 * XMLStreamReader reader = factory.createXMLStreamReader(resourceAsStream);
132 final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
134 final NormalizedNodeResult result = new NormalizedNodeResult();
135 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
137 final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, hwContainerSchema);
138 xmlParser.parse(reader);
143 NormalizedNode transformedInput = result.getResult();
144 System.out.println("Hardware Data = " + transformedInput);
146 ContainerNode hwContainer = (ContainerNode) transformedInput;
147 MapNode containerMap = (MapNode) hwContainer.getChildByArg(new NodeIdentifier(HW_COMPONENT_LIST));
148 Collection<MapEntryNode> containerMapEntries = containerMap.body();
149 for (MapEntryNode mapEntryNode : containerMapEntries) {
150 NodeIdentifierWithPredicates componentKey = mapEntryNode.getIdentifier(); // list key
151 System.out.println("Key Name is - " + componentKey.keySet());
152 System.out.println("Key Value is - " + componentKey.getValue(HW_COMPONENT_LIST_KEY));
154 LeafNode<?> classField = (LeafNode<?>) mapEntryNode
155 .getChildByArg(new NodeIdentifier(HW_COMPONENT_LIST_CLASS));
156 System.out.println("Class = " + classField.getIdentifier().getNodeType().getLocalName() + " Value = "
157 + classField.body().toString());
158 Object obj = classField.body();
159 System.out.println(obj.getClass());
160 if (obj instanceof QName) {
161 System.out.println("This is of type QName");
163 LeafNode<?> aliasLeaf = (LeafNode<?>) mapEntryNode
164 .getChildByArg(new NodeIdentifier(HW_COMPONENT_LIST_ALIAS));
165 // System.out.println("Alias = " + aliasLeaf.getValue().toString());
166 System.out.println("Alias = " + getLeafValue(mapEntryNode, HW_COMPONENT_LIST_ALIAS));
169 DataContainerChild childSet = mapEntryNode
170 .getChildByArg(new NodeIdentifier(HW_COMPONENT_LIST_CONTAINS_CHILD));
172 Collection<?> childEntry = (Collection<?>) childSet.body();
173 Iterator<?> childEntryItr = childEntry.iterator();
174 while (childEntryItr.hasNext()) {
175 LeafSetEntryNode<?> childEntryNode = (LeafSetEntryNode<?>) childEntryItr.next();
176 System.out.println("Child Node - " + childEntryNode.body());
178 } catch (VerifyException ve) {
179 // System.out.println("Child not not exist");
183 LeafSetNode<?> containsChildSet = (LeafSetNode<?>) mapEntryNode
184 .getChildByArg(new NodeIdentifier(HW_COMPONENT_LIST_CONTAINS_CHILD));
185 Collection<?> containsChildSetEntry = containsChildSet.body();
186 Iterator<?> childItr = containsChildSetEntry.iterator();
187 while (childItr.hasNext()) {
188 LeafSetEntryNode<?> childEntryNode = (LeafSetEntryNode<?>) childItr.next();
189 System.out.println("Child Node - " + childEntryNode.body());
191 } catch (VerifyException | NoSuchElementException e) {
192 // System.out.println("Child not not exist");
197 .println(CodeHelpers.nonnull(getLeafListValue(mapEntryNode, HW_COMPONENT_LIST_CONTAINS_CHILD)));
198 for (String childUuid : CodeHelpers
199 .nonnull(getLeafListValue(mapEntryNode, HW_COMPONENT_LIST_CONTAINS_CHILD))) {
200 System.out.println("Calling recursively - " + childUuid);
202 } catch (VerifyException | NoSuchElementException e) {
203 // System.out.println("Child not not exist");
206 String description = (String) mapEntryNode.getChildByArg(new NodeIdentifier(HW_COMPONENT_LIST_DESC))
208 System.out.println("Description = " + description);
209 } catch (VerifyException | NoSuchElementException e) {
210 // System.out.println("Child not not exist");
213 String serialNum = (String) mapEntryNode.getChildByArg(new NodeIdentifier(HW_COMPONENT_LIST_SER_NUM))
215 System.out.println("Serial Number = " + serialNum);
216 } catch (VerifyException | NoSuchElementException e) {
217 // System.out.println("Child not not exist");
220 String firmwareRev = (String) mapEntryNode.getChildByArg(new NodeIdentifier(HW_COMPONENT_LIST_FW_REV))
222 System.out.println("Firmware Rev = " + firmwareRev);
223 } catch (VerifyException | NoSuchElementException e) {
224 // System.out.println("Child not not exist");
227 String swRev = (String) mapEntryNode.getChildByArg(new NodeIdentifier(HW_COMPONENT_LIST_SW_REV)).body();
228 System.out.println("Software Rev = " + swRev);
229 } catch (VerifyException | NoSuchElementException e) {
230 // System.out.println("Child not not exist");
233 String modelName = (String) mapEntryNode.getChildByArg(new NodeIdentifier(HW_COMPONENT_LIST_MODEL_NAME))
235 System.out.println("Model Name = " + modelName);
236 } catch (VerifyException | NoSuchElementException e) {
237 // System.out.println("Child not not exist");
240 Integer parentRelPos = (Integer) mapEntryNode
241 .getChildByArg(new NodeIdentifier(HW_COMPONENT_LIST_PARENT_REL_POS)).body();
242 System.out.println("Parent Rel Pos = " + (parentRelPos != null ? parentRelPos.intValue() : null));
243 } catch (VerifyException | NoSuchElementException e) {
244 // System.out.println("Child not not exist");
247 String parent = (String) mapEntryNode.getChildByArg(new NodeIdentifier(HW_COMPONENT_LIST_PARENT))
249 System.out.println("Parent = " + parent);
250 } catch (VerifyException | NoSuchElementException e) {
251 // System.out.println("Child not not exist");
254 String hwRev = (String) mapEntryNode.getChildByArg(new NodeIdentifier(HW_COMPONENT_LIST_HW_REV)).body();
255 System.out.println("Hardware Revision = " + hwRev);
256 } catch (VerifyException | NoSuchElementException e) {
257 // System.out.println("Child not not exist");
260 String mfgName = (String) mapEntryNode.getChildByArg(new NodeIdentifier(HW_COMPONENT_LIST_MFG_NAME))
262 System.out.println("Manufacturer Name = " + mfgName);
263 } catch (VerifyException | NoSuchElementException e) {
264 // System.out.println("Child not not exist");
267 DataContainerChild mfgNameOpt = mapEntryNode
268 .getChildByArg(new NodeIdentifier(HW_COMPONENT_LIST_MFG_NAME));
269 if (mfgNameOpt != null) {
270 System.out.println("Mfg Name - " + (String) mfgNameOpt.body());
272 } catch (VerifyException | NoSuchElementException e) {
273 // System.out.println("Child not not exist");
276 String assetID = (String) mapEntryNode.getChildByArg(new NodeIdentifier(HW_COMPONENT_LIST_ASSET_ID))
278 System.out.println("Asset ID = " + assetID);
279 } catch (VerifyException | NoSuchElementException e) {
280 // System.out.println("Child not not exist");
283 String mfgDate = (String) mapEntryNode.getChildByArg(new NodeIdentifier(HW_COMPONENT_LIST_MFG_DATE))
285 System.out.println("Mfg Date = " + mfgDate);
286 } catch (VerifyException | NoSuchElementException e) {
287 // System.out.println("Child not not exist");
290 String uri = (String) mapEntryNode.getChildByArg(new NodeIdentifier(HW_COMPONENT_LIST_URI)).body();
291 System.out.println("URI = " + uri);
292 } catch (VerifyException | NoSuchElementException e) {
293 // System.out.println("Child not not exist");
296 Boolean isFRU = (Boolean) mapEntryNode.getChildByArg(new NodeIdentifier(HW_COMPONENT_LIST_IS_FRU))
298 System.out.println("IS FRU = " + isFRU);
299 } catch (VerifyException | NoSuchElementException e) {
300 // System.out.println("Child not not exist");
303 String uuid = (String) mapEntryNode.getChildByArg(new NodeIdentifier(HW_COMPONENT_LIST_UUID)).body()
305 System.out.println("UUID = " + uuid);
306 } catch (VerifyException | NoSuchElementException e) {
307 // System.out.println("Child not not exist");
311 ContainerNode state = (ContainerNode) mapEntryNode
312 .getChildByArg(new NodeIdentifier(HW_COMPONENT_LIST_STATE));
313 String adminState = (String) state.getChildByArg(new NodeIdentifier(HW_COMPONENT_LIST_ADMIN_STATE))
315 System.out.println("Admin State = " + adminState);
316 String operState = (String) state.getChildByArg(new NodeIdentifier(HW_COMPONENT_LIST_OPER_STATE)).body()
319 System.out.println("Oper State = " + operState);
320 } catch (VerifyException | NoSuchElementException e) {
321 System.out.println("Child not not exist");
324 System.out.println("********************************************");
327 // assertNotNull(transformedInput);
331 public static String getLeafValue(DataContainerNode componentEntry, QName leafQName) {
332 NodeIdentifier leafNodeIdentifier = new NodeIdentifier(leafQName);
334 LeafNode<?> optLeafNode = (LeafNode<?>) componentEntry.getChildByArg(leafNodeIdentifier);
335 if (optLeafNode.body() instanceof QName) {
336 System.out.println("Leaf is of type QName");
338 return optLeafNode.body().toString();
339 } catch (NoSuchElementException nsee) {
340 System.out.println("Leaf with QName {} not found" + leafQName.toString());
345 public static List<String> getLeafListValue(DataContainerNode componentEntry, QName leafListQName) {
346 if (componentEntry instanceof MapEntryNode) {
347 List<String> containsChildList = new ArrayList<String>();
348 DataContainerChild childSet = componentEntry.getChildByArg(new NodeIdentifier(leafListQName));
349 if (childSet != null) {
350 Collection<?> childEntry = (Collection<?>) childSet.body();
351 Iterator<?> childEntryItr = childEntry.iterator();
352 while (childEntryItr.hasNext()) {
353 LeafSetEntryNode<?> childEntryNode = (LeafSetEntryNode<?>) childEntryItr.next();
354 containsChildList.add(childEntryNode.body().toString());
357 return containsChildList;
363 public void testIetfHardwareFromNormalizedNode() {
364 buildIetfHardwareContainerNode();
367 private static NormalizedNode buildIetfHardwareContainerNode() {
368 MapNode componentMap = Builders.mapBuilder().withNodeIdentifier(new NodeIdentifier(HW_COMPONENT_LIST))
369 .withChild(Builders.mapEntryBuilder()
371 NodeIdentifierWithPredicates.of(HW_COMPONENT_LIST, HW_COMPONENT_LIST_KEY, "chassis"))
373 Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(HW_COMPONENT_LIST_CLASS))
374 .withValue("ianahw:chassis").build())
375 .withChild(Builders.leafBuilder()
376 .withNodeIdentifier(new NodeIdentifier(HW_COMPONENT_LIST_PHYSICAL_INDEX)).withValue(1)