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