Last modified by Frank Fock on 2024/05/25 12:16

From version 1.1
edited by Frank Fock
on 2024/05/25 12:11
Change comment: There is no comment for this version
To version 2.1
edited by Frank Fock
on 2024/05/25 12:16
Change comment: Uploaded new attachment "Snmp4JAgentTutorialFileTreeBUModel.java", version 1.1

Summary

Details

Snmp4JAgentTutorialFileTreeBUModel.java
Author
... ... @@ -1,0 +1,1 @@
1 +XWiki.FRANKFOCK
Size
... ... @@ -1,0 +1,1 @@
1 +6.4 KB
Content
... ... @@ -1,0 +1,195 @@
1 +package org.snmp4j.agent.tutorial.impl;
2 +
3 +import org.snmp4j.agent.mo.*;
4 +import org.snmp4j.agent.tutorial.Snmp4jAgentTutorialMib;
5 +import org.snmp4j.smi.OID;
6 +import org.snmp4j.smi.OctetString;
7 +import org.snmp4j.smi.Variable;
8 +
9 +import java.io.File;
10 +import java.io.IOException;
11 +import java.nio.file.*;
12 +import java.nio.file.attribute.BasicFileAttributes;
13 +import java.util.*;
14 +
15 +/**
16 + * The {@link Snmp4JAgentTutorialFileTreeBUModel} implements a virtual table instrumentation
17 + * using the SNMP4J-AgentX {@link BufferedMOTableModel}.
18 + *
19 + * @author Frank Fock
20 + */
21 +public class Snmp4JAgentTutorialFileTreeBUModel
22 + extends BufferedMOMutableTableModel<Snmp4jAgentTutorialMib.Snmp4jAgentTutorialFileTreeBUEntryRow> {
23 +
24 + private MOScalar<OctetString> rootPathScalar;
25 +
26 + public Snmp4JAgentTutorialFileTreeBUModel(OID tableOID, MOTableIndex indexDef, MOColumn[] columns) {
27 + super(null);
28 + maxBufferSize = 1000;
29 + this.rootPathScalar = rootPathScalar;
30 + }
31 +
32 + public void setRootPathScalar(MOScalar<OctetString> rootPathScalar) {
33 + this.rootPathScalar = rootPathScalar;
34 + }
35 +
36 + protected File getRootDir() {
37 + if (rootPathScalar != null) {
38 + return new File(rootPathScalar.getValue().toString());
39 + }
40 + return new File(".");
41 + }
42 +
43 + @Override
44 + public int getRowCount() {
45 + Path root = getRootDir().toPath();
46 + try {
47 + long count = Files.walk(root).count();
48 + return (int)count;
49 + }
50 + catch (IOException iox) {
51 + iox.printStackTrace();
52 + }
53 + return 1;
54 + }
55 +
56 + @Override
57 + public boolean isEmpty() {
58 + return false;
59 + }
60 +
61 + @Override
62 + public boolean containsRow(OID oid) {
63 + return (fetchRow(oid) != null);
64 + }
65 +
66 + @Override
67 + public OID lastIndex() {
68 + return getLastFileIndexInDepthFirstOrder(getRootDir(), new OID("1"));
69 + }
70 +
71 + @Override
72 + public OID firstIndex() {
73 + return new OID("1");
74 + }
75 +
76 + @Override
77 + protected Variable[] fetchRow(OID oid) {
78 + File f = getFileForOID(getRootDir(), oid);
79 + if (f == null) {
80 + return null;
81 + }
82 + try {
83 + return Snmp4JAgentTutorialFileTreeRowHelper.createRowFromFile(f);
84 + } catch (IOException e) {
85 + e.printStackTrace();
86 + return null;
87 + }
88 + }
89 +
90 + @Override
91 + protected List<Snmp4jAgentTutorialMib.Snmp4jAgentTutorialFileTreeBUEntryRow> fetchNextRows(OID oid, int chunkSize) {
92 + File startFile = getRootDir();
93 + try {
94 + final OID index = new OID("1");
95 + final List<Snmp4jAgentTutorialMib.Snmp4jAgentTutorialFileTreeBUEntryRow> rows = new ArrayList<>(chunkSize);
96 + SimpleFileVisitor<Path> visitor = new SimpleFileVisitor<Path>() {
97 + @Override
98 + public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
99 + index.append(1);
100 + if (index.compareTo(oid) > 0) {
101 + rows.add(createRow(new OID(index), Snmp4JAgentTutorialFileTreeRowHelper.createRowFromPath(dir)));
102 + }
103 + if (rows.size() >= chunkSize) {
104 + return FileVisitResult.TERMINATE;
105 + }
106 + return super.preVisitDirectory(dir, attrs);
107 + }
108 +
109 + @Override
110 + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
111 + index.set(index.size()-1, index.last()+1);
112 + if (index.compareTo(oid) > 0) {
113 + rows.add(createRow(new OID(index), Snmp4JAgentTutorialFileTreeRowHelper.createRowFromPath(file)));
114 + }
115 + if (rows.size() >= chunkSize) {
116 + return FileVisitResult.TERMINATE;
117 + }
118 + return super.visitFile(file, attrs);
119 + }
120 +
121 + @Override
122 + public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
123 + index.trim(1);
124 + return super.postVisitDirectory(dir, exc);
125 + }
126 + };
127 + Files.walkFileTree(startFile.toPath(), visitor);
128 + return rows;
129 + } catch (IOException e) {
130 + e.printStackTrace();
131 + }
132 + return null;
133 + }
134 +
135 + private OID getLastFileIndexInDepthFirstOrder(File rootDir, OID rootOID) {
136 + if (rootDir.isDirectory()) {
137 + File[] files = rootDir.listFiles();
138 + if (files != null && files.length > 0) {
139 + return getLastFileIndexInDepthFirstOrder(files[files.length - 1], new OID(rootOID.getValue(), files.length - 1));
140 + }
141 + }
142 + return rootOID;
143 + }
144 +
145 + private File getFileForOID(File rootDir, OID fileIndex) {
146 + if (fileIndex.size() == 0 || fileIndex.equals(firstIndex()) ||
147 + (fileIndex.size() > 0 && fileIndex.get(1) == 0)) {
148 + return rootDir;
149 + }
150 + OID remainingIndex = new OID(fileIndex.getValue(), 1, fileIndex.size()-1);
151 + if (remainingIndex.size() > 0 && rootDir.isDirectory()) {
152 + // TODO: 20.06.2016 Use Files.walk to read only those files until index (might be faster)?
153 + File[] files = rootDir.listFiles();
154 + if (files != null && files.length > remainingIndex.get(0)) {
155 + return getFileForOID(files[remainingIndex.get(0)-1], remainingIndex);
156 + }
157 + }
158 + return rootDir;
159 + }
160 +
161 + @Override
162 + protected void deleteAllRows() {
163 +
164 + }
165 +
166 + @Override
167 + protected void bulkDeleteRows(List<OID> indexList) {
168 +
169 + }
170 +
171 + @Override
172 + protected void insertRow(OID index, Variable[] values) {
173 +
174 + }
175 +
176 + @Override
177 + protected void writeRow(OID index, Variable[] values) {
178 +
179 + }
180 +
181 + @Override
182 + protected void deleteRow(OID index) {
183 +
184 + }
185 +
186 + @Override
187 + public Snmp4jAgentTutorialMib.Snmp4jAgentTutorialFileTreeBUEntryRow createRow(OID oid, Variable[] variables) throws UnsupportedOperationException {
188 + return rowFactory.createRow(oid, variables);
189 + }
190 +
191 + @Override
192 + public void freeRow(Snmp4jAgentTutorialMib.Snmp4jAgentTutorialFileTreeBUEntryRow snmp4jAgentTutorialFileTreeBUEntryRow) {
193 + }
194 +}
195 +