Migration to TAPI 2.4 Step2
[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.rev230728.NotificationTapiService;
17 import org.opendaylight.yang.gen.v1.nbi.notifications.rev230728.NotificationTapiServiceBuilder;
18 import org.opendaylight.yang.gen.v1.nbi.notifications.rev230728.PublishTapiNotificationService;
19 import org.opendaylight.yang.gen.v1.nbi.notifications.rev230728.notification.tapi.service.AdditionalInfo;
20 import org.opendaylight.yang.gen.v1.nbi.notifications.rev230728.notification.tapi.service.AdditionalInfoBuilder;
21 import org.opendaylight.yang.gen.v1.nbi.notifications.rev230728.notification.tapi.service.AdditionalInfoKey;
22 import org.opendaylight.yang.gen.v1.nbi.notifications.rev230728.notification.tapi.service.ChangedAttributes;
23 import org.opendaylight.yang.gen.v1.nbi.notifications.rev230728.notification.tapi.service.ChangedAttributesBuilder;
24 import org.opendaylight.yang.gen.v1.nbi.notifications.rev230728.notification.tapi.service.ChangedAttributesKey;
25 import org.opendaylight.yang.gen.v1.nbi.notifications.rev230728.notification.tapi.service.TargetObjectName;
26 import org.opendaylight.yang.gen.v1.nbi.notifications.rev230728.notification.tapi.service.TargetObjectNameBuilder;
27 import org.opendaylight.yang.gen.v1.nbi.notifications.rev230728.notification.tapi.service.TargetObjectNameKey;
28 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev221121.global._class.Name;
29 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev221121.global._class.NameKey;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 public final class NbiNotificationsUtils {
34
35     private static final Logger LOG = LoggerFactory.getLogger(NbiNotificationsUtils.class);
36
37     private NbiNotificationsUtils() {
38     }
39
40     public static Properties loadProperties(String propertyFileName) {
41         Properties props = new Properties();
42         InputStream inputStream = NbiNotificationsUtils.class.getClassLoader().getResourceAsStream(propertyFileName);
43         try {
44             if (inputStream != null) {
45                 props.load(inputStream);
46             } else {
47                 LOG.warn("Kafka property file '{}' is empty", propertyFileName);
48             }
49         } catch (IOException e) {
50             LOG.error("Kafka property file '{}' was not found in the classpath", propertyFileName, e);
51         }
52         return props;
53     }
54
55     public static NotificationTapiService transformTapiNotification(PublishTapiNotificationService notification) {
56         Map<AdditionalInfoKey, AdditionalInfo> addInfoMap = notification.nonnullAdditionalInfo().values().stream()
57             .collect(Collectors.toMap(
58                     e -> new AdditionalInfoKey(e.getValueName()),
59                     e -> new AdditionalInfoBuilder(e).build()));
60         Map<ChangedAttributesKey, ChangedAttributes> changedAttMap = notification.nonnullChangedAttributes().values()
61                 .stream().collect(Collectors.toMap(
62                         e -> new ChangedAttributesKey(e.getValueName()),
63                         e -> new ChangedAttributesBuilder(e).build()));
64         Map<NameKey, Name> nameMap = notification.nonnullName().values().stream()
65                 .collect(Collectors.toMap(Name::key, Function.identity()));
66         Map<TargetObjectNameKey, TargetObjectName> targetObjNameMap = notification.nonnullTargetObjectName().values()
67                 .stream().collect(Collectors.toMap(
68                         e -> new TargetObjectNameKey(e.getValueName()),
69                         e -> new TargetObjectNameBuilder(e).build()));
70         LOG.info("Notification uuid = {}", notification.getUuid());
71
72         return new NotificationTapiServiceBuilder()
73                 .setAlarmInfo(notification.getAlarmInfo())
74                 .setAdditionalText(notification.getAdditionalText())
75                 .setAdditionalInfo(addInfoMap)
76                 .setNotificationType(notification.getNotificationType())
77                 .setChangedAttributes(changedAttMap)
78                 .setEventTimeStamp(notification.getEventTimeStamp())
79                 .setLayerProtocolName(notification.getLayerProtocolName())
80                 .setName(nameMap)
81                 .setSequenceNumber(notification.getSequenceNumber())
82                 .setSourceIndicator(notification.getSourceIndicator())
83                 .setTargetObjectIdentifier(notification.getTargetObjectIdentifier())
84                 .setTargetObjectName(targetObjNameMap)
85                 .setTargetObjectType(notification.getTargetObjectType())
86                 .setTcaInfo(notification.getTcaInfo())
87                 .setUuid(notification.getUuid())
88                 .build();
89     }
90 }