Add service listener to notify Kafka
[transportpce.git] / nbinotifications / src / main / java / org / opendaylight / transportpce / nbinotifications / serialization / NotificationAlarmServiceDeserializer.java
1 /*
2  * Copyright © 2021 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.serialization;
9
10 import java.nio.charset.StandardCharsets;
11 import java.util.Map;
12 import org.apache.kafka.common.serialization.Deserializer;
13 import org.opendaylight.transportpce.common.converter.JsonStringConverter;
14 import org.opendaylight.yang.gen.v1.nbi.notifications.rev210628.get.notifications.alarm.service.output.NotificationAlarmService;
15 import org.opendaylight.yang.gen.v1.nbi.notifications.rev210628.get.notifications.alarm.service.output.NotificationAlarmServiceBuilder;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
17 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 public class NotificationAlarmServiceDeserializer implements Deserializer<NotificationAlarmService> {
22     private static final Logger LOG = LoggerFactory.getLogger(NotificationAlarmServiceDeserializer.class);
23     private JsonStringConverter<org.opendaylight.yang.gen.v1
24         .nbi.notifications.rev210628.NotificationAlarmService> converter;
25
26     @SuppressWarnings("unchecked")
27     @Override
28     public void configure(Map<String, ?> configs, boolean isKey) {
29         LOG.info("Deserializer configuration {}", configs);
30         if (configs.containsKey(ConfigConstants.CONVERTER)
31                 && configs.get(ConfigConstants.CONVERTER) instanceof JsonStringConverter<?>) {
32             converter = (JsonStringConverter<org.opendaylight.yang.gen.v1
33                     .nbi.notifications.rev210628.NotificationAlarmService>) configs
34                     .get(ConfigConstants.CONVERTER);
35         }
36     }
37
38     @Override
39     public NotificationAlarmService deserialize(String topic, byte[] data) {
40         if (converter == null) {
41             throw new IllegalArgumentException(
42                     "Converter should be configured through configure method of deserializer");
43         }
44         String value = new String(data, StandardCharsets.UTF_8);
45         // The message published is
46         // org.opendaylight.yang.gen.v1.nbi.notifications.rev201130.NotificationService
47         // we have to map it to
48         // org.opendaylight.yang.gen
49         // .v1.nbi.notifications.rev201130.get.notifications.service.output.NotificationService
50         org.opendaylight.yang.gen.v1.nbi.notifications.rev210628.NotificationAlarmService mappedString = converter
51                 .createDataObjectFromJsonString(YangInstanceIdentifier.of(
52                         org.opendaylight.yang.gen.v1.nbi.notifications.rev210628.NotificationAlarmService.QNAME),
53                         value,
54                         JSONCodecFactorySupplier.RFC7951);
55         if (mappedString != null) {
56             LOG.info("Reading event {}", mappedString);
57             return new NotificationAlarmServiceBuilder().setConnectionType(mappedString.getConnectionType())
58                     .setMessage(mappedString.getMessage())
59                     .setOperationalState(mappedString.getOperationalState())
60                     .setServiceName(mappedString.getServiceName())
61                     .build();
62         }
63         return null;
64     }
65 }