Search in sources :

Example 1 with Coordinates

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;
}
Also used : NodeInfo(org.opennms.features.geolocation.api.NodeInfo) Coordinates(org.opennms.features.geolocation.api.Coordinates) GeolocationInfo(org.opennms.features.geolocation.api.GeolocationInfo) OnmsGeolocation(org.opennms.netmgt.model.OnmsGeolocation)

Example 2 with Coordinates

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);
}
Also used : OnmsNode(org.opennms.netmgt.model.OnmsNode) Coordinates(org.opennms.features.geolocation.api.Coordinates) GeolocationResolver(org.opennms.features.geolocation.api.GeolocationResolver) OnmsGeolocation(org.opennms.netmgt.model.OnmsGeolocation) Test(org.junit.Test)

Example 3 with Coordinates

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;
}
Also used : HashMap(java.util.HashMap) Coordinates(org.opennms.features.geolocation.api.Coordinates)

Example 4 with Coordinates

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);
}
Also used : CriteriaBuilder(org.opennms.core.criteria.CriteriaBuilder) GeocoderService(org.opennms.features.geocoder.GeocoderService) NodeDao(org.opennms.netmgt.dao.api.NodeDao) Logger(org.slf4j.Logger) Collection(java.util.Collection) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) Criteria(org.opennms.core.criteria.Criteria) GeocoderException(org.opennms.features.geocoder.GeocoderException) Coordinates(org.opennms.features.geolocation.api.Coordinates) Objects(java.util.Objects) OnmsGeolocation(org.opennms.netmgt.model.OnmsGeolocation) Strings(com.google.common.base.Strings) Map(java.util.Map) CriteriaBuilder(org.opennms.core.criteria.CriteriaBuilder) GeolocationResolver(org.opennms.features.geolocation.api.GeolocationResolver) OnmsNode(org.opennms.netmgt.model.OnmsNode) HashMap(java.util.HashMap) Criteria(org.opennms.core.criteria.Criteria)

Example 5 with Coordinates

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());
        }
    }
}
Also used : Coordinates(org.opennms.features.geolocation.api.Coordinates) OnmsGeolocation(org.opennms.netmgt.model.OnmsGeolocation)

Aggregations

Coordinates (org.opennms.features.geolocation.api.Coordinates)5 OnmsGeolocation (org.opennms.netmgt.model.OnmsGeolocation)4 HashMap (java.util.HashMap)2 GeolocationResolver (org.opennms.features.geolocation.api.GeolocationResolver)2 OnmsNode (org.opennms.netmgt.model.OnmsNode)2 Strings (com.google.common.base.Strings)1 Collection (java.util.Collection)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Collectors (java.util.stream.Collectors)1 Test (org.junit.Test)1 Criteria (org.opennms.core.criteria.Criteria)1 CriteriaBuilder (org.opennms.core.criteria.CriteriaBuilder)1 GeocoderException (org.opennms.features.geocoder.GeocoderException)1 GeocoderService (org.opennms.features.geocoder.GeocoderService)1 GeolocationInfo (org.opennms.features.geolocation.api.GeolocationInfo)1 NodeInfo (org.opennms.features.geolocation.api.NodeInfo)1 NodeDao (org.opennms.netmgt.dao.api.NodeDao)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1