get rid of mdsal.binding.dom.codec.impl
[transportpce.git] / pce / src / main / java / org / opendaylight / transportpce / pce / gnpy / ServiceDataStoreOperationsImpl.java
1 /*
2  * Copyright © 2018 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
9 package org.opendaylight.transportpce.pce.gnpy;
10
11 import com.google.common.collect.FluentIterable;
12 import java.io.BufferedWriter;
13 import java.io.FileWriter;
14 import java.io.IOException;
15 import java.io.StringWriter;
16 import java.io.Writer;
17 import java.nio.charset.StandardCharsets;
18 import java.util.Collection;
19 import java.util.Collections;
20 import java.util.Optional;
21 import org.eclipse.jdt.annotation.NonNull;
22 import org.opendaylight.binding.runtime.spi.BindingRuntimeHelpers;
23 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
24 import org.opendaylight.transportpce.common.DataStoreContext;
25 import org.opendaylight.transportpce.common.converter.XMLDataObjectConverter;
26 import org.opendaylight.transportpce.common.network.NetworkTransactionService;
27 import org.opendaylight.yang.gen.v1.gnpy.path.rev200202.Result;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.OrgOpenroadmDevice;
29 import org.opendaylight.yangtools.yang.binding.DataObject;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
32 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
34 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier;
35 import org.opendaylight.yangtools.yang.data.codec.gson.JSONNormalizedNodeStreamWriter;
36 import org.opendaylight.yangtools.yang.data.codec.gson.JsonWriterFactory;
37 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
38 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 @edu.umd.cs.findbugs.annotations.SuppressFBWarnings(
43     value = "DLS_DEAD_LOCAL_STORE",
44     justification = "FIXME aluminium migration pending: need to convert GNPy to BindingDOMCodecServices")
45 public class ServiceDataStoreOperationsImpl implements ServiceDataStoreOperations {
46
47     private static final Logger LOG = LoggerFactory.getLogger(ServiceDataStoreOperationsImpl.class);
48
49     public ServiceDataStoreOperationsImpl(NetworkTransactionService networkTransactionService) {
50     }
51
52     @Override
53     public void createXMLFromDevice(DataStoreContext dataStoreContextUtil, OrgOpenroadmDevice device, String output)
54         throws GnpyException {
55
56         if (device != null) {
57             Optional<NormalizedNode<?, ?>> transformIntoNormalizedNode = null;
58             XMLDataObjectConverter cwDsU = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil);
59             transformIntoNormalizedNode = cwDsU.toNormalizedNodes(device, OrgOpenroadmDevice.class);
60             if (!transformIntoNormalizedNode.isPresent()) {
61                 throw new GnpyException(String.format(
62                     "In ServiceDataStoreOperationsImpl: Cannot transform the device %s into normalized nodes",
63                     device.toString()));
64             }
65             Writer writerFromDataObject =
66                 cwDsU.writerFromDataObject(device, OrgOpenroadmDevice.class,cwDsU.dataContainer());
67             try (BufferedWriter writer = new BufferedWriter(new FileWriter(output,StandardCharsets.UTF_8))) {
68                 writer.write(writerFromDataObject.toString());
69             } catch (IOException e) {
70                 throw new GnpyException(
71                     String.format("In ServiceDataStoreOperationsImpl : Bufferwriter error %s",e));
72             }
73             LOG.debug("GNPy: device xml : {}", writerFromDataObject);
74         }
75     }
76
77     @Override
78     public String createJsonStringFromDataObject(final InstanceIdentifier<?> id, DataObject object)
79         throws GnpyException, Exception {
80
81         final SchemaPath scPath = SchemaPath.create(FluentIterable
82                 .from(id.getPathArguments())
83                 .transform(input -> BindingReflections.findQName(input.getType())), true);
84
85         // Prepare the variables
86         // Create the schema context
87         Collection<? extends YangModuleInfo> moduleInfos = Collections.singleton(BindingReflections
88                 .getModuleInfo(Result.class));
89         @NonNull
90         EffectiveModelContext schemaContext = BindingRuntimeHelpers.createEffectiveModel(moduleInfos);
91
92         /*
93          * This function needs : - context - scPath.getParent() -
94          * scPath.getLastComponent().getNamespace(), -
95          * JsonWriterFactory.createJsonWriter(writer)
96          */
97         final Writer writer = new StringWriter();
98
99         try (NormalizedNodeStreamWriter domWriter = JSONNormalizedNodeStreamWriter.createExclusiveWriter(
100                 JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.createSimple(schemaContext),
101                 scPath.getParent(), scPath.getLastComponent().getNamespace(),
102                 JsonWriterFactory.createJsonWriter(writer, 2));) {
103             // The write part
104             //FIXME
105             //codecRegistry.getSerializer(id.getTargetType()).serialize(object, codecRegistry.newWriter(id, domWriter));
106         } catch (IOException e) {
107             throw new GnpyException("In ServiceDataStoreOperationsImpl: exception during json file creation",e);
108         }
109         return writer.toString();
110     }
111
112     // Write the json as a string in a file
113     @Override
114     public void writeStringFile(String jsonString, String fileName) throws GnpyException {
115         try (FileWriter file = new FileWriter(fileName,StandardCharsets.UTF_8)) {
116             file.write(jsonString);
117         } catch (IOException e) {
118             throw new GnpyException("In ServiceDataStoreOperationsImpl : exception during file writing",e);
119         }
120     }
121 }