a13ffaa9d7e1a14ed6a899395a827d4a901385ec
[ccsdk/features.git] /
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.util;
23
24 import com.google.common.base.VerifyException;
25 import java.time.Instant;
26 import java.util.Collection;
27 import java.util.Date;
28 import java.util.HashSet;
29 import java.util.Iterator;
30 import java.util.Set;
31 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.NetconfTimeStamp;
32 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.types.NetconfTimeStampImpl;
33 import org.opendaylight.mdsal.dom.api.DOMEvent;
34 import org.opendaylight.mdsal.dom.api.DOMNotification;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DateAndTime;
36 import org.opendaylight.yangtools.yang.common.QName;
37 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
38 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
39 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
40 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
41 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
42 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
43 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 public class ORanDMDOMUtility {
48     public static final Logger LOG = LoggerFactory.getLogger(ORanDMDOMUtility.class);
49     private static final NetconfTimeStamp NETCONFTIME_CONVERTER = NetconfTimeStampImpl.getConverter();
50
51     public static String getKeyValue(MapEntryNode componentEntry) {
52         NodeIdentifierWithPredicates componentKey = componentEntry.getIdentifier(); // list key
53         return (String) componentKey.getValue(ORanDeviceManagerQNames.IETF_HW_COMPONENT_LIST_KEY);
54     }
55
56     public static String getLeafValue(DataContainerNode componentEntry, QName leafQName) {
57         NodeIdentifier leafNodeIdentifier = new NodeIdentifier(leafQName);
58         try {
59             LeafNode<?> optLeafNode = (LeafNode<?>) componentEntry.getChildByArg(leafNodeIdentifier);
60             if (optLeafNode.body() instanceof QName) {
61                 LOG.debug("Leaf is of type QName");
62             }
63             return optLeafNode.body().toString();
64         } catch (VerifyException ve) {
65             LOG.debug("Leaf with QName {} not found", leafQName.toString());
66             return null;
67         }
68     }
69
70     public static Set<String> getLeafListValue(DataContainerNode componentEntry, QName leafListQName) {
71         Set<String> containsChildList = new HashSet<String>();
72         try {
73             DataContainerChild childSet = componentEntry.getChildByArg(new NodeIdentifier(leafListQName));
74             Collection<?> childEntry = (Collection<?>) childSet.body();
75             Iterator<?> childEntryItr = childEntry.iterator();
76             while (childEntryItr.hasNext()) {
77                 LeafSetEntryNode<?> childEntryNode = (LeafSetEntryNode<?>) childEntryItr.next();
78                 containsChildList.add(childEntryNode.body().toString());
79             }
80         } catch (VerifyException ve) {
81             LOG.debug("Child for {} does not exist", leafListQName);
82         }
83         return containsChildList;
84     }
85
86     public static Instant getNotificationInstant(DOMNotification notification) {
87         if (notification instanceof DOMEvent) {
88             return ((DOMEvent) notification).getEventInstant();
89         } else {
90             return Instant.now();
91         }
92     }
93
94     /**
95      * Convert Instant to NETCONF DateAndTime
96      * @param eventTimeInstant
97      * @return DateAndTime
98      */
99     public static DateAndTime getDateAndTimeOfInstant(Instant eventTimeInstant) {
100         Date eventDate = Date.from(eventTimeInstant);
101         return new DateAndTime(NETCONFTIME_CONVERTER.getTimeStamp(eventDate));
102     }
103
104 }