Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@
<artifactId>jackson-databind</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.vdurmont</groupId>
<artifactId>semver4j</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.hk2.external</groupId>
<artifactId>javax.inject</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
*/
package com.ericsson.eiffel.remrem.semantics.clone;

import java.io.File;
import java.io.IOException;
import java.net.Authenticator;
import java.net.InetSocketAddress;
Expand All @@ -23,10 +22,12 @@
import java.net.ProxySelector;
import java.net.SocketAddress;
import java.net.URI;
import java.util.ArrayList;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;

import org.apache.commons.io.FileUtils;
Expand Down Expand Up @@ -64,17 +65,17 @@ public class PrepareLocalEiffelSchemas {
* @param localEiffelRepoPath
* destination path to clone the repo.
*/
private void cloneEiffelRepo(final String repoURL, final String branch, final File localEiffelRepoPath) {
private void cloneEiffelRepo(final String repoURL, final String branch, final Path localEiffelRepoPath) {
Git localGitRepo = null;

// checking for repository exists or not in the localEiffelRepoPath
try {
if (!localEiffelRepoPath.exists()) {
if (Files.notExists(localEiffelRepoPath)) {
// cloning github repository by using URL and branch name into local
localGitRepo = Git.cloneRepository().setURI(repoURL).setBranch("master").setDirectory(localEiffelRepoPath).call();
localGitRepo = Git.cloneRepository().setURI(repoURL).setBranch("master").setDirectory(localEiffelRepoPath.toFile()).call();
} else {
// If required repository already exists
localGitRepo = Git.open(localEiffelRepoPath);
localGitRepo = Git.open(localEiffelRepoPath.toFile());

// Reset to normal if uncommitted changes are present
localGitRepo.reset().call();
Expand Down Expand Up @@ -152,12 +153,12 @@ public PasswordAuthentication getPasswordAuthentication() {
* @param eiffelRepoPath
* local eiffel repository url
*/
private void copyOperationSchemas(final String operationsRepoPath, final String eiffelRepoPath) {
final File operationSchemas = new File(operationsRepoPath + File.separator + EiffelConstants.SCHEMA_LOCATION);
final File eiffelSchemas = new File(eiffelRepoPath + File.separator + EiffelConstants.SCHEMA_LOCATION);
if (operationSchemas.isDirectory()) {
private void copyOperationSchemas(final Path operationsRepoPath, final Path eiffelRepoPath) {
final Path operationSchemas = operationsRepoPath.resolve(EiffelConstants.SCHEMA_LOCATION);
final Path eiffelSchemas = eiffelRepoPath.resolve(EiffelConstants.SCHEMA_LOCATION);
if (Files.isDirectory(operationSchemas)) {
try {
FileUtils.copyDirectory(operationSchemas, eiffelSchemas);
FileUtils.copyDirectory(operationSchemas.toFile(), eiffelSchemas.toFile());
} catch (IOException e) {
System.out.println("Exception occurred while copying schemas from operations repository to eiffel repository");
e.printStackTrace();
Expand All @@ -176,9 +177,8 @@ public static void main(String[] args) throws IOException {
final String operationRepoUrl = args[2];
final String operationRepoBranch = args[3];

final File localEiffelRepoPath = new File(System.getProperty(EiffelConstants.USER_HOME) + File.separator + EiffelConstants.EIFFEL);
final File localOperationsRepoPath = new File(
System.getProperty(EiffelConstants.USER_HOME) + File.separator + EiffelConstants.OPERATIONS_REPO_NAME);
final Path localEiffelRepoPath = EiffelConstants.USER_HOME.resolve(EiffelConstants.EIFFEL);
final Path localOperationsRepoPath = EiffelConstants.USER_HOME.resolve(EiffelConstants.OPERATIONS_REPO_NAME);

// Clone Eiffel Repo from GitHub
prepareLocalSchema.cloneEiffelRepo(eiffelRepoUrl, eiffelRepoBranch, localEiffelRepoPath);
Expand All @@ -187,24 +187,18 @@ public static void main(String[] args) throws IOException {
prepareLocalSchema.cloneEiffelRepo(operationRepoUrl, operationRepoBranch, localOperationsRepoPath);

//Copy operations repo Schemas to location where Eiffel repo schemas available
prepareLocalSchema.copyOperationSchemas(localOperationsRepoPath.getAbsolutePath(), localEiffelRepoPath.getAbsolutePath());
prepareLocalSchema.copyOperationSchemas(localOperationsRepoPath.toAbsolutePath(), localEiffelRepoPath.toAbsolutePath());

// Read and Load JsonSchemas from Cloned Directory
final LocalRepo localRepo = new LocalRepo(localEiffelRepoPath);
localRepo.readSchemas();

final ArrayList<String> jsonEventNames = localRepo.getJsonEventNames();
final ArrayList<File> jsonEventSchemas = localRepo.getJsonEventSchemas();

// Schema changes
// Schema changes
final SchemaFile schemaFile = new SchemaFile();

// Iterate the Each jsonSchema file to Add and Modify the necessary properties
if (jsonEventNames != null && jsonEventSchemas != null) {
for (int i = 0; i < jsonEventNames.size(); i++) {
schemaFile.modify(jsonEventSchemas.get(i), jsonEventNames.get(i));
}
// Iterate over available input schemas and create new and patched files
for (Map.Entry<String, Path> event : localRepo.getJsonEventSchemas().entrySet()) {
schemaFile.modify(event.getValue().toFile(), event.getKey());
}

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
*/
package com.ericsson.eiffel.remrem.semantics.schemas;

import java.nio.file.Path;
import java.nio.file.Paths;

/**
* This is a constants class used in other classes
*
Expand Down Expand Up @@ -41,13 +44,13 @@ public final class EiffelConstants {
public static String COM_ERICSSON_EIFFEL_SEMANTICS_EVENTS = "com.ericsson.eiffel.semantics.events.";
public static String TYPE = "type";
public static String OBJECTTYPE = "object";
public static String INPUT_EIFFEL_SCHEMAS = "src\\main\\resources\\schemas\\input";
public static Path INPUT_EIFFEL_SCHEMAS = Paths.get("src", "main", "resources", "schemas", "input");
public static String EIFFEL = "eiffel";
public static String OPERATIONS_REPO_NAME = "eiffel-operations-extension";
public static String USER_DIR = System.getProperty("user.dir");
public static String SCHEMA_LOCATION = "\\schemas";
public static Path USER_DIR = Paths.get(System.getProperty("user.dir"));
public static Path SCHEMA_LOCATION = Paths.get("schemas");
public static String JSON_MIME_TYPE = ".json";
public static String USER_HOME = "user.home";
public static Path USER_HOME = Paths.get(System.getProperty("user.home"));
public static String ACTIVITY = "activity";
public static String ARTIFACT = "artifact";
public static String SERVICE = "service";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,16 @@
*/
package com.ericsson.eiffel.remrem.semantics.schemas;

import java.io.File;
import com.vdurmont.semver4j.Semver;
import java.io.IOException;
import java.util.ArrayList;

import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.StreamSupport;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;

/**
* This class is used to Iterate over the Eiffel schemas that are cloned from
Expand All @@ -28,11 +33,10 @@
*
*/
public class LocalRepo {
private ArrayList<File> jsonEventSchemas;
private File localSchemasPath;
private ArrayList<String> jsonEventNames;
private final Map<String, Path> jsonEventSchemas = new HashMap<>();
private Path localSchemasPath;

public LocalRepo(File localSchemasPath) {
public LocalRepo(Path localSchemasPath) {
this.localSchemasPath = localSchemasPath;
}

Expand All @@ -41,16 +45,13 @@ public LocalRepo(File localSchemasPath) {
* Repo
*/

public void readSchemas() {
public void readSchemas() throws IOException {
try {
FileUtils.cleanDirectory(new File(EiffelConstants.USER_DIR + File.separator + EiffelConstants.INPUT_EIFFEL_SCHEMAS));
FileUtils.cleanDirectory(EiffelConstants.USER_DIR.resolve(EiffelConstants.INPUT_EIFFEL_SCHEMAS).toFile());
} catch (IOException e) {
e.printStackTrace();
}
jsonEventNames = new ArrayList<String>();
jsonEventSchemas = new ArrayList<File>();
String filePath = localSchemasPath + EiffelConstants.SCHEMA_LOCATION;
loadEiffelSchemas(filePath, "");
loadEiffelSchemas(localSchemasPath.resolve(EiffelConstants.SCHEMA_LOCATION));
}

/**
Expand All @@ -60,32 +61,26 @@ public void readSchemas() {
* @param jsonFilePath
* - This parameter is used to pass Location of the Schemas
* Directory
* @param directoryName
* - This parameter is used to rename the File with corresponding
* event name.
*
*/
private void loadEiffelSchemas(String jsonFilePath, String directoryName) {
File file = new File(jsonFilePath);
File[] files = file.listFiles();
for (File jsonFile : files) {
if (jsonFile.isDirectory()) {
loadEiffelSchemas(jsonFile.getAbsolutePath(), jsonFile.getName());
} else {
jsonEventNames.add(directoryName);
jsonEventSchemas.add(jsonFile);
private void loadEiffelSchemas(final Path jsonFilePath) throws IOException {
try (DirectoryStream<Path> schemaDirStream =
Files.newDirectoryStream(jsonFilePath, Files::isDirectory)) {
for (Path eventDir : schemaDirStream) {
try (DirectoryStream<Path> eventDirStream =
Files.newDirectoryStream(eventDir, file -> file.toString().endsWith(".json"))) {
// Turn the filenames into versions and find the greatest version
Semver latestSchemaVersion = StreamSupport.stream(eventDirStream.spliterator(), false)
.map(path -> new Semver(FilenameUtils.removeExtension(path.getFileName().toString())))
.max(Semver::compareTo)
.get();
jsonEventSchemas.put(eventDir.getFileName().toString(),
eventDir.resolve(latestSchemaVersion.toString() + ".json"));
}
}
}
}

public ArrayList<File> getJsonEventSchemas() {
public Map<String, Path> getJsonEventSchemas() {
return jsonEventSchemas;
}

public ArrayList<String> getJsonEventNames() {
return jsonEventNames;
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.io.File;
import java.io.FileWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Iterator;
import java.util.Map.Entry;
Expand Down Expand Up @@ -236,16 +237,15 @@ private void addingItemsProperties(String elementName, JsonElement jsonValue, Js
* an input parameter to this method
*/
public void createNewInputJsonSchema(String jsonFileName, JsonObject jsonObject) {
String currentWorkingDir = EiffelConstants.USER_DIR;
FileWriter writer = null;
String copyFilePath = currentWorkingDir + File.separator + EiffelConstants.INPUT_EIFFEL_SCHEMAS;
String newFileName = copyFilePath + File.separator + jsonFileName + EiffelConstants.JSON_MIME_TYPE;
Path newFileName = EiffelConstants.USER_DIR.resolve(EiffelConstants.INPUT_EIFFEL_SCHEMAS)
.resolve(jsonFileName + EiffelConstants.JSON_MIME_TYPE);
Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonParser jp = new JsonParser();
JsonElement je = jp.parse(jsonObject.toString());
String prettyJsonString = gson.toJson(je);
try {
writer = new FileWriter(newFileName);
writer = new FileWriter(newFileName.toFile());
writer.write(prettyJsonString);
} catch (Exception e) {
e.printStackTrace();
Expand Down