Stubpce Update
[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
13 import java.io.BufferedReader;
14 import java.io.IOException;
15 import java.io.InputStream;
16 import java.io.InputStreamReader;
17
18 import org.osgi.framework.BundleContext;
19 import org.osgi.framework.FrameworkUtil;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * Class to build Supernode
25  * topology (fakepce.xml file has to be
26  * in src/main/ressources folder
27  * to be loaded in taget/classes).
28  *
29  *
30  * @author <a href="mailto:martial.coulibaly@gfi.com">Martial Coulibaly</a> on
31  *         behalf of Orange
32  */
33 public class Topology {
34     /** Logging. */
35     private static final Logger LOG = LoggerFactory.getLogger(Topology.class);
36     /** file must be in src/main/resources in order to be in
37      * target/classes after compilation.
38      */
39
40     /** Structure of Supernode topology. */
41     private Network network;
42     public BundleContext bcontext;
43     /** String to get Supernode topolgy info freom xml file. */
44     private String xml = null;
45
46     /**
47      * load fakepce.xml file
48      * and convert the informations
49      * in Network structure.
50      *
51      */
52     public void start() {
53         setNetwork(null);
54         XmlMapper xmlMapper = new XmlMapper();
55         try {
56             InputStream is = FrameworkUtil.getBundle(Topology.class).getBundleContext()
57                     .getBundle().getEntry("/fakepce.xml").openStream();
58             /*File file = new File("target/classes/fakepce.xml");
59             InputStream is = new FileInputStream(file);*/
60             xml = inputStreamToString(is);
61             if (xml != null) {
62                 setNetwork(xmlMapper.readValue(xml, Network.class));
63                 LOG.info("Network : " + network.toString());
64             } else {
65                 LOG.info("String xml is null");
66             }
67         } catch (IOException e) {
68             LOG.error("The file fakepce.xml not found", e);
69         }
70     }
71
72     /**
73      * get xml file
74      * content.
75      *
76      * @param is InputStream
77      * @return String xml file content
78      * @throws IOException exception raised
79      */
80     private String inputStreamToString(InputStream is) throws IOException {
81         StringBuilder sb = new StringBuilder();
82         String line;
83         BufferedReader br = new BufferedReader(new InputStreamReader(is));
84         while ((line = br.readLine()) != null) {
85             sb.append(line);
86         }
87         br.close();
88         return sb.toString();
89     }
90
91     public static void main(String[] args) {
92         Topology topo = new Topology();
93         topo.start();
94     }
95
96     public Network getNetwork() {
97         return network;
98     }
99
100     public void setNetwork(Network network) {
101         this.network = network;
102     }
103
104     public void setBcontext(BundleContext bcontext) {
105         this.bcontext = bcontext;
106     }
107
108     public BundleContext getBcontext() {
109         return this.bcontext;
110     }
111 }