2 * Copyright (c) 2018 Orange and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package io.fd.honeycomb.transportpce.device.tools;
18 import java.io.BufferedWriter;
19 import java.io.ByteArrayInputStream;
21 import java.io.FileInputStream;
22 import java.io.FileNotFoundException;
23 import java.io.FileWriter;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.Writer;
27 import java.util.Optional;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.Netconf;
30 import org.opendaylight.yangtools.yang.binding.DataObject;
31 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 import io.fd.honeycomb.transportpce.binding.converter.XMLDataObjectConverter;
36 import io.fd.honeycomb.transportpce.test.common.DataStoreContext;
39 * Factory for creating the {@link Netconf} from the XML stored in classpath.
41 * @author Martial COULIBALY ( martial.coulibaly@gfi.com ) on behalf of Orange
43 public class DefaultNetconfFactory {
45 private static final Logger LOG = LoggerFactory.getLogger(DefaultNetconfFactory.class);
48 * Returns a new instance of {@link Netconf} from the loaded XML stored in File.
50 * @return {@link Netconf}
52 public Netconf createDefaultNetconf(DataStoreContext dataStoreContextUtil, File netconf_data_config) {
53 Netconf result = null;
54 if (netconf_data_config.exists()) {
55 String config = netconf_data_config.getName();
56 LOG.info("file '{}' exists at location : {}", config, netconf_data_config.getAbsolutePath());
57 InputStream targetStream;
59 targetStream = new FileInputStream(netconf_data_config);
60 Optional<NormalizedNode<?, ?>> transformIntoNormalizedNode = null;
61 transformIntoNormalizedNode = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
62 .transformIntoNormalizedNode(targetStream);
63 if (!transformIntoNormalizedNode.isPresent()) {
64 throw new IllegalStateException(
65 String.format("Could not transform the input %s into normalized nodes", config));
67 Optional<DataObject> dataObject = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
68 .getDataObject(transformIntoNormalizedNode.get(), Netconf.QNAME);
69 if (!dataObject.isPresent()) {
70 LOG.warn("Could not transform normalized nodes into data object");
73 result = (Netconf) dataObject.get();
74 } catch (FileNotFoundException e) {
75 LOG.error("File not found : {} at {}", e.getMessage(), e.getLocalizedMessage());
76 } catch (IllegalStateException e) {
77 LOG.warn("Could not transform the input Netconf into normalized nodes");
81 LOG.info("netconf xml file not existed at : '{}'", netconf_data_config.getAbsolutePath());
87 * Returns a new instance of {@link Netconf} from the loaded XML stored in
90 * @return {@link Netconf}
92 public Netconf createDefaultNetconf(DataStoreContext dataStoreContextUtil, String netconf_data_config) {
93 Netconf result = null;
94 if (netconf_data_config != null) {
95 LOG.info("Netconf data config string is ok ");
96 InputStream targetStream;
98 targetStream = new ByteArrayInputStream(netconf_data_config.getBytes());
99 Optional<NormalizedNode<?, ?>> transformIntoNormalizedNode = null;
100 transformIntoNormalizedNode = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
101 .transformIntoNormalizedNode(targetStream);
102 if (!transformIntoNormalizedNode.isPresent()) {
103 throw new IllegalStateException(
104 String.format("Could not transform the input %s into normalized nodes"));
106 Optional<DataObject> dataObject = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
107 .getDataObject(transformIntoNormalizedNode.get(), Netconf.QNAME);
108 if (!dataObject.isPresent()) {
109 LOG.warn("Could not transform normalized nodes into data object");
112 result = (Netconf) dataObject.get();
113 } catch (IllegalStateException e) {
114 LOG.warn("Could not transform the input Netconf into normalized nodes");
118 LOG.info("netconf data config string is null!");
124 * create an XML String from an instance of {@link Netconf}.
127 public void createXMLFromNetconf(DataStoreContext dataStoreContextUtil, Netconf netconf, String output) {
128 if (netconf != null) {
129 Optional<NormalizedNode<?, ?>> transformIntoNormalizedNode = null;
130 transformIntoNormalizedNode = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
131 .toNormalizedNodes(netconf, Netconf.class);
132 if (!transformIntoNormalizedNode.isPresent()) {
133 throw new IllegalStateException(
134 String.format("Could not transform the input %s into normalized nodes", netconf));
136 XMLDataObjectConverter createWithDataStoreUtil = XMLDataObjectConverter
137 .createWithDataStoreUtil(dataStoreContextUtil);
138 Writer writerFromDataObject = createWithDataStoreUtil.writerFromDataObject(netconf, Netconf.class,
139 createWithDataStoreUtil.dataContainer());
141 BufferedWriter writer = new BufferedWriter(new FileWriter(output));
142 writer.write(writerFromDataObject.toString());
144 } catch (IOException e) {
145 LOG.error("Bufferwriter error ");
147 LOG.info("netconf xml : {}", writerFromDataObject.toString());