package io.cattle.platform.configitem.context.impl;

import static io.cattle.platform.core.model.tables.DataTable.*;
import io.cattle.platform.configitem.server.model.ConfigItem;
import io.cattle.platform.configitem.server.model.impl.ArchiveContext;
import io.cattle.platform.core.constants.NetworkServiceConstants;
import io.cattle.platform.core.dao.NetworkDao;
import io.cattle.platform.core.model.Agent;
import io.cattle.platform.core.model.Data;
import io.cattle.platform.core.model.Instance;
import io.cattle.platform.core.model.Network;
import io.cattle.platform.core.model.NetworkService;

import javax.inject.Inject;
import javax.inject.Named;

@Named
public class IpsecInfoFactory extends AbstractAgentBaseContextFactory {

    NetworkDao networkDao;

    @Override
    protected void populateContext(Agent agent, Instance instance, ConfigItem item, ArchiveContext context) {
        Network network = null;
        Data data = null;
        for ( NetworkService service : networkDao.getAgentInstanceNetworkService(instance.getId(), NetworkServiceConstants.KIND_IPSEC_TUNNEL) ) {
            if ( network == null ) {
                network = objectManager.loadResource(Network.class, service.getNetworkId());
                data = objectManager.findAny(Data.class, DATA.NAME, String.format("%s/%s", service.getUuid(), "ipsecKey"));
                break;
            }
        }

        context.getData().put("ipsecNetwork", network);
        context.getData().put("ipsecKey", data.getValue());
    }

    public NetworkDao getNetworkDao() {
        return networkDao;
    }

    @Inject
    public void setNetworkDao(NetworkDao networkDao) {
        this.networkDao = networkDao;
    }

}

@@ -1380,24 +1380,58 @@
 %0A        %7D%0A%0A
+        if ( data != null ) %7B%0A    
         cont
@@ -1474,32 +1474,36 @@
 twork);%0A        
+  
+  
 context.getData(
@@ -1534,24 +1534,34 @@
 etValue());%0A
+        %7D%0A
     %7D%0A%0A    p

check for null