fix ServiceHandler SpotBugs false positives
[transportpce.git] / tests / honeynode / 2.2.1 / honeynode-plugin-impl / src / main / java / io / fd / honeycomb / transportpce / device / tools / DefaultOcTerminalDeviceFactory.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.http.openconfig.net.yang.terminal.device.rev170708.terminal.device.top.TerminalDevice;
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 default device from the XML stored in classpath.
40  *
41  * @authors Gilles THOUENON and Christophe BETOULE ( gilles.thouenon@orange.com,
42  *          christophe.betoule@orange.com )
43  */
44 public class DefaultOcTerminalDeviceFactory {
45
46     private static final Logger LOG = LoggerFactory.getLogger(DefaultOcTerminalDeviceFactory.class);
47
48     /**
49      * Returns a new instance of {@link TerminalDevice} from the loaded XML stored in
50      * File.
51      *
52      * @return {@link TerminalDevice}
53      */
54     public TerminalDevice createDefaultTerminalDevice(DataStoreContext dataStoreContextUtil, File oc_terminal_device_data) {
55         TerminalDevice result = null;
56         if (oc_terminal_device_data.exists()) {
57             String oper = oc_terminal_device_data.getName();
58             LOG.info("file '{}' exists at location : {}", oper, oc_terminal_device_data.getAbsolutePath());
59             InputStream targetStream;
60             try {
61                 targetStream = new FileInputStream(oc_terminal_device_data);
62                 Optional<NormalizedNode<?, ?>> transformIntoNormalizedNode = null;
63                 transformIntoNormalizedNode = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
64                         .transformIntoNormalizedNode(targetStream);
65                 if (!transformIntoNormalizedNode.isPresent()) {
66                     throw new IllegalStateException(
67                             String.format("Could not transform the input %s into normalized nodes", oper));
68                 }
69                 Optional<DataObject> dataObject = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
70                         .getDataObject(transformIntoNormalizedNode.get(), TerminalDevice.QNAME);
71                 if (!dataObject.isPresent()) {
72                     LOG.warn("Could not transform normalized nodes into data object");
73                     return null;
74                 }
75                 result = (TerminalDevice) dataObject.get();
76             } catch (FileNotFoundException e) {
77                 LOG.error("File not found : {} at {}", e.getMessage(), e.getLocalizedMessage());
78             } catch (IllegalStateException e) {
79                 LOG.warn("Could not transform the input OcTerminalDevice into normalized nodes");
80                 return null;
81             }
82         } else {
83             LOG.info("xml file not existed at : '{}'", oc_terminal_device_data.getAbsolutePath());
84         }
85         return result;
86     }
87
88     /**
89      * Returns a new instance of {@link TerminalDevice} from the loaded XML stored in
90      * String.
91      *
92      * @return {@link TerminalDevice}
93      */
94     public TerminalDevice createDefaultTerminalDevice(DataStoreContext dataStoreContextUtil, String oc_terminal_device_data) {
95         TerminalDevice result = null;
96         if (oc_terminal_device_data != null) {
97             LOG.info("openconfig-terminal-device data string is ok ");
98             InputStream targetStream;
99             try {
100                 targetStream = new ByteArrayInputStream(oc_terminal_device_data.getBytes());
101                 Optional<NormalizedNode<?, ?>> transformIntoNormalizedNode = null;
102                 transformIntoNormalizedNode = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
103                         .transformIntoNormalizedNode(targetStream);
104                 if (!transformIntoNormalizedNode.isPresent()) {
105                     throw new IllegalStateException(
106                             String.format("Could not transform the input %s into normalized nodes"));
107                 }
108                 Optional<DataObject> dataObject = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
109                         .getDataObject(transformIntoNormalizedNode.get(), TerminalDevice.QNAME);
110                 if (!dataObject.isPresent()) {
111                     LOG.warn("Could not transform normalized nodes into data object");
112                 }
113                 result = (TerminalDevice) dataObject.get();
114             } catch (IllegalStateException e) {
115                 LOG.warn("Could not transform the input OcTerminalDevice into normalized nodes");
116                 return null;
117             }
118         } else {
119             LOG.info("openconfig-terminal-device data string is null!");
120         }
121         return result;
122     }
123
124     /**
125      * create an XML String from an instance of {@link TerminalDevice}.
126      *
127      */
128     public void createXMLFromTerminalDevice(DataStoreContext dataStoreContextUtil, TerminalDevice terminalDevice, String output) {
129         if (terminalDevice != null) {
130             Optional<NormalizedNode<?, ?>> transformIntoNormalizedNode = null;
131             transformIntoNormalizedNode = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
132                     .toNormalizedNodes(terminalDevice, TerminalDevice.class);
133             if (!transformIntoNormalizedNode.isPresent()) {
134                 throw new IllegalStateException(
135                         String.format("Could not transform the input %s into normalized nodes", terminalDevice));
136             }
137             XMLDataObjectConverter createWithDataStoreUtil = XMLDataObjectConverter
138                     .createWithDataStoreUtil(dataStoreContextUtil);
139             Writer writerFromDataObject = createWithDataStoreUtil.writerFromDataObject(terminalDevice, TerminalDevice.class,
140                     createWithDataStoreUtil.dataContainer());
141             try {
142                 BufferedWriter writer = new BufferedWriter(new FileWriter(output));
143                 writer.write(writerFromDataObject.toString());
144                 writer.close();
145             } catch (IOException e) {
146                 LOG.error("Bufferwriter error ");
147             }
148             LOG.info("openconfig-terminal-device xml : {}", writerFromDataObject.toString());
149         }
150     }
151 }