<commit_before>package io.cattle.platform.docker.process.instancehostmap;

import static io.cattle.platform.core.model.tables.IpAddressTable.*;
import static io.cattle.platform.core.model.tables.PortTable.*;
import io.cattle.platform.archaius.util.ArchaiusUtil;
import io.cattle.platform.core.constants.NetworkServiceConstants;
import io.cattle.platform.core.constants.PortConstants;
import io.cattle.platform.core.dao.IpAddressDao;
import io.cattle.platform.core.dao.NetworkDao;
import io.cattle.platform.core.dao.NicDao;
import io.cattle.platform.core.model.Host;
import io.cattle.platform.core.model.HostIpAddressMap;
import io.cattle.platform.core.model.Instance;
import io.cattle.platform.core.model.InstanceHostMap;
import io.cattle.platform.core.model.IpAddress;
import io.cattle.platform.core.model.Nic;
import io.cattle.platform.core.model.Port;
import io.cattle.platform.core.util.PortSpec;
import io.cattle.platform.docker.constants.DockerInstanceConstants;
import io.cattle.platform.docker.constants.DockerIpAddressConstants;
import io.cattle.platform.docker.process.dao.DockerComputeDao;
import io.cattle.platform.docker.process.util.DockerProcessUtils;
import io.cattle.platform.engine.handler.HandlerResult;
import io.cattle.platform.engine.handler.ProcessPostListener;
import io.cattle.platform.engine.process.ProcessInstance;
import io.cattle.platform.engine.process.ProcessState;
import io.cattle.platform.json.JsonMapper;
import io.cattle.platform.lock.LockCallback;
import io.cattle.platform.lock.LockManager;
import io.cattle.platform.object.util.DataAccessor;
import io.cattle.platform.process.common.handler.AbstractObjectProcessLogic;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

import org.apache.commons.lang3.ObjectUtils;

import com.netflix.config.DynamicBooleanProperty;

public class DockerPostInstanceHostMapActivate extends AbstractObjectProcessLogic implements ProcessPostListener {

    public static final DynamicBooleanProperty DYNAMIC_ADD_IP = ArchaiusUtil.getBoolean("docker.compute.auto.add.host.ip");

    JsonMapper jsonMapper;
    IpAddressDao ipAddressDao;
    DockerComputeDao dockerDao;
    NicDao nicDao;
    NetworkDao networkDao;
    LockManager lockManager;

    @Override
    public String[] getProcessNames() {
        return new String[] { "instancehostmap.activate" };
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    @Override
    public HandlerResult handle(ProcessState state, ProcessInstance process) {
        InstanceHostMap map = (InstanceHostMap)state.getResource();
        Instance instance = getObjectManager().loadResource(Instance.class, map.getInstanceId());
        Host host = getObjectManager().loadResource(Host.class, map.getHostId());

        String dockerIp = DockerProcessUtils.getDockerIp(instance);
        Nic nic = nicDao.getPrimaryNic(instance);

        String hostIp = DataAccessor.fields(instance)
                .withKey(DockerInstanceConstants.FIELD_DOCKER_HOST_IP)
                .as(String.class);

        Map ports = DataAccessor.fields(instance)
                .withKey(DockerInstanceConstants.FIELD_DOCKER_PORTS)
                .as(jsonMapper, Map.class);

        if ( dockerIp != null ) {
            processDockerIp(instance, nic, dockerIp);
        }

        if ( hostIp != null && ports != null ) {
            processPorts(hostIp, ports, instance, nic, host);
        }

        return null;
    }

    protected void processDockerIp(Instance instance, Nic nic, String dockerIp) {
        if ( nic == null ) {
            return;
        }

        IpAddress dockerIpAddress = dockerDao.getDockerIp(dockerIp, instance);

        if ( dockerIpAddress == null ) {
            dockerIpAddress = ipAddressDao.mapNewIpAddress(nic,
                    IP_ADDRESS.SUBNET_ID, null,
                    IP_ADDRESS.KIND, DockerIpAddressConstants.KIND_DOCKER,
                    IP_ADDRESS.ADDRESS, dockerIp);
        }

        if ( dockerIpAddress.getKind().equals(DockerIpAddressConstants.KIND_DOCKER) ) {
            if ( ! dockerIp.equals(dockerIpAddress.getAddress()) ) {
                getObjectManager().setFields(dockerIpAddress,
                        IP_ADDRESS.ADDRESS, dockerIp);
            }
            createThenActivate(dockerIpAddress, null);
        }
    }

    protected void processPorts(final String hostIp, Map<String,String> ports, Instance instance, Nic nic, final Host host) {
        IpAddress ipAddress = getIpAddress(host, hostIp);
        IpAddress dockerIpAddress = dockerDao.getDockerIp(DockerProcessUtils.getDockerIp(instance), instance);
        Long privateIpAddressId = dockerIpAddress == null ? null : dockerIpAddress.getId();

        if ( ipAddress == null && DYNAMIC_ADD_IP.get() ) {
            ipAddress = lockManager.lock(new AssignHostIpLockDefinition(host), new LockCallback<IpAddress>() {
                @Override
                public IpAddress doWithLock() {
                    IpAddress ipAddress = getIpAddress(host, hostIp);
                    return ipAddress == null ? ipAddressDao.assignNewAddress(host, hostIp) : ipAddress;
                }
            });
            ipAddress = ipAddressDao.assignNewAddress(host, hostIp);
        }

        if ( DYNAMIC_ADD_IP.get() ) {
            createThenActivate(ipAddress, null);
            for ( HostIpAddressMap map : getObjectManager().children(ipAddress, HostIpAddressMap.class) ) {
                if ( map.getHostId().longValue() == host.getId() ) {
                    createThenActivate(map, null);
                }
            }<commit_msg>docker: registered host IP twice<commit_after>package io.cattle.platform.docker.process.instancehostmap;

import static io.cattle.platform.core.model.tables.IpAddressTable.*;
import static io.cattle.platform.core.model.tables.PortTable.*;
import io.cattle.platform.archaius.util.ArchaiusUtil;
import io.cattle.platform.core.constants.NetworkServiceConstants;
import io.cattle.platform.core.constants.PortConstants;
import io.cattle.platform.core.dao.IpAddressDao;
import io.cattle.platform.core.dao.NetworkDao;
import io.cattle.platform.core.dao.NicDao;
import io.cattle.platform.core.model.Host;
import io.cattle.platform.core.model.HostIpAddressMap;
import io.cattle.platform.core.model.Instance;
import io.cattle.platform.core.model.InstanceHostMap;
import io.cattle.platform.core.model.IpAddress;
import io.cattle.platform.core.model.Nic;
import io.cattle.platform.core.model.Port;
import io.cattle.platform.core.util.PortSpec;
import io.cattle.platform.docker.constants.DockerInstanceConstants;
import io.cattle.platform.docker.constants.DockerIpAddressConstants;
import io.cattle.platform.docker.process.dao.DockerComputeDao;
import io.cattle.platform.docker.process.util.DockerProcessUtils;
import io.cattle.platform.engine.handler.HandlerResult;
import io.cattle.platform.engine.handler.ProcessPostListener;
import io.cattle.platform.engine.process.ProcessInstance;
import io.cattle.platform.engine.process.ProcessState;
import io.cattle.platform.json.JsonMapper;
import io.cattle.platform.lock.LockCallback;
import io.cattle.platform.lock.LockManager;
import io.cattle.platform.object.util.DataAccessor;
import io.cattle.platform.process.common.handler.AbstractObjectProcessLogic;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

import org.apache.commons.lang3.ObjectUtils;

import com.netflix.config.DynamicBooleanProperty;

public class DockerPostInstanceHostMapActivate extends AbstractObjectProcessLogic implements ProcessPostListener {

    public static final DynamicBooleanProperty DYNAMIC_ADD_IP = ArchaiusUtil.getBoolean("docker.compute.auto.add.host.ip");

    JsonMapper jsonMapper;
    IpAddressDao ipAddressDao;
    DockerComputeDao dockerDao;
    NicDao nicDao;
    NetworkDao networkDao;
    LockManager lockManager;

    @Override
    public String[] getProcessNames() {
        return new String[] { "instancehostmap.activate" };
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    @Override
    public HandlerResult handle(ProcessState state, ProcessInstance process) {
        InstanceHostMap map = (InstanceHostMap)state.getResource();
        Instance instance = getObjectManager().loadResource(Instance.class, map.getInstanceId());
        Host host = getObjectManager().loadResource(Host.class, map.getHostId());

        String dockerIp = DockerProcessUtils.getDockerIp(instance);
        Nic nic = nicDao.getPrimaryNic(instance);

        String hostIp = DataAccessor.fields(instance)
                .withKey(DockerInstanceConstants.FIELD_DOCKER_HOST_IP)
                .as(String.class);

        Map ports = DataAccessor.fields(instance)
                .withKey(DockerInstanceConstants.FIELD_DOCKER_PORTS)
                .as(jsonMapper, Map.class);

        if ( dockerIp != null ) {
            processDockerIp(instance, nic, dockerIp);
        }

        if ( hostIp != null && ports != null ) {
            processPorts(hostIp, ports, instance, nic, host);
        }

        return null;
    }

    protected void processDockerIp(Instance instance, Nic nic, String dockerIp) {
        if ( nic == null ) {
            return;
        }

        IpAddress dockerIpAddress = dockerDao.getDockerIp(dockerIp, instance);

        if ( dockerIpAddress == null ) {
            dockerIpAddress = ipAddressDao.mapNewIpAddress(nic,
                    IP_ADDRESS.SUBNET_ID, null,
                    IP_ADDRESS.KIND, DockerIpAddressConstants.KIND_DOCKER,
                    IP_ADDRESS.ADDRESS, dockerIp);
        }

        if ( dockerIpAddress.getKind().equals(DockerIpAddressConstants.KIND_DOCKER) ) {
            if ( ! dockerIp.equals(dockerIpAddress.getAddress()) ) {
                getObjectManager().setFields(dockerIpAddress,
                        IP_ADDRESS.ADDRESS, dockerIp);
            }
            createThenActivate(dockerIpAddress, null);
        }
    }

    protected void processPorts(final String hostIp, Map<String,String> ports, Instance instance, Nic nic, final Host host) {
        IpAddress ipAddress = getIpAddress(host, hostIp);
        IpAddress dockerIpAddress = dockerDao.getDockerIp(DockerProcessUtils.getDockerIp(instance), instance);
        Long privateIpAddressId = dockerIpAddress == null ? null : dockerIpAddress.getId();

        if ( ipAddress == null && DYNAMIC_ADD_IP.get() ) {
            ipAddress = lockManager.lock(new AssignHostIpLockDefinition(host), new LockCallback<IpAddress>() {
                @Override
                public IpAddress doWithLock() {
                    IpAddress ipAddress = getIpAddress(host, hostIp);
                    return ipAddress == null ? ipAddressDao.assignNewAddress(host, hostIp) : ipAddress;
                }
            });
        }

        if ( DYNAMIC_ADD_IP.get() ) {
            createThenActivate(ipAddress, null);
            for ( HostIpAddressMap map : getObjectManager().children(ipAddress, HostIpAddressMap.class) ) {
                if ( map.getHostId().longValue() == host.getId() ) {
                    createThenActivate(map, null);
                }
            }
{'fraction_non_alphanumeric': 0.098762109795479, 'fraction_numerical': 0.0001794043774668102, 'mean_word_length': 3.2135298563869994, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6822765', 'n_tokens_mistral': 3221, 'n_tokens_neox': 3137, 'n_words': 531}