8580482fd3d49199b150522ef2bf5f24785f1031
[transportpce.git] / nbinotifications / src / main / java / org / opendaylight / transportpce / nbinotifications / utils / NbiNotificationsUtils.java
1 /*
2  * Copyright © 2020 Orange, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.transportpce.nbinotifications.utils;
9
10 import java.io.IOException;
11 import java.io.InputStream;
12 import java.util.Map;
13 import java.util.Properties;
14 import java.util.function.Function;
15 import java.util.stream.Collectors;
16 import org.opendaylight.yang.gen.v1.nbi.notifications.rev230726.NotificationTapiService;
17 import org.opendaylight.yang.gen.v1.nbi.notifications.rev230726.NotificationTapiServiceBuilder;
18 import org.opendaylight.yang.gen.v1.nbi.notifications.rev230726.PublishTapiNotificationService;
19 import org.opendaylight.yang.gen.v1.nbi.notifications.rev230726.notification.tapi.service.AdditionalInfo;
20 import org.opendaylight.yang.gen.v1.nbi.notifications.rev230726.notification.tapi.service.AdditionalInfoBuilder;
21 import org.opendaylight.yang.gen.v1.nbi.notifications.rev230726.notification.tapi.service.AdditionalInfoKey;
22 import org.opendaylight.yang.gen.v1.nbi.notifications.rev230726.notification.tapi.service.AlarmInfoBuilder;
23 import org.opendaylight.yang.gen.v1.nbi.notifications.rev230726.notification.tapi.service.ChangedAttributes;
24 import org.opendaylight.yang.gen.v1.nbi.notifications.rev230726.notification.tapi.service.ChangedAttributesBuilder;
25 import org.opendaylight.yang.gen.v1.nbi.notifications.rev230726.notification.tapi.service.ChangedAttributesKey;
26 import org.opendaylight.yang.gen.v1.nbi.notifications.rev230726.notification.tapi.service.TargetObjectName;
27 import org.opendaylight.yang.gen.v1.nbi.notifications.rev230726.notification.tapi.service.TargetObjectNameBuilder;
28 import org.opendaylight.yang.gen.v1.nbi.notifications.rev230726.notification.tapi.service.TargetObjectNameKey;
29 import org.opendaylight.yang.gen.v1.nbi.notifications.rev230726.notification.tapi.service.TcaInfoBuilder;
30 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.global._class.Name;
31 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.global._class.NameKey;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 public final class NbiNotificationsUtils {
36
37     private static final Logger LOG = LoggerFactory.getLogger(NbiNotificationsUtils.class);
38
39     private NbiNotificationsUtils() {
40     }
41
42     public static Properties loadProperties(String propertyFileName) {
43         Properties props = new Properties();
44         InputStream inputStream = NbiNotificationsUtils.class.getClassLoader().getResourceAsStream(propertyFileName);
45         try {
46             if (inputStream != null) {
47                 props.load(inputStream);
48             } else {
49                 LOG.warn("Kafka property file '{}' is empty", propertyFileName);
50             }
51         } catch (IOException e) {
52             LOG.error("Kafka property file '{}' was not found in the classpath", propertyFileName, e);
53         }
54         return props;
55     }
56
57     public static NotificationTapiService transformTapiNotification(PublishTapiNotificationService notification) {
58         Map<AdditionalInfoKey, AdditionalInfo> addInfoMap = notification.nonnullAdditionalInfo().values().stream()
59             .collect(Collectors.toMap(
60                     e -> new AdditionalInfoKey(e.getValueName()),
61                     e -> new AdditionalInfoBuilder(e).build()));
62         Map<ChangedAttributesKey, ChangedAttributes> changedAttMap = notification.nonnullChangedAttributes().values()
63                 .stream().collect(Collectors.toMap(
64                         e -> new ChangedAttributesKey(e.getValueName()),
65                         e -> new ChangedAttributesBuilder(e).build()));
66         Map<NameKey, Name> nameMap = notification.nonnullName().values().stream()
67                 .collect(Collectors.toMap(Name::key, Function.identity()));
68         Map<TargetObjectNameKey, TargetObjectName> targetObjNameMap = notification.nonnullTargetObjectName().values()
69                 .stream().collect(Collectors.toMap(
70                         e -> new TargetObjectNameKey(e.getValueName()),
71                         e -> new TargetObjectNameBuilder(e).build()));
72         LOG.info("Notification uuid = {}", notification.getUuid());
73         return new NotificationTapiServiceBuilder()
74                 .setAlarmInfo(notification.getAlarmInfo() == null ? null
75                         : new AlarmInfoBuilder(notification.getAlarmInfo()).build())
76                 .setAdditionalText(notification.getAdditionalText())
77                 .setAdditionalInfo(addInfoMap)
78                 .setNotificationType(notification.getNotificationType())
79                 .setChangedAttributes(changedAttMap)
80                 .setEventTimeStamp(notification.getEventTimeStamp())
81                 .setLayerProtocolName(notification.getLayerProtocolName())
82                 .setName(nameMap)
83                 .setSequenceNumber(notification.getSequenceNumber())
84                 .setSourceIndicator(notification.getSourceIndicator())
85                 .setTargetObjectIdentifier(notification.getTargetObjectIdentifier())
86                 .setTargetObjectName(targetObjNameMap)
87                 .setTargetObjectType(notification.getTargetObjectType())
88                 .setTcaInfo(notification.getTcaInfo() == null ? null
89                         : new TcaInfoBuilder(notification.getTcaInfo()).build())
90                 .setUuid(notification.getUuid())
91                 .build();
92     }
93 }