2 * Copyright © 2018 Orange, Inc. and others. All rights reserved.
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
9 package org.opendaylight.transportpce.pce.gnpy;
11 import com.google.common.base.Function;
12 import com.google.common.collect.FluentIterable;
14 import java.io.BufferedWriter;
15 import java.io.FileWriter;
16 import java.io.IOException;
17 import java.io.StringWriter;
18 import java.io.Writer;
19 import java.util.Collections;
20 import java.util.Optional;
22 import org.opendaylight.mdsal.binding.dom.codec.impl.BindingNormalizedNodeCodecRegistry;
23 import org.opendaylight.mdsal.binding.generator.impl.ModuleInfoBackedContext;
24 import org.opendaylight.mdsal.binding.generator.util.BindingRuntimeContext;
25 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
26 import org.opendaylight.transportpce.common.DataStoreContext;
27 import org.opendaylight.transportpce.common.converter.XMLDataObjectConverter;
28 import org.opendaylight.transportpce.common.network.NetworkTransactionService;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.OrgOpenroadmDevice;
30 import org.opendaylight.yangtools.yang.binding.BindingStreamEventWriter;
31 import org.opendaylight.yangtools.yang.binding.DataObject;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument;
34 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
35 import org.opendaylight.yangtools.yang.common.QName;
36 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
38 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier;
39 import org.opendaylight.yangtools.yang.data.codec.gson.JSONNormalizedNodeStreamWriter;
40 import org.opendaylight.yangtools.yang.data.codec.gson.JsonWriterFactory;
41 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
42 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
43 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
44 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
48 public class ServiceDataStoreOperationsImpl implements ServiceDataStoreOperations {
50 private static final Logger LOG = LoggerFactory.getLogger(ServiceDataStoreOperationsImpl.class);
52 public ServiceDataStoreOperationsImpl(NetworkTransactionService networkTransactionService) {
55 public void createXMLFromDevice(DataStoreContext dataStoreContextUtil, OrgOpenroadmDevice device, String output)
56 throws GnpyException {
59 Optional<NormalizedNode<?, ?>> transformIntoNormalizedNode = null;
60 XMLDataObjectConverter cwDsU = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil);
61 transformIntoNormalizedNode = cwDsU.toNormalizedNodes(device, OrgOpenroadmDevice.class);
62 if (!transformIntoNormalizedNode.isPresent()) {
63 throw new GnpyException(String.format(
64 "In ServiceDataStoreOperationsImpl: Cannot transform the device %s into normalized nodes",
67 Writer writerFromDataObject =
68 cwDsU.writerFromDataObject(device, OrgOpenroadmDevice.class,cwDsU.dataContainer());
70 BufferedWriter writer = new BufferedWriter(new FileWriter(output));
71 writer.write(writerFromDataObject.toString());
73 } catch (IOException e) {
74 throw new GnpyException(
75 String.format("In ServiceDataStoreOperationsImpl : Bufferwriter error"),e);
77 LOG.debug("GNPy: device xml : {}", writerFromDataObject.toString());
81 public String createJsonStringFromDataObject(final InstanceIdentifier<?> id, DataObject object)
82 throws GnpyException, Exception {
84 final SchemaPath scPath = SchemaPath
85 .create(FluentIterable.from(id.getPathArguments()).transform(new Function<PathArgument, QName>() {
87 public QName apply(final PathArgument input) {
88 return BindingReflections.findQName(input.getType());
91 final Writer writer = new StringWriter();
92 NormalizedNodeStreamWriter domWriter;
95 // Prepare the variables
96 final ModuleInfoBackedContext moduleContext = ModuleInfoBackedContext.create();
97 Iterable<? extends YangModuleInfo> moduleInfos = Collections
98 .singleton(BindingReflections.getModuleInfo(object.getClass()));
99 moduleContext.addModuleInfos(moduleInfos);
100 SchemaContext schemaContext = moduleContext.tryToCreateSchemaContext().get();
101 BindingRuntimeContext bindingContext;
102 bindingContext = BindingRuntimeContext.create(moduleContext, schemaContext);
103 final BindingNormalizedNodeCodecRegistry codecRegistry =
104 new BindingNormalizedNodeCodecRegistry(bindingContext);
107 * This function needs : - context - scPath.getParent() -
108 * scPath.getLastComponent().getNamespace(), -
109 * JsonWriterFactory.createJsonWriter(writer)
111 domWriter = JSONNormalizedNodeStreamWriter.createExclusiveWriter(
112 JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.createSimple(schemaContext),
113 scPath.getParent(), scPath.getLastComponent().getNamespace(),
114 JsonWriterFactory.createJsonWriter(writer, 2));
116 final BindingStreamEventWriter bindingWriter = codecRegistry.newWriter(id, domWriter);
117 codecRegistry.getSerializer(id.getTargetType()).serialize(object, bindingWriter);
120 } catch (IOException | YangSyntaxErrorException | ReactorException e) {
121 throw new GnpyException("In ServiceDataStoreOperationsImpl: exception during json file creation",e);
123 return writer.toString();
126 // Write the json as a string in a file
127 public void writeStringFile(String jsonString, String fileName) throws GnpyException {
129 FileWriter file = new FileWriter(fileName);
130 file.write(jsonString);
132 } catch (IOException e) {
133 throw new GnpyException("In ServiceDataStoreOperationsImpl : exception during file writing",e);