Skip to content

Commit de13203

Browse files
committed
Application implemented
1 parent 136cbb6 commit de13203

File tree

9 files changed

+316
-0
lines changed

9 files changed

+316
-0
lines changed

examples/config.properties

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# .csv file properties
2+
csv.file.name=nodes.csv
3+
csv.separator.regex=,
4+
5+
# NetXMS server connection info
6+
netxms.address=netxms.server.local
7+
netxms.port=4701
8+
netxms.username=admin
9+
netxms.password=netxms

examples/nodes.csv

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Node name, Address, Container to bind to
2+
Example server 1, 1.1.1.1, Servers
3+
Example server 2, 2.2.2.2, Other container

pom.xml

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.github.tomaskir</groupId>
8+
<artifactId>netxms-csv-importer</artifactId>
9+
<version>1.0.0</version>
10+
<packaging>jar</packaging>
11+
12+
<properties>
13+
<source.encoding>utf-8</source.encoding>
14+
<java.version>1.7</java.version>
15+
<lombok.version>1.16.8</lombok.version>
16+
<netxms-client.version>2.1-SNAPSHOT</netxms-client.version>
17+
<simple-xml.version>2.7.1</simple-xml.version> <!--remove this when NetXMS maven repo updates-->
18+
</properties>
19+
20+
<dependencies>
21+
<dependency>
22+
<groupId>org.projectlombok</groupId>
23+
<artifactId>lombok</artifactId>
24+
<version>${lombok.version}</version>
25+
</dependency>
26+
27+
<dependency>
28+
<groupId>org.netxms</groupId>
29+
<artifactId>netxms-client</artifactId>
30+
<version>${netxms-client.version}</version>
31+
</dependency>
32+
33+
<!--remove this when NetXMS maven repo updates-->
34+
<dependency>
35+
<groupId>org.netxms</groupId>
36+
<artifactId>netxms-base</artifactId>
37+
<version>${netxms-client.version}</version>
38+
</dependency>
39+
40+
<!--remove this when NetXMS maven repo updates-->
41+
<dependency>
42+
<groupId>org.simpleframework</groupId>
43+
<artifactId>simple-xml</artifactId>
44+
<version>${simple-xml.version}</version>
45+
</dependency>
46+
</dependencies>
47+
48+
<repositories>
49+
<repository>
50+
<id>data-local</id>
51+
<name>data</name>
52+
<url>file://${project.basedir}/libs</url>
53+
</repository>
54+
</repositories>
55+
56+
<build>
57+
<plugins>
58+
<plugin>
59+
<groupId>org.apache.maven.plugins</groupId>
60+
<artifactId>maven-compiler-plugin</artifactId>
61+
<version>3.5</version>
62+
<configuration>
63+
<encoding>${source.encoding}</encoding>
64+
<source>${java.version}</source>
65+
<target>${java.version}</target>
66+
</configuration>
67+
</plugin>
68+
69+
<plugin>
70+
<artifactId>maven-assembly-plugin</artifactId>
71+
<version>2.6</version>
72+
<configuration>
73+
<descriptorRefs>
74+
<descriptorRef>jar-with-dependencies</descriptorRef>
75+
</descriptorRefs>
76+
<archive>
77+
<manifest>
78+
<mainClass>com.github.tomaskir.netxms.csvimporter.Application</mainClass>
79+
</manifest>
80+
</archive>
81+
</configuration>
82+
<executions>
83+
<execution>
84+
<id>make-assembly</id>
85+
<phase>package</phase>
86+
<goals>
87+
<goal>single</goal>
88+
</goals>
89+
</execution>
90+
</executions>
91+
</plugin>
92+
</plugins>
93+
</build>
94+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.github.tomaskir.netxms.csvimporter;
2+
3+
import com.github.tomaskir.netxms.csvimporter.configuration.ConfigurationLoader;
4+
import com.github.tomaskir.netxms.csvimporter.csv.CsvNode;
5+
import com.github.tomaskir.netxms.csvimporter.csv.CsvParser;
6+
import com.github.tomaskir.netxms.csvimporter.netxms.NetxmsConnector;
7+
8+
import java.util.List;
9+
import java.util.Properties;
10+
11+
public final class Application {
12+
private static final String PROPERTIES_FILE = "config.properties";
13+
private static final String CSV_FILE_NAME_PROPERTY = "csv.file.name";
14+
private static final String CSV_SEPARATOR_REGEX_PROPERTY = "csv.separator.regex";
15+
private static final String NXMS_ADDRESS_PROPERTY = "netxms.address";
16+
private static final String NXMS_PORT_PROPERTY = "netxms.port";
17+
private static final String NXMS_USERNAME_PROPERTY = "netxms.username";
18+
private static final String NXMS_PASSWORD_PROPERTY = "netxms.password";
19+
20+
public static void main(String[] args) {
21+
Properties properties;
22+
List<CsvNode> nodeList;
23+
24+
System.out.println("Application starting.");
25+
26+
try {
27+
System.out.println("Loading configuration properties file.");
28+
properties = ConfigurationLoader.getInstance().load(PROPERTIES_FILE);
29+
30+
System.out.println("Loading nodes from the .csv file.");
31+
nodeList = CsvParser.getInstance().parseCsv(properties.getProperty(CSV_FILE_NAME_PROPERTY), properties.getProperty(CSV_SEPARATOR_REGEX_PROPERTY));
32+
33+
System.out.println("Connecting to the NetXMS server.");
34+
NetxmsConnector.getInstance().connect(properties.getProperty(NXMS_ADDRESS_PROPERTY), properties.getProperty(NXMS_PORT_PROPERTY),
35+
properties.getProperty(NXMS_USERNAME_PROPERTY), properties.getProperty(NXMS_PASSWORD_PROPERTY));
36+
37+
System.out.println("Creating nodes on the NetXMS server.");
38+
NetxmsConnector.getInstance().addNodes(nodeList);
39+
} catch (Exception e) {
40+
System.out.println("Error - '" + e.getClass().getSimpleName() + "'!");
41+
System.out.println("Error message - '" + e.getMessage() + "'.");
42+
return;
43+
}
44+
45+
NetxmsConnector.getInstance().disconnect();
46+
System.out.println("Application finished successfully.");
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.github.tomaskir.netxms.csvimporter.configuration;
2+
3+
import lombok.AccessLevel;
4+
import lombok.Getter;
5+
import lombok.NoArgsConstructor;
6+
7+
import java.io.FileReader;
8+
import java.io.IOException;
9+
import java.nio.file.Paths;
10+
import java.util.Properties;
11+
12+
@NoArgsConstructor(access = AccessLevel.PRIVATE)
13+
public final class ConfigurationLoader {
14+
@Getter
15+
private final static ConfigurationLoader instance = new ConfigurationLoader();
16+
17+
public Properties load(String fileName) throws IOException {
18+
Properties properties = new Properties();
19+
FileReader fileReader = new FileReader(Paths.get(fileName).toAbsolutePath().toString());
20+
21+
properties.load(fileReader);
22+
fileReader.close();
23+
24+
return properties;
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.github.tomaskir.netxms.csvimporter.csv;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Getter;
5+
6+
@Getter
7+
@AllArgsConstructor
8+
public final class CsvNode {
9+
private final String name;
10+
private final String address;
11+
private final String container;
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.github.tomaskir.netxms.csvimporter.csv;
2+
3+
import com.github.tomaskir.netxms.csvimporter.exceptions.InvalidCsvDataException;
4+
import lombok.AccessLevel;
5+
import lombok.Getter;
6+
import lombok.NoArgsConstructor;
7+
8+
import java.io.File;
9+
import java.io.FileNotFoundException;
10+
import java.nio.file.Paths;
11+
import java.util.ArrayList;
12+
import java.util.List;
13+
import java.util.Scanner;
14+
15+
@NoArgsConstructor(access = AccessLevel.PRIVATE)
16+
public final class CsvParser {
17+
@Getter
18+
private final static CsvParser instance = new CsvParser();
19+
20+
public List<CsvNode> parseCsv(String fileName, String separatorRegex) throws FileNotFoundException, InvalidCsvDataException {
21+
Scanner fileScanner = new Scanner(new File(Paths.get(fileName).toAbsolutePath().toString()));
22+
List<CsvNode> nodeList = new ArrayList<>();
23+
int processedLine = 0;
24+
25+
// read the data in the .csv file
26+
while (fileScanner.hasNext()) {
27+
processedLine++;
28+
29+
// skip field descriptions in the first line
30+
if (processedLine == 1) {
31+
fileScanner.nextLine();
32+
continue;
33+
}
34+
35+
String line = fileScanner.nextLine();
36+
String[] elements = line.split(separatorRegex);
37+
38+
if (elements.length != 3)
39+
throw new InvalidCsvDataException("Invalid data in .csv file - line " + processedLine + "!");
40+
41+
// cleanup on the data read from the .csv file and validate them
42+
for (int i = 0; i < elements.length; i++) {
43+
elements[i] = elements[i].trim();
44+
45+
if (elements[i].equals(""))
46+
throw new InvalidCsvDataException("One of required node properties is empty - line " + processedLine + "!");
47+
}
48+
49+
// create node object and add it to the node list
50+
nodeList.add(new CsvNode(elements[0], elements[1], elements[2]));
51+
}
52+
53+
fileScanner.close();
54+
return nodeList;
55+
}
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.github.tomaskir.netxms.csvimporter.exceptions;
2+
3+
public final class InvalidCsvDataException extends Exception {
4+
public InvalidCsvDataException(String message) {
5+
super(message);
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.github.tomaskir.netxms.csvimporter.netxms;
2+
3+
import com.github.tomaskir.netxms.csvimporter.csv.CsvNode;
4+
import lombok.AccessLevel;
5+
import lombok.Getter;
6+
import lombok.NoArgsConstructor;
7+
import org.netxms.client.NXCException;
8+
import org.netxms.client.NXCObjectCreationData;
9+
import org.netxms.client.NXCSession;
10+
import org.netxms.client.objects.AbstractObject;
11+
12+
import java.io.IOException;
13+
import java.util.List;
14+
15+
@NoArgsConstructor(access = AccessLevel.PRIVATE)
16+
public final class NetxmsConnector {
17+
@Getter
18+
private final static NetxmsConnector instance = new NetxmsConnector();
19+
20+
private NXCSession nxcSession;
21+
22+
public void connect(String address, String port, String username, String password) throws IOException, NXCException {
23+
nxcSession = new NXCSession(address, Integer.parseInt(port));
24+
nxcSession.connect();
25+
nxcSession.login(username, password);
26+
nxcSession.syncObjects();
27+
}
28+
29+
public void addNodes(List<CsvNode> nodeList) throws IOException, NXCException {
30+
for (CsvNode node : nodeList) {
31+
System.out.print(".");
32+
33+
AbstractObject object = nxcSession.findObjectByName(node.getName());
34+
if (object != null) {
35+
System.out.println();
36+
System.out.println("Warning - object with name '" + node.getName() + "' already exists, not creating the node.");
37+
continue;
38+
}
39+
40+
AbstractObject container = nxcSession.findObjectByName(node.getContainer());
41+
if (container == null) {
42+
System.out.println();
43+
System.out.println("Warning - container '" + node.getContainer() + "' not found for node '" + node.getName() + "', not creating node.");
44+
continue;
45+
}
46+
47+
// object creation data
48+
NXCObjectCreationData objectCreationData = new NXCObjectCreationData(AbstractObject.OBJECT_NODE, node.getName(), container.getObjectId());
49+
objectCreationData.setPrimaryName(node.getAddress());
50+
51+
// create the object
52+
nxcSession.createObject(objectCreationData);
53+
}
54+
55+
System.out.println();
56+
}
57+
58+
public void disconnect() {
59+
nxcSession.disconnect();
60+
}
61+
}

0 commit comments

Comments
 (0)