Honeynode Enhancements
[transportpce.git] / tests / honeynode / honeynode-plugin-impl / src / main / java / io / fd / honeycomb / transportpce / device / tools / DefaultNetconfFactory.java
1 /*
2  * Copyright (c) 2018 Orange and/or its affiliates.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package io.fd.honeycomb.transportpce.device.tools;
17
18 import io.fd.honeycomb.transportpce.binding.converter.XMLDataObjectConverter;
19 import io.fd.honeycomb.transportpce.test.common.DataStoreContext;
20
21 import java.io.BufferedWriter;
22 import java.io.ByteArrayInputStream;
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.FileNotFoundException;
26 import java.io.FileWriter;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.io.Writer;
30 import java.util.Optional;
31
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.Netconf;
33 import org.opendaylight.yangtools.yang.binding.DataObject;
34 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /**
39  * Factory for creating the {@link Netconf} from the XML stored in
40  * classpath.
41  *
42  * @author Martial COULIBALY ( martial.coulibaly@gfi.com ) on behalf of Orange
43  */
44 public class DefaultNetconfFactory {
45
46     private static final Logger LOG = LoggerFactory.getLogger(DefaultNetconfFactory.class);
47
48     /**
49      * Returns a new instance of {@link Netconf} from the loaded XML stored
50      * in File.
51      *
52      * @return {@link Netconf}
53      */
54     public Netconf createDefaultNetconf(DataStoreContext dataStoreContextUtil, File netconf_data_config) {
55         Netconf result = null;
56         if (netconf_data_config.exists()) {
57             String config = netconf_data_config.getName();
58             LOG.info("file '{}' exists at location : {}", config, netconf_data_config.getAbsolutePath());
59             InputStream targetStream;
60             try {
61                 targetStream = new FileInputStream(netconf_data_config);
62                 Optional<NormalizedNode<?, ?>> transformIntoNormalizedNode = null;
63                 transformIntoNormalizedNode =
64                         XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
65                         .transformIntoNormalizedNode(targetStream);
66                 if (!transformIntoNormalizedNode.isPresent()) {
67                     throw new IllegalStateException(
68                             String.format("Could not transform the input %s into normalized nodes",
69                             config));
70                 }
71                 Optional<DataObject> dataObject = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
72                         .getDataObject(transformIntoNormalizedNode.get(), Netconf.QNAME);
73                 if (!dataObject.isPresent()) {
74                     throw new IllegalStateException("Could not transform normalized nodes into data object");
75                 }
76                 result =  (Netconf) dataObject.get();
77             } catch (FileNotFoundException | IllegalStateException e) {
78                 LOG.error("File not found : {} at {}", e);
79             }
80         } else {
81             LOG.info("netconf xml file not existed at : '{}'", netconf_data_config.getAbsolutePath());
82         }
83         return result;
84     }
85     /**
86      * Returns a new instance of {@link Netconf} from the loaded XML stored
87      * in String.
88      *
89      * @return {@link Netconf}
90      */
91     public Netconf createDefaultNetconf(DataStoreContext dataStoreContextUtil, String netconf_data_config) {
92         Netconf result = null;
93         if (netconf_data_config != null) {
94             LOG.info("Netconf data config string is ok ");
95             InputStream targetStream;
96             targetStream = new ByteArrayInputStream(netconf_data_config.getBytes());
97             Optional<NormalizedNode<?, ?>> transformIntoNormalizedNode = null;
98             transformIntoNormalizedNode =
99                     XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
100                     .transformIntoNormalizedNode(targetStream);
101             if (!transformIntoNormalizedNode.isPresent()) {
102                 throw new IllegalStateException(
103                         String.format("Could not transform the input %s into normalized nodes"));
104             }
105             Optional<DataObject> dataObject = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
106                     .getDataObject(transformIntoNormalizedNode.get(), Netconf.QNAME);
107             if (!dataObject.isPresent()) {
108                 throw new IllegalStateException("Could not transform normalized nodes into data object");
109             }
110             result =  (Netconf) dataObject.get();
111         } else {
112             LOG.info("netconf data config string is null!");
113         }
114         return result;
115     }
116
117
118     /**
119      * create an XML String from an instance of {@link Netconf}.
120      *
121      */
122     public void createXMLFromNetconf(DataStoreContext dataStoreContextUtil, Netconf netconf, String output) {
123         if (netconf != null) {
124             Optional<NormalizedNode<?, ?>> transformIntoNormalizedNode = null;
125             transformIntoNormalizedNode =
126                     XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
127                     .toNormalizedNodes(netconf, Netconf.class);
128             if (!transformIntoNormalizedNode.isPresent()) {
129                 throw new IllegalStateException(String.format("Could not transform the input %s into normalized nodes",
130                         netconf));
131             }
132             XMLDataObjectConverter createWithDataStoreUtil = XMLDataObjectConverter
133                     .createWithDataStoreUtil(dataStoreContextUtil);
134             Writer writerFromDataObject =
135                     createWithDataStoreUtil.writerFromDataObject(netconf, Netconf.class,
136                             createWithDataStoreUtil.dataContainer());
137             try {
138                 BufferedWriter writer = new BufferedWriter(new FileWriter(output));
139                 writer.write(writerFromDataObject.toString());
140                 writer.close();
141             } catch (IOException e) {
142                 LOG.error("Bufferwriter error ");
143             }
144             LOG.info("netconf xml : {}", writerFromDataObject.toString());
145         }
146     }
147 }