<commit_before>/*
# Licensed Materials - Property of IBM
# Copyright IBM Corp. 2015,2018
 */
package com.ibm.streamsx.topology.internal.context.streams;

import static com.ibm.streamsx.topology.internal.context.remote.DeployKeys.deploy;
import static com.ibm.streamsx.topology.internal.context.remote.DeployKeys.keepArtifacts;

import java.io.File;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Map.Entry;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicBoolean;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.ibm.streamsx.rest.Instance;
import com.ibm.streamsx.rest.Job;
import com.ibm.streamsx.rest.Result;
import com.ibm.streamsx.rest.StreamsConnection;
import com.ibm.streamsx.rest.internal.ICP4DAuthenticator;
import com.ibm.streamsx.topology.context.ContextProperties;
import com.ibm.streamsx.topology.context.remote.RemoteContext;
import com.ibm.streamsx.topology.internal.context.remote.SubmissionResultsKeys;
import com.ibm.streamsx.topology.internal.context.streamsrest.DistributedStreamsRestContext;
import com.ibm.streamsx.topology.internal.context.streamsrest.StreamsKeys;
import com.ibm.streamsx.topology.internal.gson.GsonUtilities;
import com.ibm.streamsx.topology.internal.process.CompletedFuture;
import com.ibm.streamsx.topology.internal.streams.InvokeSubmit;
import com.ibm.streamsx.topology.internal.streams.Util;

public class DistributedStreamsContext extends
        BundleUserStreamsContext<BigInteger> {
	
	private final AtomicBoolean useRestApi = new AtomicBoolean();
	private Instance instance;	

    public DistributedStreamsContext() {
        super(false);
    }
    
    public boolean useRestApi() {
    	return useRestApi.get();
    }

    @Override
    public Type getType() {
        return Type.DISTRIBUTED;
    }
    
    public synchronized Instance instance() throws IOException {
    	if (!useRestApi())
    		throw new IllegalStateException(/*internal error*/);
    	
    	return instance;
    }
    
    private synchronized Instance createInstance(AppEntity entity) throws IOException {
    	if (!useRestApi())
    		throw new IllegalStateException(/*internal error*/);
    	
		Instance instance = RemoteDistributedStreamsContext.getConfigInstance(entity);        
		if (instance == null) {
		    boolean verify = true;
		    if (deploy(entity.submission).has(ContextProperties.SSL_VERIFY))
		        verify = deploy(entity.submission).get(ContextProperties.SSL_VERIFY).getAsBoolean();
		    instance = Instance.ofEndpoint(
		            (String) null, (String) null, (String) null, (String) null,
		            verify);		    
		}
		return instance;
	}
    

    @Override
    protected void preSubmit(AppEntity entity) {
            	
    	if (RemoteDistributedStreamsContext.getConfigInstance(entity) != null) {  		
    	    // Allow the config to provide an instance.
    		useRestApi.set(true);
    		return;
    	}
    		
    	try {
            InvokeSubmit.checkPreconditions();
            useRestApi.set(false);
    	} catch (IllegalStateException e) {
    		// See if the REST api is setup.
    	    Util.getenv(Util.ICP4D_DEPLOYMENT_URL);
    		Util.getenv(Util.STREAMS_INSTANCE_ID);
    		Util.getenv(Util.STREAMS_PASSWORD);
    		useRestApi.set(true);
    	}
    }
    
    @Override
    protected final Future<BigInteger> action(AppEntity entity) throws Exception {
        if (useRemoteBuild(entity, e -> 7))
            return fullRemoteAction(entity);

        return super.action(entity);
    }
    
    /**
     * Called to build and submit using REST.<commit_msg>Fix #2233 - Only remote buuilds for cpd instances<commit_after>/*
# Licensed Materials - Property of IBM
# Copyright IBM Corp. 2015,2018
 */
package com.ibm.streamsx.topology.internal.context.streams;

import static com.ibm.streamsx.topology.internal.context.remote.DeployKeys.deploy;
import static com.ibm.streamsx.topology.internal.context.remote.DeployKeys.keepArtifacts;

import java.io.File;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Map.Entry;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicBoolean;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.ibm.streamsx.rest.Instance;
import com.ibm.streamsx.rest.Job;
import com.ibm.streamsx.rest.Result;
import com.ibm.streamsx.rest.StreamsConnection;
import com.ibm.streamsx.rest.internal.ICP4DAuthenticator;
import com.ibm.streamsx.topology.context.ContextProperties;
import com.ibm.streamsx.topology.context.remote.RemoteContext;
import com.ibm.streamsx.topology.internal.context.remote.SubmissionResultsKeys;
import com.ibm.streamsx.topology.internal.context.streamsrest.DistributedStreamsRestContext;
import com.ibm.streamsx.topology.internal.context.streamsrest.StreamsKeys;
import com.ibm.streamsx.topology.internal.gson.GsonUtilities;
import com.ibm.streamsx.topology.internal.process.CompletedFuture;
import com.ibm.streamsx.topology.internal.streams.InvokeSubmit;
import com.ibm.streamsx.topology.internal.streams.Util;

public class DistributedStreamsContext extends
        BundleUserStreamsContext<BigInteger> {
	
	private final AtomicBoolean useRestApi = new AtomicBoolean();
	private Instance instance;	

    public DistributedStreamsContext() {
        super(false);
    }
    
    public boolean useRestApi() {
    	return useRestApi.get();
    }

    @Override
    public Type getType() {
        return Type.DISTRIBUTED;
    }
    
    public synchronized Instance instance() throws IOException {
    	if (!useRestApi())
    		throw new IllegalStateException(/*internal error*/);
    	
    	return instance;
    }
    
    private synchronized Instance createInstance(AppEntity entity) throws IOException {
    	if (!useRestApi())
    		throw new IllegalStateException(/*internal error*/);
    	
		Instance instance = RemoteDistributedStreamsContext.getConfigInstance(entity);        
		if (instance == null) {
		    boolean verify = true;
		    if (deploy(entity.submission).has(ContextProperties.SSL_VERIFY))
		        verify = deploy(entity.submission).get(ContextProperties.SSL_VERIFY).getAsBoolean();
		    instance = Instance.ofEndpoint(
		            (String) null, (String) null, (String) null, (String) null,
		            verify);		    
		}
		return instance;
	}
    

    @Override
    protected void preSubmit(AppEntity entity) {
            	
    	if (RemoteDistributedStreamsContext.getConfigInstance(entity) != null) {  		
    	    // Allow the config to provide an instance.
    		useRestApi.set(true);
    		return;
    	}
    		
    	try {
            InvokeSubmit.checkPreconditions();
            useRestApi.set(false);
    	} catch (IllegalStateException e) {
    		// See if the REST api is setup.
    	    Util.getenv(Util.ICP4D_DEPLOYMENT_URL);
    		Util.getenv(Util.STREAMS_INSTANCE_ID);
    		Util.getenv(Util.STREAMS_PASSWORD);
    		useRestApi.set(true);
    	}
    }
    
    @Override
    protected final Future<BigInteger> action(AppEntity entity) throws Exception {
        if (useRestApi.get() && useRemoteBuild(entity, e -> 7))
            return fullRemoteAction(entity);

        return super.action(entity);
    }
    
    /**
     * Called to build and submit using REST.
{'fraction_non_alphanumeric': 0.09884836852207293, 'fraction_numerical': 0.003564573622155196, 'mean_word_length': 3.418534221683828, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, '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': '5400541', 'n_tokens_mistral': 2312, 'n_tokens_neox': 2231, 'n_words': 407}