1 20 21 22 package org.snmp4j.agent.mo.snmp4j; 23 24 27 import java.io.*; 28 29 import org.snmp4j.*; 30 import org.snmp4j.agent.*; 31 import org.snmp4j.agent.io.*; 32 import org.snmp4j.agent.mo.*; 33 import org.snmp4j.agent.mo.snmp.*; 34 import org.snmp4j.agent.request.*; 35 import org.snmp4j.mp.*; 36 import org.snmp4j.smi.*; 37 import org.snmp4j.agent.mo.snmp4j.Snmp4jConfigMib.Snmp4jCfgStorageEntryRow; 38 import org.snmp4j.log.LogFactory; 39 import org.snmp4j.log.LogAdapter; 40 41 44 public class Snmp4jConfigMib 45 implements MOGroup 48 { 51 52 private static final LogAdapter logger = 53 LogFactory.getLogger(Snmp4jConfigMib.class); 54 55 private static MOFactory moFactory = DefaultMOFactory.getInstance(); 57 58 private static final OID oidSnmp4jCfgSecSrcAddrValidation = 60 new OID(new int[] { 1,3,6,1,4,1,4976,10,1,1,2,1,1,1,0 }); 61 62 private MOScalar snmp4jCfgSecSrcAddrValidation; 64 65 private static final OID oidSnmp4jCfgStorageEntry = 67 new OID(new int[] { 1,3,6,1,4,1,4976,10,1,1,2,1,2,1,1 }); 68 69 private static final int colSnmp4jCfgStoragePath = 2; 71 private static final int colSnmp4jCfgStorageFormat = 3; 72 private static final int colSnmp4jCfgStorageLastStore = 4; 73 private static final int colSnmp4jCfgStorageLastRestore = 5; 74 private static final int colSnmp4jCfgStorageRestoreMode = 6; 75 private static final int colSnmp4jCfgStorageOperation = 7; 76 private static final int colSnmp4jCfgStorageStorageType = 8; 77 private static final int colSnmp4jCfgStorageStatus = 9; 78 79 private static final int idxSnmp4jCfgStoragePath = 0; 81 private static final int idxSnmp4jCfgStorageFormat = 1; 82 private static final int idxSnmp4jCfgStorageLastStore = 2; 83 private static final int idxSnmp4jCfgStorageLastRestore = 3; 84 private static final int idxSnmp4jCfgStorageRestoreMode = 4; 85 private static final int idxSnmp4jCfgStorageOperation = 5; 86 private static final int idxSnmp4jCfgStorageStorageType = 6; 87 private static final int idxSnmp4jCfgStorageStatus = 7; 88 private static MOTableSubIndex[] snmp4jCfgStorageEntryIndexes = 89 new MOTableSubIndex[] { 90 moFactory.createSubIndex(SMIConstants.SYNTAX_OCTET_STRING, 0, 255) 91 }; 92 93 private static MOTableIndex snmp4jCfgStorageEntryIndex = 94 moFactory.createIndex(snmp4jCfgStorageEntryIndexes, 95 false); 96 97 private MOTable snmp4jCfgStorageEntry; 98 private MOMutableTableModel snmp4jCfgStorageEntryModel; 99 100 101 public static final OID PRIMARY_INDEX = 103 new OctetString("primary").toSubIndex(false); 104 105 protected SnmpCommunityMIB snmpCommunityMIB; 106 protected MOPersistenceProvider[] persistenceProvider = 107 new MOPersistenceProvider[Snmp4jCfgStorageFormatEnum.xml]; 108 protected File defaultConfigFile; 109 protected SysUpTime sysUpTime; 110 111 private File configPathRoot; 112 113 115 public Snmp4jConfigMib(SysUpTime sysUpTime) { 116 this.sysUpTime = sysUpTime; 117 snmp4jCfgSecSrcAddrValidation = 118 new Snmp4jCfgSecSrcAddrValidation(oidSnmp4jCfgSecSrcAddrValidation, 119 MOAccessImpl.ACCESS_READ_WRITE); 120 createSnmp4jCfgStorageEntry(); 121 configPathRoot = new File(System.getProperty("user.dir", "")); 122 } 123 124 125 public MOTable getSnmp4jCfgStorageEntry() { 126 return snmp4jCfgStorageEntry; 127 } 128 129 public File getDefaultConfigFile() { 130 return defaultConfigFile; 131 } 132 133 private void createSnmp4jCfgStorageEntry() { 134 MOColumn[] snmp4jCfgStorageEntryColumns = new MOColumn[8]; 135 snmp4jCfgStorageEntryColumns[idxSnmp4jCfgStoragePath] = 136 new DisplayString(colSnmp4jCfgStoragePath, 137 MOAccessImpl.ACCESS_READ_CREATE, 138 null, 139 true, 140 0, 255); 141 ((MOMutableColumn)snmp4jCfgStorageEntryColumns[idxSnmp4jCfgStoragePath]). 142 addMOValueValidationListener(new Snmp4jCfgStoragePathValidator()); 143 snmp4jCfgStorageEntryColumns[idxSnmp4jCfgStorageFormat] = 144 new Enumerated(colSnmp4jCfgStorageFormat, 145 MOAccessImpl.ACCESS_READ_CREATE, 146 new Integer32(1), 147 true, 148 new int[] { Snmp4jCfgStorageFormatEnum.binary, 149 Snmp4jCfgStorageFormatEnum.xml }); 150 ((MOMutableColumn)snmp4jCfgStorageEntryColumns[idxSnmp4jCfgStorageFormat]). 151 addMOValueValidationListener(new Snmp4jCfgStorageFormatValidator()); 152 snmp4jCfgStorageEntryColumns[idxSnmp4jCfgStorageLastStore] = 153 new MOColumn(colSnmp4jCfgStorageLastStore, 154 SMIConstants.SYNTAX_TIMETICKS, 155 MOAccessImpl.ACCESS_READ_ONLY); 156 snmp4jCfgStorageEntryColumns[idxSnmp4jCfgStorageLastRestore] = 157 new MOColumn(colSnmp4jCfgStorageLastRestore, 158 SMIConstants.SYNTAX_TIMETICKS, 159 MOAccessImpl.ACCESS_READ_ONLY); 160 snmp4jCfgStorageEntryColumns[idxSnmp4jCfgStorageRestoreMode] = 161 new Enumerated(colSnmp4jCfgStorageRestoreMode, 162 MOAccessImpl.ACCESS_READ_CREATE, 163 new Integer32(1), 164 true, 165 new int[] { Snmp4jCfgStorageRestoreModeEnum.replaceAndCreate, 166 Snmp4jCfgStorageRestoreModeEnum.updateAndCreate, 167 Snmp4jCfgStorageRestoreModeEnum.updateOnly, 168 Snmp4jCfgStorageRestoreModeEnum.createOnly }); 169 snmp4jCfgStorageEntryColumns[idxSnmp4jCfgStorageOperation] = 170 new Enumerated(colSnmp4jCfgStorageOperation, 171 MOAccessImpl.ACCESS_READ_CREATE, 172 new Integer32(1), 173 true, 174 new int[] { Snmp4jCfgStorageOperationEnum.idle, 175 Snmp4jCfgStorageOperationEnum.inProgress, 176 Snmp4jCfgStorageOperationEnum.store, 177 Snmp4jCfgStorageOperationEnum.restore }); 178 ((MOMutableColumn)snmp4jCfgStorageEntryColumns[idxSnmp4jCfgStorageOperation]). 179 addMOValueValidationListener(new Snmp4jCfgStorageOperationValidator()); 180 snmp4jCfgStorageEntryColumns[idxSnmp4jCfgStorageStorageType] = 181 new StorageType(colSnmp4jCfgStorageStorageType, 182 MOAccessImpl.ACCESS_READ_CREATE, 183 new Integer32(3), 184 true); 185 snmp4jCfgStorageEntryColumns[idxSnmp4jCfgStorageStatus] = 186 new RowStatus(colSnmp4jCfgStorageStatus); 187 188 snmp4jCfgStorageEntryModel = new DefaultMOMutableTableModel(); 189 snmp4jCfgStorageEntryModel.setRowFactory(new Snmp4jCfgStorageEntryRowFactory()); 190 snmp4jCfgStorageEntry = 191 moFactory.createTable(oidSnmp4jCfgStorageEntry, 192 snmp4jCfgStorageEntryIndex, 193 snmp4jCfgStorageEntryColumns, 194 snmp4jCfgStorageEntryModel); 195 } 196 197 198 199 public void registerMOs(MOServer server, OctetString context) 200 throws DuplicateRegistrationException 201 { 202 server.register(this.snmp4jCfgSecSrcAddrValidation, context); 204 server.register(this.snmp4jCfgStorageEntry, context); 205 } 208 209 public void unregisterMOs(MOServer server, OctetString context) { 210 server.unregister(this.snmp4jCfgSecSrcAddrValidation, context); 212 server.unregister(this.snmp4jCfgStorageEntry, context); 213 } 216 217 219 public class Snmp4jCfgSecSrcAddrValidation extends EnumeratedScalar { 221 Snmp4jCfgSecSrcAddrValidation(OID oid, MOAccess access) { 222 super(oid, access, new Integer32(), 223 new int[] { Snmp4jCfgSecSrcAddrValidationEnum.enabled, 224 Snmp4jCfgSecSrcAddrValidationEnum.disabled, 225 Snmp4jCfgSecSrcAddrValidationEnum.notAvailable }); 226 } 229 230 public Variable getValue() { 231 if (snmpCommunityMIB != null) { 232 if (snmpCommunityMIB.isSourceAddressFiltering()) { 233 setValue(new Integer32(Snmp4jCfgSecSrcAddrValidationEnum.enabled)); 234 } 235 else { 236 setValue(new Integer32(Snmp4jCfgSecSrcAddrValidationEnum.disabled)); 237 } 238 } 239 else { 240 setValue(new Integer32(Snmp4jCfgSecSrcAddrValidationEnum.notAvailable)); 241 } 242 return (Variable) super.getValue().clone(); 243 } 244 245 public void commit(SubRequest request) { 246 Integer32 newValue = 248 (Integer32) request.getVariableBinding().getVariable(); 249 switch (newValue.getValue()) { 250 case Snmp4jCfgSecSrcAddrValidationEnum.disabled: 251 snmpCommunityMIB.setSourceAddressFiltering(false); 252 break; 253 case Snmp4jCfgSecSrcAddrValidationEnum.enabled: 254 snmpCommunityMIB.setSourceAddressFiltering(true); 255 break; 256 default: 257 request.getRequest().setErrorStatus(PDU.commitFailed); 258 } 259 super.commit(request); 261 } 262 263 public void cleanup(SubRequest request) { 264 super.cleanup(request); 267 } 268 269 public int isValueOK(SubRequest request) { 270 Variable newValue = 271 request.getVariableBinding().getVariable(); 272 if (snmpCommunityMIB != null) { 274 switch (((Integer32)newValue).getValue()) { 275 case Snmp4jCfgSecSrcAddrValidationEnum.disabled: 276 case Snmp4jCfgSecSrcAddrValidationEnum.enabled: 277 break; 278 default: 279 return PDU.wrongValue; 280 } 281 } 282 else if (((Integer32)newValue).getValue() != 283 Snmp4jCfgSecSrcAddrValidationEnum.notAvailable) { 284 return PDU.inconsistentValue; 285 } 286 else { 287 return PDU.wrongValue; 288 } 289 return super.isValueOK(request); 291 } 292 } 293 294 295 297 301 static class Snmp4jCfgStoragePathValidator implements MOValueValidationListener { 302 303 public void validate(MOValueValidationEvent validationEvent) { 304 Variable newValue = validationEvent.getNewValue(); 305 OctetString os = (OctetString)newValue; 306 if (!(((os.length() >= 0) && (os.length() <= 255)))) { 307 validationEvent.setValidationStatus(SnmpConstants.SNMP_ERROR_WRONG_LENGTH); 308 return; 309 } 310 } 313 } 314 318 static class Snmp4jCfgStorageFormatValidator implements MOValueValidationListener { 319 320 public void validate(MOValueValidationEvent validationEvent) { 321 Variable newValue = validationEvent.getNewValue(); 322 if (((Integer32)newValue).getValue() != Snmp4jCfgStorageFormatEnum.binary) { 324 validationEvent.setValidationStatus(PDU.wrongValue); 325 } 326 } 328 } 329 333 static class Snmp4jCfgStorageOperationValidator implements MOValueValidationListener { 334 335 public void validate(MOValueValidationEvent validationEvent) { 336 Variable newValue = validationEvent.getNewValue(); 337 switch (newValue.toInt()) { 339 case Snmp4jCfgStorageOperationEnum.restore: 340 case Snmp4jCfgStorageOperationEnum.store: 341 break; 342 default: 343 validationEvent.setValidationStatus(PDU.wrongValue); 344 } 345 } 347 } 348 349 public static final class Snmp4jCfgSecSrcAddrValidationEnum { 351 public static final int enabled = 1; 352 public static final int disabled = 2; 353 public static final int notAvailable = 3; 354 } 355 356 public static final class Snmp4jCfgStorageFormatEnum { 357 358 public static final int binary = 1; 359 public static final int xml = 2; 360 } 361 public static final class Snmp4jCfgStorageRestoreModeEnum { 362 public static final int replaceAndCreate = 1; 363 public static final int updateAndCreate = 2; 364 public static final int updateOnly = 3; 365 public static final int createOnly = 4; 366 } 367 public static final class Snmp4jCfgStorageOperationEnum { 368 369 public static final int idle = 1; 370 371 public static final int inProgress = 2; 372 373 public static final int store = 3; 374 375 public static final int restore = 4; 376 } 377 378 class Snmp4jCfgStorageEntryRowFactory 380 extends DefaultMOMutableRow2PCFactory 381 { 382 public Snmp4jCfgStorageEntryRowFactory() {} 383 384 public MOTableRow createRow(OID index, Variable[] values) 385 throws UnsupportedOperationException 386 { 387 Snmp4jCfgStorageEntryRow row = new Snmp4jCfgStorageEntryRow(index, values); 388 ((Integer32)values[idxSnmp4jCfgStorageOperation]). 390 setValue(Snmp4jCfgStorageOperationEnum.idle); 391 return row; 393 } 394 395 public void freeRow(MOTableRow row) { 396 } 399 } 400 401 class Snmp4jCfgStorageEntryRow extends DefaultMOMutableRow2PC { 402 403 public Snmp4jCfgStorageEntryRow(OID index, Variable[] values) { 404 super(index, values); 405 } 406 407 public OctetString getSnmp4jCfgStoragePath() { 408 return (OctetString) getValue(idxSnmp4jCfgStoragePath); 409 } 410 411 public void setSnmp4jCfgStoragePath(OctetString newValue) { 412 setValue(idxSnmp4jCfgStoragePath, newValue); 413 } 414 415 public Integer32 getSnmp4jCfgStorageFormat() { 416 return (Integer32) getValue(idxSnmp4jCfgStorageFormat); 417 } 418 419 public void setSnmp4jCfgStorageFormat(Integer32 newValue) { 420 setValue(idxSnmp4jCfgStorageFormat, newValue); 421 } 422 423 public TimeTicks getSnmp4jCfgStorageLastStore() { 424 return (TimeTicks) getValue(idxSnmp4jCfgStorageLastStore); 425 } 426 427 public void setSnmp4jCfgStorageLastStore(TimeTicks newValue) { 428 setValue(idxSnmp4jCfgStorageLastStore, newValue); 429 } 430 431 public TimeTicks getSnmp4jCfgStorageLastRestore() { 432 return (TimeTicks) getValue(idxSnmp4jCfgStorageLastRestore); 433 } 434 435 public void setSnmp4jCfgStorageLastRestore(TimeTicks newValue) { 436 setValue(idxSnmp4jCfgStorageLastRestore, newValue); 437 } 438 439 public Integer32 getSnmp4jCfgStorageRestoreMode() { 440 return (Integer32) getValue(idxSnmp4jCfgStorageRestoreMode); 441 } 442 443 public void setSnmp4jCfgStorageRestoreMode(Integer32 newValue) { 444 setValue(idxSnmp4jCfgStorageRestoreMode, newValue); 445 } 446 447 public Integer32 getSnmp4jCfgStorageOperation() { 448 return (Integer32) getValue(idxSnmp4jCfgStorageOperation); 449 } 450 451 public void setSnmp4jCfgStorageOperation(Integer32 newValue) { 452 setValue(idxSnmp4jCfgStorageOperation, newValue); 453 } 454 455 public Integer32 getSnmp4jCfgStorageStorageType() { 456 return (Integer32) getValue(idxSnmp4jCfgStorageStorageType); 457 } 458 459 public void setSnmp4jCfgStorageStorageType(Integer32 newValue) { 460 setValue(idxSnmp4jCfgStorageStorageType, newValue); 461 } 462 463 public Integer32 getSnmp4jCfgStorageStatus() { 464 return (Integer32) getValue(idxSnmp4jCfgStorageStatus); 465 } 466 467 public void setSnmp4jCfgStorageStatus(Integer32 newValue) { 468 setValue(idxSnmp4jCfgStorageStatus, newValue); 469 } 470 471 473 public void prepareRow(SubRequest subRequest, MOTableRow changeSet) { 474 if (PRIMARY_INDEX.equals(changeSet.getIndex())) { 475 if (snmp4jCfgStorageEntryModel.getRow(PRIMARY_INDEX) == null) { 476 subRequest.getRequest().setErrorStatus(PDU.noCreation); 477 } 478 } 479 } 480 481 public void commitRow(SubRequest subRequest, MOTableRow changeSet) { 482 Integer32 operation = getSnmp4jCfgStorageOperation(); 483 Integer32 format = getSnmp4jCfgStorageFormat(); 484 if (format.getValue() == Snmp4jCfgStorageFormatEnum.binary) { 485 MOPersistenceProvider provider = getPersistenceProvider(format.getValue()); 486 if (provider == null) { 487 subRequest.getRequest().setErrorStatus(PDU.commitFailed); 488 } 489 else { 490 Operation op = 491 new Operation(this, provider, 492 getSnmp4jCfgStorageRestoreMode().getValue(), 493 operation.getValue()); 494 setValue(idxSnmp4jCfgStorageOperation, 495 new Integer32(Snmp4jCfgStorageOperationEnum.inProgress)); 496 op.start(); 497 } 498 } 499 else { 500 subRequest.getRequest().setErrorStatus(PDU.commitFailed); 501 } 502 } 503 504 } 506 507 508 509 511 public void setSnmpCommunityMIB(SnmpCommunityMIB snmpCommunityMIB) { 512 this.snmpCommunityMIB = snmpCommunityMIB; 513 } 514 515 public void setDefaultConfigFile(File defaultConfigFile) { 516 this.defaultConfigFile = defaultConfigFile; 517 518 Snmp4jCfgStorageEntryRow primary = (Snmp4jCfgStorageEntryRow) 519 snmp4jCfgStorageEntryModel.getRow(PRIMARY_INDEX); 520 if (primary == null) { 521 Variable[] vbs = snmp4jCfgStorageEntry.getDefaultValues(); 522 vbs[idxSnmp4jCfgStorageStatus] = new Integer32(RowStatus.active); 523 primary = (Snmp4jCfgStorageEntryRow) 524 snmp4jCfgStorageEntry.createRow(PRIMARY_INDEX, vbs); 525 primary.setSnmp4jCfgStorageStorageType( 526 new Integer32(StorageType.permanent)); 527 primary.setSnmp4jCfgStorageOperation( 528 new Integer32(Snmp4jCfgStorageOperationEnum.idle)); 529 snmp4jCfgStorageEntry.addRow(primary); 530 } 531 primary.setSnmp4jCfgStoragePath(new OctetString(defaultConfigFile.getPath())); 532 } 533 534 public void setPersistenceProvider(int format, 535 MOPersistenceProvider provider) { 536 persistenceProvider[format-1] = provider; 537 } 538 539 public MOPersistenceProvider getPersistenceProvider(int format) { 540 return persistenceProvider[format-1]; 541 } 542 543 public SnmpCommunityMIB getCoexistenceInfoProvider() { 544 return this.snmpCommunityMIB; 545 } 546 547 549 551 private class Operation extends Thread { 552 553 private Snmp4jCfgStorageEntryRow row; 554 private int operation; 555 private int restoreType; 556 private MOPersistenceProvider provider; 557 558 public Operation(Snmp4jCfgStorageEntryRow row, 559 MOPersistenceProvider provider, 560 int restoreType, 561 int operation) { 562 this.operation = operation; 563 this.provider = provider; 564 this.restoreType = restoreType; 565 this.row = row; 566 } 567 568 public void run() { 569 switch (operation) { 570 case Snmp4jCfgStorageOperationEnum.store: { 571 File f = new File(row.getValue(idxSnmp4jCfgStoragePath).toString()); 572 FileOutputStream fos = null; 573 try { 574 fos = new FileOutputStream(f); 575 provider.saveConfig(fos); 576 fos.close(); 577 row.setValue(idxSnmp4jCfgStorageLastStore, sysUpTime.get()); 578 row.setValue(idxSnmp4jCfgStorageOperation, 579 new Integer32(Snmp4jCfgStorageOperationEnum.idle)); 580 } 581 catch (Exception iox) { 582 logger.warn("Failed to store config to '"+f+"': "+iox.getMessage()); 583 row.setValue(idxSnmp4jCfgStorageOperation, 584 new Integer32(Snmp4jCfgStorageOperationEnum.idle)); 585 } 586 finally { 587 if (fos != null) { 588 try { 589 fos.close(); 590 } 591 catch (IOException ex) { 592 logger.warn(ex); 593 } 594 } 595 } 596 break; 597 } 598 case Snmp4jCfgStorageOperationEnum.restore: { 599 File f = new File(row.getValue(idxSnmp4jCfgStoragePath).toString()); 600 if (f.exists() && f.canRead()) { 601 FileInputStream fis = null; 602 try { 603 fis = new FileInputStream(f); 604 provider.loadConfig(fis, restoreType); 605 fis.close(); 606 row.setValue(idxSnmp4jCfgStorageLastRestore, sysUpTime.get()); 607 row.setValue(idxSnmp4jCfgStorageOperation, 608 new Integer32(Snmp4jCfgStorageOperationEnum.idle)); 609 } 610 catch (Exception iox) { 611 logger.warn("Failed to restore config from '" + f + "': "+ 612 iox.getMessage()); 613 row.setValue(idxSnmp4jCfgStorageOperation, 614 new Integer32(Snmp4jCfgStorageOperationEnum.idle)); 615 } 616 finally { 617 if (fis != null) { 618 try { 619 fis.close(); 620 } 621 catch (IOException ex) { 622 logger.warn(ex); 623 } 624 } 625 } 626 } 627 else { 628 logger.warn("File '"+f+"' does not exists or is not readable"); 629 row.setValue(idxSnmp4jCfgStorageOperation, 630 new Integer32(Snmp4jCfgStorageOperationEnum.idle)); 631 } 632 break; 633 } 634 } 635 } 636 } 637 638 640 } 643 644 645 | Popular Tags |