Skip to content

Commit 088cd6b

Browse files
committed
Added possibility to create containers during import if they are not found (controlled by config file)
1 parent 662d177 commit 088cd6b

File tree

4 files changed

+30
-9
lines changed

4 files changed

+30
-9
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ These should be places in the working directory of the application (same directo
1212
More on the configuration files in the next section.
1313

1414
After downloading the .jar and creating the configuration files, you can run the importer using:
15-
> java -jar netxms-csv-importer.1.0.1.jar
15+
> java -jar netxms-csv-importer.1.1.0.jar
1616
1717
## Configuration files
1818

examples/config.properties

+3
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ netxms.address=netxms.server.local
77
netxms.port=4701
88
netxms.username=admin
99
netxms.password=netxms
10+
11+
# import configuration
12+
import.create.containers=false

src/main/java/com/github/tomaskir/netxms/csvimporter/Application.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public final class Application {
1616
private static final String NXMS_PORT_PROPERTY = "netxms.port";
1717
private static final String NXMS_USERNAME_PROPERTY = "netxms.username";
1818
private static final String NXMS_PASSWORD_PROPERTY = "netxms.password";
19+
private static final String IMPORT_CREATE_CONTAINERS_PROPERTY = "import.create.containers";
1920

2021
public static void main(String[] args) {
2122
Properties properties;
@@ -35,7 +36,7 @@ public static void main(String[] args) {
3536
properties.getProperty(NXMS_USERNAME_PROPERTY), properties.getProperty(NXMS_PASSWORD_PROPERTY));
3637

3738
System.out.println("Creating nodes on the NetXMS server.");
38-
NetxmsConnector.getInstance().addNodes(nodeList);
39+
NetxmsConnector.getInstance().addNodes(nodeList, Boolean.parseBoolean(properties.getProperty(IMPORT_CREATE_CONTAINERS_PROPERTY)));
3940
} catch (Exception e) {
4041
System.out.println("Error: " + e.getClass().getSimpleName() + " - " + e.getMessage() + ".");
4142
return;

src/main/java/com/github/tomaskir/netxms/csvimporter/netxms/NetxmsConnector.java

+24-7
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@
99
import org.netxms.client.NXCSession;
1010
import org.netxms.client.ObjectFilter;
1111
import org.netxms.client.objects.AbstractObject;
12+
import org.netxms.client.objects.Node;
1213

1314
import java.io.IOException;
1415
import java.util.List;
1516

1617
@NoArgsConstructor(access = AccessLevel.PRIVATE)
1718
public final class NetxmsConnector {
1819
@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;
2023

2124
private NXCSession nxcSession;
2225

@@ -27,8 +30,10 @@ public void connect(String address, String port, String username, String passwor
2730
nxcSession.syncObjects();
2831
}
2932

30-
public void addNodes(List<CsvNode> nodeList) throws IOException, NXCException {
33+
public void addNodes(List<CsvNode> nodeList, boolean createContainers) throws IOException, NXCException {
3134
ObjectFilter filter;
35+
NXCObjectCreationData objectCreationData;
36+
long nodeParentId;
3237

3338
for (final CsvNode node : nodeList) {
3439
System.out.print(".");
@@ -42,9 +47,10 @@ public boolean filter(AbstractObject object) {
4247
}
4348
};
4449

50+
AbstractObject object = nxcSession.findObject(filter);
4551
if (object != null) {
4652
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.");
4854
continue;
4955
}
5056

@@ -59,13 +65,24 @@ public boolean filter(AbstractObject object) {
5965
// find node's container
6066
AbstractObject container = nxcSession.findObject(filter);
6167
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();
6582
}
6683

6784
// 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);
6986
objectCreationData.setPrimaryName(node.getAddress());
7087

7188
// create the object

0 commit comments

Comments
 (0)