95f10fb639657813553656329d8d8244821ce8f5
[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.rev201130.get.notifications.service.output.NotificationService;
15 import org.opendaylight.yang.gen.v1.nbi.notifications.rev201130.get.notifications.service.output.NotificationServiceBuilder;
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 NotificationServiceDeserializer implements Deserializer<NotificationService> {
22     private static final Logger LOG = LoggerFactory.getLogger(NotificationServiceDeserializer.class);
23     private JsonStringConverter<org.opendaylight.yang.gen.v1
24         .nbi.notifications.rev201130.NotificationService> 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.rev201130.NotificationService>) configs
34                     .get(ConfigConstants.CONVERTER);
35         }
36     }
37
38     @Override
39     public NotificationService 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.rev201130.NotificationService mappedString = converter
51                 .createDataObjectFromJsonString(YangInstanceIdentifier.of(
52                         org.opendaylight.yang.gen.v1.nbi.notifications.rev201130.NotificationService.QNAME),
53                         value,
54                         JSONCodecFactorySupplier.RFC7951);
55         if (mappedString != null) {
56             LOG.info("Reading event {}", mappedString);
57             return new NotificationServiceBuilder().setCommonId(mappedString.getCommonId())
58                     .setConnectionType(mappedString.getConnectionType()).setMessage(mappedString.getMessage())
59                     .setOperationalState(mappedString.getOperationalState())
60                     .setResponseFailed(mappedString.getResponseFailed()).setServiceName(mappedString.getServiceName())
61                     .setServiceAEnd(mappedString.getServiceAEnd()).setServiceZEnd(mappedString.getServiceZEnd())
62                     .build();
63         }
64         return null;
65     }
66
67 }