AlertGeneration.java Source code

Java tutorial

Introduction

Here is the source code for AlertGeneration.java

Source

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

import org.yaml.snakeyaml.Yaml;

import com.google.gson.JsonObject;
import com.jayway.restassured.RestAssured;
import com.jayway.restassured.http.ContentType;
import com.jayway.restassured.response.Response;

/*
 * This computer program is the confidential information and proprietary trade
 * secret of VistaraIT, Inc. Possessions and use of this program must
 * conform strictly to the license agreement between the user and
 * VistaraIT, Inc., and receipt or possession does not convey any rights
 * to divulge, reproduce, or allow others to use this program without specific
 * written authorization of VistaraIT, Inc.
 * Copyright  2015 VistaraIT, Inc. All Rights Reserved.
*/

/**
 * @author Venky
 *
 */
public class AlertGeneration {

    @SuppressWarnings("unchecked")
    public static void main(String[] args) throws InterruptedException {
        Yaml yaml = new Yaml();
        InputStream ios = AlertGeneration.class.getClassLoader().getResourceAsStream("properties.yml");
        Map<String, Object> result = (Map<String, Object>) yaml.load(ios);

        String apiBaseURL = (String) result.get("api.base.url");
        String apiClientKey = (String) result.get("api.client.key");
        String apiClientSecret = (String) result.get("api.client.secret");
        String apiGrantType = (String) result.get("api.grant.type");

        long clientId = new Long((Integer) result.get("client.id")).longValue();

        JsonObject json = new JsonObject();
        json.addProperty("subject", "Test Alert Subject");
        json.addProperty("description", "Test alert description");
        json.addProperty("serviceName", "system.ping.rta");
        json.addProperty("app", "Vistara");

        JsonObject device = new JsonObject();
        device.addProperty("hostName", "meena");
        device.addProperty("resourceUUID", "e32f9e6a-8909-4181-8684-910e82bd6f33");
        json.add("device", device);

        RestAssured.useRelaxedHTTPSValidation();
        RestAssured.baseURI = apiBaseURL;
        String accessToken = generateAccessToken(apiBaseURL, apiClientKey, apiClientSecret, apiGrantType);

        for (int i = 1; i <= 600; i++) {
            try {
                Calendar cal = Calendar.getInstance();
                int currentSecond = cal.get(Calendar.SECOND);

                if ((currentSecond / 5) % 2 == 0) {
                    json.addProperty("currentState", "Ok");
                } else {
                    json.addProperty("currentState", "Critical");
                }

                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                json.addProperty("alertTime", formatter.format(cal.getTime()));
                System.out.println("Input payload " + i + ":" + json.toString());

                Response response = RestAssured.given().auth().oauth2(accessToken).contentType(ContentType.JSON)
                        .body(json.toString()).post("/api/v2/tenants/client_" + clientId + "/alert");
                System.out.println("Response: " + response.getStatusCode() + ",Output: " + response.prettyPrint());
                Thread.sleep(5000);
            } catch (Exception e) {
                e.printStackTrace();
                accessToken = generateAccessToken(apiBaseURL, apiClientKey, apiClientSecret, apiGrantType);
            }
        }
    }

    private static String generateAccessToken(String apiBaseURL, String apiClientKey, String apiClientSecret,
            String apiGrantType) {
        RestAssured.useRelaxedHTTPSValidation();
        RestAssured.baseURI = apiBaseURL;
        String accessToken = RestAssured.given().formParam("grant_type", apiGrantType)
                .formParam("client_id", apiClientKey).formParam("client_secret", apiClientSecret)
                .post("/auth/oauth/token").body().jsonPath().getString("access_token");
        System.out.println("accessToken: " + accessToken + ", Time: " + new Date().toString());
        return accessToken;
    }
}