fix functional tests
[transportpce.git] / tests / stubpce / src / main / java / org / opendaylight / transportpce / stubpce / topology / Topology.java
1 /*
2  * Copyright © 2017 Orange, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.transportpce.stubpce.topology;
10
11 import com.fasterxml.jackson.dataformat.xml.XmlMapper;
12 import java.io.BufferedReader;
13 import java.io.IOException;
14 import java.io.InputStream;
15 import java.io.InputStreamReader;
16 import org.osgi.framework.BundleContext;
17 import org.osgi.framework.FrameworkUtil;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 /**
22  * Class to build Supernode
23  * topology (fakepce.xml file has to be
24  * in src/main/ressources folder
25  * to be loaded in taget/classes).
26  *
27  *
28  * @author <a href="mailto:martial.coulibaly@gfi.com">Martial Coulibaly</a> on
29  *         behalf of Orange
30  */
31 public class Topology {
32     /** Logging. */
33     private static final Logger LOG = LoggerFactory.getLogger(Topology.class);
34     /** file must be in src/main/resources in order to be in
35      * target/classes after compilation.
36      */
37
38     /** Structure of Supernode topology. */
39     private Network network;
40     private BundleContext bcontext;
41     /** String to get Supernode topolgy info freom xml file. */
42     private String xml = null;
43
44     /**
45      * load fakepce.xml file
46      * and convert the informations
47      * in Network structure.
48      *
49      */
50     public void start() {
51         setNetwork(null);
52         XmlMapper xmlMapper = new XmlMapper();
53         try {
54             InputStream is = FrameworkUtil.getBundle(Topology.class).getBundleContext()
55                     .getBundle().getEntry("/fakepce.xml").openStream();
56             xml = inputStreamToString(is);
57             if (xml != null) {
58                 setNetwork(xmlMapper.readValue(xml, Network.class));
59                 LOG.info("Network : {}", network.toString());
60             } else {
61                 LOG.info("String xml is null");
62             }
63         } catch (IOException e) {
64             LOG.error("The file fakepce.xml not found", e);
65         }
66     }
67
68     /**
69      * get xml file
70      * content.
71      *
72      * @param is InputStream
73      * @return String xml file content
74      * @throws IOException exception raised
75      */
76     private String inputStreamToString(InputStream is) throws IOException {
77         StringBuilder sb = new StringBuilder();
78         String line;
79         BufferedReader br = new BufferedReader(new InputStreamReader(is));
80         while ((line = br.readLine()) != null) {
81             sb.append(line);
82         }
83         br.close();
84         return sb.toString();
85     }
86
87     public static void main(String[] args) {
88         Topology topo = new Topology();
89         topo.start();
90     }
91
92     public Network getNetwork() {
93         return network;
94     }
95
96     public void setNetwork(Network network) {
97         this.network = network;
98     }
99
100     public void setBcontext(BundleContext bcontext) {
101         this.bcontext = bcontext;
102     }
103
104     public BundleContext getBcontext() {
105         return this.bcontext;
106     }
107 }