Add bounds to sphinx requirement
[ccsdk/features.git] / sdnr / wt / devicemanager-o-ran-sc / o-ran / ru-fh / provider / src / test / java / org / onap / ccsdk / features / sdnr / wt / devicemanager / oran / impl / dom / TestORANReadHardware.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2021 highstreet technologies GmbH Intellectual Property.
6  * All rights reserved.
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
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  *
21  */
22 package org.onap.ccsdk.features.sdnr.wt.devicemanager.oran.impl.dom;
23
24 import com.google.common.base.VerifyException;
25 import com.google.common.collect.ImmutableList;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.net.URI;
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;
70
71 public class TestORANReadHardware {
72
73         /*
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
106
107         private static EffectiveModelContext schemaContext;
108         private static Inference hwContainerSchema;
109
110         @BeforeClass
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("/"));
115         }
116
117         @AfterClass
118         public static void cleanup() {
119                 schemaContext = null;
120                 hwContainerSchema = null;
121         }
122
123         @Test
124         public void testIetfHardwareFromXML() throws XMLStreamException, URISyntaxException, IOException, SAXException {
125
126                 final InputStream resourceAsStream = TestORANReadHardware.class.getResourceAsStream("/ietf-hardware.xml");
127
128                 /*
129                  * final XMLInputFactory factory = XMLInputFactory.newInstance();
130                  * XMLStreamReader reader = factory.createXMLStreamReader(resourceAsStream);
131                  */
132                 final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
133
134                 final NormalizedNodeResult result = new NormalizedNodeResult();
135                 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
136
137                 final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, hwContainerSchema);
138                 xmlParser.parse(reader);
139
140                 xmlParser.flush();
141                 xmlParser.close();
142
143                 NormalizedNode transformedInput = result.getResult();
144                 System.out.println("Hardware Data = " + transformedInput);
145
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));
153
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");
162                         }
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));
167
168                         try {
169                                 DataContainerChild childSet = mapEntryNode
170                                                 .getChildByArg(new NodeIdentifier(HW_COMPONENT_LIST_CONTAINS_CHILD));
171
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());
177                                 }
178                         } catch (VerifyException ve) {
179                                 // System.out.println("Child not not exist");
180                         }
181
182                         try {
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());
190                                 }
191                         } catch (VerifyException | NoSuchElementException e) {
192                                 // System.out.println("Child not not exist");
193                         }
194
195                         try {
196                                 System.out
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);
201                                 }
202                         } catch (VerifyException | NoSuchElementException e) {
203                                 // System.out.println("Child not not exist");
204                         }
205                         try {
206                                 String description = (String) mapEntryNode.getChildByArg(new NodeIdentifier(HW_COMPONENT_LIST_DESC))
207                                                 .body();
208                                 System.out.println("Description = " + description);
209                         } catch (VerifyException | NoSuchElementException e) {
210                                 // System.out.println("Child not not exist");
211                         }
212                         try {
213                                 String serialNum = (String) mapEntryNode.getChildByArg(new NodeIdentifier(HW_COMPONENT_LIST_SER_NUM))
214                                                 .body();
215                                 System.out.println("Serial Number = " + serialNum);
216                         } catch (VerifyException | NoSuchElementException e) {
217                                 // System.out.println("Child not not exist");
218                         }
219                         try {
220                                 String firmwareRev = (String) mapEntryNode.getChildByArg(new NodeIdentifier(HW_COMPONENT_LIST_FW_REV))
221                                                 .body();
222                                 System.out.println("Firmware Rev = " + firmwareRev);
223                         } catch (VerifyException | NoSuchElementException e) {
224                                 // System.out.println("Child not not exist");
225                         }
226                         try {
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");
231                         }
232                         try {
233                                 String modelName = (String) mapEntryNode.getChildByArg(new NodeIdentifier(HW_COMPONENT_LIST_MODEL_NAME))
234                                                 .body();
235                                 System.out.println("Model Name = " + modelName);
236                         } catch (VerifyException | NoSuchElementException e) {
237                                 // System.out.println("Child not not exist");
238                         }
239                         try {
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");
245                         }
246                         try {
247                                 String parent = (String) mapEntryNode.getChildByArg(new NodeIdentifier(HW_COMPONENT_LIST_PARENT))
248                                                 .body();
249                                 System.out.println("Parent = " + parent);
250                         } catch (VerifyException | NoSuchElementException e) {
251                                 // System.out.println("Child not not exist");
252                         }
253                         try {
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");
258                         }
259                         try {
260                                 String mfgName = (String) mapEntryNode.getChildByArg(new NodeIdentifier(HW_COMPONENT_LIST_MFG_NAME))
261                                                 .body();
262                                 System.out.println("Manufacturer Name = " + mfgName);
263                         } catch (VerifyException | NoSuchElementException e) {
264                                 // System.out.println("Child not not exist");
265                         }
266                         try {
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());
271                                 }
272                         } catch (VerifyException | NoSuchElementException e) {
273                                 // System.out.println("Child not not exist");
274                         }
275                         try {
276                                 String assetID = (String) mapEntryNode.getChildByArg(new NodeIdentifier(HW_COMPONENT_LIST_ASSET_ID))
277                                                 .body();
278                                 System.out.println("Asset ID = " + assetID);
279                         } catch (VerifyException | NoSuchElementException e) {
280                                 // System.out.println("Child not not exist");
281                         }
282                         try {
283                                 String mfgDate = (String) mapEntryNode.getChildByArg(new NodeIdentifier(HW_COMPONENT_LIST_MFG_DATE))
284                                                 .body();
285                                 System.out.println("Mfg Date = " + mfgDate);
286                         } catch (VerifyException | NoSuchElementException e) {
287                                 // System.out.println("Child not not exist");
288                         }
289                         try {
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");
294                         }
295                         try {
296                                 Boolean isFRU = (Boolean) mapEntryNode.getChildByArg(new NodeIdentifier(HW_COMPONENT_LIST_IS_FRU))
297                                                 .body();
298                                 System.out.println("IS FRU = " + isFRU);
299                         } catch (VerifyException | NoSuchElementException e) {
300                                 // System.out.println("Child not not exist");
301                         }
302                         try {
303                                 String uuid = (String) mapEntryNode.getChildByArg(new NodeIdentifier(HW_COMPONENT_LIST_UUID)).body()
304                                                 .toString();
305                                 System.out.println("UUID = " + uuid);
306                         } catch (VerifyException | NoSuchElementException e) {
307                                 // System.out.println("Child not not exist");
308                         }
309
310                         try {
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))
314                                                 .body().toString();       
315                                 System.out.println("Admin State = " + adminState);
316                                 String operState = (String) state.getChildByArg(new NodeIdentifier(HW_COMPONENT_LIST_OPER_STATE)).body()
317                                                 .toString();
318
319                                 System.out.println("Oper State = " + operState);
320                         } catch (VerifyException | NoSuchElementException e) {
321                                 System.out.println("Child not not exist");
322                         }
323
324                         System.out.println("********************************************");
325
326                 }
327                 // assertNotNull(transformedInput);
328
329         }
330
331         public static String getLeafValue(DataContainerNode componentEntry, QName leafQName) {
332                 NodeIdentifier leafNodeIdentifier = new NodeIdentifier(leafQName);
333                 try {
334                         LeafNode<?> optLeafNode = (LeafNode<?>) componentEntry.getChildByArg(leafNodeIdentifier);
335                         if (optLeafNode.body() instanceof QName) {
336                                 System.out.println("Leaf is of type QName");
337                         }
338                         return optLeafNode.body().toString();
339                 } catch (NoSuchElementException nsee) {
340                         System.out.println("Leaf with QName {} not found" + leafQName.toString());
341                         return null;
342                 }
343         }
344
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());
355                                 }
356                         }
357                         return containsChildList;
358                 }
359                 return null;
360         }
361
362         @Test
363         public void testIetfHardwareFromNormalizedNode() {
364                 buildIetfHardwareContainerNode();
365         }
366
367         private static NormalizedNode buildIetfHardwareContainerNode() {
368                 MapNode componentMap = Builders.mapBuilder().withNodeIdentifier(new NodeIdentifier(HW_COMPONENT_LIST))
369                                 .withChild(Builders.mapEntryBuilder()
370                                                 .withNodeIdentifier(
371                                                                 NodeIdentifierWithPredicates.of(HW_COMPONENT_LIST, HW_COMPONENT_LIST_KEY, "chassis"))
372                                                 .withChild(
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)
377                                                                 .build())
378                                                 .build())
379                                 .build();
380                 return componentMap;
381         }
382
383 }