0742ce9244fd4bbc090c7fd3f92e055d919dacbf
[transportpce.git] / tests / honeynode / 2.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
29 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.platform.rev180130.platform.component.top.Components;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.pm.rev181019.CurrentPmList;
31 import org.opendaylight.yangtools.yang.binding.DataObject;
32 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 import io.fd.honeycomb.transportpce.binding.converter.XMLDataObjectConverter;
37 import io.fd.honeycomb.transportpce.test.common.DataStoreContext;
38
39 /**
40  * Factory for creating the default device from the XML stored in classpath.
41  *
42  * @authors Gilles THOUENON and Christophe BETOULE ( gilles.thouenon@orange.com,
43  *          christophe.betoule@orange.com )
44  */
45 public class DefaultOcPlatformFactory {
46
47     private static final Logger LOG = LoggerFactory.getLogger(DefaultOcPlatformFactory.class);
48
49     /**
50      * Returns a new instance of {@link CurrentPmlist} from the loaded XML stored in
51      * File.
52      *
53      * @return {@link CurrentPmlist}
54      */
55     public Components createDefaultComponents(DataStoreContext dataStoreContextUtil, File oc_platform_data) {
56         Components result = null;
57         if (oc_platform_data.exists()) {
58             String oper = oc_platform_data.getName();
59             LOG.info("file '{}' exists at location : {}", oper, oc_platform_data.getAbsolutePath());
60             InputStream targetStream;
61             try {
62                 targetStream = new FileInputStream(oc_platform_data);
63                 Optional<NormalizedNode<?, ?>> transformIntoNormalizedNode = null;
64                 transformIntoNormalizedNode = 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", oper));
69                 }
70                 Optional<DataObject> dataObject = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
71                         .getDataObject(transformIntoNormalizedNode.get(), Components.QNAME);
72                 if (!dataObject.isPresent()) {
73                     LOG.warn("Could not transform normalized nodes into data object");
74                     return null;
75                 }
76                 result = (Components) dataObject.get();
77             } catch (FileNotFoundException e) {
78                 LOG.error("File not found : {} at {}", e.getMessage(), e.getLocalizedMessage());
79             } catch (IllegalStateException e) {
80                 LOG.warn("Could not transform the input OCPlatform into normalized nodes");
81                 return null;
82             }
83         } else {
84             LOG.info("xml file not existed at : '{}'", oc_platform_data.getAbsolutePath());
85         }
86         return result;
87     }
88
89     /**
90      * Returns a new instance of {@link CurrentPmlist} from the loaded XML stored in
91      * String.
92      *
93      * @return {@link CurrentPmlist}
94      */
95     public Components createDefaultComponents(DataStoreContext dataStoreContextUtil, String oc_platform_data) {
96         Components result = null;
97         if (oc_platform_data != null) {
98             LOG.info("openconfig platform data string is ok ");
99             LOG.info(oc_platform_data);
100             InputStream targetStream;
101             try {
102                 targetStream = new ByteArrayInputStream(oc_platform_data.getBytes());
103                 LOG.info("targetStream = {}", targetStream);
104                 Optional<NormalizedNode<?, ?>> transformIntoNormalizedNode = null;
105                 transformIntoNormalizedNode = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
106                         .transformIntoNormalizedNode(targetStream);
107                 if (!transformIntoNormalizedNode.isPresent()) {
108                     throw new IllegalStateException(
109                             String.format("Could not transform the input into normalized nodes"));
110                 }
111                 Optional<DataObject> dataObject = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
112                         .getDataObject(transformIntoNormalizedNode.get(), Components.QNAME);
113                 if (!dataObject.isPresent()) {
114                     LOG.warn("Could not transform normalized nodes into data object");
115                 }
116                 result = (Components) dataObject.get();
117             } catch (IllegalStateException e) {
118                 LOG.warn("Could not transform the input OCPlatform into normalized nodes");
119                 return null;
120             }
121         } else {
122             LOG.info("openconfig platform data string is null!");
123         }
124         return result;
125     }
126
127     /**
128      * create an XML String from an instance of {@link CurrentPmlist}.
129      *
130      */
131     public void createXMLFromComponents(DataStoreContext dataStoreContextUtil, Components components, String output) {
132         if (components != null) {
133             Optional<NormalizedNode<?, ?>> transformIntoNormalizedNode = null;
134             transformIntoNormalizedNode = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
135                     .toNormalizedNodes(components, Components.class);
136             if (!transformIntoNormalizedNode.isPresent()) {
137                 throw new IllegalStateException(
138                         String.format("Could not transform the input %s into normalized nodes", components));
139             }
140             XMLDataObjectConverter createWithDataStoreUtil = XMLDataObjectConverter
141                     .createWithDataStoreUtil(dataStoreContextUtil);
142             Writer writerFromDataObject = createWithDataStoreUtil.writerFromDataObject(components, Components.class,
143                     createWithDataStoreUtil.dataContainer());
144             try {
145                 BufferedWriter writer = new BufferedWriter(new FileWriter(output));
146                 writer.write(writerFromDataObject.toString());
147                 writer.close();
148             } catch (IOException e) {
149                 LOG.error("Bufferwriter error ");
150             }
151             LOG.info("openconf platform xml : {}", writerFromDataObject.toString());
152         }
153     }
154 }