GNPy client refactor
[transportpce.git] / pce / src / main / java / org / opendaylight / transportpce / pce / gnpy / consumer / GnpyApiSerializer.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.pce.gnpy.consumer;
9
10 import com.fasterxml.jackson.core.JsonGenerator;
11 import com.fasterxml.jackson.databind.SerializerProvider;
12 import com.fasterxml.jackson.databind.ser.std.StdSerializer;
13 import java.io.IOException;
14 import org.opendaylight.transportpce.common.converter.JsonStringConverter;
15 import org.opendaylight.yang.gen.v1.gnpy.gnpy.api.rev190103.GnpyApi;
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 //This class is a temporary workaround while waiting jackson
22 //support in yang tools https://git.opendaylight.org/gerrit/c/yangtools/+/94852
23 @edu.umd.cs.findbugs.annotations.SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "temporary class")
24 public class GnpyApiSerializer extends StdSerializer<GnpyApi> {
25     private static final long serialVersionUID = 1L;
26     private static final Logger LOG = LoggerFactory.getLogger(GnpyApiSerializer.class);
27     private JsonStringConverter<GnpyApi> converter;
28     private InstanceIdentifier<GnpyApi> idGnpyApi = InstanceIdentifier.builder(GnpyApi.class).build();
29
30     public GnpyApiSerializer(JsonStringConverter<GnpyApi> converter) {
31         super(GnpyApi.class);
32         this.converter = converter;
33     }
34
35     @Override
36     public void serialize(GnpyApi value, JsonGenerator gen, SerializerProvider provider) throws IOException {
37         String requestStr = converter
38                 .createJsonStringFromDataObject(idGnpyApi, value,
39                         JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02);
40         requestStr =  requestStr.replace("gnpy-eqpt-config:", "")
41                 .replace("gnpy-path-computation-simplified:", "").replace("gnpy-network-topology:", "");
42         LOG.info("Serialized request {}", requestStr);
43         gen.writeRaw(requestStr);
44     }
45
46 }