forked from lstoll/libvirt-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetwork.java
More file actions
246 lines (222 loc) · 7.57 KB
/
Copy pathNetwork.java
File metadata and controls
246 lines (222 loc) · 7.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package org.libvirt;
import org.libvirt.jna.Libvirt;
import org.libvirt.jna.NetworkPointer;
import com.sun.jna.Native;
import com.sun.jna.ptr.IntByReference;
/**
* A network object defined by libvirt.
*/
public class Network {
/**
* The native virNetworkPtr
*/
NetworkPointer VNP;
/**
* The Connect Object that represents the Hypervisor of this Network
*/
protected Connect virConnect;
/**
* The libvirt connection from the hypervisor
*/
protected Libvirt libvirt;
/**
* Constructs a Network object from a known native virNetworkPtr, and a
* Connect object. For use when native libvirt returns a virConnectPtr, i.e.
* error handling.
*
* @param virConnect
* @param VNP
*/
Network(Connect virConnect, NetworkPointer VNP) {
this.virConnect = virConnect;
this.VNP = VNP;
libvirt = virConnect.libvirt;
}
/**
* Creates and starts this defined network. If the call succeeds the network
* moves from the defined to the running networks pools.
*
* @throws LibvirtException
*/
public void create() throws LibvirtException {
int result = libvirt.virNetworkCreate(VNP);
ErrorHandler.processError(libvirt, result);
}
/**
* Destroy this network object. The running instance is shutdown if not down
* already and all resources used by it are given back to the hypervisor.
* The object becomes invalid and should not be used thereafter if the call
* does not return an error. This function may require priviledged access
*
* @throws LibvirtException
*/
public void destroy() throws LibvirtException {
int result = libvirt.virNetworkDestroy(VNP);
ErrorHandler.processError(libvirt, result);
}
@Override
public void finalize() throws LibvirtException {
free();
}
/**
* Frees this network object. The running instance is kept alive. The object
* becomes invalid and should not be used thereafter if the call does not
* return an error.
*
* @throws LibvirtException
* @return number of references left (>= 0) for success, -1 for failure.
*/
public int free() throws LibvirtException {
int success = 0;
if (VNP != null) {
success = libvirt.virNetworkFree(VNP);
ErrorHandler.processError(libvirt, success);
VNP = null;
}
return success;
}
/**
* Provides a boolean value indicating whether this network is configured to
* be automatically started when the host machine boots.
*
* @return true if autostarted, false otherwise
* @throws LibvirtException
*/
public boolean getAutostart() throws LibvirtException {
IntByReference autoStart = new IntByReference();
int result = libvirt.virNetworkGetAutostart(VNP, autoStart);
ErrorHandler.processError(libvirt, result);
return (autoStart.getValue() != 0) ? true : false;
}
/**
* Provides a bridge interface name to which a domain may connect a network
* interface in order to join this network.
*
* @return the interface name
* @throws LibvirtException
*/
public String getBridgeName() throws LibvirtException {
String returnValue = libvirt.virNetworkGetBridgeName(VNP);
ErrorHandler.processError(libvirt, returnValue);
return returnValue;
}
/**
* Provides the connection pointer associated with this network.
*
* @return the Connect object
*/
public Connect getConnect() {
return virConnect;
}
/**
* Gets the public name for this network
*
* @return the public name
* @throws LibvirtException
*/
public String getName() throws LibvirtException {
String returnValue = libvirt.virNetworkGetName(VNP);
ErrorHandler.processError(libvirt, returnValue);
return returnValue;
}
/**
* Gets the UUID for this network
*
* @return the UUID as an unpacked int array
* @throws LibvirtException
* @see <a href="http://www.ietf.org/rfc/rfc4122.txt">rfc4122</a>
*/
public int[] getUUID() throws LibvirtException {
byte[] bytes = new byte[Libvirt.VIR_UUID_BUFLEN];
int success = libvirt.virNetworkGetUUID(VNP, bytes);
ErrorHandler.processError(libvirt, success);
int[] returnValue = new int[0];
if (success == 0) {
returnValue = Connect.convertUUIDBytes(bytes);
}
return returnValue;
}
/**
* Gets the UUID for a network as string.
*
* @return the UUID in canonical String format
* @throws LibvirtException
* @see <a href="http://www.ietf.org/rfc/rfc4122.txt">rfc4122</a>
*/
public String getUUIDString() throws LibvirtException {
byte[] bytes = new byte[Libvirt.VIR_UUID_STRING_BUFLEN];
int success = libvirt.virNetworkGetUUIDString(VNP, bytes);
ErrorHandler.processError(libvirt, success);
String returnValue = null;
if (success == 0) {
returnValue = Native.toString(bytes);
}
return returnValue;
}
/**
* Provides an XML description of this network. The description may be
* reused later to relaunch the network with
* Virconnect.virNetworkCreateXML().
*
* @param flags
* and OR'ed set of extraction flags, not used yet
* @return The XML representation of this network
* @throws LibvirtException
*/
public String getXMLDesc(int flags) throws LibvirtException {
String returnValue = libvirt.virNetworkGetXMLDesc(VNP, flags);
ErrorHandler.processError(libvirt, returnValue);
return returnValue;
}
/**
* Determine if the network is currently running
*
* @see <a
* href="http://www.libvirt.org/html/libvirt-libvirt.html#virNetworkIsActive">Libvirt
* Documentation</a>
* @return 1 if running, 0 if inactive, -1 on error
* @throws LibvirtException
*/
public int isActive() throws LibvirtException {
int returnValue = libvirt.virNetworkIsActive(VNP);
ErrorHandler.processError(libvirt, returnValue);
return returnValue;
}
/**
* Determine if the network has a persistent configuration which means it
* will still exist after shutting down
*
* @see <a
* href="http://www.libvirt.org/html/libvirt-libvirt.html#virNetworkIsPersistent">Libvirt
* Documentation</a>
* @return 1 if persistent, 0 if transient, -1 on error
* @throws LibvirtException
*/
public int isPersistent() throws LibvirtException {
int returnValue = libvirt.virNetworkIsPersistent(VNP);
ErrorHandler.processError(libvirt, returnValue);
return returnValue;
}
/**
* Configures this network to be automatically started when the host machine
* boots.
*
* @param autostart
* whether the network should be automatically started 0 or 1
* @throws LibvirtException
*/
public void setAutostart(boolean autostart) throws LibvirtException {
int autoValue = autostart ? 1 : 0;
int result = libvirt.virNetworkSetAutostart(VNP, autoValue);
ErrorHandler.processError(libvirt, result);
}
/**
* Undefines this network but does not stop it if it is running
*
* @throws LibvirtException
*/
public void undefine() throws LibvirtException {
int result = libvirt.virNetworkUndefine(VNP);
ErrorHandler.processError(libvirt, result);
}
}