@Test public void testSuccessfulQuery() throws IOException { new Expectations() { { mockOdeProperties.getRsuSrmSlots(); result = 1; capturingSnmpSession.getSnmp(); result = mockSnmp; mockSnmp.send((PDU) any, (UserTarget) any); result = mockResponseEvent; mockResponseEvent.getResponse(); result = mockPDU; mockPDU.getVariableBindings(); result = new Vector<VariableBinding>(); } }; ResponseEntity<String> actualResponse = testTimQueryController.bulkQuery("{\"request\":{},\"tim\":{}}"); assertEquals(HttpStatus.OK, actualResponse.getStatusCode()); assertTrue(actualResponse.getBody().contains("indicies_set")); }
@CrossOrigin @PostMapping(value = "/tim/query") public synchronized ResponseEntity<String> bulkQuery(@RequestBody String jsonString) { if (null == jsonString || jsonString.isEmpty()) { logger.error("Empty request."); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(JsonUtils.jsonKeyValue(ERRSTR, "Empty request.")); } RSU queryTarget = (RSU) JsonUtils.fromJson(jsonString, RSU.class); TimTransmogrifier.updateRsuCreds(queryTarget, odeProperties); SnmpSession snmpSession = null; try { snmpSession = new SnmpSession(queryTarget); snmpSession.startListen(); } catch (IOException e) { logger.error("Error creating SNMP session.", e); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) .body(JsonUtils.jsonKeyValue(ERRSTR, "Failed to create SNMP session.")); } PDU pdu0 = new ScopedPDU(); pdu0.setType(PDU.GET); PDU pdu1 = new ScopedPDU(); pdu1.setType(PDU.GET); for (int i = 0; i < odeProperties.getRsuSrmSlots() - 50; i++) { pdu0.add(new VariableBinding(new OID("1.0.15628.4.1.4.1.11.".concat(Integer.toString(i))))); } for (int i = 50; i < odeProperties.getRsuSrmSlots(); i++) { pdu1.add(new VariableBinding(new OID("1.0.15628.4.1.4.1.11.".concat(Integer.toString(i))))); } ResponseEvent response0 = null; ResponseEvent response1 = null; try { response0 = snmpSession.getSnmp().send(pdu0, snmpSession.getTarget()); response1 = snmpSession.getSnmp().send(pdu1, snmpSession.getTarget()); } catch (IOException e) { logger.error("Error creating SNMP session.", e); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) .body(JsonUtils.jsonKeyValue(ERRSTR, "Failed to create SNMP session.")); } if (response0 == null || response0.getResponse() == null || response1 == null || response1.getResponse() == null) { logger.error("RSU query failed, timeout."); return ResponseEntity.status(HttpStatus.BAD_REQUEST) .body(JsonUtils.jsonKeyValue(ERRSTR, "Timeout, no response from RSU.")); } HashMap<String, Boolean> resultsMap = new HashMap<>(); for (Object vbo : response0.getResponse().getVariableBindings().toArray()) { VariableBinding vb = (VariableBinding) vbo; if (vb.getVariable().toInt() == 1) { resultsMap.put(vb.getOid().toString().substring(21), true); } } for (Object vbo : response1.getResponse().getVariableBindings().toArray()) { VariableBinding vb = (VariableBinding) vbo; if (vb.getVariable().toInt() == 1) { resultsMap.put(vb.getOid().toString().substring(21), true); } } try { snmpSession.endSession(); } catch (IOException e) { logger.error("Error closing SNMP session.", e); } logger.info("RSU query successful: {}", resultsMap.keySet()); return ResponseEntity.status(HttpStatus.OK).body(JsonUtils.jsonKeyValue("indicies_set", resultsMap.keySet().toString())); }
TimQueryController { @CrossOrigin @PostMapping(value = "/tim/query") public synchronized ResponseEntity<String> bulkQuery(@RequestBody String jsonString) { if (null == jsonString || jsonString.isEmpty()) { logger.error("Empty request."); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(JsonUtils.jsonKeyValue(ERRSTR, "Empty request.")); } RSU queryTarget = (RSU) JsonUtils.fromJson(jsonString, RSU.class); TimTransmogrifier.updateRsuCreds(queryTarget, odeProperties); SnmpSession snmpSession = null; try { snmpSession = new SnmpSession(queryTarget); snmpSession.startListen(); } catch (IOException e) { logger.error("Error creating SNMP session.", e); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) .body(JsonUtils.jsonKeyValue(ERRSTR, "Failed to create SNMP session.")); } PDU pdu0 = new ScopedPDU(); pdu0.setType(PDU.GET); PDU pdu1 = new ScopedPDU(); pdu1.setType(PDU.GET); for (int i = 0; i < odeProperties.getRsuSrmSlots() - 50; i++) { pdu0.add(new VariableBinding(new OID("1.0.15628.4.1.4.1.11.".concat(Integer.toString(i))))); } for (int i = 50; i < odeProperties.getRsuSrmSlots(); i++) { pdu1.add(new VariableBinding(new OID("1.0.15628.4.1.4.1.11.".concat(Integer.toString(i))))); } ResponseEvent response0 = null; ResponseEvent response1 = null; try { response0 = snmpSession.getSnmp().send(pdu0, snmpSession.getTarget()); response1 = snmpSession.getSnmp().send(pdu1, snmpSession.getTarget()); } catch (IOException e) { logger.error("Error creating SNMP session.", e); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) .body(JsonUtils.jsonKeyValue(ERRSTR, "Failed to create SNMP session.")); } if (response0 == null || response0.getResponse() == null || response1 == null || response1.getResponse() == null) { logger.error("RSU query failed, timeout."); return ResponseEntity.status(HttpStatus.BAD_REQUEST) .body(JsonUtils.jsonKeyValue(ERRSTR, "Timeout, no response from RSU.")); } HashMap<String, Boolean> resultsMap = new HashMap<>(); for (Object vbo : response0.getResponse().getVariableBindings().toArray()) { VariableBinding vb = (VariableBinding) vbo; if (vb.getVariable().toInt() == 1) { resultsMap.put(vb.getOid().toString().substring(21), true); } } for (Object vbo : response1.getResponse().getVariableBindings().toArray()) { VariableBinding vb = (VariableBinding) vbo; if (vb.getVariable().toInt() == 1) { resultsMap.put(vb.getOid().toString().substring(21), true); } } try { snmpSession.endSession(); } catch (IOException e) { logger.error("Error closing SNMP session.", e); } logger.info("RSU query successful: {}", resultsMap.keySet()); return ResponseEntity.status(HttpStatus.OK).body(JsonUtils.jsonKeyValue("indicies_set", resultsMap.keySet().toString())); } }
TimQueryController { @CrossOrigin @PostMapping(value = "/tim/query") public synchronized ResponseEntity<String> bulkQuery(@RequestBody String jsonString) { if (null == jsonString || jsonString.isEmpty()) { logger.error("Empty request."); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(JsonUtils.jsonKeyValue(ERRSTR, "Empty request.")); } RSU queryTarget = (RSU) JsonUtils.fromJson(jsonString, RSU.class); TimTransmogrifier.updateRsuCreds(queryTarget, odeProperties); SnmpSession snmpSession = null; try { snmpSession = new SnmpSession(queryTarget); snmpSession.startListen(); } catch (IOException e) { logger.error("Error creating SNMP session.", e); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) .body(JsonUtils.jsonKeyValue(ERRSTR, "Failed to create SNMP session.")); } PDU pdu0 = new ScopedPDU(); pdu0.setType(PDU.GET); PDU pdu1 = new ScopedPDU(); pdu1.setType(PDU.GET); for (int i = 0; i < odeProperties.getRsuSrmSlots() - 50; i++) { pdu0.add(new VariableBinding(new OID("1.0.15628.4.1.4.1.11.".concat(Integer.toString(i))))); } for (int i = 50; i < odeProperties.getRsuSrmSlots(); i++) { pdu1.add(new VariableBinding(new OID("1.0.15628.4.1.4.1.11.".concat(Integer.toString(i))))); } ResponseEvent response0 = null; ResponseEvent response1 = null; try { response0 = snmpSession.getSnmp().send(pdu0, snmpSession.getTarget()); response1 = snmpSession.getSnmp().send(pdu1, snmpSession.getTarget()); } catch (IOException e) { logger.error("Error creating SNMP session.", e); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) .body(JsonUtils.jsonKeyValue(ERRSTR, "Failed to create SNMP session.")); } if (response0 == null || response0.getResponse() == null || response1 == null || response1.getResponse() == null) { logger.error("RSU query failed, timeout."); return ResponseEntity.status(HttpStatus.BAD_REQUEST) .body(JsonUtils.jsonKeyValue(ERRSTR, "Timeout, no response from RSU.")); } HashMap<String, Boolean> resultsMap = new HashMap<>(); for (Object vbo : response0.getResponse().getVariableBindings().toArray()) { VariableBinding vb = (VariableBinding) vbo; if (vb.getVariable().toInt() == 1) { resultsMap.put(vb.getOid().toString().substring(21), true); } } for (Object vbo : response1.getResponse().getVariableBindings().toArray()) { VariableBinding vb = (VariableBinding) vbo; if (vb.getVariable().toInt() == 1) { resultsMap.put(vb.getOid().toString().substring(21), true); } } try { snmpSession.endSession(); } catch (IOException e) { logger.error("Error closing SNMP session.", e); } logger.info("RSU query successful: {}", resultsMap.keySet()); return ResponseEntity.status(HttpStatus.OK).body(JsonUtils.jsonKeyValue("indicies_set", resultsMap.keySet().toString())); } @Autowired  TimQueryController(OdeProperties odeProperties); }
TimQueryController { @CrossOrigin @PostMapping(value = "/tim/query") public synchronized ResponseEntity<String> bulkQuery(@RequestBody String jsonString) { if (null == jsonString || jsonString.isEmpty()) { logger.error("Empty request."); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(JsonUtils.jsonKeyValue(ERRSTR, "Empty request.")); } RSU queryTarget = (RSU) JsonUtils.fromJson(jsonString, RSU.class); TimTransmogrifier.updateRsuCreds(queryTarget, odeProperties); SnmpSession snmpSession = null; try { snmpSession = new SnmpSession(queryTarget); snmpSession.startListen(); } catch (IOException e) { logger.error("Error creating SNMP session.", e); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) .body(JsonUtils.jsonKeyValue(ERRSTR, "Failed to create SNMP session.")); } PDU pdu0 = new ScopedPDU(); pdu0.setType(PDU.GET); PDU pdu1 = new ScopedPDU(); pdu1.setType(PDU.GET); for (int i = 0; i < odeProperties.getRsuSrmSlots() - 50; i++) { pdu0.add(new VariableBinding(new OID("1.0.15628.4.1.4.1.11.".concat(Integer.toString(i))))); } for (int i = 50; i < odeProperties.getRsuSrmSlots(); i++) { pdu1.add(new VariableBinding(new OID("1.0.15628.4.1.4.1.11.".concat(Integer.toString(i))))); } ResponseEvent response0 = null; ResponseEvent response1 = null; try { response0 = snmpSession.getSnmp().send(pdu0, snmpSession.getTarget()); response1 = snmpSession.getSnmp().send(pdu1, snmpSession.getTarget()); } catch (IOException e) { logger.error("Error creating SNMP session.", e); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) .body(JsonUtils.jsonKeyValue(ERRSTR, "Failed to create SNMP session.")); } if (response0 == null || response0.getResponse() == null || response1 == null || response1.getResponse() == null) { logger.error("RSU query failed, timeout."); return ResponseEntity.status(HttpStatus.BAD_REQUEST) .body(JsonUtils.jsonKeyValue(ERRSTR, "Timeout, no response from RSU.")); } HashMap<String, Boolean> resultsMap = new HashMap<>(); for (Object vbo : response0.getResponse().getVariableBindings().toArray()) { VariableBinding vb = (VariableBinding) vbo; if (vb.getVariable().toInt() == 1) { resultsMap.put(vb.getOid().toString().substring(21), true); } } for (Object vbo : response1.getResponse().getVariableBindings().toArray()) { VariableBinding vb = (VariableBinding) vbo; if (vb.getVariable().toInt() == 1) { resultsMap.put(vb.getOid().toString().substring(21), true); } } try { snmpSession.endSession(); } catch (IOException e) { logger.error("Error closing SNMP session.", e); } logger.info("RSU query successful: {}", resultsMap.keySet()); return ResponseEntity.status(HttpStatus.OK).body(JsonUtils.jsonKeyValue("indicies_set", resultsMap.keySet().toString())); } @Autowired  TimQueryController(OdeProperties odeProperties); @CrossOrigin @PostMapping(value = "/tim/query") synchronized ResponseEntity<String> bulkQuery(@RequestBody String jsonString); }
TimQueryController { @CrossOrigin @PostMapping(value = "/tim/query") public synchronized ResponseEntity<String> bulkQuery(@RequestBody String jsonString) { if (null == jsonString || jsonString.isEmpty()) { logger.error("Empty request."); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(JsonUtils.jsonKeyValue(ERRSTR, "Empty request.")); } RSU queryTarget = (RSU) JsonUtils.fromJson(jsonString, RSU.class); TimTransmogrifier.updateRsuCreds(queryTarget, odeProperties); SnmpSession snmpSession = null; try { snmpSession = new SnmpSession(queryTarget); snmpSession.startListen(); } catch (IOException e) { logger.error("Error creating SNMP session.", e); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) .body(JsonUtils.jsonKeyValue(ERRSTR, "Failed to create SNMP session.")); } PDU pdu0 = new ScopedPDU(); pdu0.setType(PDU.GET); PDU pdu1 = new ScopedPDU(); pdu1.setType(PDU.GET); for (int i = 0; i < odeProperties.getRsuSrmSlots() - 50; i++) { pdu0.add(new VariableBinding(new OID("1.0.15628.4.1.4.1.11.".concat(Integer.toString(i))))); } for (int i = 50; i < odeProperties.getRsuSrmSlots(); i++) { pdu1.add(new VariableBinding(new OID("1.0.15628.4.1.4.1.11.".concat(Integer.toString(i))))); } ResponseEvent response0 = null; ResponseEvent response1 = null; try { response0 = snmpSession.getSnmp().send(pdu0, snmpSession.getTarget()); response1 = snmpSession.getSnmp().send(pdu1, snmpSession.getTarget()); } catch (IOException e) { logger.error("Error creating SNMP session.", e); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) .body(JsonUtils.jsonKeyValue(ERRSTR, "Failed to create SNMP session.")); } if (response0 == null || response0.getResponse() == null || response1 == null || response1.getResponse() == null) { logger.error("RSU query failed, timeout."); return ResponseEntity.status(HttpStatus.BAD_REQUEST) .body(JsonUtils.jsonKeyValue(ERRSTR, "Timeout, no response from RSU.")); } HashMap<String, Boolean> resultsMap = new HashMap<>(); for (Object vbo : response0.getResponse().getVariableBindings().toArray()) { VariableBinding vb = (VariableBinding) vbo; if (vb.getVariable().toInt() == 1) { resultsMap.put(vb.getOid().toString().substring(21), true); } } for (Object vbo : response1.getResponse().getVariableBindings().toArray()) { VariableBinding vb = (VariableBinding) vbo; if (vb.getVariable().toInt() == 1) { resultsMap.put(vb.getOid().toString().substring(21), true); } } try { snmpSession.endSession(); } catch (IOException e) { logger.error("Error closing SNMP session.", e); } logger.info("RSU query successful: {}", resultsMap.keySet()); return ResponseEntity.status(HttpStatus.OK).body(JsonUtils.jsonKeyValue("indicies_set", resultsMap.keySet().toString())); } @Autowired  TimQueryController(OdeProperties odeProperties); @CrossOrigin @PostMapping(value = "/tim/query") synchronized ResponseEntity<String> bulkQuery(@RequestBody String jsonString);  }