BUG-1422 Introduce Call-Home functionality for the NETCONF Topology.
[netconf.git] / netconf / callhome-provider / src / main / java / org / opendaylight / netconf / callhome / mount / Configuration.java
1 /*
2  * Copyright (c) 2016 Brocade Communication Systems 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.netconf.callhome.mount;
10
11 import java.io.File;
12 import java.io.FileInputStream;
13 import java.io.IOException;
14 import java.io.InputStream;
15 import java.util.Properties;
16
17 public class Configuration {
18     public abstract static class ConfigurationException extends RuntimeException {
19         ConfigurationException(String msg) {
20             super(msg);
21         }
22
23         ConfigurationException(String msg, Exception cause) {
24             super(msg, cause);
25         }
26     }
27
28     public static class ReadException extends ConfigurationException {
29         ReadException(String msg, Exception e) {
30             super(msg, e);
31         }
32     }
33
34     public static class MissingException extends ConfigurationException {
35         private final String key;
36
37         MissingException(String key) {
38             super("Key not found: " + key);
39             this.key = key;
40         }
41
42         public String getKey() {
43             return key;
44         }
45     }
46
47     public static class IllegalValueException extends ConfigurationException {
48         private final String key;
49         private final String value;
50
51         IllegalValueException(String key, String value) {
52             super("Key has an illegal value. Key: " + key + ", Value: " + value);
53             this.key = key;
54             this.value = value;
55         }
56
57         public String getKey() {
58             return key;
59         }
60
61         public String getValue() {
62             return value;
63         }
64     }
65
66     private String path;
67     private Properties properties;
68
69     public Configuration(String path) throws ConfigurationException {
70         this.path = path;
71         try {
72             this.properties = readFromPath(path);
73         } catch (IOException ioe) {
74             throw new ReadException(path, ioe);
75         }
76     }
77
78     private Properties readFromPath(String path) throws IOException {
79         return readFromFile(new File(path));
80     }
81
82     private Properties readFromFile(File file) throws IOException {
83         FileInputStream stream = new FileInputStream(file);
84         properties = readFrom(stream);
85         return properties;
86     }
87
88     private Properties readFrom(InputStream stream) throws IOException {
89         Properties properties = new Properties();
90         properties.load(stream);
91         return properties;
92     }
93
94     public String get(String key) {
95         String result = (String) properties.get(key);
96         if (result == null)
97             throw new MissingException(key);
98         return result;
99     }
100
101     public int getAsPort(String key) {
102         String s = get(key);
103         try {
104             int newPort = Integer.parseInt(s);
105             if (newPort < 0 || newPort > 65535)
106                 throw new IllegalValueException(key, s);
107             return newPort;
108         } catch (NumberFormatException e) {
109             throw new IllegalValueException(key, s);
110         }
111     }
112 }