Use DOM API for ORAN Devicemanager
[ccsdk/features.git] / sdnr / wt / devicemanager-o-ran-sc / o-ran / ru-fh / provider / src / main / java / org / onap / ccsdk / features / sdnr / wt / devicemanager / oran / impl / dom / ORanDMDOMUtility.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 java.time.Instant;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.Iterator;
28 import java.util.List;
29 import org.opendaylight.mdsal.dom.api.DOMEvent;
30 import org.opendaylight.mdsal.dom.api.DOMNotification;
31 import org.opendaylight.yangtools.yang.common.QName;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
34 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
35 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 import com.google.common.base.VerifyException;
43
44 public class ORanDMDOMUtility {
45     public static final Logger LOG = LoggerFactory.getLogger(ORanDMDOMUtility.class);
46
47     public static String getKeyValue(MapEntryNode componentEntry) {
48         NodeIdentifierWithPredicates componentKey = componentEntry.getIdentifier(); // list key
49         return (String) componentKey.getValue(ORanDeviceManagerQNames.IETF_HW_COMPONENT_LIST_KEY);
50     }
51
52     public static String getLeafValue(DataContainerNode componentEntry, QName leafQName) {
53         NodeIdentifier leafNodeIdentifier = new NodeIdentifier(leafQName);
54         try {
55             LeafNode<?> optLeafNode = (LeafNode<?>) componentEntry.getChildByArg(leafNodeIdentifier);
56             if (optLeafNode.body() instanceof QName) {
57                 LOG.debug("Leaf is of type QName");
58             }
59             return optLeafNode.body().toString();
60         } catch (VerifyException ve) {
61             LOG.debug("Leaf with QName {} not found", leafQName.toString());
62             return null;
63         }
64     }
65
66     public static List<String> getLeafListValue(DataContainerNode componentEntry, QName leafListQName) {
67         List<String> containsChildList = new ArrayList<String>();
68         try {
69             DataContainerChild childSet = componentEntry.getChildByArg(new NodeIdentifier(leafListQName));
70             Collection<?> childEntry = (Collection<?>) childSet.body();
71             Iterator<?> childEntryItr = childEntry.iterator();
72             while (childEntryItr.hasNext()) {
73                 LeafSetEntryNode<?> childEntryNode = (LeafSetEntryNode<?>) childEntryItr.next();
74                 containsChildList.add(childEntryNode.body().toString());
75             }
76         } catch (VerifyException ve) {
77             LOG.debug("Child for {} does not exist", leafListQName);
78         }
79         return containsChildList;
80     }
81
82     public static Instant getNotificationInstant(DOMNotification notification) {
83         if (notification instanceof DOMEvent) {
84             return ((DOMEvent) notification).getEventInstant();
85         } else {
86             return Instant.now();
87         }
88     }
89
90 }