Search in sources :

Example 1 with InsightsTools

use of com.cognizant.devops.platformdal.outcome.InsightsTools in project Insights by CognizantOneDevOps.

the class OutcomeServiceTest method testSaveOutcomeDefinitionRecord.

@Test(priority = 1)
public void testSaveOutcomeDefinitionRecord() throws InsightsCustomException {
    try {
        InsightsTools insightsMilestoneTools = outComeConfigDAL.getOutComeByToolName(toolName);
        if (insightsMilestoneTools == null) {
            prepareRequestData();
            insightsMilestoneTools = outComeConfigDAL.getOutComeByToolName(toolName);
        }
        int toolId = insightsMilestoneTools.getId();
        saveOutcomeJson = saveOutcomeJson.replace("toolNameeee1", String.valueOf(toolId));
        JsonObject saveOutcomeConfigjson = outcomeConfigController.saveOutcomeConfig(saveOutcomeJson);
        Assert.assertNotNull(saveOutcomeConfigjson);
        if (saveOutcomeConfigjson.has(MilestoneConstants.STATUS) && saveOutcomeConfigjson.get(MilestoneConstants.STATUS).getAsString().equalsIgnoreCase(PlatformServiceConstants.SUCCESS)) {
            JsonObject outcomeJsonList = outcomeConfigController.getAllActiveOutcome();
            List<JsonObject> outcomeList = gson.fromJson(outcomeJsonList.get("data"), new TypeToken<List<JsonObject>>() {
            }.getType());
            Assert.assertTrue(outcomeList.stream().anyMatch(outcomeJson -> outcomeJson.get("outcomeName").getAsString().equalsIgnoreCase(outcomeNameString)));
            Assert.assertTrue(saveOutcomeConfigjson.get("status").getAsString().equalsIgnoreCase(PlatformServiceConstants.SUCCESS));
        } else {
            Assert.fail(" Save outcome configure has issue  ");
        }
    } catch (AssertionError e) {
        Assert.fail(e.getMessage());
    } catch (Exception e) {
        log.error(e);
        Assert.fail(e.getMessage());
    }
}
Also used : OutComeConfigDAL(com.cognizant.devops.platformdal.outcome.OutComeConfigDAL) OutComeController(com.cognizant.devops.platformservice.outcome.controller.OutComeController) AfterClass(org.testng.annotations.AfterClass) JsonObject(com.google.gson.JsonObject) TypeToken(com.google.gson.reflect.TypeToken) BeforeClass(org.testng.annotations.BeforeClass) Test(org.testng.annotations.Test) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) InsightsTools(com.cognizant.devops.platformdal.outcome.InsightsTools) List(java.util.List) PlatformServiceConstants(com.cognizant.devops.platformcommons.constants.PlatformServiceConstants) Logger(org.apache.logging.log4j.Logger) MileStoneServiceImpl(com.cognizant.devops.platformservice.milestone.service.MileStoneServiceImpl) Gson(com.google.gson.Gson) MilestoneConstants(com.cognizant.devops.platformcommons.constants.MilestoneConstants) InsightsCustomException(com.cognizant.devops.platformcommons.exception.InsightsCustomException) Assert(org.junit.Assert) LogManager(org.apache.logging.log4j.LogManager) ApplicationConfigCache(com.cognizant.devops.platformcommons.config.ApplicationConfigCache) InsightsTools(com.cognizant.devops.platformdal.outcome.InsightsTools) TypeToken(com.google.gson.reflect.TypeToken) JsonObject(com.google.gson.JsonObject) InsightsCustomException(com.cognizant.devops.platformcommons.exception.InsightsCustomException) Test(org.testng.annotations.Test)

Example 2 with InsightsTools

use of com.cognizant.devops.platformdal.outcome.InsightsTools in project Insights by CognizantOneDevOps.

the class AgentManagementServiceImpl method registerAgent.

@Override
public String registerAgent(String toolName, String agentVersion, String osversion, String configDetails, String trackingDetails, boolean vault, boolean isWebhook, String type) throws InsightsCustomException {
    try {
        String agentId = null;
        Gson gson = new Gson();
        JsonElement jelement = gson.fromJson(configDetails.trim(), JsonElement.class);
        JsonObject json = jelement.getAsJsonObject();
        String labelName = getLabelName(configDetails);
        Date updateDate = Timestamp.valueOf(LocalDateTime.now());
        agentId = getAndValidateAgentId(toolName, json);
        json.addProperty("osversion", osversion);
        json.addProperty("agentVersion", agentVersion);
        json.addProperty("toolName", toolName.toUpperCase());
        json.addProperty("labelName", labelName);
        json.get(PlatformServiceConstants.SUBSCRIBE).getAsJsonObject().addProperty("agentCtrlQueue", agentId);
        if (isWebhook) {
            String webhookSubscribeQueue = AgentCommonConstant.WEBHOOK_QUEUE_CONSTANT + agentId;
            json.get(PlatformServiceConstants.SUBSCRIBE).getAsJsonObject().addProperty("webhookPayloadDataQueue", webhookSubscribeQueue);
            json.addProperty(AgentCommonConstant.WEBHOOK_ENABLED, true);
        } else if (type.equalsIgnoreCase(AgentCommonConstant.ROI_AGENT)) {
            InsightsTools configs = outComeConfigDAL.getOutComeByToolName(toolName.toUpperCase());
            String communicationQueue = configs.getAgentCommunicationQueue();
            json.get(PlatformServiceConstants.SUBSCRIBE).getAsJsonObject().addProperty("roiExecutionQueue", communicationQueue);
            json.addProperty(AgentCommonConstant.IS_ROI_AGENT, true);
        }
        /**
         * Create agent based folder and complete basic steps using agent instance 1.
         * Create folder with instance id (agent key) 2. Copy all files from agent
         * folder under instance id folder 3. Replace __AGENT_KEY__ with instance id in
         * service file based on OS 4. Rename InSights<agentName>Agent.sh to
         * instanceId.sh name 5. Use new path in rest of the steps for agent
         * registration
         */
        setupAgentInstanceCreation(toolName, osversion, agentId, isWebhook);
        // Update tracking.json file
        if (!trackingDetails.isEmpty()) {
            JsonElement trackingJsonElement = gson.fromJson(trackingDetails.trim(), JsonElement.class);
            JsonObject trackingDetailsJson = trackingJsonElement.getAsJsonObject();
            updateTrackingJson(toolName, trackingDetailsJson, agentId);
        }
        // Store secrets to vault based on agentsSecretDetails in config.json
        if (vault && ApplicationConfigProvider.getInstance().getVault().isVaultEnable()) {
            log.debug("-- Store secrets to vault for Agent {} --", agentId);
            Map<String, String> dataMap = getToolbasedSecret(json, agentId);
            prepareSecret(agentId, dataMap);
        } else if (vault && !ApplicationConfigProvider.getInstance().getVault().isVaultEnable()) {
            throw new InsightsCustomException("Please enable vault on servre side.");
        }
        // Create zip/tar file with updated config.json Zipping back the agent folder
        Path agentZipPath = updateAgentConfig(toolName, json, agentId);
        byte[] data = Files.readAllBytes(agentZipPath);
        String fileName = agentId + AgentCommonConstant.ZIPEXTENSION;
        // Sending the packet in Rabbit MQ
        sendAgentPackage(data, AGENTACTION.REGISTER.name(), fileName, agentId, toolName, osversion);
        performAgentAction(agentId, toolName, osversion, AGENTACTION.START.name(), agentId);
        // Delete tracking.json
        if (!trackingDetails.isEmpty()) {
            deleteTrackingJson(agentId);
        }
        if (agentZipPath.toFile().exists()) {
            try {
                FileUtils.deleteDirectory(Paths.get(fileUnzipPath + File.separator + toolName).toFile());
                FileUtils.deleteDirectory(Paths.get(fileUnzipPath + File.separator + agentId).toFile());
                Files.delete(agentZipPath);
            } catch (Exception e) {
                log.error(e);
            }
        }
        // register agent in DB
        agentConfigDAL.saveAgentConfigFromUI(agentId, json.get("toolCategory").getAsString(), labelName, toolName, json, agentVersion, osversion, updateDate, vault, isWebhook);
        return AgentCommonConstant.SUCCESS;
    } catch (Exception e) {
        log.error("Error while registering agent {}", toolName, e);
        throw new InsightsCustomException(e.getMessage());
    }
}
Also used : Path(java.nio.file.Path) InsightsTools(com.cognizant.devops.platformdal.outcome.InsightsTools) InsightsCustomException(com.cognizant.devops.platformcommons.exception.InsightsCustomException) JsonElement(com.google.gson.JsonElement) Gson(com.google.gson.Gson) JsonObject(com.google.gson.JsonObject) Date(java.util.Date) TimeoutException(java.util.concurrent.TimeoutException) InsightsCustomException(com.cognizant.devops.platformcommons.exception.InsightsCustomException) IOException(java.io.IOException)

Example 3 with InsightsTools

use of com.cognizant.devops.platformdal.outcome.InsightsTools in project Insights by CognizantOneDevOps.

the class OutComeServiceImpl method saveOutcomeConfig.

@Override
public int saveOutcomeConfig(JsonObject configJson) throws InsightsCustomException {
    String outcomeName = configJson.get("outcomeName").getAsString();
    InsightsOutcomeTools outcomeConfig = outComeConfigDAL.getOutComeConfigByName(outcomeName);
    if (outcomeConfig != null) {
        throw new InsightsCustomException("Outcome with given name already exists.");
    }
    InsightsOutcomeTools insightsOutcomeTools = new InsightsOutcomeTools();
    insightsOutcomeTools.setOutcomeName(outcomeName);
    insightsOutcomeTools.setOutcomeType(configJson.get("outcomeType").getAsString());
    if (configJson.has("toolConfigJson")) {
        insightsOutcomeTools.setConfigJson(configJson.get("toolConfigJson").getAsJsonObject().toString());
    }
    insightsOutcomeTools.setIsActive(configJson.get(CommonsAndDALConstants.ISACTIVE).getAsBoolean());
    insightsOutcomeTools.setMetricUrl(configJson.get("metricUrl").getAsString());
    InsightsTools insightsMilestoneTools = outComeConfigDAL.getOutComeByToolId(configJson.get("toolName").getAsInt());
    insightsOutcomeTools.setInsightsTools(insightsMilestoneTools);
    insightsOutcomeTools.setCreatedDate(InsightsUtils.getCurrentTimeInEpochMilliSeconds());
    insightsOutcomeTools.setRequestParameters(configJson.get("parameters").getAsJsonArray().toString());
    return outComeConfigDAL.saveOutcomeConfig(insightsOutcomeTools);
}
Also used : InsightsCustomException(com.cognizant.devops.platformcommons.exception.InsightsCustomException) InsightsTools(com.cognizant.devops.platformdal.outcome.InsightsTools) InsightsOutcomeTools(com.cognizant.devops.platformdal.outcome.InsightsOutcomeTools)

Example 4 with InsightsTools

use of com.cognizant.devops.platformdal.outcome.InsightsTools in project Insights by CognizantOneDevOps.

the class OutComeServiceImpl method getMileStoneTools.

@Override
public JsonArray getMileStoneTools() throws InsightsCustomException {
    try {
        List<InsightsTools> configs = outComeConfigDAL.getMileStoneTools();
        Gson gson = new Gson();
        JsonElement element = gson.toJsonTree(configs, new TypeToken<List<InsightsTools>>() {
        }.getType());
        if (!element.isJsonArray()) {
            throw new InsightsCustomException("Unable to parse Json");
        }
        return element.getAsJsonArray();
    } catch (Exception e) {
        log.error("Error getting milestone list ..", e);
        throw new InsightsCustomException(e.getMessage());
    }
}
Also used : InsightsTools(com.cognizant.devops.platformdal.outcome.InsightsTools) InsightsCustomException(com.cognizant.devops.platformcommons.exception.InsightsCustomException) JsonElement(com.google.gson.JsonElement) TypeToken(com.google.gson.reflect.TypeToken) Gson(com.google.gson.Gson) InsightsCustomException(com.cognizant.devops.platformcommons.exception.InsightsCustomException)

Example 5 with InsightsTools

use of com.cognizant.devops.platformdal.outcome.InsightsTools in project Insights by CognizantOneDevOps.

the class MilestoneOutcomeTestData method prepareRequestData.

void prepareRequestData() {
    InsightsTools newtool = new InsightsTools();
    newtool.setCategory("APPMONITORING");
    newtool.setToolName(toolName);
    newtool.setToolConfigJson("{}");
    newtool.setIsActive(Boolean.TRUE);
    newtool.setAgentCommunicationQueue("NEWRELIC_MILESTONE_EXECUTION");
    outComeConfigDAL.saveInsightsTools(newtool);
}
Also used : InsightsTools(com.cognizant.devops.platformdal.outcome.InsightsTools)

Aggregations

InsightsTools (com.cognizant.devops.platformdal.outcome.InsightsTools)6 InsightsCustomException (com.cognizant.devops.platformcommons.exception.InsightsCustomException)5 Gson (com.google.gson.Gson)4 JsonObject (com.google.gson.JsonObject)3 TypeToken (com.google.gson.reflect.TypeToken)3 ApplicationConfigCache (com.cognizant.devops.platformcommons.config.ApplicationConfigCache)2 MilestoneConstants (com.cognizant.devops.platformcommons.constants.MilestoneConstants)2 PlatformServiceConstants (com.cognizant.devops.platformcommons.constants.PlatformServiceConstants)2 OutComeConfigDAL (com.cognizant.devops.platformdal.outcome.OutComeConfigDAL)2 MileStoneServiceImpl (com.cognizant.devops.platformservice.milestone.service.MileStoneServiceImpl)2 OutComeController (com.cognizant.devops.platformservice.outcome.controller.OutComeController)2 JsonElement (com.google.gson.JsonElement)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 LogManager (org.apache.logging.log4j.LogManager)2 Logger (org.apache.logging.log4j.Logger)2 Assert (org.junit.Assert)2 AfterClass (org.testng.annotations.AfterClass)2 BeforeClass (org.testng.annotations.BeforeClass)2