/*******************************************************************************
 * This file is part of OpenNMS(R).
 *
 * Copyright (C) 2017-2017 The OpenNMS Group, Inc.
 * OpenNMS(R) is Copyright (C) 1999-2017 The OpenNMS Group, Inc.
 *
 * OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
 *
 * OpenNMS(R) is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published
 * by the Free Software Foundation, either version 3 of the License,
 * or (at your option) any later version.
 *
 * OpenNMS(R) is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with OpenNMS(R).  If not, see:
 *      http://www.gnu.org/licenses/
 *
 * For more information contact:
 *     OpenNMS(R) Licensing <license@opennms.org>
 *     http://www.opennms.org/
 *     http://www.opennms.com/
 *******************************************************************************/

package org.opennms.netmgt.provision;

import java.util.Objects;

import org.opennms.core.soa.ServiceRegistry;
import org.opennms.core.spring.BeanUtils;
import org.opennms.features.geolocation.api.Coordinates;
import org.opennms.features.geolocation.api.GeolocationResolver;
import org.opennms.netmgt.dao.api.NodeDao;
import org.opennms.netmgt.model.OnmsGeolocation;
import org.opennms.netmgt.model.OnmsNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;

import com.google.common.base.Strings;

public class GeolocationProvisioningAdapter extends SimplerQueuedProvisioningAdapter implements InitializingBean {

    private static final Logger LOG = LoggerFactory.getLogger(GeolocationProvisioningAdapter.class);

    private NodeDao nodeDao;

    private ServiceRegistry serviceRegistry;

    public GeolocationProvisioningAdapter() {
        super("GeolocationProvisioningAdapter");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        Objects.requireNonNull(nodeDao);
    }

    @Override
    public void doAddNode(int nodeId) {
        LOG.debug("Invoked doAddNode on node with id {}.", nodeId);
        doUpdateNodeInternal(nodeId);
    }

    @Override
    public boolean isNodeReady(AdapterOperation op) {
        if (op.getType() == AdapterOperationType.ADD || op.getType() == AdapterOperationType.UPDATE) {
            return nodeDao.get(op.getNodeId()) != null && getGeolocationResolver() != null;
        }
        return true;
    }

    @Override
    public void doUpdateNode(int nodeId) {
        LOG.debug("Invoked doUpdateNode on node with id {}.", nodeId);
        doUpdateNodeInternal(nodeId);
    }

    private void doUpdateNodeInternal(int nodeId) {
        final OnmsNode node = getNode(nodeId);
        final OnmsGeolocation geolocation = node.getAssetRecord().getGeolocation();

        // Only resolve long/lat if not already set and an address is set
        if (geolocation.getLatitude() == null
                    && geolocation.getLatitude() == null
                    && !Strings.isNullOrEmpty(geolocation.asAddressString())) {

            final Coordinates coordinates = getGeolocationResolver().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(), nodeId);
            }
        }
    }

    @Override
    public void doDeleteNode(int nodeid) {
        LOG.debug("Invoked doDeleteNode on node with id {}. Nothing to do.", nodeid);
    }

    @Override
    public void doNotifyConfigChange(int nodeid) {
        LOG.debug("Invoked doNotifyConfigChange on node with id {}. Nothing to do.", nodeid);
    }

    public void setNodeDao(NodeDao nodeDao) {
        this.nodeDao = nodeDao;
    }

    private OnmsNode getNode(int nodeId) {
        final OnmsNode onmsNode = nodeDao.get(nodeId);
        if (onmsNode == null) {
            throw new ProvisioningAdapterException("Node with id " + nodeId + " not found in database");
        }
        return onmsNode;
    }

    private ServiceRegistry getServiceRegistry() {
        if (serviceRegistry == null) {
            serviceRegistry = BeanUtils.getBean("soaContext", "serviceRegistry", ServiceRegistry.class);
            Objects.requireNonNull(serviceRegistry);
        }
        return serviceRegistry;
    }

    private GeolocationResolver getGeolocationResolver() {
        return getServiceRegistry().findProvider(GeolocationResolver.class);
    }
}

@@ -1980,24 +1980,169 @@
 er.class);%0A%0A
+    private boolean enabled = Boolean.valueOf(System.getProperty(%22org.opennms.provisiond.resolveMissingCoordinatesFromAddressString%22, %22true%22));%0A%0A
     private 
@@ -2389,38 +2389,188 @@
     
-Objects.requireNonNull(nodeDao
+serviceRegistry = BeanUtils.getBean(%22soaContext%22, %22serviceRegistry%22, ServiceRegistry.class);%0A        Objects.requireNonNull(nodeDao);%0A        Objects.requireNonNull(serviceRegistry
 );%0A 
@@ -2771,211 +2771,231 @@
 lic 
-boolean isNodeReady(A
+void doUp
 da
-p
 te
-rOperation op) %7B%0A        if (op.getType() == A
+Node(int nodeId) %7B%0A        LOG.debug(%22Invoked doUp
 da
-p
 te
-rOperationType.ADD %7C%7C op.getType() == AdapterOperationType.UPDATE) %7B%0A 
+Node on node with id %7B%7D.%22, nodeId);%0A        doUpdateNodeInternal(nodeId);%0A
     
+%7D%0A%0A
     
-   return nodeDao.get(op.getNodeId()) != null &&
+private void doUpdateNodeInternal(int nodeId) %7B%0A        if (!enabled %7C%7C
  get
@@ -3020,279 +3020,293 @@
 r() 
-!
+=
 = null
-;%0A  
+) %7B%0A
       
-%7D%0A
       
-  return true;%0A    %7D%0A%0A    @Override%0A    public void doUpdateNode(int nodeId) %7B%0A        LOG.debug(%22Invoked doUpdateNode on node with id %7B%7D.%22, nodeId);%0A        doUpdateNodeInternal(nodeId);%0A
+LOG.info(%22%7B%7D is either disabled manually or no GeocoderService is available (disabled=%7B%7D, GeocoderService available=%7B%7D)%22, getName(), !enabled, getGeolocationResolver() != null);%0A            return;%0A  
     
+  
 %7D%0A%0A    
-private void doUpdateNodeInternal(int nodeId) %7B
+    // Update geolocation information if required
 %0A   
@@ -4882,305 +4882,8 @@
  %7D%0A%0A
-    private ServiceRegistry getServiceRegistry() %7B%0A        if (serviceRegistry == null) %7B%0A            serviceRegistry = BeanUtils.getBean(%22soaContext%22, %22serviceRegistry%22, ServiceRegistry.class);%0A            Objects.requireNonNull(serviceRegistry);%0A        %7D%0A        return serviceRegistry;%0A    %7D%0A%0A
     
@@ -4956,12 +4956,9 @@
 urn 
-getS
+s
 ervi
@@ -4967,18 +4967,16 @@
 Registry
-()
 .findPro

Make it disableable