526792c5370eccbd9ec40038ba332972ac8d2a03
[public/netxms.git] / src / java / netxms-eclipse / ServerConfig / src / org / netxms / ui / eclipse / serverconfig / views / helpers / DiscoveryConfig.java
1 /**
2 *
3 */
4 package org.netxms.ui.eclipse.serverconfig.views.helpers;
5
6 import java.io.IOException;
7 import java.util.List;
8 import java.util.Map;
9 import org.netxms.client.IpAddressListElement;
10 import org.netxms.client.NXCException;
11 import org.netxms.client.NXCSession;
12 import org.netxms.client.server.ServerVariable;
13 import org.netxms.ui.eclipse.shared.ConsoleSharedData;
14
15 /**
16 * Class which holds all elements of network discovery configuration
17 */
18 public class DiscoveryConfig
19 {
20 private boolean enabled;
21 private boolean active;
22 private boolean useSnmpTraps;
23 private int filterFlags;
24 private String filter;
25 private List<IpAddressListElement> targets;
26 private List<IpAddressListElement> addressFilter;
27
28 /**
29 * Create empty object
30 */
31 private DiscoveryConfig()
32 {
33 }
34
35 /**
36 * Load discovery configuration from server. This method directly calls
37 * communication API, so it should not be called from UI thread.
38 *
39 * @return network discovery configuration
40 * @throws IOException if socket I/O error occurs
41 * @throws NXCException if NetXMS server returns an error or operation was timed out
42 */
43 public static DiscoveryConfig load() throws NXCException, IOException
44 {
45 DiscoveryConfig config = new DiscoveryConfig();
46
47 final NXCSession session = ConsoleSharedData.getSession();
48 Map<String, ServerVariable> variables = session.getServerVariables();
49
50 config.enabled = getBoolean(variables, "RunNetworkDiscovery", false); //$NON-NLS-1$
51 config.active = getBoolean(variables, "ActiveNetworkDiscovery", false); //$NON-NLS-1$
52 config.useSnmpTraps = getBoolean(variables, "UseSNMPTrapsForDiscovery", false); //$NON-NLS-1$
53 config.filterFlags = getInteger(variables, "DiscoveryFilterFlags", 0); //$NON-NLS-1$
54 config.filter = getString(variables, "DiscoveryFilter", "none"); //$NON-NLS-1$ //$NON-NLS-2$
55
56 config.addressFilter = session.getAddressList(NXCSession.ADDRESS_LIST_DISCOVERY_FILTER);
57 config.targets = session.getAddressList(NXCSession.ADDRESS_LIST_DISCOVERY_TARGETS);
58
59 return config;
60 }
61
62 /**
63 * Get boolean value from server variables
64 *
65 * @param variables
66 * @param name
67 * @param defVal
68 * @return
69 */
70 private static boolean getBoolean(Map<String, ServerVariable> variables, String name, boolean defVal)
71 {
72 ServerVariable v = variables.get(name);
73 if (v == null)
74 return defVal;
75 try
76 {
77 return Integer.parseInt(v.getValue()) != 0;
78 }
79 catch(NumberFormatException e)
80 {
81 return defVal;
82 }
83 }
84
85 /**
86 * Get integer value from server variables
87 *
88 * @param variables
89 * @param name
90 * @param defVal
91 * @return
92 */
93 private static int getInteger(Map<String, ServerVariable> variables, String name, int defVal)
94 {
95 ServerVariable v = variables.get(name);
96 if (v == null)
97 return defVal;
98 try
99 {
100 return Integer.parseInt(v.getValue());
101 }
102 catch(NumberFormatException e)
103 {
104 return defVal;
105 }
106 }
107
108 /**
109 * Get string value from server variables
110 *
111 * @param variables
112 * @param name
113 * @param defVal
114 * @return
115 */
116 private static String getString(Map<String, ServerVariable> variables, String name, String defVal)
117 {
118 ServerVariable v = variables.get(name);
119 if (v == null)
120 return defVal;
121 return v.getValue();
122 }
123
124 /**
125 * Save discovery configuration on server. This method calls communication
126 * API directly, so it should not be called from UI thread.
127 *
128 * @throws IOException if socket I/O error occurs
129 * @throws NXCException if NetXMS server returns an error or operation was timed out
130 */
131 public void save() throws NXCException, IOException
132 {
133 final NXCSession session = (NXCSession)ConsoleSharedData.getSession();
134
135 session.setServerVariable("RunNetworkDiscovery", enabled ? "1" : "0"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
136 session.setServerVariable("ActiveNetworkDiscovery", active ? "1" : "0"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
137 session.setServerVariable("UseSNMPTrapsForDiscovery", useSnmpTraps ? "1" : "0"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
138 session.setServerVariable("DiscoveryFilterFlags", Integer.toString(filterFlags)); //$NON-NLS-1$
139 session.setServerVariable("DiscoveryFilter", filter); //$NON-NLS-1$
140
141 session.setAddressList(NXCSession.ADDRESS_LIST_DISCOVERY_FILTER, addressFilter);
142 session.setAddressList(NXCSession.ADDRESS_LIST_DISCOVERY_TARGETS, targets);
143
144 session.resetServerComponent(NXCSession.SERVER_COMPONENT_DISCOVERY_MANAGER);
145 }
146
147 /**
148 * @return the enabled
149 */
150 public boolean isEnabled()
151 {
152 return enabled;
153 }
154
155 /**
156 * @param enabled the enabled to set
157 */
158 public void setEnabled(boolean enabled)
159 {
160 this.enabled = enabled;
161 }
162
163 /**
164 * @return the active
165 */
166 public boolean isActive()
167 {
168 return active;
169 }
170
171 /**
172 * @param active the active to set
173 */
174 public void setActive(boolean active)
175 {
176 this.active = active;
177 }
178
179 /**
180 * @return the filterFlags
181 */
182 public int getFilterFlags()
183 {
184 return filterFlags;
185 }
186
187 /**
188 * @param filterFlags the filterFlags to set
189 */
190 public void setFilterFlags(int filterFlags)
191 {
192 this.filterFlags = filterFlags;
193 }
194
195 /**
196 * @return the filter
197 */
198 public String getFilter()
199 {
200 return filter;
201 }
202
203 /**
204 * @param filter the filter to set
205 */
206 public void setFilter(String filter)
207 {
208 this.filter = filter;
209 }
210
211 /**
212 * @return the targets
213 */
214 public List<IpAddressListElement> getTargets()
215 {
216 return targets;
217 }
218
219 /**
220 * @param targets the targets to set
221 */
222 public void setTargets(List<IpAddressListElement> targets)
223 {
224 this.targets = targets;
225 }
226
227 /**
228 * @return the addressFilter
229 */
230 public List<IpAddressListElement> getAddressFilter()
231 {
232 return addressFilter;
233 }
234
235 /**
236 * @param addressFilter the addressFilter to set
237 */
238 public void setAddressFilter(List<IpAddressListElement> addressFilter)
239 {
240 this.addressFilter = addressFilter;
241 }
242
243 /**
244 * @return the useSnmpTraps
245 */
246 public boolean isUseSnmpTraps()
247 {
248 return useSnmpTraps;
249 }
250
251 /**
252 * @param useSnmpTraps the useSnmpTraps to set
253 */
254 public void setUseSnmpTraps(boolean useSnmpTraps)
255 {
256 this.useSnmpTraps = useSnmpTraps;
257 }
258 }