9
9
import org .netxms .client .NXCSession ;
10
10
import org .netxms .client .ObjectFilter ;
11
11
import org .netxms .client .objects .AbstractObject ;
12
+ import org .netxms .client .objects .Node ;
12
13
13
14
import java .io .IOException ;
14
15
import java .util .List ;
15
16
16
17
@ NoArgsConstructor (access = AccessLevel .PRIVATE )
17
18
public final class NetxmsConnector {
18
19
@ Getter
19
- private final static NetxmsConnector instance = new NetxmsConnector ();
20
+ private static final NetxmsConnector instance = new NetxmsConnector ();
21
+
22
+ private static final int INFRASTRUCTURE_SERVICES_ID = 2 ;
20
23
21
24
private NXCSession nxcSession ;
22
25
@@ -27,8 +30,10 @@ public void connect(String address, String port, String username, String passwor
27
30
nxcSession .syncObjects ();
28
31
}
29
32
30
- public void addNodes (List <CsvNode > nodeList ) throws IOException , NXCException {
33
+ public void addNodes (List <CsvNode > nodeList , boolean createContainers ) throws IOException , NXCException {
31
34
ObjectFilter filter ;
35
+ NXCObjectCreationData objectCreationData ;
36
+ long nodeParentId ;
32
37
33
38
for (final CsvNode node : nodeList ) {
34
39
System .out .print ("." );
@@ -42,9 +47,10 @@ public boolean filter(AbstractObject object) {
42
47
}
43
48
};
44
49
50
+ AbstractObject object = nxcSession .findObject (filter );
45
51
if (object != null ) {
46
52
System .out .println ();
47
- System .out .println ("Warning - object with name '" + node .getName () + "' already exists, not creating the node." );
53
+ System .out .println ("Warning - object with name '" + node .getName () + "' or address '" + node . getAddress () + "' already exists, not creating the node." );
48
54
continue ;
49
55
}
50
56
@@ -59,13 +65,24 @@ public boolean filter(AbstractObject object) {
59
65
// find node's container
60
66
AbstractObject container = nxcSession .findObject (filter );
61
67
if (container == null ) {
62
- System .out .println ();
63
- System .out .println ("Warning - container '" + node .getContainer () + "' not found for node '" + node .getName () + "', not creating node." );
64
- continue ;
68
+ // if not found, behaviour determined by "import.create.containers" config property
69
+ if (!createContainers ) {
70
+ // log warning
71
+ System .out .println ();
72
+ System .out .println ("Warning - container '" + node .getContainer () + "' not found for node '" + node .getName () + "', not creating node." );
73
+ continue ;
74
+ } else {
75
+ // create container
76
+ objectCreationData = new NXCObjectCreationData (AbstractObject .OBJECT_CONTAINER , node .getContainer (), INFRASTRUCTURE_SERVICES_ID );
77
+ nodeParentId = nxcSession .createObject (objectCreationData );
78
+ }
79
+ } else {
80
+ // if found, use it as node's parent
81
+ nodeParentId = container .getObjectId ();
65
82
}
66
83
67
84
// object creation data
68
- NXCObjectCreationData objectCreationData = new NXCObjectCreationData (AbstractObject .OBJECT_NODE , node .getName (), container . getObjectId () );
85
+ objectCreationData = new NXCObjectCreationData (AbstractObject .OBJECT_NODE , node .getName (), nodeParentId );
69
86
objectCreationData .setPrimaryName (node .getAddress ());
70
87
71
88
// create the object
0 commit comments