Refactor NBINotifications and serviceHandlerImpl
[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.rev210813.NotificationAlarmService;
15 import org.opendaylight.yang.gen.v1.nbi.notifications.rev210813.get.notifications.alarm.service.output.NotificationsAlarmService;
16 import org.opendaylight.yang.gen.v1.nbi.notifications.rev210813.get.notifications.alarm.service.output.NotificationsAlarmServiceBuilder;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
18 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 public class NotificationAlarmServiceDeserializer implements Deserializer<NotificationsAlarmService> {
23     private static final Logger LOG = LoggerFactory.getLogger(NotificationAlarmServiceDeserializer.class);
24     private JsonStringConverter<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<NotificationAlarmService>) configs.get(ConfigConstants.CONVERTER);
33         }
34     }
35
36     @Override
37     public NotificationsAlarmService deserialize(String topic, byte[] data) {
38         if (converter == null) {
39             throw new IllegalArgumentException(
40                     "Converter should be configured through configure method of deserializer");
41         }
42         String value = new String(data, StandardCharsets.UTF_8);
43         // The message published is
44         // org.opendaylight.yang.gen.v1.nbi.notifications.rev210813.NotificationAlarmService
45         // we have to map it to
46         // org.opendaylight.yang.gen
47         // .v1.nbi.notifications.rev210813.get.notifications.service.output.NotificationsAlarmService
48         NotificationAlarmService mappedString = converter.createDataObjectFromJsonString(
49                 YangInstanceIdentifier.of(NotificationAlarmService.QNAME), value, JSONCodecFactorySupplier.RFC7951);
50         if (mappedString == null) {
51             return null;
52         }
53         LOG.info("Reading event {}", mappedString);
54         return new NotificationsAlarmServiceBuilder()
55                 .setConnectionType(mappedString.getConnectionType())
56                 .setMessage(mappedString.getMessage())
57                 .setOperationalState(mappedString.getOperationalState())
58                 .setServiceName(mappedString.getServiceName())
59                 .build();
60     }
61 }