dd20b5e6ab3486a6c665a1c8b26176682841f4c2
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2022 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.onf14.dom.impl.util;
23
24 import com.google.common.base.VerifyException;
25 import java.time.Instant;
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.Iterator;
29 import java.util.List;
30 import java.util.regex.Matcher;
31 import java.util.regex.Pattern;
32 import org.eclipse.jdt.annotation.Nullable;
33 import org.onap.ccsdk.features.sdnr.wt.devicemanager.onf14.dom.impl.dataprovider.InternalDataModelSeverity;
34 import org.opendaylight.mdsal.dom.api.DOMEvent;
35 import org.opendaylight.mdsal.dom.api.DOMNotification;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DateAndTime;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.SeverityType;
38 import org.opendaylight.yangtools.yang.common.QName;
39 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
40 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
41 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
42 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
43 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
44 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
45 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49 public class Onf14DMDOMUtility {
50
51     public static final Logger LOG = LoggerFactory.getLogger(Onf14DMDOMUtility.class);
52
53     /*
54      * /(urn:onf:yang:core-model-1-4?revision=2019-11-27)control-construct/logical-
55      * termination-point/logical-termination-point[{(urn:onf:yang:core-model-1-4?
56      * revision=2019-11-27)uuid=10004041-0000-0001-0001-3c4cd0db3b20}]
57      */
58     private static final Pattern ALARM_RESOURCE_PATTERN =
59             Pattern.compile(".*uuid=([0-9a-z]*-[0-9a-z]*-[0-9a-z]*-[0-9a-z]*-[0-9a-z]*).*", Pattern.MULTILINE);
60
61     private Onf14DMDOMUtility() {}
62
63     private static String getLeafValueX(DataContainerNode componentEntry, QName leafQName) {
64         NodeIdentifier leafNodeIdentifier = new NodeIdentifier(leafQName);
65         LeafNode<?> optLeafNode = (LeafNode<?>) componentEntry.getChildByArg(leafNodeIdentifier);
66         if (optLeafNode.body() instanceof QName) {
67             LOG.debug("Leaf is of type QName"); // Ex:
68                                                 // ImmutableLeafNode{identifier=(urn:onf:yang:air-interface-2-0?revision=2020-01-21)severity,
69                                                 // body=(urn:onf:yang:air-interface-2-0?revision=2020-01-21)SEVERITY_TYPE_MAJOR}
70             String severity_ = optLeafNode.body().toString();
71             return severity_.substring(severity_.indexOf(')') + 1); // Any other solution??
72         }
73         return optLeafNode.body().toString();
74     }
75
76     /**
77      * Return value as String
78      * @param componentEntry Container node with data
79      * @param leafQName Leaf to be converted
80      * @return String or null
81      */
82     public static String getLeafValue(DataContainerNode componentEntry, QName leafQName) {
83         try {
84             return getLeafValueX(componentEntry, leafQName);
85         } catch (VerifyException ve) {
86             LOG.debug("Leaf with QName {} not found", leafQName);
87             return null;
88         }
89     }
90
91     /**
92      * Return value as Integer
93      *
94      * @param componentEntry Container node with data
95      * @param leafQName Leaf to be converted
96      * @return Integer with value
97      * @throws IllegalArgumentException, VerifyException
98      */
99     public static Integer getLeafValueInt(DataContainerNode componentEntry, QName leafQName) {
100         String val = getLeafValueX(componentEntry, leafQName);
101         if (val == null || val.isEmpty())
102             throw new IllegalArgumentException("Value should not be null or empty");
103         return Integer.parseInt(val);
104     }
105
106     /**
107      * Return value as DateAndTime
108      *
109      * @param componentEntry Container node with data
110      * @param leafQName Leaf to be converted
111      * @return DateAndTime value
112      * @throws IllegalArgumentException, VerifyException
113      */
114     public static DateAndTime getLeafValueDateAndTime(DataContainerNode componentEntry, QName leafQName) {
115         return new DateAndTime(getLeafValueX(componentEntry, leafQName));
116     }
117
118     /**
119      * return string with Uuid
120      * @param componentEntry Container node with data
121      * @param resource Leaf to be converted
122      * @return Uuid
123      */
124     public static @Nullable String getLeafValueUuid(DataContainerNode componentEntry, QName resource) {
125         return extractUuid(getLeafValue(componentEntry, resource));
126     }
127
128     /**
129      * return internal severity
130      * @param componentEntry Container node with data
131      * @param resource Leaf to be converted
132      * @return Internal SeverityType
133      */
134     public static @Nullable SeverityType getLeafValueInternalSeverity(DataContainerNode componentEntry, QName resource) {
135         return InternalDataModelSeverity
136         .mapSeverity(Onf14DMDOMUtility.getLeafValue(componentEntry, resource));
137     }
138
139
140     public static List<String> getLeafListValue(DataContainerNode componentEntry, QName leafListQName) {
141         List<String> containsChildList = new ArrayList<>();
142         try {
143             DataContainerChild childSet = componentEntry.getChildByArg(new NodeIdentifier(leafListQName));
144             Collection<?> childEntry = (Collection<?>) childSet.body();
145             Iterator<?> childEntryItr = childEntry.iterator();
146             while (childEntryItr.hasNext()) {
147                 LeafSetEntryNode<?> childEntryNode = (LeafSetEntryNode<?>) childEntryItr.next();
148                 containsChildList.add(childEntryNode.body().toString());
149             }
150         } catch (VerifyException ve) {
151             LOG.debug("Child for {} does not exist", leafListQName);
152         }
153         return containsChildList;
154     }
155
156     public static String getUuidFromEquipment(MapEntryNode equipment, QName qName) {
157         LOG.debug("Equipment Identifier is {}", equipment.getIdentifier());
158         NodeIdentifierWithPredicates componentKey = equipment.getIdentifier(); // list key
159         LOG.debug("Key Name is - {}", componentKey.keySet());
160         LOG.debug("Key Value is - {}", componentKey.getValue(qName));
161
162         return componentKey.getValue(qName).toString();
163     }
164
165     public static Instant getNotificationInstant(DOMNotification notification) {
166         if (notification instanceof DOMEvent) {
167             return ((DOMEvent) notification).getEventInstant();
168         } else {
169             return Instant.now();
170         }
171     }
172
173     private static String extractUuid(String leafValue) {
174         String uuid;
175
176         Matcher matcher = ALARM_RESOURCE_PATTERN.matcher(leafValue);
177         if (matcher.matches() && matcher.groupCount() == 1) {
178             uuid = matcher.group(1);
179         } else {
180             uuid = leafValue;
181         }
182         return uuid;
183     }
184
185 }