Refactor NBINotifications and serviceHandlerImpl
[transportpce.git] / nbinotifications / src / main / java / org / opendaylight / transportpce / nbinotifications / serialization / NotificationServiceDeserializer.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.NotificationProcessService;
15 import org.opendaylight.yang.gen.v1.nbi.notifications.rev210813.get.notifications.process.service.output.NotificationsProcessService;
16 import org.opendaylight.yang.gen.v1.nbi.notifications.rev210813.get.notifications.process.service.output.NotificationsProcessServiceBuilder;
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 NotificationServiceDeserializer implements Deserializer<NotificationsProcessService> {
23     private static final Logger LOG = LoggerFactory.getLogger(NotificationServiceDeserializer.class);
24     private JsonStringConverter<NotificationProcessService> 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<NotificationProcessService>) configs.get(ConfigConstants.CONVERTER);
33         }
34     }
35
36     @Override
37     public NotificationsProcessService 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.NotificationProcessService
45         // we have to map it to
46         // org.opendaylight.yang.gen
47         // .v1.nbi.notifications.rev210813.get.notifications.service.output.NotificationService
48         NotificationProcessService mappedString = converter.createDataObjectFromJsonString(
49                 YangInstanceIdentifier.of(NotificationProcessService.QNAME), value, JSONCodecFactorySupplier.RFC7951);
50         if (mappedString == null) {
51             return null;
52         }
53         LOG.info("Reading event {}", mappedString);
54         return new NotificationsProcessServiceBuilder()
55                 .setCommonId(mappedString.getCommonId())
56                 .setConnectionType(mappedString.getConnectionType())
57                 .setMessage(mappedString.getMessage())
58                 .setOperationalState(mappedString.getOperationalState())
59                 .setResponseFailed(mappedString.getResponseFailed())
60                 .setServiceName(mappedString.getServiceName())
61                 .setServiceAEnd(mappedString.getServiceAEnd())
62                 .setServiceZEnd(mappedString.getServiceZEnd())
63                 .build();
64     }
65
66 }