cac60898157628db4797a05e5ac0844f749aebb9
[transportpce.git] / tests / honeynode / 1.2.1 / honeynode-plugin-impl / src / main / java / io / fd / honeycomb / transportpce / device / tools / DefaultOcPlatformFactory.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 import org.opendaylight.yang.gen.v1.http.org.openroadm.pm.rev161014.CurrentPmlist;
29 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.platform.rev180130.platform.component.top.Components;
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 DefaultOcPlatformFactory {
45
46     private static final Logger LOG = LoggerFactory.getLogger(DefaultOcPlatformFactory.class);
47
48     /**
49      * Returns a new instance of {@link CurrentPmlist} from the loaded XML stored in
50      * File.
51      *
52      * @return {@link CurrentPmlist}
53      */
54     public Components createDefaultComponents(DataStoreContext dataStoreContextUtil, File oc_platform_data) {
55         Components result = null;
56         if (oc_platform_data.exists()) {
57             String oper = oc_platform_data.getName();
58             LOG.info("file '{}' exists at location : {}", oper, oc_platform_data.getAbsolutePath());
59             InputStream targetStream;
60             try {
61                 targetStream = new FileInputStream(oc_platform_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(), Components.QNAME);
71                 if (!dataObject.isPresent()) {
72                     LOG.warn("Could not transform normalized nodes into data object");
73                     return null;
74                 }
75                 result = (Components) 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 OCPlatform into normalized nodes");
80                 return null;
81             }
82         } else {
83             LOG.info("xml file not existed at : '{}'", oc_platform_data.getAbsolutePath());
84         }
85         return result;
86     }
87
88     /**
89      * Returns a new instance of {@link CurrentPmlist} from the loaded XML stored in
90      * String.
91      *
92      * @return {@link CurrentPmlist}
93      */
94     public Components createDefaultComponents(DataStoreContext dataStoreContextUtil, String oc_platform_data) {
95         Components result = null;
96         if (oc_platform_data != null) {
97             LOG.info("openconfig platform data string is ok ");
98             LOG.info(oc_platform_data);
99             InputStream targetStream;
100             try {
101                 targetStream = new ByteArrayInputStream(oc_platform_data.getBytes());
102                 LOG.info("targetStream = {}", targetStream);
103                 Optional<NormalizedNode<?, ?>> transformIntoNormalizedNode = null;
104                 transformIntoNormalizedNode = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
105                         .transformIntoNormalizedNode(targetStream);
106                 if (!transformIntoNormalizedNode.isPresent()) {
107                     throw new IllegalStateException(
108                             String.format("Could not transform the input into normalized nodes"));
109                 }
110                 Optional<DataObject> dataObject = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
111                         .getDataObject(transformIntoNormalizedNode.get(), Components.QNAME);
112                 if (!dataObject.isPresent()) {
113                     LOG.warn("Could not transform normalized nodes into data object");
114                 }
115                 result = (Components) dataObject.get();
116             } catch (IllegalStateException e) {
117                 LOG.warn("Could not transform the input OCPlatform into normalized nodes");
118                 return null;
119             }
120         } else {
121             LOG.info("openconfig platform data string is null!");
122         }
123         return result;
124     }
125
126     /**
127      * create an XML String from an instance of {@link CurrentPmlist}.
128      *
129      */
130     public void createXMLFromComponents(DataStoreContext dataStoreContextUtil, Components components, String output) {
131         if (components != null) {
132             Optional<NormalizedNode<?, ?>> transformIntoNormalizedNode = null;
133             transformIntoNormalizedNode = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
134                     .toNormalizedNodes(components, Components.class);
135             if (!transformIntoNormalizedNode.isPresent()) {
136                 throw new IllegalStateException(
137                         String.format("Could not transform the input %s into normalized nodes", components));
138             }
139             XMLDataObjectConverter createWithDataStoreUtil = XMLDataObjectConverter
140                     .createWithDataStoreUtil(dataStoreContextUtil);
141             Writer writerFromDataObject = createWithDataStoreUtil.writerFromDataObject(components, Components.class,
142                     createWithDataStoreUtil.dataContainer());
143             try {
144                 BufferedWriter writer = new BufferedWriter(new FileWriter(output));
145                 writer.write(writerFromDataObject.toString());
146                 writer.close();
147             } catch (IOException e) {
148                 LOG.error("Bufferwriter error ");
149             }
150             LOG.info("openconf platform xml : {}", writerFromDataObject.toString());
151         }
152     }
153 }