1e96505451ce3886f8c670237801489126b9e38d
[public/netxms.git] / webui / webapp / SNMP / src / org / netxms / ui / eclipse / snmp / views / helpers / SnmpConfig.java
1 package org.netxms.ui.eclipse.snmp.views.helpers;
2
3 import java.io.IOException;
4 import java.util.ArrayList;
5 import java.util.Arrays;
6 import java.util.List;
7 import java.util.Map;
8 import org.netxms.client.NXCException;
9 import org.netxms.client.NXCSession;
10 import org.netxms.client.server.ServerVariable;
11 import org.netxms.client.snmp.SnmpUsmCredential;
12
13 public class SnmpConfig
14 {
15 private List<String> communities;
16 private List<SnmpUsmCredential> usmCredentials;
17 private List<String> ports;
18
19 /**
20 * Create empty object
21 */
22 private SnmpConfig()
23 {
24 }
25
26 /**
27 * Load SNMP configuration from server. This method directly calls
28 * communication API, so it should not be called from UI thread.
29 *
30 * @param session communication session to use
31 * @return SNMP configuration
32 * @throws IOException if socket I/O error occurs
33 * @throws NXCException if NetXMS server returns an error or operation was timed out
34 */
35 public static SnmpConfig load(NXCSession session) throws NXCException, IOException
36 {
37 SnmpConfig config = new SnmpConfig();
38
39 config.communities = session.getSnmpCommunities();
40 config.usmCredentials = session.getSnmpUsmCredentials();
41 Map<String, ServerVariable> variables = session.getServerVariables();
42 ServerVariable v = variables.get("SNMPPorts");
43 config.ports = parsePorts(v != null ? v.getValue() : "" );
44
45 return config;
46 }
47
48 /**
49 *
50 * @param portList
51 */
52 public static List<String> parsePorts(String portList)
53 {
54 String[] arr = portList.split(",");
55 List<String> list = new ArrayList<String>(Arrays.asList(arr));
56 return list;
57 }
58
59 public String parsePorts()
60 {
61 StringBuilder str = new StringBuilder();
62 for(int i = 0; i < ports.size(); i++)
63 {
64 str.append(ports.get(i));
65 if(i != ports.size() - 1)
66 {
67 str.append(",");
68 }
69 }
70 return str.toString();
71 }
72 /**
73 * Save SNMP configuration on server. This method calls communication
74 * API directly, so it should not be called from UI thread.
75 *
76 * @params session communication session to use
77 * @throws IOException if socket I/O error occurs
78 * @throws NXCException if NetXMS server returns an error or operation was timed out
79 */
80 public void save(NXCSession session) throws NXCException, IOException
81 {
82 session.updateSnmpCommunities(communities);
83 session.updateSnmpUsmCredentials(usmCredentials);
84 session.setServerVariable("SNMPPorts", parsePorts());
85 }
86
87 /**
88 * @return the communities
89 */
90 public List<String> getCommunities()
91 {
92 return communities;
93 }
94
95 /**
96 * @param communities the communities to set
97 */
98 public void setCommunities(List<String> communities)
99 {
100 this.communities = communities;
101 }
102
103 /**
104 * @return the ports
105 */
106 public List<String> getPorts()
107 {
108 return ports;
109 }
110
111 /**
112 * @param communities the communities to set
113 */
114 public void setPorts(List<String> ports)
115 {
116 this.ports = ports;
117 }
118
119 /**
120 * @return the usmCredentials
121 */
122 public List<SnmpUsmCredential> getUsmCredentials()
123 {
124 return usmCredentials;
125 }
126
127 /**
128 * @param usmCredentials the usmCredentials to set
129 */
130 public void setUsmCredentials(List<SnmpUsmCredential> usmCredentials)
131 {
132 this.usmCredentials = usmCredentials;
133 }
134
135 }