Skip to content

Commit

Permalink
#169 Feature: Migrate aiops references in PDP python projects
Browse files Browse the repository at this point in the history
  - Refactor the PDP package.
  - Refactor all the child directories, classes, and methods.
  - Corrected all the imports inside the velocity templates to ensure a successful build of PDP Python.
  - Baton migration scripts for automated migrations from legacy package names to the new aiSSEMBLE landscape.
  • Loading branch information
habibimoiz committed Jun 27, 2024
1 parent 70134c6 commit fdf4196
Show file tree
Hide file tree
Showing 18 changed files with 140 additions and 211 deletions.
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,22 @@
public class DockerGenerator extends AbstractResourcesGenerator {
/*--~-~-~~
* Usages:
* | Target | Template | Generated File |
* |---------------------------------------------|------------------------------------------------------------------------|------------------------------------------------|
* | airflowDockerFile | general-docker/airflow.docker.file.vm | docker/Dockerfile |
* | dataAccessDockerFile | general-docker/data.access.docker.file.vm | docker/Dockerfile |
* | itDockerFile | general-docker/it.java.docker.file.vm | docker/Dockerfile |
* | jenkinsControllerDockerFile | general-docker/jenkins.controller.docker.file.vm | docker/Dockerfile |
* | mlflowDockerFile | general-docker/mlflow.docker.file.vm | docker/Dockerfile |
* | policyDecisionPointAiopsSecurityProperties | general-docker/policy-decision-point.aissemble-security.properties.vm | krausening/base/aissemble-security.properties |
* | policyDecisionPointDockerFile | general-docker/policy.decision.point.docker.file.vm | docker/Dockerfile |
* | quarkusServiceDockerFile | general-docker/quarkus.service.docker.file.vm | docker/Dockerfile |
* | sparkWorkerDockerFile | general-docker/spark-worker.docker.file.vm | docker/Dockerfile |
* | vaultDockerFile | general-docker/vault.docker.file.vm | docker/Dockerfile |
* | versioningAuthProperties | general-docker/versioning.auth.properties.vm | krausening/base/auth.properties |
* | versioningDockerFile | general-docker/versioning.docker.file.vm | docker/Dockerfile |
* | versioningMavenSettings | general-docker/versioning.docker.maven.settings.xml.vm | config/settings.xml |
* | versioningModelPomFile | general-docker/versioning.docker.model.pom.xml.vm | config/model-pom.xml |
* | Target | Template | Generated File |
* |-------------------------------------------------|------------------------------------------------------------------------|------------------------------------------------|
* | airflowDockerFile | general-docker/airflow.docker.file.vm | docker/Dockerfile |
* | dataAccessDockerFile | general-docker/data.access.docker.file.vm | docker/Dockerfile |
* | itDockerFile | general-docker/it.java.docker.file.vm | docker/Dockerfile |
* | jenkinsControllerDockerFile | general-docker/jenkins.controller.docker.file.vm | docker/Dockerfile |
* | mlflowDockerFile | general-docker/mlflow.docker.file.vm | docker/Dockerfile |
* | policyDecisionPointAissembleSecurityProperties | general-docker/policy-decision-point.aissemble-security.properties.vm | krausening/base/aissemble-security.properties |
* | policyDecisionPointDockerFile | general-docker/policy.decision.point.docker.file.vm | docker/Dockerfile |
* | quarkusServiceDockerFile | general-docker/quarkus.service.docker.file.vm | docker/Dockerfile |
* | sparkWorkerDockerFile | general-docker/spark-worker.docker.file.vm | docker/Dockerfile |
* | vaultDockerFile | general-docker/vault.docker.file.vm | docker/Dockerfile |
* | versioningAuthProperties | general-docker/versioning.auth.properties.vm | krausening/base/auth.properties |
* | versioningDockerFile | general-docker/versioning.docker.file.vm | docker/Dockerfile |
* | versioningMavenSettings | general-docker/versioning.docker.maven.settings.xml.vm | config/settings.xml |
* | versioningModelPomFile | general-docker/versioning.docker.model.pom.xml.vm | config/model-pom.xml |
*/

/**
Expand Down
2 changes: 1 addition & 1 deletion foundation/foundation-mda/src/main/resources/profiles.json
Original file line number Diff line number Diff line change
Expand Up @@ -2137,7 +2137,7 @@
"name": "policyDecisionPointDockerFile"
},
{
"name": "policyDecisionPointAiopsSecurityProperties"
"name": "policyDecisionPointAissembleSecurityProperties"
}
]
},
Expand Down
2 changes: 1 addition & 1 deletion foundation/foundation-mda/src/main/resources/targets.json
Original file line number Diff line number Diff line change
Expand Up @@ -2328,7 +2328,7 @@
"overwritable": false
},
{
"name": "policyDecisionPointAiopsSecurityProperties",
"name": "policyDecisionPointAissembleSecurityProperties",
"templateName": "templates/general-docker/policy-decision-point.aissemble-security.properties.vm",
"outputFile": "krausening/base/aissemble-security.properties",
"generator": "com.boozallen.aiops.mda.generator.DockerGenerator",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,11 @@
import com.boozallen.aissemble.upgrade.migration.AbstractAissembleMigration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.technologybrewery.baton.BatonException;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;

import static com.boozallen.aissemble.upgrade.util.FileUtils.replaceLiteralInFile;
Expand All @@ -33,7 +30,11 @@ public class AiopsReferencePDPPythonMigration extends AbstractAissembleMigration

public static final Logger logger = LoggerFactory.getLogger(AiopsReferencePDPPythonMigration.class);
public static final Map<String, String> AIOPS_REFERENCE_PYTHON_PROPERTIES_FILE_PACKAGE_MAP = Map.of(
"policy-decision-point.aiops-security.properties.vm", "policy-decision-point.aissemble-security.properties.vm"
"policy-decision-point.aiops-security.properties.vm", "policy-decision-point.aissemble-security.properties.vm",
"aiops-security.properties", "aissemble-security.properties",
"aiops.authority","aissemble.authority",
"aiopskey","aissemblekey",
"/deployments/aiops-secure.jks","/deployments/aissemble-secure.jks"
);

/**
Expand All @@ -47,43 +48,45 @@ protected boolean shouldExecuteOnFile(File file) {

try {
if(file.getName().equals("aiops-security.properties")) {
BufferedReader aiopsReferencePythonPropertiesFileConfig = new BufferedReader((new FileReader(file)));

String lineOne;
while((lineOne = aiopsReferencePythonPropertiesFileConfig.readLine()) !=null && !shouldExecute) {
for (String key : AIOPS_REFERENCE_PYTHON_PROPERTIES_FILE_PACKAGE_MAP.keySet()) {
if (lineOne.contains(key)) {
shouldExecute = true;
}
}
}
shouldExecute = true;
} else {
BufferedReader aiopsReferencePythonDockerfileConfig = new BufferedReader((new FileReader(file)));

String lineTwo;
while((lineTwo = aiopsReferencePythonDockerfileConfig.readLine()) !=null && !shouldExecute) {
String line;
while((line = aiopsReferencePythonDockerfileConfig.readLine()) !=null && !shouldExecute) {
for (String key : AIOPS_REFERENCE_PYTHON_PROPERTIES_FILE_PACKAGE_MAP.keySet()) {
if (lineTwo.contains(key)) {
return true;
if (line.contains(key)) {
shouldExecute = true;
}
}
}
}
} catch (Exception e) {
} catch (IOException e) {
logger.error("Error in determining whether a 'AIOPS' renamed properties file requires a migration.");
throw new BatonException(e);
}

return shouldExecute;
}

private static void fileNameRefactor(Path oldPath, Path newPath) {
/**
* Performs the migration if the shouldExecuteOnFile() returns true.
* @param file file that will be getting refactored.
* @param refactoredName name in which the file will be refactored to.
* @return isRefactoredSuccessfully - Whether the file has been refactored successfully.
*/
private static boolean fileNameRefactor(File file, String refactoredName) {
boolean isRefactoredSuccessfully = false;
try {
Files.move(oldPath, newPath);
} catch (IOException e) {
logger.error("Error in refactoring the file in AiopsReferencePythonPropertiesFileMigration.");
throw new BatonException(e);
// Construct new file path
File renamedFile = new File(file.getParent(), refactoredName);

// Rename the file
isRefactoredSuccessfully = file.renameTo(renamedFile);
} catch (Exception e) {
logger.error("Error in refactoring aiops-security.properties.");
}

return isRefactoredSuccessfully;
}

/**
Expand All @@ -96,6 +99,10 @@ protected boolean performMigration(File file) {
boolean isMigrated = false;

try {
if(file.getName().equals("aiops-security.properties")) {
fileNameRefactor(file, "aissemble-security.properties");
}

if(file.getName().equals("aissemble-security.properties")) {
for (Map.Entry<String, String> entry : AIOPS_REFERENCE_PYTHON_PROPERTIES_FILE_PACKAGE_MAP.entrySet()) {
replaceLiteralInFile(file, entry.getKey(), entry.getValue());
Expand All @@ -108,7 +115,7 @@ protected boolean performMigration(File file) {
}
}
} catch (Exception e) {
throw new BatonException("Error in migrating renamed 'AIOPS' python properties file.", e);
logger.error("Error in migrating renamed 'AIOPS' python properties file.");
}
return isMigrated;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
import java.io.FileReader;

import java.util.Map;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import static com.boozallen.aissemble.upgrade.util.FileUtils.replaceLiteralInFile;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@
"fileSets": [
{
"includes": [
"*-docker/*/src/main/resources/krausening/base/*.properties",
"*-docker/*/src/main/resources/krausening/base/aiops-security.properties",
"*-docker/*/src/main/resources/krausening/base/aissemble-security.properties",
"*-docker/*/src/main/resources/docker/Dockerfile"
]
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.boozallen.aissemble.upgrade.migration.v1_8_0;

/*-
* #%L
* aiSSEMBLE::Foundation::Upgrade
* %%
* Copyright (C) 2021 Booz Allen
* %%
* This software package is licensed under the Booz Allen Public License. All Rights Reserved.
* #L%
*/

import com.boozallen.aissemble.upgrade.migration.AbstractAissembleMigration;
import com.boozallen.aissemble.upgrade.migration.AbstractMigrationTest;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import org.apache.commons.io.FileUtils;

import java.io.File;

import static org.junit.Assert.assertTrue;

public class AiopsReferencePDPPythonMigrationSteps extends AbstractMigrationTest {

private AbstractAissembleMigration migration;
private File validatedFile;
private static Boolean validateMigration(File original, File migrated) {
Boolean isMigrated = false;

try {
isMigrated = FileUtils.contentEquals(original, migrated);
} catch (Exception e) {
e.printStackTrace();
}
return isMigrated;
}

/**
* Function to check whether the migration is necessary.
* @param file file to check
* @return isFileNameSame - Boolean indicating whether the file names are the same.
*/

public static Boolean isAissembleSecurityProperties(File file) {
return file.getName().equals("aissemble-security.properties");
}

@Given("a Python implemented project policy decision point docker properties file is referencing aiops")
public void aPythonImplementedProjectPolicyDecisionPointDockerPropertiesFileIsReferencingAiops() {
testFile = getTestFile("v1_8_0/AiopsReferencePDPPythonMigration/migration/aiops-security.properties");
}

@When("the 1.8.0 aiops reference pdp python migration executes")
public void theAiopsReferencePdpPythonMigrationExecutes() {
migration = new AiopsReferencePDPPythonMigration();
performMigration(migration);
}

@Then("the properties file will be renamed to aissemble-security.properties")
public void thePropertiesFileWillBeRenamedToAissembleSecurityProperties() {
assertTrue("aissemble-securities.properties file was unabled to be renamed.", isAissembleSecurityProperties(getTestFile("v1_8_0/AiopsReferencePDPPythonMigration/migration/aissemble-security.properties")));
}

@Given("a Python implemented project policy decision point docker Dockerfile is referencing aiops in the target path of the properties file")
public void aPythonImplementedProjectPolicyDecisionPointDockerDockerfileIsReferencingAiopsInTheTargetPathOfThePropertiesFile() {
testFile = getTestFile("v1_8_0/AiopsReferencePDPPythonMigration/migration/Dockerfile");
}

@Then("the target path in the Dockerfile will be refactored to referenced aissemble-security.properties")
public void theTargetPathInTheDockerfileWillBeRefactoredToReferencedAissembleSecurityProperties() {
validatedFile = getTestFile("/v1_8_0/AiopsReferencePDPPythonMigration/validation/Dockerfile_Validated");
assertTrue("PDP Dockerfile is still referencing aiops in the target path.", validateMigration(testFile, validatedFile));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ public void theReferenceAreUpdatedToAissemble_core_filestoreFile_store_factory()
validatedFile = getTestFile("/v1_8_0/AiopsReferencePythonMigration/validation/IngestValidated.py");
assertTrue("Python modules are referencing an older package in imports.", validateMigration(testFile, validatedFile));
}
}
}
Loading

0 comments on commit fdf4196

Please sign in to comment.