fix ServiceHandler SpotBugs false positives
[transportpce.git] / tests / honeynode / 2.2.1 / 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 java.io.BufferedWriter;
19 import java.io.ByteArrayInputStream;
20 import java.io.File;
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;
28
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;
34
35 import io.fd.honeycomb.transportpce.binding.converter.XMLDataObjectConverter;
36 import io.fd.honeycomb.transportpce.test.common.DataStoreContext;
37
38 /**
39  * Factory for creating the {@link Netconf} from the XML stored in classpath.
40  *
41  * @author Martial COULIBALY ( martial.coulibaly@gfi.com ) on behalf of Orange
42  */
43 public class DefaultNetconfFactory {
44
45     private static final Logger LOG = LoggerFactory.getLogger(DefaultNetconfFactory.class);
46
47     /**
48      * Returns a new instance of {@link Netconf} from the loaded XML stored in File.
49      *
50      * @return {@link Netconf}
51      */
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;
58             try {
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));
66                 }
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");
71                     return null;
72                 }
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");
78                 return null;
79             }
80         } else {
81             LOG.info("netconf xml file not existed at : '{}'", netconf_data_config.getAbsolutePath());
82         }
83         return result;
84     }
85
86     /**
87      * Returns a new instance of {@link Netconf} from the loaded XML stored in
88      * String.
89      *
90      * @return {@link Netconf}
91      */
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;
97             try {
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"));
105                 }
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");
110                     return null;
111                 }
112                 result = (Netconf) dataObject.get();
113             } catch (IllegalStateException e) {
114                 LOG.warn("Could not transform the input Netconf into normalized nodes");
115                 return null;
116             }
117         } else {
118             LOG.info("netconf data config string is null!");
119         }
120         return result;
121     }
122
123     /**
124      * create an XML String from an instance of {@link Netconf}.
125      *
126      */
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));
135             }
136             XMLDataObjectConverter createWithDataStoreUtil = XMLDataObjectConverter
137                     .createWithDataStoreUtil(dataStoreContextUtil);
138             Writer writerFromDataObject = createWithDataStoreUtil.writerFromDataObject(netconf, Netconf.class,
139                     createWithDataStoreUtil.dataContainer());
140             try {
141                 BufferedWriter writer = new BufferedWriter(new FileWriter(output));
142                 writer.write(writerFromDataObject.toString());
143                 writer.close();
144             } catch (IOException e) {
145                 LOG.error("Bufferwriter error ");
146             }
147             LOG.info("netconf xml : {}", writerFromDataObject.toString());
148         }
149     }
150 }