Refactor NBINotifications and serviceHandlerImpl
[transportpce.git] / nbinotifications / src / main / java / org / opendaylight / transportpce / nbinotifications / serialization / NotificationAlarmServiceSerializer.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.io.IOException;
11 import java.nio.charset.StandardCharsets;
12 import java.util.Map;
13 import org.apache.kafka.common.serialization.Serializer;
14 import org.opendaylight.transportpce.common.converter.JsonStringConverter;
15 import org.opendaylight.yang.gen.v1.nbi.notifications.rev210813.NotificationAlarmService;
16 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
17 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21
22 public class NotificationAlarmServiceSerializer implements Serializer<NotificationAlarmService> {
23     private static final Logger LOG = LoggerFactory.getLogger(NotificationAlarmServiceSerializer.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 byte[] serialize(String topic, NotificationAlarmService data) {
38         if (converter == null) {
39             throw new IllegalArgumentException("Converter should be configured through configure method of serializer");
40         }
41         if (data == null) {
42             return new byte[0];
43         }
44         try {
45             InstanceIdentifier<NotificationAlarmService> iid =
46                     InstanceIdentifier.builder(NotificationAlarmService.class).build();
47             String serialized = converter.createJsonStringFromDataObject(iid, data, JSONCodecFactorySupplier.RFC7951);
48             LOG.info("Serialized event {}", serialized);
49             return serialized.getBytes(StandardCharsets.UTF_8);
50         } catch (IOException e) {
51             return new byte[0];
52         }
53     }
54 }