1cae567fff807d1fb5ce7bed8aa18ef1b95bad5c
[ccsdk/features.git] / sdnr / wt / devicemanager-onap / onf14 / provider / src / main / java / org / onap / ccsdk / features / sdnr / wt / devicemanager / onf14 / dom / impl / util / Onf14DMDOMUtility.java
1 package org.onap.ccsdk.features.sdnr.wt.devicemanager.onf14.dom.impl.util;
2
3 import com.google.common.base.VerifyException;
4 import java.time.Instant;
5 import java.util.ArrayList;
6 import java.util.Collection;
7 import java.util.Iterator;
8 import java.util.List;
9 import org.opendaylight.mdsal.dom.api.DOMEvent;
10 import org.opendaylight.mdsal.dom.api.DOMNotification;
11 import org.opendaylight.yangtools.yang.common.QName;
12 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
13 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
14 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
15 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
16 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
17 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
18 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 public class Onf14DMDOMUtility {
23
24     public static final Logger LOG = LoggerFactory.getLogger(Onf14DMDOMUtility.class);
25
26     private Onf14DMDOMUtility() {}
27
28     public static String getLeafValue(DataContainerNode componentEntry, QName leafQName) {
29         NodeIdentifier leafNodeIdentifier = new NodeIdentifier(leafQName);
30         try {
31             LeafNode<?> optLeafNode = (LeafNode<?>) componentEntry.getChildByArg(leafNodeIdentifier);
32             if (optLeafNode.body() instanceof QName) {
33                 LOG.debug("Leaf is of type QName"); //Ex: ImmutableLeafNode{identifier=(urn:onf:yang:air-interface-2-0?revision=2020-01-21)severity, body=(urn:onf:yang:air-interface-2-0?revision=2020-01-21)SEVERITY_TYPE_MAJOR}
34                 String severity_ = optLeafNode.body().toString();
35                 return severity_.substring(severity_.indexOf(')')+1); //Any other solution??
36             }
37             return optLeafNode.body().toString();
38         } catch (VerifyException ve) {
39             LOG.debug("Leaf with QName {} not found", leafQName);
40             return null;
41         }
42     }
43
44     public static List<String> getLeafListValue(DataContainerNode componentEntry, QName leafListQName) {
45         List<String> containsChildList = new ArrayList<>();
46         try {
47             DataContainerChild childSet = componentEntry.getChildByArg(new NodeIdentifier(leafListQName));
48             Collection<?> childEntry = (Collection<?>) childSet.body();
49             Iterator<?> childEntryItr = childEntry.iterator();
50             while (childEntryItr.hasNext()) {
51                 LeafSetEntryNode<?> childEntryNode = (LeafSetEntryNode<?>) childEntryItr.next();
52                 containsChildList.add(childEntryNode.body().toString());
53             }
54         } catch (VerifyException ve) {
55             LOG.debug("Child for {} does not exist", leafListQName);
56         }
57         return containsChildList;
58     }
59
60     public static String getUuidFromEquipment(MapEntryNode equipment) {
61         LOG.debug("Equipment Identifier is {}", equipment.getIdentifier());
62         NodeIdentifierWithPredicates componentKey = equipment.getIdentifier(); // list key
63         LOG.debug("Key Name is - {}", componentKey.keySet());
64         LOG.debug("Key Value is - {}",
65                 componentKey.getValue(Onf14DevicemanagerQNames.CORE_MODEL_CC_EQPT_GLOBAL_CLASS_UUID));
66
67         return componentKey.getValue(Onf14DevicemanagerQNames.CORE_MODEL_CC_EQPT_GLOBAL_CLASS_UUID).toString();
68     }
69
70     public static Instant getNotificationInstant(DOMNotification notification) {
71         if (notification instanceof DOMEvent) {
72             return ((DOMEvent) notification).getEventInstant();
73         } else {
74             return Instant.now();
75         }
76     }
77 }