Search in sources :

Example 56 with Yaml

use of org.yaml.snakeyaml.Yaml in project solr-cmd-utils by tblsoft.

the class PipelineExecuter method readPipelineFromYamlFile.

Pipeline readPipelineFromYamlFile(String fileName) {
    try {
        InputStream input = new FileInputStream(new File(fileName));
        Yaml yaml = new Yaml(new Constructor(Pipeline.class));
        Pipeline pipeline = (Pipeline) yaml.load(input);
        input.close();
        return pipeline;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Constructor(org.yaml.snakeyaml.constructor.Constructor) File(java.io.File) FileInputStream(java.io.FileInputStream) Yaml(org.yaml.snakeyaml.Yaml) Pipeline(de.tblsoft.solr.pipeline.bean.Pipeline)

Example 57 with Yaml

use of org.yaml.snakeyaml.Yaml in project okta-sdk-java by okta.

the class AbstractOktaJavaClientCodegen method preprocessSwagger.

@Override
public void preprocessSwagger(Swagger swagger) {
    // make sure we have the apiFile location
    String apiFile = (String) additionalProperties.get(API_FILE_KEY);
    if (apiFile == null || apiFile.isEmpty()) {
        throw new SwaggerException("'additionalProperties." + API_FILE_KEY + " property is required. This must be " + "set to the same file that Swagger is using.");
    }
    try (Reader reader = new InputStreamReader(new FileInputStream(apiFile), StandardCharsets.UTF_8.toString())) {
        rawSwaggerConfig = new Yaml().loadAs(reader, Map.class);
    } catch (IOException e) {
        throw new IllegalStateException("Failed to parse apiFile: " + apiFile, e);
    }
    vendorExtensions.put("basePath", swagger.getBasePath());
    super.preprocessSwagger(swagger);
    tagEnums(swagger);
    buildTopLevelResourceList(swagger);
    addListModels(swagger);
    buildModelTagMap(swagger);
    removeListAfterAndLimit(swagger);
    moveOperationsToSingleClient(swagger);
    handleOktaLinkedOperations(swagger);
    buildDiscriminationMap(swagger);
}
Also used : SwaggerException(io.swagger.parser.SwaggerException) InputStreamReader(java.io.InputStreamReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) FileInputStream(java.io.FileInputStream) Yaml(org.yaml.snakeyaml.Yaml)

Example 58 with Yaml

use of org.yaml.snakeyaml.Yaml in project okta-sdk-java by okta.

the class DiscriminatorConfig method loadConfig.

/**
 * Loads DiscriminatorConfig from the classpath file: {code}/com/okta/sdk/resource/discrimination.yaml{code}.
 * If this file cannot be found an empty DiscriminatorConfig is returned.
 *
 * @return a DiscriminatorConfig based on the discrimination.yaml found on the classpath.
 */
static DiscriminatorConfig loadConfig() {
    Yaml yaml = new Yaml();
    String configFile = "/com/okta/sdk/resource/discrimination.yaml";
    URL configYaml = DefaultDiscriminatorRegistry.class.getResource(configFile);
    if (configYaml != null) {
        try (InputStream is = configYaml.openStream()) {
            return yaml.loadAs(is, DiscriminatorConfig.class);
        } catch (IOException e) {
            logger.warn("Failed to load config file: {}", configFile, e);
        }
    } else {
        logger.warn("Could not find config file on the classpath: {}", configFile);
    }
    return new DiscriminatorConfig();
}
Also used : InputStream(java.io.InputStream) IOException(java.io.IOException) Yaml(org.yaml.snakeyaml.Yaml) URL(java.net.URL)

Example 59 with Yaml

use of org.yaml.snakeyaml.Yaml in project helix by apache.

the class TestJobQueuesResource method test.

@Test
public void test() throws Exception {
    String className = TestHelper.getTestClassName();
    String methodName = TestHelper.getTestMethodName();
    String clusterName = className + "_" + methodName;
    final int n = 5;
    final int p = 20;
    final int r = 3;
    System.out.println("START " + clusterName + " at " + new Date(System.currentTimeMillis()));
    _gSetupTool.addCluster(clusterName, true);
    for (int i = 0; i < n; i++) {
        String instanceName = "localhost_" + (12918 + i);
        _gSetupTool.addInstanceToCluster(clusterName, instanceName);
    }
    // Set up target db
    _gSetupTool.addResourceToCluster(clusterName, WorkflowGenerator.DEFAULT_TGT_DB, p, "MasterSlave");
    _gSetupTool.rebalanceStorageCluster(clusterName, WorkflowGenerator.DEFAULT_TGT_DB, r);
    Map<String, TaskFactory> taskFactoryReg = new HashMap<String, TaskFactory>();
    taskFactoryReg.put("DummyTask", new TaskFactory() {

        @Override
        public Task createNewTask(TaskCallbackContext context) {
            return new MockTask(context);
        }
    });
    // Start dummy participants
    MockParticipantManager[] participants = new MockParticipantManager[n];
    for (int i = 0; i < n; i++) {
        String instanceName = "localhost_" + (12918 + i);
        participants[i] = new MockParticipantManager(ZK_ADDR, clusterName, instanceName);
        // Register a Task state model factory.
        StateMachineEngine stateMachine = participants[i].getStateMachineEngine();
        stateMachine.registerStateModelFactory("Task", new TaskStateModelFactory(participants[i], taskFactoryReg));
        participants[i].syncStart();
    }
    // start controller
    String controllerName = "controller";
    ClusterControllerManager controller = new ClusterControllerManager(ZK_ADDR, clusterName, controllerName);
    controller.syncStart();
    boolean result = ClusterStateVerifier.verifyByZkCallback(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR, clusterName));
    Assert.assertTrue(result);
    // Start a queue
    String queueName = "myQueue1";
    LOG.info("Starting job-queue: " + queueName);
    String jobQueueYamlConfig = "name: " + queueName;
    String resourceUrl = "http://localhost:" + ADMIN_PORT + "/clusters/" + clusterName + "/jobQueues";
    ZNRecord postRet = AdminTestHelper.post(_gClient, resourceUrl, jobQueueYamlConfig);
    LOG.info("Started job-queue: " + queueName + ", ret: " + postRet);
    LOG.info("Getting all job-queues");
    ZNRecord getRet = AdminTestHelper.get(_gClient, resourceUrl);
    LOG.info("Got job-queues: " + getRet);
    // Enqueue job
    resourceUrl = "http://localhost:" + ADMIN_PORT + "/clusters/" + clusterName + "/jobQueues/" + queueName;
    WorkflowBean wfBean = new WorkflowBean();
    wfBean.name = queueName;
    JobBean jBean1 = new JobBean();
    jBean1.name = "myJob1";
    jBean1.command = "DummyTask";
    jBean1.targetResource = WorkflowGenerator.DEFAULT_TGT_DB;
    jBean1.targetPartitionStates = Lists.newArrayList("MASTER");
    JobBean jBean2 = new JobBean();
    jBean2.name = "myJob2";
    jBean2.command = "DummyTask";
    jBean2.targetResource = WorkflowGenerator.DEFAULT_TGT_DB;
    jBean2.targetPartitionStates = Lists.newArrayList("SLAVE");
    wfBean.jobs = Lists.newArrayList(jBean1, jBean2);
    String jobYamlConfig = new Yaml().dump(wfBean);
    LOG.info("Enqueuing jobs: " + jobQueueYamlConfig);
    Map<String, String> paraMap = new HashMap<String, String>();
    paraMap.put(JsonParameters.MANAGEMENT_COMMAND, TaskDriver.DriverCommand.start.toString());
    String postBody = String.format("%s=%s&%s=%s", JsonParameters.JSON_PARAMETERS, ClusterRepresentationUtil.ObjectToJson(paraMap), ResourceUtil.YamlParamKey.NEW_JOB.toString(), jobYamlConfig);
    postRet = AdminTestHelper.post(_gClient, resourceUrl, postBody);
    LOG.info("Enqueued job, ret: " + postRet);
    // Get job
    resourceUrl = "http://localhost:" + ADMIN_PORT + "/clusters/" + clusterName + "/jobQueues/" + queueName + "/" + jBean1.name;
    getRet = AdminTestHelper.get(_gClient, resourceUrl);
    LOG.info("Got job: " + getRet);
    // Stop job queue
    resourceUrl = "http://localhost:" + ADMIN_PORT + "/clusters/" + clusterName + "/jobQueues/" + queueName;
    paraMap.put(JsonParameters.MANAGEMENT_COMMAND, TaskDriver.DriverCommand.stop.toString());
    postBody = String.format("%s=%s", JsonParameters.JSON_PARAMETERS, ClusterRepresentationUtil.ObjectToJson(paraMap));
    postRet = AdminTestHelper.post(_gClient, resourceUrl, postBody);
    LOG.info("Stopped job-queue, ret: " + postRet);
    // Delete a job
    resourceUrl = "http://localhost:" + ADMIN_PORT + "/clusters/" + clusterName + "/jobQueues/" + queueName + "/" + jBean2.name;
    AdminTestHelper.delete(_gClient, resourceUrl);
    LOG.info("Delete a job: ");
    // Resume job queue
    resourceUrl = "http://localhost:" + ADMIN_PORT + "/clusters/" + clusterName + "/jobQueues/" + queueName;
    paraMap.put(JsonParameters.MANAGEMENT_COMMAND, TaskDriver.DriverCommand.resume.toString());
    postBody = String.format("%s=%s", JsonParameters.JSON_PARAMETERS, ClusterRepresentationUtil.ObjectToJson(paraMap));
    postRet = AdminTestHelper.post(_gClient, resourceUrl, postBody);
    LOG.info("Resumed job-queue, ret: " + postRet);
    // Flush job queue
    paraMap.put(JsonParameters.MANAGEMENT_COMMAND, "flush");
    postBody = JsonParameters.JSON_PARAMETERS + "=" + ClusterRepresentationUtil.ObjectToJson(paraMap);
    postRet = AdminTestHelper.post(_gClient, resourceUrl, postBody);
    LOG.info("Flushed job-queue, ret: " + postRet);
    // clean up
    controller.syncStop();
    for (int i = 0; i < n; i++) {
        if (participants[i] != null && participants[i].isConnected()) {
            participants[i].syncStop();
        }
    }
    System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
}
Also used : Task(org.apache.helix.task.Task) MockTask(org.apache.helix.integration.task.MockTask) StateMachineEngine(org.apache.helix.participant.StateMachineEngine) MockParticipantManager(org.apache.helix.integration.manager.MockParticipantManager) HashMap(java.util.HashMap) MockTask(org.apache.helix.integration.task.MockTask) WorkflowBean(org.apache.helix.task.beans.WorkflowBean) ClusterStateVerifier(org.apache.helix.tools.ClusterStateVerifier) TaskCallbackContext(org.apache.helix.task.TaskCallbackContext) Date(java.util.Date) Yaml(org.yaml.snakeyaml.Yaml) ClusterControllerManager(org.apache.helix.integration.manager.ClusterControllerManager) JobBean(org.apache.helix.task.beans.JobBean) TaskFactory(org.apache.helix.task.TaskFactory) TaskStateModelFactory(org.apache.helix.task.TaskStateModelFactory) ZNRecord(org.apache.helix.ZNRecord) Test(org.testng.annotations.Test)

Example 60 with Yaml

use of org.yaml.snakeyaml.Yaml in project helix by apache.

the class Workflow method parse.

/**
 * Helper function to parse workflow from a generic {@link Reader}
 */
private static Workflow parse(Reader reader) throws Exception {
    Yaml yaml = new Yaml(new Constructor(WorkflowBean.class));
    WorkflowBean wf = (WorkflowBean) yaml.load(reader);
    Builder workflowBuilder = new Builder(wf.name);
    if (wf != null && wf.jobs != null) {
        for (JobBean job : wf.jobs) {
            if (job.name == null) {
                throw new IllegalArgumentException("A job must have a name.");
            }
            JobConfig.Builder jobConfigBuilder = JobConfig.Builder.from(job);
            jobConfigBuilder.setWorkflow(wf.name);
            workflowBuilder.addJob(job.name, jobConfigBuilder);
            if (job.parents != null) {
                for (String parent : job.parents) {
                    workflowBuilder.addParentChildDependency(parent, job.name);
                }
            }
        }
    }
    workflowBuilder.setWorkflowConfig(WorkflowConfig.Builder.from(wf).build());
    return workflowBuilder.build();
}
Also used : Constructor(org.yaml.snakeyaml.constructor.Constructor) JobBean(org.apache.helix.task.beans.JobBean) WorkflowBean(org.apache.helix.task.beans.WorkflowBean) Yaml(org.yaml.snakeyaml.Yaml)

Aggregations

Yaml (org.yaml.snakeyaml.Yaml)276 Map (java.util.Map)104 HashMap (java.util.HashMap)85 IOException (java.io.IOException)58 FileInputStream (java.io.FileInputStream)49 InputStream (java.io.InputStream)49 File (java.io.File)43 DumperOptions (org.yaml.snakeyaml.DumperOptions)42 Constructor (org.yaml.snakeyaml.constructor.Constructor)30 Test (org.junit.Test)26 ArrayList (java.util.ArrayList)25 FileNotFoundException (java.io.FileNotFoundException)22 SafeConstructor (org.yaml.snakeyaml.constructor.SafeConstructor)22 List (java.util.List)21 Writer (java.io.Writer)18 Path (java.nio.file.Path)17 LinkedHashMap (java.util.LinkedHashMap)17 Reader (java.io.Reader)16 Properties (java.util.Properties)14 InputStreamReader (java.io.InputStreamReader)13