Delete AppController.java

parent 3ff6333b
package eu.DECIDEh2020.appcontroller;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.io.*;
import java.util.Date;
import java.util.ListIterator;
/*
* Copyright (c) 2017 Fraunhofer FOKUS.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the
* Eclipse Public License version 2.0 which accompanies
* this distribution, and is available at
* https://opensource.org/licenses/EPL-2.0
*
* Contributors:
*
* Leo Li (Fraunhofer FOKUS) **Initially developed in the context of DECIDE EU project www.DECIDE-h2020.eu */
public class AppController {
private Git GitRepository;
private String Username;
private String Password;
private String ApplicationDesc;
private String RepositoryUrl;
private String Directory;
/**
* Constructor for the AppController. This constructor enables the possibility to use a username and a password for repositories which are private.
*
* @param Password The password which is used to access a git account.
* @param Username The username used to access a git account.
* @param ApplicationDesc The path of the application description file (not yet needed).
* @param RepositoryUrl The url of the to be used git repository (master repository).
* @param Directory The local directory to save the git repository content.
*/
public AppController(String Username, String Password, String ApplicationDesc, String RepositoryUrl, String Directory) {
this.ApplicationDesc = ApplicationDesc;
this.Username = Username;
this.Password = Password;
this.RepositoryUrl = RepositoryUrl;
this.Directory = Directory;
}
/**
* Constructor for the AppController. This constructor may only be used on public git repositories.
*
* @param ApplicationDesc The path of the application description file (not yet needed).
* @param RepositoryUrl The url of the to be used git repository (master repository).
* @param Directory The local directory to save the git repository content.
*/
public AppController(String ApplicationDesc, String RepositoryUrl, String Directory) {
this.ApplicationDesc = ApplicationDesc;
this.RepositoryUrl = RepositoryUrl;
this.Directory = Directory;
}
/**
* Clone the defined repository to the defined directory or if it already exists opens the repository. The method uses the git password and username to clone private repositories.
*
* @throws GitAPIException
* @throws IOException
*/
public void cloneRepositoryWithCred() throws GitAPIException, IOException {
if (!checkRepository()) {
GitRepository = Git.cloneRepository()
.setURI(RepositoryUrl)
.setDirectory(new File(Directory))
.setCredentialsProvider(new UsernamePasswordCredentialsProvider(Username, Password))
.setCloneAllBranches(true)
.call();
} else GitRepository = Git.open(new File(Directory));
}
/**
* Clone the defined repository to the defined directory or if it already exists opens the repository.
*
* @throws GitAPIException
* @throws IOException
*/
public void cloneRepository() throws GitAPIException, IOException {
if (!checkRepository()) {
GitRepository = Git.cloneRepository()
.setURI(RepositoryUrl)
.setDirectory(new File(Directory))
.setCloneAllBranches(true)
.call();
} else GitRepository = Git.open(new File(Directory));
}
/**
* Pull new changes from defined repository which is private and include them into the local directory.
*
* @throws GitAPIException
* @throws IOException
*/
public void pullRepoWithCred() throws GitAPIException, IOException {
GitRepository = Git.open(new File(Directory));
GitRepository
.pull()
.setCredentialsProvider(new UsernamePasswordCredentialsProvider(Username, Password))
.call();
}
/**
* Pull new changes from the defined repository and includes them in the local directory.
*
* @throws GitAPIException
* @throws IOException
*/
public void pullRepo() throws GitAPIException, IOException {
GitRepository = Git.open(new File(Directory));
GitRepository
.pull()
.call();
}
/**
* Push new content to the repository.
*
* @throws GitAPIException
* @throws IOException
*/
public void pushRepo() throws GitAPIException, IOException {
GitRepository = Git.open(new File(Directory));
GitRepository
.push()
.call();
}
/**
* Push new content to the private repository.
*
* @throws GitAPIException
* @throws IOException
*/
public void pushRepoWithCred() throws GitAPIException, IOException {
GitRepository = Git.open(new File(Directory));
GitRepository
.push()
.setCredentialsProvider(new UsernamePasswordCredentialsProvider(Username, Password))
.call();
}
/**
* Add the JSONHistory file to the index to be commited next.
*
* @throws GitAPIException
* @throws IOException
*/
public void addHistory() throws GitAPIException, IOException {
GitRepository = Git.open(new File(Directory));
GitRepository.add().addFilepattern("JSONHistory.json").call();
}
/**
* Commit the latest changes in the repository.
*
* @param message The commit message which has to be set by the commiter.
* @throws GitAPIException
* @throws IOException
*/
public void commitHistory(String message) throws GitAPIException, IOException {
GitRepository = Git.open(new File(Directory));
GitRepository.commit().setMessage(message).call();
}
/**
* Check if the directory to save the master repository already exists.
*
* @return Boolean value if the set directory already exists.
*/
private boolean checkRepository() {
File folder = new File(Directory);
if (folder.exists()) {
if (folder.isDirectory()) {
String[] content = folder.list();
for (String file : content) {
if (file.equals(".git"))
return true;
}
}
}
return false;
}
/**
* Create a new SLABreach object and add it to the last saved deployment of the defined service in the existing history.
*
* @param serviceId A string to identify the microservice where a breach occurred.
* @param slaBreach A JSONObject which represents the new SLABreach.
* @throws IOException
*/
public void newBreach(String serviceId, JSONObject slaBreach) throws IOException {
JSONArray history = getHistory();
JSONObject microservice;
JSONArray entry;
ListIterator listIterator = history.listIterator();
for (int i = 0; i < history.size(); i++) {
JSONObject service = (JSONObject) listIterator.next();
if (service.keySet().contains(serviceId)) {
microservice = service;
history.remove(service);
entry = (JSONArray) microservice.get(serviceId);
JSONObject date = (JSONObject) entry.get(entry.size() - 1);
entry.remove(entry.get(entry.size() - 1));
JSONArray breaches = (JSONArray) date.get("sla_breaches");
breaches.add(slaBreach);
JSONObject changed = new JSONObject();
changed.put("date", date.get("date"));
changed.put("deployed_on", date.get("deployed_on"));
changed.put("sla_breaches", breaches);
entry.add(changed);
microservice.put(serviceId, entry);
history.add(microservice);
break;
}
}
try (FileWriter file = new FileWriter(Directory + "/JSONHistory.json")) {
file.write(history.toString());
}
}
/**
* Create a new SLABreach object and add it to the defined dated deployment of the defined service in the existing history.
*
* @param date A string containing the date of the deployment to find the deployment where a new SLABreach is to set.
* @param serviceId The service id to identify the microservice where a new breach occurred.
* @param slaBreach A JSONObject which represents the new SLABreach.
*/
public void newBreach(String date, String serviceId, JSONObject slaBreach) throws IOException {
JSONArray history = getHistory();
JSONObject microservice = null;
JSONArray entry;
ListIterator listIterator = history.listIterator();
for (int i = 0; i < history.size(); i++) {
JSONObject service = (JSONObject) listIterator.next();
if (service.keySet().contains(serviceId)) {
microservice = service;
}
}
if (microservice != null) {
entry = (JSONArray) microservice.get(serviceId);
ListIterator listIterator1 = entry.listIterator();
for (int k = 0; k < entry.size(); k++) {
JSONObject holder = (JSONObject) listIterator1.next();
if (holder.values().contains(date)) {
history.remove(microservice);
entry.remove(holder);
JSONArray breaches = (JSONArray) holder.get("sla_breaches");
breaches.add(slaBreach);
JSONObject changed = new JSONObject();
changed.put("date", holder.get("date"));
changed.put("deployed_on", holder.get("deployed_on"));
changed.put("sla_breaches", breaches);
entry.add(changed);
microservice.put(serviceId, entry);
history.add(microservice);
break;
}
}
}
try (FileWriter file = new FileWriter(Directory + "/JSONHistory.json")) {
file.write(history.toString());
}
}
/**
* Create a new history entry in the existing history. If no history exists, create it aswell.
*
* @param serviceId The identifier of the deployed service.
* @param serviceProvider The Name/Identifier of the service provide used to deploy the service on.
*/
public void writeHistory(String serviceId, String serviceProvider) throws IOException {
JSONArray history = getHistory();
// DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
String date = String.valueOf(new Date().getTime());
JSONObject microservice = new JSONObject();
JSONArray entry = new JSONArray();
boolean found = false;
ListIterator listIterator = history.listIterator();
for (int i = 0; i < history.size(); i++) {
JSONObject service = (JSONObject) listIterator.next();
if (service.keySet().contains(serviceId)) {
microservice = service;
history.remove(service);
entry = (JSONArray) microservice.get(serviceId);
entry.add(newEntry(date, serviceProvider));
microservice.put(serviceId, entry);
history.add(microservice);
found = true;
break;
}
}
if (!found) {
JSONObject his = (newEntry(date, serviceProvider));
entry.add(his);
microservice.put(serviceId, entry);
history.add(microservice);
}
try (FileWriter file = new FileWriter(Directory + "/JSONHistory.json")) {
file.write(history.toString());
}
}
/**
* Create a new entry to be added into the history.
*
* @return JSONObject which represents a possibly newly created entry for the history
*/
private JSONObject newEntry(String date, String serviceProvider) {
JSONObject history = new JSONObject();
history.put("date", date);
history.put("deployed_on", serviceProvider);
history.put("sla_breaches", new JSONArray());
return history;
}
/**
* Retrieve an existing or create a new history.
*
* @return JSONArray which gives a representation of the existing history or returns a new empty one
*/
private JSONArray getHistory() {
File folder = new File(Directory);
JSONParser parser = new JSONParser();
if (folder.exists()) {
if (folder.isDirectory()) {
String[] content = folder.list();
for (String file : content) {
if (file.contains("JSONHistory")) {
try {
return (JSONArray) parser.parse(new FileReader(Directory + "/" + file));
} catch (ParseException | IOException e) {
e.printStackTrace();
}
}
}
File history = new File(Directory + "/JSONHistory.json");
}
}
return new JSONArray();
}
public Git getGitRepository() {
return GitRepository;
}
public void setGitRepository(Git gitRepository) {
GitRepository = gitRepository;
}
public String getRepositoryUrl() {
return RepositoryUrl;
}
public void setRepositoryUrl(String repositoryUrl) {
RepositoryUrl = repositoryUrl;
}
public String getDirectory() {
return Directory;
}
public void setDirectory(String directory) {
Directory = directory;
}
public String getUsername() {
return Username;
}
public void setUsername(String username) {
Username = username;
}
public String getApplicationDesc() {
return ApplicationDesc;
}
public void setApplicationDesc(String applicationDesc) {
ApplicationDesc = applicationDesc;
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment