All Downloads are FREE. Search and download functionalities are using the official Maven repository.

prerna.engine.impl.function.AbstractFunctionEngine Maven / Gradle / Ivy

The newest version!
package prerna.engine.impl.function;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.apache.commons.io.FileUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONObject;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import prerna.auth.utils.SecurityEngineUtils;
import prerna.engine.api.IEngine;
import prerna.engine.api.IFunctionEngine;
import prerna.engine.impl.SmssUtilities;
import prerna.io.connector.secrets.ISecrets;
import prerna.io.connector.secrets.SecretsFactory;
import prerna.util.Constants;
import prerna.util.EngineUtility;
import prerna.util.UploadUtilities;
import prerna.util.Utility;

public abstract class AbstractFunctionEngine implements IFunctionEngine {

	private static final Logger classLogger = LogManager.getLogger(AbstractFunctionEngine.class);

	protected String engineId;
	protected String engineName;

	protected String smssFilePath;
	protected Properties smssProp;

	protected String functionName;
	protected String functionDescription;
	protected List parameters;
	protected List requiredParameters;

	@Override
	public void open(String smssFilePath) throws Exception {
		setSmssFilePath(smssFilePath);
		open(Utility.loadProperties(smssFilePath));
	}

	@Override
	public void open(Properties smssProp) throws Exception {
		setSmssProp(smssProp);
		this.engineId = this.smssProp.getProperty(Constants.ENGINE);
		this.engineName = this.smssProp.getProperty(Constants.ENGINE_ALIAS);

		ISecrets secretStore = SecretsFactory.getSecretConnector();
		if(secretStore != null) {
			Map engineSecrets = secretStore.getEngineSecrets(getCatalogType(), this.engineId, this.engineName);
			if(engineSecrets == null || engineSecrets.isEmpty()) {
				classLogger.info("No secrets found for " + SmssUtilities.getUniqueName(this.engineName, this.engineId));
			} else {
				classLogger.info("Successfully pulled secrets for " + SmssUtilities.getUniqueName(this.engineName, this.engineId));
				this.smssProp.putAll(engineSecrets);
			}
		}

		if(!smssProp.containsKey(IFunctionEngine.NAME_KEY)) {
			throw new IllegalArgumentException("Must have key " + IFunctionEngine.NAME_KEY + " in SMSS");
		}
		if(!smssProp.containsKey(IFunctionEngine.DESCRIPTION_KEY)) {
			throw new IllegalArgumentException("Must have key " + IFunctionEngine.DESCRIPTION_KEY + " in SMSS");
		}

		this.functionName = smssProp.getProperty(IFunctionEngine.NAME_KEY);
		this.functionDescription = smssProp.getProperty(IFunctionEngine.DESCRIPTION_KEY);

		if(smssProp.containsKey(IFunctionEngine.PARAMETER_KEY)) {
			this.parameters = new Gson().fromJson(smssProp.getProperty(IFunctionEngine.PARAMETER_KEY), new TypeToken>() {}.getType());
		}

		if(smssProp.containsKey(IFunctionEngine.REQUIRED_PARAMETER_KEY)) {
			this.requiredParameters = new Gson().fromJson(smssProp.getProperty(IFunctionEngine.REQUIRED_PARAMETER_KEY), new TypeToken>() {}.getType());
		}
	}

	@Override
	public void delete() throws IOException {
		classLogger.debug("Delete function engine " + SmssUtilities.getUniqueName(this.engineName, this.engineId));
		try {
			this.close();
		} catch(IOException e) {
			classLogger.warn("Error occurred trying to close service engine");
			classLogger.error(Constants.STACKTRACE, e);
		}

		File engineFolder = new File(EngineUtility.getSpecificEngineBaseFolder(
				getCatalogType(), this.engineId, this.engineName)
				);
		try {
			FileUtils.deleteDirectory(engineFolder);
		} catch (IOException e) {
			classLogger.error(Constants.STACKTRACE, e);
		}

		classLogger.debug("Deleting smss " + this.smssFilePath);
		File smssFile = new File(this.smssFilePath);
		try {
			FileUtils.forceDelete(smssFile);
		} catch(IOException e) {
			classLogger.error(Constants.STACKTRACE, e);
		}

		// remove from DIHelper
		UploadUtilities.removeEngineFromDIHelper(this.engineId);
	}

	@Override
	public JSONObject getFunctionDefintionJson() {
		JSONObject json = new JSONObject();
		json.put("name", this.functionName);
		json.put("description", this.functionDescription);

		JSONObject parameterJSON = new JSONObject();
		if(this.parameters != null && !this.parameters.isEmpty()) {
			parameterJSON.put("type", "object");
			JSONObject propertiesJSON = new JSONObject();
			for(FunctionParameter fParam : this.parameters) {
				JSONObject thisPropJSON = new JSONObject();
				thisPropJSON.put("type", fParam.getParameterType());
				thisPropJSON.put("description", fParam.getParameterDescription());
				propertiesJSON.put(fParam.getParameterName(), thisPropJSON);
			}
			parameterJSON.put("properties", propertiesJSON);
		}
		json.put("parameters", parameterJSON);

		JSONArray requiredJSON = new JSONArray();
		if(this.requiredParameters != null && !this.requiredParameters.isEmpty()) {
			requiredJSON.put(this.requiredParameters);
		}
		json.put("required", requiredJSON);

		return json;
	}

	@Override
	public void setEngineId(String engineId) {
		this.engineId = engineId;
	}

	@Override
	public String getEngineId() {
		return this.engineId;
	}

	@Override
	public void setEngineName(String engineName) {
		this.engineName = engineName;
	}

	@Override
	public String getEngineName() {
		return this.engineName;
	}

	@Override
	public String getFunctionName() {
		return functionName;
	}

	@Override
	public void setFunctionName(String functionName) {
		this.functionName = functionName;
	}

	@Override
	public String getFunctionDescription() {
		return functionDescription;
	}

	@Override
	public void setFunctionDescription(String functionDescription) {
		this.functionDescription = functionDescription;
	}

	@Override
	public List getParameters() {
		return parameters;
	}

	@Override
	public void setParameters(List parameters) {
		this.parameters = parameters;
	}

	@Override
	public List getRequiredParameters() {
		return this.requiredParameters;
	}

	@Override
	public void setRequiredParameters(List requiredParameters) {
		this.requiredParameters = requiredParameters;
	}

	@Override
	public void setSmssFilePath(String smssFilePath) {
		this.smssFilePath = smssFilePath;
	}

	@Override
	public String getSmssFilePath() {
		return this.smssFilePath;
	}

	@Override
	public void setSmssProp(Properties smssProp) {
		this.smssProp = smssProp;
	}

	@Override
	public Properties getSmssProp() {
		return this.smssProp;
	}

	@Override
	public Properties getOrigSmssProp() {
		return this.smssProp;
	}

	@Override
	public CATALOG_TYPE getCatalogType() {
		return IEngine.CATALOG_TYPE.FUNCTION;
	}

	@Override
	public boolean holdsFileLocks() {
		return false;
	}

	/**
	 * 
	 */
	public Map buildFunctionEngineToolMap() {
		// Fetch metadata for the engine
		Map metadata = SecurityEngineUtils.getAggregateEngineMetadata(
				this.getEngineId(),
				Arrays.asList("description"),
				true
				);

		// Extract the description from metadata
		String description = (String) metadata.get("description");
		if (description == null) {
			description = "No description available.";
		}

		// Create the main map
		Map toolMap = new HashMap<>();
		toolMap.put("type", "function");

		// Create the function map
		Map functionMap = new HashMap<>();
		functionMap.put("name", "function_engine");
		functionMap.put("description", description);

		// Create the parameters map
		Map parametersMap = new HashMap<>();
		parametersMap.put("type", "object");

		// Create the properties map
		Map propertiesMap = new HashMap<>();

		// Add the id property
		Map idMap = new HashMap<>();
		idMap.put("type", "string");
		idMap.put("description", "The unique identifier for this function_engine used to call this specific engine");
		idMap.put("enum", Arrays.asList(this.getEngineId()));
		propertiesMap.put("id", idMap);

		// Add the map property
		Map mapMap = new HashMap<>();
		mapMap.put("type", "object");

		// Create the map properties map
		Map mapPropertiesMap = new HashMap<>();
		for (FunctionParameter param : this.getParameters()) {
			Map paramMap = new HashMap<>();
			paramMap.put("type", param.getParameterType());
			paramMap.put("description", param.getParameterDescription());
			mapPropertiesMap.put(param.getParameterName(), paramMap);
		}
		mapMap.put("properties", mapPropertiesMap);
		mapMap.put("required", this.getRequiredParameters());
		mapMap.put("description", "A map containing the parameters to pass into the function_engine call.");

		propertiesMap.put("map", mapMap);

		// Finalize parameters map
		parametersMap.put("properties", propertiesMap);
		parametersMap.put("required", Arrays.asList("id", "map"));

		// Add parameters to function map
		functionMap.put("parameters", parametersMap);

		// Add function map to main map
		toolMap.put("function", functionMap);

		return toolMap;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy