use of org.opennms.features.geolocation.api.Coordinates in project opennms by OpenNMS.
the class DefaultGeolocationService method convert.
private GeolocationInfo convert(OnmsNode node) {
GeolocationInfo geolocationInfo = new GeolocationInfo();
// Coordinates
OnmsGeolocation onmsGeolocation = geoLocation(node);
if (onmsGeolocation != null) {
geolocationInfo.setAddressInfo(toAddressInfo(onmsGeolocation));
if (onmsGeolocation.getLongitude() != null && onmsGeolocation.getLatitude() != null) {
geolocationInfo.setCoordinates(new Coordinates(onmsGeolocation.getLongitude(), onmsGeolocation.getLatitude()));
}
}
// NodeInfo
NodeInfo nodeInfo = new NodeInfo();
nodeInfo.setNodeId(node.getId());
nodeInfo.setNodeLabel(node.getLabel());
nodeInfo.setNodeLabel(node.getLabel());
nodeInfo.setForeignSource(node.getForeignSource());
nodeInfo.setForeignId(node.getForeignId());
nodeInfo.setLocation(node.getLocation().getLocationName());
if (node.getAssetRecord() != null) {
nodeInfo.setDescription(node.getAssetRecord().getDescription());
nodeInfo.setMaintcontract(node.getAssetRecord().getMaintcontract());
}
if (node.getPrimaryInterface() != null) {
nodeInfo.setIpAddress(InetAddressUtils.str(node.getPrimaryInterface().getIpAddress()));
}
nodeInfo.setCategories(node.getCategories().stream().map(c -> c.getName()).collect(Collectors.toList()));
geolocationInfo.setNodeInfo(nodeInfo);
return geolocationInfo;
}
use of org.opennms.features.geolocation.api.Coordinates in project opennms by OpenNMS.
the class GeolocationProvisioningAdapterIT method canHandleNullGeolocation.
// See NMS-9187
@Test
public void canHandleNullGeolocation() throws Exception {
// Ensure that the geolocation is null
final OnmsNode node = nodeDao.get(databasePopulator.getNode1().getId());
Assert.assertNull(node.getAssetRecord().getGeolocation());
// Mock the geolocation resolution
final Coordinates coordinates = new Coordinates(-3.179090f, 51.481583f);
final GeolocationResolver geolocationResolverMock = Mockito.mock(GeolocationResolver.class);
Mockito.when(geolocationResolverMock.resolve(Mockito.anyString())).thenReturn(coordinates);
// Manually invoke provisioning adapter
final GeolocationProvisioningAdapter geolocationProvisioningAdapter = new GeolocationProvisioningAdapter();
geolocationProvisioningAdapter.setNodeDao(nodeDao);
geolocationProvisioningAdapter.afterPropertiesSet();
geolocationProvisioningAdapter.updateGeolocation(geolocationResolverMock, node);
// Node should not have been updated
Assert.assertNull(node.getAssetRecord().getGeolocation());
// Set a geolocation and resolve coordinates
node.getAssetRecord().setGeolocation(new OnmsGeolocation());
node.getAssetRecord().getGeolocation().setCity("Cardiff");
nodeDao.saveOrUpdate(node);
geolocationProvisioningAdapter.updateGeolocation(geolocationResolverMock, node);
// Node should have been updated
Assert.assertEquals(coordinates.getLongitude(), node.getAssetRecord().getGeolocation().getLongitude(), 0.001);
Assert.assertEquals(coordinates.getLatitude(), node.getAssetRecord().getGeolocation().getLatitude(), 0.001);
}
use of org.opennms.features.geolocation.api.Coordinates in project opennms by OpenNMS.
the class DefaultGeolocationResolver method resolve.
@Override
public Map<Integer, Coordinates> resolve(Map<Integer, String> nodeIdAddressMap) {
if (nodeIdAddressMap == null || nodeIdAddressMap.isEmpty()) {
// nothing to do
return new HashMap<>();
}
// 1st filter out invalid values
nodeIdAddressMap = nodeIdAddressMap.entrySet().stream().filter(e -> !Strings.isNullOrEmpty(e.getValue()) && e.getKey() != null).collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue()));
// 2nd Resolve longitude/latitude coordinates from an address string
final Map<Integer, Coordinates> resultMap = new HashMap<>();
nodeIdAddressMap.entrySet().stream().forEach(entry -> {
final String addressString = entry.getValue();
final Coordinates coordinates = resolve(addressString);
if (coordinates != null) {
resultMap.put(entry.getKey(), coordinates);
}
});
return resultMap;
}
use of org.opennms.features.geolocation.api.Coordinates in project opennms by OpenNMS.
the class DefaultGeolocationResolver method resolve.
@Override
public Map<Integer, Coordinates> resolve(Collection<Integer> nodeIds) {
if (nodeIds == null || nodeIds.isEmpty()) {
// nothing to do
return new HashMap<>();
}
// Lookup all nodes and gather the address string
final Criteria criteria = new CriteriaBuilder(OnmsNode.class).in("id", nodeIds).toCriteria();
final Map<Integer, String> nodeIdAddressMap = nodeDao.findMatching(criteria).stream().filter(n -> getGeoLocation(n) != null).filter(n -> getGeoLocation(n).getLatitude() == null && getGeoLocation(n).getLongitude() == null).filter(n -> !Strings.isNullOrEmpty(getGeoLocation(n).asAddressString())).collect(Collectors.toMap(n -> n.getId(), n -> n.getAssetRecord().getGeolocation().asAddressString()));
return resolve(nodeIdAddressMap);
}
use of org.opennms.features.geolocation.api.Coordinates in project opennms by OpenNMS.
the class GeolocationProvisioningAdapter method updateGeolocation.
protected void updateGeolocation(GeolocationResolver geolocationResolver, OnmsNode node) {
Objects.requireNonNull(geolocationResolver);
Objects.requireNonNull(node);
// Only resolve long/lat if not already set and an address is set
final OnmsGeolocation geolocation = node.getAssetRecord().getGeolocation();
if (geolocation != null && geolocation.getLatitude() == null && geolocation.getLatitude() == null && !Strings.isNullOrEmpty(geolocation.asAddressString())) {
final Coordinates coordinates = geolocationResolver.resolve(geolocation.asAddressString());
if (coordinates != null) {
geolocation.setLongitude(coordinates.getLongitude());
geolocation.setLatitude(coordinates.getLatitude());
nodeDao.saveOrUpdate(node);
} else {
LOG.warn("Could not resolve address string '{}' for node with id {}", geolocation.asAddressString(), node.getId());
}
}
}
Aggregations