<commit_before>/*
# Licensed Materials - Property of IBM
# Copyright IBM Corp. 2019
 */
package com.ibm.streamsx.topology.internal.context.streamsrest;

import static com.ibm.streamsx.topology.context.ContextProperties.KEEP_ARTIFACTS;
import static com.ibm.streamsx.topology.generator.spl.SPLGenerator.getSPLCompatibleName;
import static com.ibm.streamsx.topology.internal.context.remote.DeployKeys.createJobConfigOverlayFile;
import static com.ibm.streamsx.topology.internal.gson.GsonUtilities.jboolean;
import static com.ibm.streamsx.topology.internal.gson.GsonUtilities.object;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.ThreadLocalRandom;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.ibm.streamsx.rest.build.Artifact;
import com.ibm.streamsx.rest.build.Build;
import com.ibm.streamsx.rest.build.BuildService;
import com.ibm.streamsx.topology.context.ContextProperties;
import com.ibm.streamsx.topology.internal.context.remote.BuildRemoteContext;
import com.ibm.streamsx.topology.internal.context.remote.SubmissionResultsKeys;
import com.ibm.streamsx.topology.internal.gson.GsonUtilities;

/**
 * Streams V5 (ICP4D) build service context.
 */
public class BuildServiceContext extends BuildRemoteContext<BuildService> {
    
    @Override
    public Type getType() {
        return Type.BUNDLE;
    }
    
    protected boolean sslVerify(JsonObject deploy) {
        if (deploy.has(ContextProperties.SSL_VERIFY))
            return GsonUtilities.jboolean(deploy, ContextProperties.SSL_VERIFY);                
        return true;
    }

    @Override
    protected BuildService createSubmissionContext(JsonObject deploy) throws Exception {
        JsonObject serviceDefinition = object(deploy, StreamsKeys.SERVICE_DEFINITION);
        return BuildService.ofServiceDefinition(serviceDefinition, sslVerify(deploy));
    }

    @Override
    protected JsonObject submitBuildArchive(BuildService context, File buildArchive,
            JsonObject deploy, JsonObject jco, String buildName,
            JsonObject buildConfig) throws Exception {
            
        if (!sslVerify(deploy))
            context.allowInsecureHosts();
        
        buildName = getSPLCompatibleName(buildName) + "_" + randomHex(16);
        
        report("Building bundle");

        Build build = context.createBuild(buildName, buildConfig);
        
        try {
            
            JsonObject result = new JsonObject();
            result.add(SubmissionResultsKeys.SUBMIT_METRICS, build.getMetrics());
            JsonObject buildInfo = new JsonObject();
            result.add("build", buildInfo);           
            buildInfo.addProperty("name", build.getName());

            build.uploadArchiveAndBuild(buildArchive);<commit_msg>Support standalone build serice for remote<commit_after>/*
# Licensed Materials - Property of IBM
# Copyright IBM Corp. 2019
 */
package com.ibm.streamsx.topology.internal.context.streamsrest;

import static com.ibm.streamsx.topology.context.ContextProperties.KEEP_ARTIFACTS;
import static com.ibm.streamsx.topology.generator.spl.SPLGenerator.getSPLCompatibleName;
import static com.ibm.streamsx.topology.internal.context.remote.DeployKeys.createJobConfigOverlayFile;
import static com.ibm.streamsx.topology.internal.gson.GsonUtilities.jboolean;
import static com.ibm.streamsx.topology.internal.gson.GsonUtilities.object;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.ThreadLocalRandom;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.ibm.streamsx.rest.build.Artifact;
import com.ibm.streamsx.rest.build.Build;
import com.ibm.streamsx.rest.build.BuildService;
import com.ibm.streamsx.topology.context.ContextProperties;
import com.ibm.streamsx.topology.internal.context.remote.BuildRemoteContext;
import com.ibm.streamsx.topology.internal.context.remote.SubmissionResultsKeys;
import com.ibm.streamsx.topology.internal.gson.GsonUtilities;

/**
 * Streams V5 (ICP4D) build service context.
 */
public class BuildServiceContext extends BuildRemoteContext<BuildService> {
    
    @Override
    public Type getType() {
        return Type.BUNDLE;
    }
    
    protected boolean sslVerify(JsonObject deploy) {
        if (deploy.has(ContextProperties.SSL_VERIFY))
            return GsonUtilities.jboolean(deploy, ContextProperties.SSL_VERIFY);                
        return true;
    }

    @Override
    protected BuildService createSubmissionContext(JsonObject deploy) throws Exception {
        JsonObject serviceDefinition = object(deploy, StreamsKeys.SERVICE_DEFINITION);
        if (serviceDefinition != null)
            return BuildService.ofServiceDefinition(serviceDefinition, sslVerify(deploy));
        
        // Remote environment context set through environment variables.
        return BuildService.ofEndpoint(null, null, null, null, sslVerify(deploy));
    }

    @Override
    protected JsonObject submitBuildArchive(BuildService context, File buildArchive,
            JsonObject deploy, JsonObject jco, String buildName,
            JsonObject buildConfig) throws Exception {
            
        if (!sslVerify(deploy))
            context.allowInsecureHosts();
        
        buildName = getSPLCompatibleName(buildName) + "_" + randomHex(16);
        
        report("Building bundle");

        Build build = context.createBuild(buildName, buildConfig);
        
        try {
            
            JsonObject result = new JsonObject();
            result.add(SubmissionResultsKeys.SUBMIT_METRICS, build.getMetrics());
            JsonObject buildInfo = new JsonObject();
            result.add("build", buildInfo);           
            buildInfo.addProperty("name", build.getName());

            build.uploadArchiveAndBuild(buildArchive);
{'fraction_non_alphanumeric': 0.09028610587630632, 'fraction_numerical': 0.0027411341442521845, 'mean_word_length': 3.4804297774366844, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '380785', 'n_tokens_mistral': 1666, 'n_tokens_neox': 1638, 'n_words': 296}