aboutsummaryrefslogtreecommitdiffstats
path: root/auth/cli-editor/src/main/java
diff options
context:
space:
mode:
Diffstat (limited to 'auth/cli-editor/src/main/java')
-rw-r--r--auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/ApexCLIEditorMain.java195
-rw-r--r--auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/ApexModelHandler.java185
-rw-r--r--auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/ApexModelProperties.java215
-rw-r--r--auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/CLIArgument.java144
-rw-r--r--auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/CLIArgumentValue.java91
-rw-r--r--auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/CLICommand.java240
-rw-r--r--auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/CLICommands.java42
-rw-r--r--auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/CLIEditorLoop.java543
-rw-r--r--auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/CLIException.java49
-rw-r--r--auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/CLILineParser.java321
-rw-r--r--auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/CLIParameterParser.java156
-rw-r--r--auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/CLIParameters.java571
-rw-r--r--auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/KeywordNode.java188
-rw-r--r--auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/package-info.java28
14 files changed, 2968 insertions, 0 deletions
diff --git a/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/ApexCLIEditorMain.java b/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/ApexCLIEditorMain.java
new file mode 100644
index 000000000..4a2635efa
--- /dev/null
+++ b/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/ApexCLIEditorMain.java
@@ -0,0 +1,195 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2016-2018 Ericsson. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.apex.auth.clieditor;
+
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Set;
+import java.util.TreeSet;
+
+import org.onap.policy.apex.model.utilities.json.JSONHandler;
+import org.slf4j.ext.XLogger;
+import org.slf4j.ext.XLoggerFactory;
+
+/**
+ * This class initiates an Apex CLI editor from a java main method.
+ *
+ * @author Liam Fallon (liam.fallon@ericsson.com)
+ */
+public class ApexCLIEditorMain {
+ // Get a reference to the logger
+ private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexCLIEditorMain.class);
+
+ // The editor parameters
+ private CLIParameters parameters;
+
+ // The CLI commands read in from JSON
+ private CLICommands commands;
+
+ // The Apex model properties read in from JSON
+ private ApexModelProperties apexModelProperties;
+
+ // The number of errors encountered in command processing
+ private int errorCount = 0;
+
+ /**
+ * Instantiates the Apex CLI editor.
+ *
+ * @param args the command line arguments
+ */
+ public ApexCLIEditorMain(final String[] args) {
+ LOGGER.info("Starting Apex CLI editor " + Arrays.toString(args) + " . . .");
+
+ try {
+ final CLIParameterParser parser = new CLIParameterParser();
+ parameters = parser.parse(args);
+
+ if (parameters.isHelpSet()) {
+ parser.help(ApexCLIEditorMain.class.getCanonicalName());
+ return;
+ }
+ parameters.validate();
+ } catch (final Exception e) {
+ LOGGER.error("start of Apex command line editor failed, " + e.getMessage());
+ errorCount++;
+ return;
+ }
+
+ LOGGER.debug("parameters are: " + parameters.toString());
+
+ // Read the command definitions
+ try {
+ commands = new JSONHandler<CLICommands>().read(CLICommands.class, parameters.getMetadataStream());
+ LOGGER.debug("found " + commands.getCommandSet().size() + " commands");
+ } catch (final Exception e) {
+ LOGGER.error("start of Apex command line editor failed, error reading command metadata from "
+ + parameters.getMetadataLocation());
+ LOGGER.error(e.getMessage());
+ errorCount++;
+ return;
+ }
+
+ // The JSON processing returns null if there is an empty file
+ if (null == commands) {
+ LOGGER.error("start of Apex command line editor failed, no commands found in "
+ + parameters.getApexPropertiesLocation());
+ errorCount++;
+ return;
+ }
+
+ // Read the Apex properties
+ try {
+ apexModelProperties = new JSONHandler<ApexModelProperties>().read(ApexModelProperties.class,
+ parameters.getApexPropertiesStream());
+ LOGGER.debug("model properties are: " + apexModelProperties.toString());
+ } catch (final Exception e) {
+ LOGGER.error("start of Apex command line editor failed, error reading Apex model properties from "
+ + parameters.getApexPropertiesLocation());
+ LOGGER.error(e.getMessage());
+ errorCount++;
+ return;
+ }
+
+ // The JSON processing returns null if there is an empty file
+ if (apexModelProperties == null) {
+ LOGGER.error("start of Apex command line editor failed, no Apex model properties found in "
+ + parameters.getApexPropertiesLocation());
+ errorCount++;
+ return;
+ }
+
+ // Find the system commands
+ final Set<KeywordNode> systemCommandNodes = new TreeSet<>();
+ for (final CLICommand command : commands.getCommandSet()) {
+ if (command.isSystemCommand()) {
+ systemCommandNodes.add(new KeywordNode(command.getName(), command));
+ }
+ }
+
+ // Read in the command hierarchy, this builds a tree of commands
+ final KeywordNode rootKeywordNode = new KeywordNode("root");
+ for (final CLICommand command : commands.getCommandSet()) {
+ rootKeywordNode.processKeywords(command.getKeywordlist(), command);
+ }
+ rootKeywordNode.addSystemCommandNodes(systemCommandNodes);
+
+ // Create the model we will work towards
+ ApexModelHandler modelHandler = null;
+ try {
+ modelHandler =
+ new ApexModelHandler(apexModelProperties.getProperties(), parameters.getInputModelFileName());
+ } catch (final Exception e) {
+ LOGGER.error("execution of Apex command line editor failed: " + e.getMessage());
+ errorCount++;
+ return;
+ }
+
+ final CLIEditorLoop cliEditorLoop =
+ new CLIEditorLoop(apexModelProperties.getProperties(), modelHandler, rootKeywordNode);
+ try {
+ errorCount =
+ cliEditorLoop.runLoop(parameters.getCommandInputStream(), parameters.getOutputStream(), parameters);
+
+ if (errorCount == 0) {
+ LOGGER.info("Apex CLI editor completed execution");
+ } else {
+ LOGGER.error("execution of Apex command line editor failed: " + errorCount
+ + " command execution failure(s) occurred");
+ }
+ } catch (final IOException e) {
+ LOGGER.error("execution of Apex command line editor failed: " + e.getMessage());
+ return;
+ }
+ }
+
+ /**
+ * Get the number of errors encountered in command processing
+ *
+ * @return the number of errors
+ */
+ public int getErrorCount() {
+ return errorCount;
+ }
+
+ /**
+ * Sets the number of errors encountered in command processing.
+ *
+ * @param errorCount the number of errors
+ */
+ public void setErrorCount(final int errorCount) {
+ this.errorCount = errorCount;
+ }
+
+
+ /**
+ * The main method, kicks off the editor.
+ *
+ * @param args the arguments
+ */
+ public static void main(final String[] args) {
+ final ApexCLIEditorMain cliEditor = new ApexCLIEditorMain(args);
+
+ // Only call system.exit on errors as it brings the JVM down
+ if (cliEditor.getErrorCount() > 0) {
+ System.exit(cliEditor.getErrorCount());
+ }
+ }
+}
diff --git a/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/ApexModelHandler.java b/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/ApexModelHandler.java
new file mode 100644
index 000000000..a5e5302f9
--- /dev/null
+++ b/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/ApexModelHandler.java
@@ -0,0 +1,185 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2016-2018 Ericsson. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.apex.auth.clieditor;
+
+import java.io.PrintWriter;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.Properties;
+import java.util.TreeMap;
+
+import org.onap.policy.apex.model.modelapi.ApexAPIResult;
+import org.onap.policy.apex.model.modelapi.ApexModel;
+import org.onap.policy.apex.model.modelapi.ApexModelFactory;
+
+/**
+ * This class instantiates and holds the Apex model being manipulated by the editor.
+ *
+ * @author Liam Fallon (liam.fallon@ericsson.com)
+ */
+public class ApexModelHandler {
+ private ApexModel apexModel = null;
+
+ /**
+ * Create the Apex Model with the properties specified.
+ *
+ * @param properties The properties of the Apex model
+ */
+ public ApexModelHandler(final Properties properties) {
+ apexModel = new ApexModelFactory().createApexModel(properties, true);
+ }
+
+ /**
+ * Create the Apex Model with the properties specified and load it from a file.
+ *
+ * @param properties The properties of the Apex model
+ * @param modelFileName The name of the model file to edit
+ */
+ public ApexModelHandler(final Properties properties, final String modelFileName) {
+ this(properties);
+
+ if (modelFileName == null) {
+ return;
+ }
+
+ final ApexAPIResult result = apexModel.loadFromFile(modelFileName);
+ if (result.isNOK()) {
+ throw new CLIException(result.getMessages().get(0));
+ }
+ }
+
+ /**
+ * Execute a command on the Apex model.
+ *
+ * @param command The command to execute
+ * @param argumentValues Arguments of the command
+ * @param writer A writer to which to write output
+ * @return the result of the executed command
+ */
+ public ApexAPIResult executeCommand(final CLICommand command,
+ final TreeMap<String, CLIArgumentValue> argumentValues, final PrintWriter writer) {
+ // Get the method
+ final Method apiMethod = getCommandMethod(command);
+
+ // Get the method arguments
+ final Object[] parameterArray = getParameterArray(command, argumentValues, apiMethod);
+
+ try {
+ final Object returnObject = apiMethod.invoke(apexModel, parameterArray);
+
+ if (returnObject instanceof ApexAPIResult) {
+ final ApexAPIResult result = (ApexAPIResult) returnObject;
+ writer.println(result);
+ return result;
+ } else {
+ throw new CLIException(
+ "invocation of specified method \"" + command.getApiMethod() + "\" failed for command \""
+ + command.getName() + "\" the returned object is not an instance of ApexAPIResult");
+ }
+ } catch (IllegalAccessException | IllegalArgumentException e) {
+ writer.println("invocation of specified method \"" + command.getApiMethod() + "\" failed for command \""
+ + command.getName() + "\"");
+ e.printStackTrace(writer);
+ throw new CLIException("invocation of specified method \"" + command.getApiMethod()
+ + "\" failed for command \"" + command.getName() + "\"", e);
+ } catch (final InvocationTargetException e) {
+ writer.println("invocation of specified method \"" + command.getApiMethod() + "\" failed for command \""
+ + command.getName() + "\"");
+ e.getCause().printStackTrace(writer);
+ throw new CLIException("invocation of specified method \"" + command.getApiMethod()
+ + "\" failed for command \"" + command.getName() + "\"", e);
+ }
+ }
+
+ /**
+ * Find the API method for the command.
+ *
+ * @param command The command
+ * @return the API method
+ */
+ private Method getCommandMethod(final CLICommand command) {
+ final String className = command.getAPIClassName();
+ final String methodName = command.getAPIMethodName();
+
+ try {
+ final Class<? extends Object> apiClass = Class.forName(className);
+ for (final Method apiMethod : apiClass.getMethods()) {
+ if (apiMethod.getName().equals(methodName)) {
+ return apiMethod;
+ }
+ }
+ throw new CLIException("specified method \"" + command.getApiMethod() + "\" not found for command \""
+ + command.getName() + "\"");
+ } catch (final ClassNotFoundException e) {
+ throw new CLIException("specified class \"" + command.getApiMethod() + "\" not found for command \""
+ + command.getName() + "\"");
+ }
+ }
+
+ /**
+ * Get the arguments of the command as an ordered array of objects ready for the method.
+ *
+ * @param command the command that invoked the method
+ * @param argumentValues the argument values for the method
+ * @param apiMethod the method itself
+ * @return the argument list
+ */
+ private Object[] getParameterArray(final CLICommand command, final TreeMap<String, CLIArgumentValue> argumentValues,
+ final Method apiMethod) {
+ final Object[] parameterArray = new Object[argumentValues.size()];
+
+ int i = 0;
+ try {
+ for (final Class<?> parametertype : apiMethod.getParameterTypes()) {
+ final String parameterValue =
+ argumentValues.get(command.getArgumentList().get(i).getArgumentName()).getValue();
+
+ if (parametertype.equals(boolean.class)) {
+ parameterArray[i] = (boolean) Boolean.valueOf(parameterValue);
+ } else {
+ parameterArray[i] = parameterValue;
+ }
+ i++;
+ }
+ } catch (final Exception e) {
+ throw new CLIException("number of argument mismatch on method \"" + command.getApiMethod()
+ + "\" for command \"" + command.getName() + "\"");
+ }
+
+ return parameterArray;
+ }
+
+ /**
+ * Save the model to a string.
+ *
+ * @param messageWriter the writer to write status messages to
+ * @return the string
+ */
+ public String writeModelToString(final PrintWriter messageWriter) {
+ final ApexAPIResult result = apexModel.listModel();
+
+ if (result.isOK()) {
+ return result.getMessage();
+ } else {
+ return null;
+ }
+ }
+}
diff --git a/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/ApexModelProperties.java b/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/ApexModelProperties.java
new file mode 100644
index 000000000..6ed68288a
--- /dev/null
+++ b/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/ApexModelProperties.java
@@ -0,0 +1,215 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2016-2018 Ericsson. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.apex.auth.clieditor;
+
+import java.util.Properties;
+
+/**
+ * This class contains the definitions of Apex model properties.
+ *
+ * @author Liam Fallon (liam.fallon@ericsson.com)
+ */
+public class ApexModelProperties {
+ /** The default version that will be used for concepts. */
+ public static final String DEFAULT_CONCEPT_VERSION = "0.0.1";
+
+ /** The default name space that will be used for concepts. */
+ public static final String DEFAULT_EVENT_NAMESPACE = "org.onap.policy.apex";
+
+ /** The default source that will be used for events. */
+ public static final String DEFAULT_EVENT_SOURCE = "eventSource";
+
+ /** The default target that will be used for events. */
+ public static final String DEFAULT_EVENT_TARGET = "eventTarget";
+
+ /** The default logic block start token. */
+ public static final String DEFAULT_LOGIC_BLOCK_START_TAG = "LB{";
+
+ /** The default logic block end token. */
+ public static final String DEFAULT_LOGIC_BLOCK_END_TAG = "}LB";
+
+ /** The default logic block end token. */
+ public static final String DEFAULT_POLICY_TEMPLATE = "FREEFORM";
+
+ /** The default macro file token. */
+ public static final String DEFAULT_MACRO_FILE_TAG = "#MACROFILE:";
+
+ // @formatter:off
+ private String defaultConceptVersion = DEFAULT_CONCEPT_VERSION;
+ private String defaultEventNamespace = DEFAULT_EVENT_NAMESPACE;
+ private String defaultEventSource = DEFAULT_EVENT_SOURCE;
+ private String defaultEventTarget = DEFAULT_EVENT_TARGET;
+ private String defaultLogicBlockStartTag = DEFAULT_LOGIC_BLOCK_START_TAG;
+ private String defaultLogicBlockEndTag = DEFAULT_LOGIC_BLOCK_END_TAG;
+ private String defaultPolicyTemplate = DEFAULT_POLICY_TEMPLATE;
+ private String defaultMacroFileTag = DEFAULT_MACRO_FILE_TAG;
+ // @formatter:on
+
+ /**
+ * Gets the default property values for the Apex CLI editor.
+ *
+ * @return the default properties
+ */
+ public Properties getProperties() {
+ final Properties properties = new Properties();
+ // @formatter:off
+ properties.setProperty("DEFAULT_CONCEPT_VERSION", defaultConceptVersion);
+ properties.setProperty("DEFAULT_EVENT_NAMESPACE", defaultEventNamespace);
+ properties.setProperty("DEFAULT_EVENT_SOURCE", defaultEventSource);
+ properties.setProperty("DEFAULT_EVENT_TARGET", defaultEventTarget);
+ properties.setProperty("DEFAULT_LOGIC_BLOCK_START_TAG", defaultLogicBlockStartTag);
+ properties.setProperty("DEFAULT_LOGIC_BLOCK_END_TAG", defaultLogicBlockEndTag);
+ properties.setProperty("DEFAULT_MACRO_FILE_TAG", defaultMacroFileTag);
+ // @formatter:on
+ return properties;
+ }
+
+ /**
+ * Gets the default concept version.
+ *
+ * @return the default concept version
+ */
+ public String getDefaultConceptVersion() {
+ return defaultConceptVersion;
+ }
+
+ /**
+ * Sets the default concept version.
+ *
+ * @param defaultConceptVersion the default concept version
+ */
+ public void setDefaultConceptVersion(final String defaultConceptVersion) {
+ this.defaultConceptVersion = defaultConceptVersion;
+ }
+
+ /**
+ * Gets the default event namespace.
+ *
+ * @return the default event namespace
+ */
+ public String getDefaultEventNamespace() {
+ return defaultEventNamespace;
+ }
+
+ /**
+ * Sets the default event namespace.
+ *
+ * @param defaultEventNamespace the default event namespace
+ */
+ public void setDefaultEventNamespace(final String defaultEventNamespace) {
+ this.defaultEventNamespace = defaultEventNamespace;
+ }
+
+ /**
+ * Gets the default event source.
+ *
+ * @return the default event source
+ */
+ public String getDefaultEventSource() {
+ return defaultEventSource;
+ }
+
+ /**
+ * Sets the default event source.
+ *
+ * @param defaultEventSource the default event source
+ */
+ public void setDefaultEventSource(final String defaultEventSource) {
+ this.defaultEventSource = defaultEventSource;
+ }
+
+ /**
+ * Gets the default event target.
+ *
+ * @return the default event target
+ */
+ public String getDefaultEventTarget() {
+ return defaultEventTarget;
+ }
+
+ /**
+ * Sets the default event target.
+ *
+ * @param defaultEventTarget the default event target
+ */
+ public void setDefaultEventTarget(final String defaultEventTarget) {
+ this.defaultEventTarget = defaultEventTarget;
+ }
+
+ /**
+ * Gets the default logic block start tag.
+ *
+ * @return the default logic block start tag
+ */
+ public String getDefaultLogicBlockStartTag() {
+ return defaultLogicBlockStartTag;
+ }
+
+ /**
+ * Gets the default logic block end tag.
+ *
+ * @return the default logic block end tag
+ */
+ public String getDefaultLogicBlockEndTag() {
+ return defaultLogicBlockEndTag;
+ }
+
+ /**
+ * Gets the default policy template type.
+ *
+ * @return the default policy template
+ */
+ public String getDefaultPolicyTemplate() {
+ return defaultPolicyTemplate;
+ }
+
+ /**
+ * Sets the default policy template type.
+ *
+ * @param defaultPolicyTemplate the new default policy template
+ */
+ public void setDefaultPolicyTemplate(final String defaultPolicyTemplate) {
+ this.defaultPolicyTemplate = defaultPolicyTemplate;
+ }
+
+ /**
+ * Gets the default macro file tag.
+ *
+ * @return the default macro file end tag
+ */
+ public String getDefaultMacroFileTag() {
+ return defaultMacroFileTag;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.lang.Object#toString()
+ */
+ @Override
+ public String toString() {
+ return "ApexModelProperties [defaultConceptVersion=" + defaultConceptVersion + ", defaultEventNamespace="
+ + defaultEventNamespace + ", defaultEventSource=" + defaultEventSource + ", defaultEventTarget="
+ + defaultEventTarget + ", defaultLogicBlockStartTag=" + defaultLogicBlockStartTag
+ + ", defaultLogicBlockEndTag=" + defaultLogicBlockEndTag + ", defaultPolicyTemplate="
+ + defaultPolicyTemplate + ", defaultMacroFileTag=" + defaultMacroFileTag + "]";
+ }
+}
diff --git a/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/CLIArgument.java b/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/CLIArgument.java
new file mode 100644
index 000000000..b215f69e4
--- /dev/null
+++ b/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/CLIArgument.java
@@ -0,0 +1,144 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2016-2018 Ericsson. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.apex.auth.clieditor;
+
+import org.onap.policy.apex.model.utilities.Assertions;
+
+/**
+ * This class holds the definition of an argument of a CLI command.
+ *
+ * @author Liam Fallon (liam.fallon@ericsson.com)
+ */
+public class CLIArgument implements Comparable<CLIArgument> {
+ private final String argumentName;
+ private final boolean nullable;
+ private final String description;
+
+ /**
+ * This Constructor constructs a non nullable command line argument with a blank name and
+ * description.
+ */
+ public CLIArgument() {
+ this("", false, "");
+ }
+
+ /**
+ * This Constructor constructs a non nullable command line argument with the given name and
+ * description.
+ *
+ * @param incomingArgumentName the argument name
+ */
+ public CLIArgument(final String incomingArgumentName) {
+ this(incomingArgumentName, false, "");
+ }
+
+ /**
+ * This Constructor constructs a command line argument with the given name, nullability, and
+ * description.
+ *
+ * @param argumentName the argument name
+ * @param nullable the nullable
+ * @param description the description
+ */
+ public CLIArgument(final String argumentName, final boolean nullable, final String description) {
+ this.argumentName = argumentName;
+ this.nullable = nullable;
+ this.description = description;
+ }
+
+ /**
+ * Gets the argument name.
+ *
+ * @return the argument name
+ */
+ public String getArgumentName() {
+ return argumentName;
+ }
+
+ /**
+ * Checks if the argument is nullable.
+ *
+ * @return true, if checks if the argument is nullable
+ */
+ public boolean isNullable() {
+ return nullable;
+ }
+
+ /**
+ * Gets the argument description.
+ *
+ * @return the argument description
+ */
+ public String getDescription() {
+ return description;
+ }
+
+ /**
+ * Gets the argument help.
+ *
+ * @return the argument help
+ */
+ public String getHelp() {
+ final StringBuilder builder = new StringBuilder();
+ builder.append(argumentName);
+ builder.append(nullable ? ": (O) " : ": (M) ");
+ builder.append(description);
+ return builder.toString();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.lang.Object#toString()
+ */
+ @Override
+ public String toString() {
+ return "CLIArgument [argumentName=" + argumentName + ", nullable=" + nullable + ", description=" + description
+ + "]";
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.lang.Comparable#compareTo(java.lang.Object)
+ */
+ @Override
+ public int compareTo(final CLIArgument otherArgument) {
+ Assertions.argumentNotNull(otherArgument, "comparison object may not be null");
+
+ if (this == otherArgument) {
+ return 0;
+ }
+ if (getClass() != otherArgument.getClass()) {
+ return this.hashCode() - otherArgument.hashCode();
+ }
+
+ final CLIArgument other = otherArgument;
+
+ if (!argumentName.equals(other.argumentName)) {
+ return argumentName.compareTo(other.argumentName);
+ }
+ if (nullable != other.nullable) {
+ return (this.hashCode() - other.hashCode());
+ }
+ return description.compareTo(otherArgument.description);
+ }
+}
diff --git a/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/CLIArgumentValue.java b/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/CLIArgumentValue.java
new file mode 100644
index 000000000..d87a8dc5b
--- /dev/null
+++ b/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/CLIArgumentValue.java
@@ -0,0 +1,91 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2016-2018 Ericsson. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.apex.auth.clieditor;
+
+/**
+ * This class represents an argument used on a command and its value.
+ *
+ * @author Liam Fallon (liam.fallon@ericsson.com)
+ */
+public class CLIArgumentValue {
+ private final CLIArgument cliArgument;
+ private boolean specified;
+ private String value;
+
+ /**
+ * The Constructor creates an argument value for the given argument, has not been set, and has
+ * no value.
+ *
+ * @param cliArgument the argument for which this object is a value
+ */
+ public CLIArgumentValue(final CLIArgument cliArgument) {
+ this.cliArgument = cliArgument;
+ specified = false;
+ value = null;
+ }
+
+ /**
+ * Gets the argument for which this object is a value.
+ *
+ * @return the argument for which this object is a value
+ */
+ public CLIArgument getCliArgument() {
+ return cliArgument;
+ }
+
+ /**
+ * Checks if the argument value is specified.
+ *
+ * @return true, if the argument value is specified
+ */
+ public boolean isSpecified() {
+ return specified;
+ }
+
+ /**
+ * Gets the argument value.
+ *
+ * @return the argument value
+ */
+ public String getValue() {
+ return value;
+ }
+
+ /**
+ * Sets the argument value.
+ *
+ * @param value the argument value
+ */
+ public void setValue(final String value) {
+ this.value = value;
+ specified = true;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.lang.Object#toString()
+ */
+ @Override
+ public String toString() {
+ return "CLIArgumentValue [cliArgument=" + cliArgument + ", specified=" + specified + ", value=" + value + "]";
+ }
+}
diff --git a/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/CLICommand.java b/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/CLICommand.java
new file mode 100644
index 000000000..74f4a6f92
--- /dev/null
+++ b/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/CLICommand.java
@@ -0,0 +1,240 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2016-2018 Ericsson. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.apex.auth.clieditor;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.onap.policy.apex.model.utilities.Assertions;
+
+/**
+ * This class represents a single Apex CLI command that is issued to the Apex Editor Java API
+ * {@link com.ericsson.apex.model.modelapi.ApexEditorAPI}.
+ *
+ * @author Liam Fallon (liam.fallon@ericsson.com)
+ */
+public class CLICommand implements Comparable<CLICommand> {
+ private String name = "";
+ private final List<String> keywordlist = new ArrayList<>();
+ private final List<CLIArgument> argumentList = new ArrayList<>();
+ private String apiMethod = "";
+ private boolean systemCommand = false;
+ private String description = "";
+
+ /**
+ * Gets the class name of the class that executes this command in the Java API.
+ *
+ * @return the class name of the class that executes this command in the Java API
+ */
+ public String getAPIClassName() {
+ final int lastDotPos = apiMethod.lastIndexOf('.');
+ if (lastDotPos == -1) {
+ throw new CLIException("invalid API method name specified on command \"" + name
+ + "\", class name not found: " + apiMethod);
+ }
+ return apiMethod.substring(0, lastDotPos);
+ }
+
+ /**
+ * Gets the method name of the method that executes this command in the Java API.
+ *
+ * @return the the method name of the method that executes this command in the Java API
+ */
+ public String getAPIMethodName() {
+ final int lastDotPos = apiMethod.lastIndexOf('.');
+ if (lastDotPos == -1) {
+ throw new CLIException("invalid API method name specified on command \"" + name
+ + "\", class name not found: " + apiMethod);
+ }
+ if (lastDotPos == apiMethod.length() - 1) {
+ throw new CLIException("no API method name specified on command \"" + name + "\": " + apiMethod);
+ }
+ return apiMethod.substring(lastDotPos + 1);
+ }
+
+ /**
+ * Gets the name of the editor command.
+ *
+ * @return the name of the editor command
+ */
+ public String getName() {
+ return name;
+ }
+
+ /**
+ * Sets the name of the editor command.
+ *
+ * @param name the name of the editor command
+ */
+ public void setName(final String name) {
+ this.name = name;
+ }
+
+ /**
+ * Gets the list of keywords for this command.
+ *
+ * @return the list of keywords for this command
+ */
+ public List<String> getKeywordlist() {
+ return keywordlist;
+ }
+
+ /**
+ * Gets the list of arguments for this command.
+ *
+ * @return the list of arguments for this command
+ */
+ public List<CLIArgument> getArgumentList() {
+ return argumentList;
+ }
+
+ /**
+ * Gets the method of the method that executes this command in the Java API.
+ *
+ * @return the method of the method that executes this command in the Java API
+ */
+ public String getApiMethod() {
+ return apiMethod;
+ }
+
+ /**
+ * Sets the method of the method that executes this command in the Java API.
+ *
+ * @param apiMethod the method of the method that executes this command in the Java API
+ */
+ public void setApiMethod(final String apiMethod) {
+ this.apiMethod = apiMethod;
+ }
+
+ /**
+ * Gets the description of the command.
+ *
+ * @return the description of the command
+ */
+ public String getDescription() {
+ return description;
+ }
+
+ /**
+ * Sets the description of the command.
+ *
+ * @param description the description of the command
+ */
+ public void setDescription(final String description) {
+ this.description = description;
+ }
+
+ /**
+ * Checks if this command is a system command.
+ *
+ * @return true, if this command is a system command
+ */
+ public boolean isSystemCommand() {
+ return systemCommand;
+ }
+
+ /**
+ * Sets whether this command is a system command.
+ *
+ * @param systemCommand whether this command is a system command
+ */
+ public void setSystemCommand(final boolean systemCommand) {
+ this.systemCommand = systemCommand;
+ }
+
+ /**
+ * Gets help for this command.
+ *
+ * @return the help for this command
+ */
+ public String getHelp() {
+ final StringBuilder builder = new StringBuilder();
+ for (final String keyword : keywordlist) {
+ builder.append(keyword);
+ builder.append(' ');
+ }
+ builder.append('{');
+ builder.append(name);
+ builder.append("}: ");
+ builder.append(description);
+
+ for (final CLIArgument argument : argumentList) {
+ if (argument == null) {
+ continue;
+ }
+ builder.append("\n\t");
+ builder.append(argument.getHelp());
+ }
+ return builder.toString();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.lang.Object#toString()
+ */
+ @Override
+ public String toString() {
+ return "CLICommand [name=" + name + ",keywordlist=" + keywordlist + ", argumentList=" + argumentList
+ + ", apiMethod=" + apiMethod + ", systemCommand=" + systemCommand + ", description=" + description
+ + "]";
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.lang.Comparable#compareTo(java.lang.Object)
+ */
+ @Override
+ public int compareTo(final CLICommand otherCommand) {
+ Assertions.argumentNotNull(otherCommand, "comparison object may not be null");
+
+ if (this == otherCommand) {
+ return 0;
+ }
+ if (getClass() != otherCommand.getClass()) {
+ return this.hashCode() - otherCommand.hashCode();
+ }
+
+ final CLICommand other = otherCommand;
+
+ for (int i = 0, j = 0;; i++, j++) {
+ if (i < keywordlist.size() && j < otherCommand.keywordlist.size()) {
+ if (!keywordlist.get(i).equals(other.keywordlist.get(j))) {
+ return keywordlist.get(i).compareTo(other.keywordlist.get(j));
+ }
+ } else if (i == keywordlist.size() && j == otherCommand.keywordlist.size()) {
+ break;
+ } else if (i == keywordlist.size()) {
+ return -1;
+ } else {
+ return 1;
+ }
+ }
+ if (!argumentList.equals(other.argumentList)) {
+ return (argumentList.hashCode() - other.argumentList.hashCode());
+ }
+ if (systemCommand != other.systemCommand) {
+ return (this.hashCode() - other.hashCode());
+ }
+ return apiMethod.compareTo(other.apiMethod);
+ }
+}
diff --git a/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/CLICommands.java b/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/CLICommands.java
new file mode 100644
index 000000000..4c9bab045
--- /dev/null
+++ b/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/CLICommands.java
@@ -0,0 +1,42 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2016-2018 Ericsson. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.apex.auth.clieditor;
+
+import java.util.Set;
+import java.util.TreeSet;
+
+/**
+ * This class contains the CLI commands read in from a JSON file.
+ *
+ * @author Liam Fallon (liam.fallon@ericsson.com)
+ */
+public class CLICommands {
+ private final Set<CLICommand> commandList = new TreeSet<>();
+
+ /**
+ * Gets the command set.
+ *
+ * @return the command set
+ */
+ public Set<CLICommand> getCommandSet() {
+ return commandList;
+ }
+}
diff --git a/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/CLIEditorLoop.java b/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/CLIEditorLoop.java
new file mode 100644
index 000000000..560648901
--- /dev/null
+++ b/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/CLIEditorLoop.java
@@ -0,0 +1,543 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2016-2018 Ericsson. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.apex.auth.clieditor;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
+import java.util.AbstractMap.SimpleEntry;
+import java.util.ArrayDeque;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map.Entry;
+import java.util.Properties;
+import java.util.TreeMap;
+
+import org.onap.policy.apex.model.modelapi.ApexAPIResult;
+import org.onap.policy.apex.model.modelapi.ApexAPIResult.RESULT;
+import org.onap.policy.apex.model.utilities.TextFileUtils;
+import org.onap.policy.apex.model.utilities.TreeMapUtils;
+
+/**
+ * This class implements the editor loop, the loop of execution that continuously executes commands
+ * until the quit command is issued or EOF is detected on input.
+ *
+ * @author Liam Fallon (liam.fallon@ericsson.com)
+ */
+public class CLIEditorLoop {
+ // The model handler that is handling the API towards the Apex model being editied
+ private final ApexModelHandler modelHandler;
+
+ // Holds the current location in the keyword hierarchy
+ private final ArrayDeque<KeywordNode> keywordNodeDeque = new ArrayDeque<>();
+
+ // Logic block tags
+ private final String logicBlockStartTag;
+ private final String logicBlockEndTag;
+
+ // File Macro tag
+ private final String macroFileTag;
+
+ /**
+ * Initiate the loop with the keyword node tree.
+ *
+ * @param properties The CLI editor properties defined for execution
+ * @param modelHandler the model handler that will handle commands
+ * @param rootKeywordNode The root keyword node tree
+ */
+ public CLIEditorLoop(final Properties properties, final ApexModelHandler modelHandler,
+ final KeywordNode rootKeywordNode) {
+ this.modelHandler = modelHandler;
+ keywordNodeDeque.push(rootKeywordNode);
+
+ logicBlockStartTag = properties.getProperty("DEFAULT_LOGIC_BLOCK_START_TAG");
+ logicBlockEndTag = properties.getProperty("DEFAULT_LOGIC_BLOCK_END_TAG");
+ macroFileTag = properties.getProperty("DEFAULT_MACRO_FILE_TAG");
+ }
+
+ /**
+ * Run a command loop.
+ *
+ * @param inputStream The stream to read commands from
+ * @param outputStream The stream to write command output and messages to
+ * @param parameters The parameters for the CLI editor
+ * @return the exit code from command processing
+ * @throws IOException Thrown on exceptions on IO
+ */
+ public int runLoop(final InputStream inputStream, final OutputStream outputStream, final CLIParameters parameters)
+ throws IOException {
+ // Readers and writers for input and output
+ final BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
+ final PrintWriter writer = new PrintWriter(new OutputStreamWriter(outputStream));
+
+ // The parser parses the input lines into commands and arguments
+ final CLILineParser parser = new CLILineParser();
+
+ // The main loop for command handing, it continues until EOF on the input stream or until a
+ // quit command
+ int errorCount = 0;
+ ApexAPIResult result = new ApexAPIResult();
+ while (result.getResult() != RESULT.FINISHED) {
+ if (!parameters.isIgnoreCommandFailures() && errorCount > 0) {
+ break;
+ }
+
+ // Output prompt and get a line of input
+ writer.print(getPrompt());
+ writer.flush();
+ String line = reader.readLine();
+ if (line == null) {
+ break;
+ }
+
+ // Expand any macros in the script
+ try {
+ while (line.contains(macroFileTag)) {
+ line = expandMacroFile(parameters, line);
+ }
+ }
+ // Print any error messages from command parsing and finding
+ catch (final CLIException e) {
+ writer.println(e.getMessage());
+ errorCount++;
+ continue;
+ }
+
+ if (parameters.isEchoSet()) {
+ writer.println(line);
+ }
+
+ String logicBlock = null;
+ if (line.trim().endsWith(logicBlockStartTag)) {
+ line = line.replace(logicBlockStartTag, "").trim();
+
+ logicBlock = "";
+ while (true) {
+ String logicLine = reader.readLine();
+ if (logicLine == null) {
+ logicBlock = null;
+ break;
+ }
+
+ try {
+ while (logicLine.contains(macroFileTag)) {
+ logicLine = expandMacroFile(parameters, logicLine);
+ }
+ }
+ // Print any error messages from command parsing and finding
+ catch (final CLIException e) {
+ writer.println(e.getMessage());
+ errorCount++;
+ continue;
+ }
+
+ if (parameters.isEchoSet()) {
+ writer.println(logicLine);
+ }
+
+ if (logicLine.trim().endsWith(logicBlockEndTag)) {
+ logicBlock += logicLine.replace(logicBlockEndTag, "").trim() + "\n";
+ break;
+ } else {
+ logicBlock += logicLine + "\n";
+ }
+ }
+ }
+
+ try {
+ // Parse the line into a list of commands and arguments
+ final ArrayList<String> commandWords = parser.parse(line, logicBlock);
+
+ // Find the command, if the command is null, then we are simply changing position in
+ // the hierarchy
+ final CLICommand command = findCommand(commandWords);
+ if (command != null) {
+ // Check the arguments of the command
+ final TreeMap<String, CLIArgumentValue> argumentValues = getArgumentValues(command, commandWords);
+
+ // Execute the command, a FINISHED result means a command causes the loop to
+ // leave execution
+ result = executeCommand(command, argumentValues, writer);
+ if (result.isNOK()) {
+ errorCount++;
+ }
+ }
+ }
+ // Print any error messages from command parsing and finding
+ catch (final CLIException e) {
+ writer.println(e.getMessage());
+ errorCount++;
+ } catch (final Exception e) {
+ e.printStackTrace(writer);
+ }
+ }
+
+ // Get the output model
+ if (!parameters.isSuppressModelOutputSet()) {
+ final String modelString = modelHandler.writeModelToString(writer);
+
+ if (parameters.checkSetOutputModelFileName()) {
+ TextFileUtils.putStringAsTextFile(modelString, parameters.getOutputModelFileName());
+ } else {
+ System.out.println(modelString);
+ }
+ }
+
+ reader.close();
+ writer.close();
+
+ return errorCount;
+ }
+
+ /**
+ * Output a prompt that indicates where in the keyword hierarchy we are.
+ *
+ * @return A string with the prompt
+ */
+ private String getPrompt() {
+ final StringBuilder builder = new StringBuilder();
+ final Iterator<KeywordNode> keynodeDequeIter = keywordNodeDeque.descendingIterator();
+
+ while (keynodeDequeIter.hasNext()) {
+ builder.append('/');
+ builder.append(keynodeDequeIter.next().getKeyword());
+ }
+ builder.append("> ");
+
+ return builder.toString();
+ }
+
+ /**
+ * Finds a command for the given input command words. Command words need only ne specified
+ * enough to uniquely identify them. Therefore, "p s o c" will find the command "policy state
+ * output create"
+ *
+ * @param commandWords The commands and arguments parsed from the command line by the parser
+ * @return The found command
+ */
+
+ private CLICommand findCommand(final ArrayList<String> commandWords) {
+ CLICommand command = null;
+
+ final KeywordNode startKeywordNode = keywordNodeDeque.peek();
+
+ // Go down through the keywords searching for the command
+ for (int i = 0; i < commandWords.size(); i++) {
+ final KeywordNode searchKeywordNode = keywordNodeDeque.peek();
+
+ // We have got to the arguments, time to stop looking
+ if (commandWords.get(i).indexOf('=') > 0) {
+ unwindStack(startKeywordNode);
+ throw new CLIException("command not found: " + stringAL2String(commandWords));
+ }
+
+ // If the node entries found is not equal to one, then we have either no command or more
+ // than one command matching
+ final List<Entry<String, KeywordNode>> foundNodeEntries =
+ TreeMapUtils.findMatchingEntries(searchKeywordNode.getChildren(), commandWords.get(i));
+ if (foundNodeEntries.size() == 0) {
+ unwindStack(startKeywordNode);
+ throw new CLIException("command not found: " + stringAL2String(commandWords));
+ } else if (foundNodeEntries.size() > 1) {
+ unwindStack(startKeywordNode);
+ throw new CLIException("multiple commands matched: " + stringAL2String(commandWords) + " ["
+ + nodeAL2String(foundNodeEntries) + ']');
+ }
+
+ // Record the fully expanded command word
+ commandWords.set(i, foundNodeEntries.get(0).getKey());
+
+ // Check if there is a command
+ final KeywordNode childKeywordNode = foundNodeEntries.get(0).getValue();
+ command = childKeywordNode.getCommand();
+
+ // If the command is null, we go into a sub mode, otherwise we unwind the stack of
+ // commands and return the found command
+ if (command == null) {
+ keywordNodeDeque.push(childKeywordNode);
+ } else {
+ unwindStack(startKeywordNode);
+ return command;
+ }
+ }
+
+ return null;
+ }
+
+ /**
+ * Unwind the stack of keyword node entries we have placed on the queue in a command search.
+ *
+ * @param startKeywordNode The point on the queue we want to unwind to
+ */
+ private void unwindStack(final KeywordNode startKeywordNode) {
+ // Unwind the stack
+ while (true) {
+ if (keywordNodeDeque.peek().equals(startKeywordNode)) {
+ return;
+ }
+ keywordNodeDeque.pop();
+ }
+ }
+
+ /**
+ * Check the arguments of the command.
+ *
+ * @param command The command to check
+ * @param commandWords The command words entered
+ * @return the argument values
+ */
+ private TreeMap<String, CLIArgumentValue> getArgumentValues(final CLICommand command,
+ final ArrayList<String> commandWords) {
+ final TreeMap<String, CLIArgumentValue> argumentValues = new TreeMap<>();
+ for (final CLIArgument argument : command.getArgumentList()) {
+ if (argument != null) {
+ argumentValues.put(argument.getArgumentName(), new CLIArgumentValue(argument));
+ }
+ }
+
+ // Set the value of the arguments
+ for (final Entry<String, String> argument : getCommandArguments(commandWords)) {
+ final List<Entry<String, CLIArgumentValue>> foundArguments =
+ TreeMapUtils.findMatchingEntries(argumentValues, argument.getKey());
+ if (foundArguments.size() == 0) {
+ throw new CLIException("command " + stringAL2String(commandWords) + ": " + " argument \""
+ + argument.getKey() + "\" not allowed on command");
+ } else if (foundArguments.size() > 1) {
+ throw new CLIException("command " + stringAL2String(commandWords) + ": " + " argument " + argument
+ + " matches multiple arguments [" + argumentAL2String(foundArguments) + ']');
+ }
+
+ // Set the value of the argument, stripping off any quotes
+ final String argumentValue = argument.getValue().replaceAll("^\"", "").replaceAll("\"$", "");
+ foundArguments.get(0).getValue().setValue(argumentValue);
+ }
+
+ // Now check all mandatory arguments are set
+ for (final CLIArgumentValue argumentValue : argumentValues.values()) {
+ if (!argumentValue.isSpecified()) {
+ // Argument values are null by default so if this argument is not nullable it is
+ // mandatory
+ if (!argumentValue.getCliArgument().isNullable()) {
+ throw new CLIException("command " + stringAL2String(commandWords) + ": " + " mandatory argument \""
+ + argumentValue.getCliArgument().getArgumentName() + "\" not specified");
+ }
+ }
+ }
+
+ return argumentValues;
+ }
+
+ /**
+ * Get the arguments of the command, the command words have already been conditioned into an
+ * array starting with the command words and ending with the arguments as name=value tuples.
+ *
+ * @param commandWords The command words entered by the user
+ * @return the arguments as an entry array list
+ */
+ private ArrayList<Entry<String, String>> getCommandArguments(final ArrayList<String> commandWords) {
+ final ArrayList<Entry<String, String>> arguments = new ArrayList<>();
+
+ // Iterate over the command words, arguments are of the format name=value
+ for (final String word : commandWords) {
+ final int equalsPos = word.indexOf('=');
+ if (equalsPos > 0) {
+ arguments.add(
+ new SimpleEntry<>(word.substring(0, equalsPos), word.substring(equalsPos + 1, word.length())));
+ }
+ }
+
+ return arguments;
+ }
+
+ /**
+ * Execute system and editor commands.
+ *
+ * @param command The command to execute
+ * @param argumentValues The arguments input on the command line to invoke the command
+ * @param writer The writer to use for any output from the command
+ * @return the result of execution of the command
+ */
+ private ApexAPIResult executeCommand(final CLICommand command,
+ final TreeMap<String, CLIArgumentValue> argumentValues, final PrintWriter writer) {
+ if (command.isSystemCommand()) {
+ return exceuteSystemCommand(command, writer);
+ } else {
+ return modelHandler.executeCommand(command, argumentValues, writer);
+ }
+ }
+
+ /**
+ * Execute system commands.
+ *
+ * @param command The command to execute
+ * @param writer The writer to use for any output from the command
+ * @return the result of execution of the command
+ */
+ private ApexAPIResult exceuteSystemCommand(final CLICommand command, final PrintWriter writer) {
+ if (command.getName().equals("back")) {
+ return executeBackCommand();
+ } else if (command.getName().equals("help")) {
+ return executeHelpCommand(writer);
+ } else if (command.getName().equals("quit")) {
+ return executeQuitCommand();
+ } else {
+ return new ApexAPIResult(RESULT.SUCCESS);
+ }
+ }
+
+ /**
+ * Execute the "back" command.
+ *
+ * @return the result of execution of the command
+ */
+ private ApexAPIResult executeBackCommand() {
+ if (keywordNodeDeque.size() > 1) {
+ keywordNodeDeque.pop();
+ }
+ return new ApexAPIResult(RESULT.SUCCESS);
+ }
+
+ /**
+ * Execute the "quit" command.
+ *
+ * @return the result of execution of the command
+ */
+ private ApexAPIResult executeQuitCommand() {
+ return new ApexAPIResult(RESULT.FINISHED);
+ }
+
+ /**
+ * Execute the "help" command.
+ *
+ * @param writer The writer to use for output from the command
+ * @return the result of execution of the command
+ */
+ private ApexAPIResult executeHelpCommand(final PrintWriter writer) {
+ for (final CLICommand command : keywordNodeDeque.peek().getCommands()) {
+ writer.println(command.getHelp());
+ }
+ return new ApexAPIResult(RESULT.SUCCESS);
+ }
+
+ /**
+ * Helper method to output an array list of keyword node entries to a string.
+ *
+ * @param nodeEntryArrayList the array list of keyword node entries
+ * @return the string
+ */
+ private String nodeAL2String(final List<Entry<String, KeywordNode>> nodeEntryArrayList) {
+ final ArrayList<String> stringArrayList = new ArrayList<>();
+ for (final Entry<String, KeywordNode> node : nodeEntryArrayList) {
+ stringArrayList.add(node.getValue().getKeyword());
+ }
+
+ return stringAL2String(stringArrayList);
+ }
+
+ /**
+ * Helper method to output an array list of argument entries to a string.
+ *
+ * @param argumentArrayList the argument array list
+ * @return the string
+ */
+ private String argumentAL2String(final List<Entry<String, CLIArgumentValue>> argumentArrayList) {
+ final ArrayList<String> stringArrayList = new ArrayList<>();
+ for (final Entry<String, CLIArgumentValue> argument : argumentArrayList) {
+ stringArrayList.add(argument.getValue().getCliArgument().getArgumentName());
+ }
+
+ return stringAL2String(stringArrayList);
+ }
+
+ /**
+ * Helper method to output an array list of strings to a string.
+ *
+ * @param stringArrayList the array list of strings
+ * @return the string
+ */
+ private String stringAL2String(final List<String> stringArrayList) {
+ final StringBuilder builder = new StringBuilder();
+ boolean first = true;
+ for (final String word : stringArrayList) {
+ if (first) {
+ first = false;
+ } else {
+ builder.append(',');
+ }
+ builder.append(word);
+ }
+
+ return builder.toString();
+ }
+
+ /**
+ * This method reads in the file from a file macro statement, expands the macro, and replaces
+ * the Macro tag in the line with the file contents.
+ *
+ * @param parameters The parameters for the CLI editor
+ * @param line The line with the macro keyword in it
+ * @return the expanded line
+ */
+ private String expandMacroFile(final CLIParameters parameters, final String line) {
+ final int macroTagPos = line.indexOf(macroFileTag);
+
+ // Get the line before and after the macro tag
+ final String lineBeforeMacroTag = line.substring(0, macroTagPos);
+ final String lineAfterMacroTag = line.substring(macroTagPos + macroFileTag.length()).replaceAll("^\\s*", "");
+
+ // Get the file name that is the argument of the Macro tag
+ final String[] lineWords = lineAfterMacroTag.split("\\s+");
+
+ if (lineWords.length == 0) {
+ throw new CLIException("no file name specified for Macro File Tag");
+ }
+
+ // Get the macro file name and the remainder of the line after the file name
+ String macroFileName = lineWords[0];
+ final String lineAfterMacroFileName = lineAfterMacroTag.replaceFirst(macroFileName, "");
+
+ if (macroFileName.length() > 2 && macroFileName.startsWith("\"") && macroFileName.endsWith("\"")) {
+ macroFileName = macroFileName.substring(1, macroFileName.length() - 1);
+ } else {
+ throw new CLIException(
+ "macro file name \"" + macroFileName + "\" must exist and be quoted with double quotes \"\"");
+ }
+
+ // Append the working directory to the macro file name
+ macroFileName = parameters.getWorkingDirectory() + File.separatorChar + macroFileName;
+
+ // Now, get the text file for the argument of the macro
+ String macroFileContents = null;
+ try {
+ macroFileContents = TextFileUtils.getTextFileAsString(macroFileName);
+ } catch (final IOException e) {
+ throw new CLIException("file \"" + macroFileName + "\" specified in Macro File Tag not found", e);
+ }
+
+ return lineBeforeMacroTag + macroFileContents + lineAfterMacroFileName;
+ }
+}
diff --git a/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/CLIException.java b/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/CLIException.java
new file mode 100644
index 000000000..8e1104c3b
--- /dev/null
+++ b/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/CLIException.java
@@ -0,0 +1,49 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2016-2018 Ericsson. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.apex.auth.clieditor;
+
+/**
+ * A run time exception used to report parsing and command input errors.
+ *
+ * @author Liam Fallon (liam.fallon@ericsson.com)
+ */
+public class CLIException extends IllegalArgumentException {
+ private static final long serialVersionUID = 6520231162404452427L;
+
+ /**
+ * Create a CLIException with a message.
+ *
+ * @param message the message
+ */
+ public CLIException(final String message) {
+ super(message);
+ }
+
+ /**
+ * Create a CLIException with a message and an exception.
+ *
+ * @param message the message
+ * @param t the t
+ */
+ public CLIException(final String message, final Throwable t) {
+ super(message, t);
+ }
+}
diff --git a/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/CLILineParser.java b/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/CLILineParser.java
new file mode 100644
index 000000000..fd4541f28
--- /dev/null
+++ b/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/CLILineParser.java
@@ -0,0 +1,321 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2016-2018 Ericsson. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.apex.auth.clieditor;
+
+import java.util.ArrayList;
+
+/**
+ * This class chops a command line up into commands, parameters and arguments.
+ *
+ * @author Liam Fallon (liam.fallon@ericsson.com)
+ */
+public class CLILineParser {
+
+ /**
+ * This method breaks a line of input up into commands, parameters, and arguments. Commands are
+ * standalone words at the beginning of the line, of which there may be multiple Parameters are
+ * single words followed by an '=' character Arguments are single words or a block of quoted
+ * text following an '=' character.
+ *
+ * Format: command [command....] parameter=argument [parameter = argument]
+ *
+ * Examples entity create name=hello description="description of hello" help entity list
+ *
+ * @param line The line to parse
+ * @param logicBlock A block of logic code to be taken literally
+ * @return the string array list
+ */
+ public ArrayList<String> parse(final String line, final String logicBlock) {
+ return checkFormat(
+ mergeArguments(mergeEquals(
+ splitOnEquals(stripAndSplitWords(mergeQuotes(splitOnChar(stripComments(line), '\"')))))),
+ logicBlock);
+ }
+
+ /**
+ * Strip comments from lines, comments start with a # character.
+ *
+ * @param line the line
+ * @return the line without comments
+ */
+ private String stripComments(final String line) {
+ final int commentPos = line.indexOf('#');
+ if (commentPos == -1) {
+ return line;
+ } else {
+ return line.substring(0, commentPos);
+ }
+ }
+
+ /**
+ * This method merges an array with separate quotes into an array with quotes delimiting the
+ * start and end of quoted words Example [Humpty ],["],[Dumpty sat on the wall],["],[, Humpty
+ * Dumpty had ],["],["],a ["],[great],["],[ fall] becomes [Humpty ],["Dumpty sat on the
+ * wall"],[, Humpty Dumpty had ],[""],[a],["great"],[ fall].
+ *
+ * @param wordsSplitOnQuotes the words split on quotes
+ * @return the merged array list
+ */
+ private ArrayList<String> mergeQuotes(final ArrayList<String> wordsSplitOnQuotes) {
+ final ArrayList<String> wordsWithQuotesMerged = new ArrayList<>();
+
+ for (int i = 0; i < wordsSplitOnQuotes.size();) {
+ if (wordsSplitOnQuotes.get(i).equals("\"")) {
+ String quotedWord = wordsSplitOnQuotes.get(i++);
+
+ for (; i < wordsSplitOnQuotes.size(); i++) {
+ quotedWord += wordsSplitOnQuotes.get(i);
+ if (wordsSplitOnQuotes.get(i).equals("\"")) {
+ i++;
+ break;
+ }
+ }
+ if (quotedWord.matches("^\".*\"$")) {
+ wordsWithQuotesMerged.add(quotedWord);
+ } else {
+ throw new CLIException("trailing quote found in input " + wordsSplitOnQuotes);
+ }
+ } else {
+ wordsWithQuotesMerged.add(wordsSplitOnQuotes.get(i++));
+ }
+ }
+
+ return wordsWithQuotesMerged;
+ }
+
+ /**
+ * This method splits the words on an array list into an array list where each portion of the
+ * line is split into words by '=', quoted words are ignored Example: aaa = bbb = ccc=ddd=eee =
+ * becomes [aaa ],[=],[bbb ],[=],[ccc],[=],[ddd],[=],[eee ],[=].
+ *
+ * @param words the words
+ * @return the merged array list
+ */
+ private ArrayList<String> splitOnEquals(final ArrayList<String> words) {
+ final ArrayList<String> wordsSplitOnEquals = new ArrayList<>();
+
+ for (final String word : words) {
+ // Is this a quoted word ?
+ if (word.startsWith("\"")) {
+ wordsSplitOnEquals.add(word);
+ continue;
+ }
+
+ // Split on equals character
+ final ArrayList<String> splitWords = splitOnChar(word, '=');
+ for (final String splitWord : splitWords) {
+ wordsSplitOnEquals.add(splitWord);
+ }
+ }
+
+ return wordsSplitOnEquals;
+ }
+
+ /**
+ * This method merges an array with separate equals into an array with equals delimiting the
+ * start of words Example: [aaa ],[=],[bbb ],[=],[ccc],[=],[ddd],[=],[eee ],[=] becomes [aaa
+ * ],[= bbb ],[= ccc],[=ddd],[=eee ],[=].
+ *
+ * @param wordsSplitOnEquals the words split on equals
+ * @return the merged array list
+ */
+ private ArrayList<String> mergeEquals(final ArrayList<String> wordsSplitOnEquals) {
+ final ArrayList<String> wordsWithEqualsMerged = new ArrayList<>();
+
+ for (int i = 0; i < wordsSplitOnEquals.size();) {
+ // Is this a quoted word ?
+ if (wordsSplitOnEquals.get(i).startsWith("\"")) {
+ wordsWithEqualsMerged.add(wordsSplitOnEquals.get(i));
+ continue;
+ }
+
+ if (wordsSplitOnEquals.get(i).equals("=")) {
+ if (i < wordsSplitOnEquals.size() - 1 && !wordsSplitOnEquals.get(i + 1).startsWith("=")) {
+ wordsWithEqualsMerged.add(wordsSplitOnEquals.get(i) + wordsSplitOnEquals.get(i + 1));
+ i += 2;
+ } else {
+ wordsWithEqualsMerged.add(wordsSplitOnEquals.get(i++));
+ }
+ } else {
+ wordsWithEqualsMerged.add(wordsSplitOnEquals.get(i++));
+ }
+ }
+
+ return wordsWithEqualsMerged;
+ }
+
+ /**
+ * This method merges words that start with an '=' character with the previous word if that word
+ * does not start with an '='.
+ *
+ * @param words the words
+ * @return the merged array list
+ */
+ private ArrayList<String> mergeArguments(final ArrayList<String> words) {
+ final ArrayList<String> mergedArguments = new ArrayList<>();
+
+ for (int i = 0; i < words.size(); i++) {
+ // Is this a quoted word ?
+ if (words.get(i).startsWith("\"")) {
+ mergedArguments.add(words.get(i));
+ continue;
+ }
+
+ if (words.get(i).startsWith("=")) {
+ if (i > 0 && !words.get(i - 1).startsWith("=")) {
+ mergedArguments.remove(mergedArguments.size() - 1);
+ mergedArguments.add(words.get(i - 1) + words.get(i));
+ } else {
+ mergedArguments.add(words.get(i));
+ }
+ } else {
+ mergedArguments.add(words.get(i));
+ }
+ }
+
+ return mergedArguments;
+ }
+
+ /**
+ * This method strips all non quoted white space down to single spaces and splits non-quoted
+ * words into separate words.
+ *
+ * @param words the words
+ * @return the array list with white space stripped and words split
+ */
+ private ArrayList<String> stripAndSplitWords(final ArrayList<String> words) {
+ final ArrayList<String> strippedAndSplitWords = new ArrayList<>();
+
+ for (String word : words) {
+ // Is this a quoted word
+ if (word.startsWith("\"")) {
+ strippedAndSplitWords.add(word);
+ continue;
+ }
+
+ // Strip white space by replacing all white space with blanks and then removing leading
+ // and trailing blanks
+ word = word.replaceAll("\\s+", " ").trim();
+
+ if (word.length() == 0) {
+ continue;
+ }
+
+ // Split on space characters
+ final String[] splitWords = word.split(" ");
+ for (final String splitWord : splitWords) {
+ strippedAndSplitWords.add(splitWord);
+ }
+ }
+
+ return strippedAndSplitWords;
+ }
+
+ /**
+ * This method splits a line of text into an array list where each portion of the line is split
+ * into words by a character, with the characters themselves as separate words Example: Humpty
+ * "Dumpty sat on the wall", Humpty Dumpty had ""a "great" fall becomes [Humpty ],["],[Dumpty
+ * sat on the wall],["],[, Humpty Dumpty had ],["],["],a ["],[great],["],[ fall].
+ *
+ * @param line the input line
+ * @param splitChar the split char
+ * @return the split array list
+ */
+ private ArrayList<String> splitOnChar(final String line, final char splitChar) {
+ final ArrayList<String> wordsSplitOnQuotes = new ArrayList<>();
+
+ int currentPos = 0;
+ while (currentPos != -1) {
+ final int quotePos = line.indexOf(splitChar, currentPos);
+ if (quotePos != -1) {
+ if (currentPos < quotePos) {
+ wordsSplitOnQuotes.add(line.substring(currentPos, quotePos));
+ }
+ wordsSplitOnQuotes.add("" + splitChar);
+ currentPos = quotePos + 1;
+
+ if (currentPos == line.length()) {
+ currentPos = -1;
+ }
+ } else {
+ wordsSplitOnQuotes.add(line.substring(currentPos));
+ currentPos = quotePos;
+ }
+ }
+
+ return wordsSplitOnQuotes;
+ }
+
+ /**
+ * This method checks that an array list containing a command is in the correct format.
+ *
+ * @param commandWords the command words
+ * @param logicBlock A block of logic code to be taken literally
+ * @return the checked array list
+ */
+ private ArrayList<String> checkFormat(final ArrayList<String> commandWords, final String logicBlock) {
+ // There should be at least one word
+ if (commandWords.size() == 0) {
+ return commandWords;
+ }
+
+ // The first word must be alphanumeric, that is a command
+ if (!commandWords.get(0).matches("^[a-zA-Z0-9]*$")) {
+ throw new CLIException(
+ "first command word is not alphanumeric or is not a command: " + commandWords.get(0));
+ }
+
+ // Now check that we have a sequence of commands at the beginning
+ int currentWordPos = 0;
+ while (currentWordPos < commandWords.size()) {
+ if (commandWords.get(currentWordPos).matches("^[a-zA-Z0-9]*$")) {
+ currentWordPos++;
+ } else {
+ break;
+ }
+ }
+
+ while (currentWordPos < commandWords.size()) {
+ // From now on we should have a sequence of parameters with arguments delimited by a
+ // single '=' character
+ if (currentWordPos < commandWords.size() - 1 || logicBlock == null) {
+ // No logic block
+ if (commandWords.get(currentWordPos).matches("^[a-zA-Z0-9]+=[a-zA-Z0-9/\"].*$")) {
+ currentWordPos++;
+ } else {
+ throw new CLIException(
+ "command argument is not properly formed: " + commandWords.get(currentWordPos));
+ }
+ } else {
+ // Logic block
+ if (commandWords.get(currentWordPos).matches("^[a-zA-Z0-9]+=")) {
+ commandWords.set(currentWordPos, commandWords.get(currentWordPos) + logicBlock);
+ currentWordPos++;
+ } else {
+ throw new CLIException(
+ "command argument is not properly formed: " + commandWords.get(currentWordPos));
+ }
+ }
+ }
+
+ return commandWords;
+ }
+}
diff --git a/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/CLIParameterParser.java b/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/CLIParameterParser.java
new file mode 100644
index 000000000..382f23fb0
--- /dev/null
+++ b/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/CLIParameterParser.java
@@ -0,0 +1,156 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2016-2018 Ericsson. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.apex.auth.clieditor;
+
+import java.nio.file.Paths;
+import java.util.Arrays;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.DefaultParser;
+import org.apache.commons.cli.HelpFormatter;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.Options;
+import org.apache.commons.cli.ParseException;
+
+/**
+ * This class reads and handles command line parameters to the Apex CLI editor.
+ *
+ * @author Liam Fallon (liam.fallon@ericsson.com)
+ */
+public class CLIParameterParser {
+ private static final int MAX_HELP_LINE_LENGTH = 120;
+
+ // Apache Commons CLI options
+ private final Options options;
+
+ /**
+ * Construct the options for the CLI editor.
+ */
+ public CLIParameterParser() {
+ options = new Options();
+ options.addOption(Option.builder("h").longOpt("help").desc("outputs the usage of this command").required(false)
+ .type(Boolean.class).build());
+ options.addOption(Option.builder("m").longOpt("metadata-file").desc("name of the command metadata file to use")
+ .hasArg().argName("CMD_METADATA_FILE").required(false).type(String.class).build());
+ options.addOption(
+ Option.builder("a").longOpt("model-props-file").desc("name of the apex model properties file to use")
+ .hasArg().argName("MODEL_PROPS_FILE").required(false).type(String.class).build());
+ options.addOption(Option.builder("c").longOpt("command-file")
+ .desc("name of a file containing editor commands to run into the editor").hasArg()
+ .argName("COMMAND_FILE").required(false).type(String.class).build());
+ options.addOption(Option.builder("l").longOpt("log-file")
+ .desc("name of a file that will contain command logs from the editor, will log to standard output if not specified or suppressed with \"-nl\" flag")
+ .hasArg().argName("LOG_FILE").required(false).type(String.class).build());
+ options.addOption(Option.builder("nl").longOpt("no-log")
+ .desc("if specified, no logging or output of commands to standard output or log file is carried out")
+ .required(false).type(Boolean.class).build());
+ options.addOption(Option.builder("nm").longOpt("no-model-output")
+ .desc("if specified, no output of a model to standard output or model output file is carried out, "
+ + "the user can use the \"save\" command in a script to save a model")
+ .required(false).type(Boolean.class).build());
+ options.addOption(Option.builder("i").longOpt("input-model-file")
+ .desc("name of a file that contains an input model for the editor").hasArg().argName("INPUT_MODEL_FILE")
+ .required(false).type(String.class).build());
+ options.addOption(Option.builder("o").longOpt("output-model-file")
+ .desc("name of a file that will contain the output model for the editor, "
+ + "will output model to standard output if not specified or suppressed with \"-nm\" flag")
+ .hasArg().argName("OUTPUT_MODEL_FILE").required(false).type(String.class).build());
+ options.addOption(Option.builder("if").longOpt("ignore-failures")
+ .desc("true or false, ignore failures of commands in command files and continue executing the command file")
+ .hasArg().argName("IGNORE_FAILURES_FLAG").required(false).type(Boolean.class).build());
+ options.addOption(Option.builder("wd").longOpt("working-directory")
+ .desc("the working directory that is the root for the CLI editor and is the root from which to look for included macro files")
+ .hasArg().argName("WORKING_DIRECTORY").required(false).type(String.class).build());
+ }
+
+ /**
+ * Parse the command line options.
+ *
+ * @param args The arguments
+ * @return the CLI parameters
+ */
+ public CLIParameters parse(final String[] args) {
+ CommandLine commandLine = null;
+ try {
+ commandLine = new DefaultParser().parse(options, args);
+ } catch (final ParseException e) {
+ throw new CLIException("invalid command line arguments specified : " + e.getMessage());
+ }
+
+ final CLIParameters parameters = new CLIParameters();
+ final String[] remainingArgs = commandLine.getArgs();
+
+ if (remainingArgs.length > 0) {
+ throw new CLIException("too many command line arguments specified : " + Arrays.toString(remainingArgs));
+ }
+
+ if (commandLine.hasOption('h')) {
+ parameters.setHelp(true);
+ }
+ if (commandLine.hasOption('m')) {
+ parameters.setMetadataFileName(commandLine.getOptionValue('m'));
+ }
+ if (commandLine.hasOption('a')) {
+ parameters.setApexPorpertiesFileName(commandLine.getOptionValue('a'));
+ }
+ if (commandLine.hasOption('c')) {
+ parameters.setCommandFileName(commandLine.getOptionValue('c'));
+ }
+ if (commandLine.hasOption('l')) {
+ parameters.setLogFileName(commandLine.getOptionValue('l'));
+ }
+ if (commandLine.hasOption("nl")) {
+ parameters.setSuppressLog(true);
+ }
+ if (commandLine.hasOption("nm")) {
+ parameters.setSuppressModelOutput(true);
+ }
+ if (commandLine.hasOption('i')) {
+ parameters.setInputModelFileName(commandLine.getOptionValue('i'));
+ }
+ if (commandLine.hasOption('o')) {
+ parameters.setOutputModelFileName(commandLine.getOptionValue('o'));
+ }
+ if (commandLine.hasOption("if")) {
+ parameters.setIgnoreCommandFailuresSet(true);
+ parameters.setIgnoreCommandFailures(Boolean.valueOf(commandLine.getOptionValue("if")));
+ } else {
+ parameters.setIgnoreCommandFailuresSet(false);
+ }
+ if (commandLine.hasOption("wd")) {
+ parameters.setWorkingDirectory(commandLine.getOptionValue("wd"));
+ } else {
+ parameters.setWorkingDirectory(Paths.get("").toAbsolutePath().toString());
+ }
+
+ return parameters;
+ }
+
+ /**
+ * Print help information.
+ *
+ * @param mainClassName the main class name
+ */
+ public void help(final String mainClassName) {
+ final HelpFormatter helpFormatter = new HelpFormatter();
+ helpFormatter.printHelp(MAX_HELP_LINE_LENGTH, mainClassName + " [options...]", "options", options, "");
+ }
+}
diff --git a/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/CLIParameters.java b/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/CLIParameters.java
new file mode 100644
index 000000000..476d17e3f
--- /dev/null
+++ b/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/CLIParameters.java
@@ -0,0 +1,571 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2016-2018 Ericsson. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.apex.auth.clieditor;
+
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import org.onap.policy.apex.model.utilities.ResourceUtils;
+
+/**
+ * This class reads and handles command line parameters to the Apex CLI editor.
+ *
+ * @author Liam Fallon (liam.fallon@ericsson.com)
+ */
+public class CLIParameters {
+ // Default location of the command definition meta data in JSON
+ private static final String JSON_COMMAND_METADATA_RESOURCE = "etc/editor/Commands.json";
+ private static final String APEX_MODEL_PROPERTIES_RESOURCE = "etc/editor/ApexModelProperties.json";
+
+ // The editor parameters
+ private boolean helpSet = false;
+ private String metadataFileName = null;
+ private String apexPropertiesFileName = null;
+ private String commandFileName = null;
+ private String inputModelFileName = null;
+ private String outputModelFileName = null;
+ private String workingDirectory = null;
+ private String logFileName = null;
+ private boolean echo = false;
+ private boolean suppressLog = false;
+ private boolean suppressModelOutput = false;
+ private boolean ignoreCommandFailuresSet = false;
+ private boolean ignoreCommandFailures = false;
+
+ /**
+ * Validates the command line parameters.
+ */
+ public void validate() {
+ validateReadableFile("Metadata File", metadataFileName);
+ validateReadableFile("Properties File", apexPropertiesFileName);
+ validateReadableFile("Command File", commandFileName);
+ validateReadableFile("Input Model File", inputModelFileName);
+ validateWritableFile("Output Model File", outputModelFileName);
+ validateWritableFile("Log File", logFileName);
+ validateWritableDirectory("Working Directory", workingDirectory);
+
+ if (isSuppressLogSet()) {
+ setEcho(false);
+ } else {
+ if (checkSetCommandFileName()) {
+ setEcho(true);
+ if (!checkSetIgnoreCommandFailures()) {
+ setIgnoreCommandFailures(false);
+ }
+ } else {
+ setEcho(false);
+ if (!checkSetIgnoreCommandFailures()) {
+ setIgnoreCommandFailures(true);
+ }
+ }
+ }
+ }
+
+ /**
+ * Gets the command metadata for the editor commands as a stream.
+ *
+ * @return the command metadata for the editor commands as a stream.
+ * @throws IOException the IO exception
+ */
+ public InputStream getMetadataStream() throws IOException {
+ if (metadataFileName == null) {
+ return ResourceUtils.getResourceAsStream(JSON_COMMAND_METADATA_RESOURCE);
+ } else {
+ return new FileInputStream(new File(metadataFileName));
+ }
+ }
+
+ /**
+ * Gets the location of command metadata for the editor commands.
+ *
+ * @return the location of command metadata for the editor commands
+ */
+ public String getMetadataLocation() {
+ if (metadataFileName == null) {
+ return "resource: \"" + JSON_COMMAND_METADATA_RESOURCE + "\"";
+ } else {
+ return "file: \"" + metadataFileName + "\"";
+ }
+ }
+
+ /**
+ * Gets the properties that are used for command default values as a stream.
+ *
+ * @return the properties that are used for command default values as a stream
+ * @throws IOException the IO exception
+ */
+ public InputStream getApexPropertiesStream() throws IOException {
+ if (apexPropertiesFileName == null) {
+ return ResourceUtils.getResourceAsStream(APEX_MODEL_PROPERTIES_RESOURCE);
+ } else {
+ return new FileInputStream(new File(apexPropertiesFileName));
+ }
+ }
+
+ /**
+ * Gets the location of the properties that are used for command default values.
+ *
+ * @return the location of the properties that are used for command default values
+ */
+ public String getApexPropertiesLocation() {
+ if (metadataFileName == null) {
+ return "resource: \"" + APEX_MODEL_PROPERTIES_RESOURCE + "\"";
+ } else {
+ return "file: \"" + apexPropertiesFileName + "\"";
+ }
+ }
+
+ /**
+ * Gets the input stream on which commands are being received.
+ *
+ * @return the input stream on which commands are being received
+ * @throws IOException the IO exception
+ */
+ public InputStream getCommandInputStream() throws IOException {
+ if (commandFileName == null) {
+ return System.in;
+ } else {
+ return new FileInputStream(new File(commandFileName));
+ }
+ }
+
+ /**
+ * Gets the output stream on which command result messages are being output.
+ *
+ * @return the output stream on which command result messages are being output
+ * @throws IOException the IO exception
+ */
+ public OutputStream getOutputStream() throws IOException {
+ // Check if log suppression is active, if so, consume all output on a byte array output
+ // stream
+ if (isSuppressLogSet()) {
+ return new ByteArrayOutputStream();
+
+ }
+ if (logFileName == null) {
+ return System.out;
+ } else {
+ return new FileOutputStream(new File(logFileName), true);
+ }
+ }
+
+ /**
+ * Validate that a file is readable.
+ *
+ * @param fileTag the file tag, a tag used for information and error messages
+ * @param fileName the file name to check
+ */
+ private void validateReadableFile(final String fileTag, final String fileName) {
+ if (fileName == null) {
+ return;
+ }
+ final File theFile = new File(fileName);
+ if (!theFile.exists()) {
+ throw new CLIException("file " + fileName + " of type " + fileTag + " does not exist");
+ }
+ if (!theFile.isFile()) {
+ throw new CLIException("file " + fileName + " of type " + fileTag + " is not a normal file");
+ }
+ if (!theFile.canRead()) {
+ throw new CLIException("file " + fileName + " of type " + fileTag + " is ureadable");
+ }
+ }
+
+ /**
+ * Validate that a file is writable.
+ *
+ * @param fileTag the file tag, a tag used for information and error messages
+ * @param fileName the file name to check
+ */
+ private void validateWritableFile(final String fileTag, final String fileName) {
+ if (fileName == null) {
+ return;
+ }
+ final File theFile = new File(fileName);
+ if (theFile.exists()) {
+ if (!theFile.isFile()) {
+ throw new CLIException("file " + fileName + " of type " + fileTag + " is not a normal file");
+ }
+ if (!theFile.canWrite()) {
+ throw new CLIException("file " + fileName + " of type " + fileTag + " cannot be written");
+ }
+ } else {
+ try {
+ theFile.createNewFile();
+ } catch (final IOException e) {
+ throw new CLIException("file " + fileName + " cannot be created: " + e.getMessage());
+ }
+ }
+ }
+
+ /**
+ * Validate that a directory exists and is writable.
+ *
+ * @param directoryTag the directory tag, a tag used for information and error messages
+ * @param directoryName the directory name to check
+ */
+ private void validateWritableDirectory(final String directoryTag, final String directoryName) {
+ if (directoryName == null) {
+ return;
+ }
+ final File theDirectory = new File(directoryName);
+ if (theDirectory.exists()) {
+ if (!theDirectory.isDirectory()) {
+ throw new CLIException(
+ "directory " + directoryName + " of type " + directoryTag + " is not a directory");
+ }
+ if (!theDirectory.canWrite()) {
+ throw new CLIException(
+ "directory " + directoryName + " of type " + directoryTag + " cannot be written");
+ }
+ }
+ }
+
+ /**
+ * Checks if help is set.
+ *
+ * @return true, if help is set
+ */
+ public boolean isHelpSet() {
+ return helpSet;
+ }
+
+ /**
+ * Sets whether the help flag is set or not.
+ *
+ * @param isHelpSet the value of the help flag
+ */
+ public void setHelp(final boolean isHelpSet) {
+ this.helpSet = isHelpSet;
+ }
+
+ /**
+ * Gets the file name of the command metadata file for the editor commands.
+ *
+ * @return the file name of the command metadata file for the editor commands
+ */
+ public String getMetadataFileName() {
+ return metadataFileName;
+ }
+
+ /**
+ * Sets the file name of the command metadata file for the editor commands.
+ *
+ * @param metadataFileName the file name of the command metadata file for the editor commands
+ */
+ public void setMetadataFileName(final String metadataFileName) {
+ this.metadataFileName = metadataFileName.trim();
+ }
+
+ /**
+ * Check if the file name of the command metadata file for the editor commands is set.
+ *
+ * @return true, if the file name of the command metadata file for the editor commands is set
+ */
+ public boolean checkSetMetadataFileName() {
+ return metadataFileName != null && metadataFileName.length() > 0;
+ }
+
+ /**
+ * Gets the file name of the file containing properties that are used for command default
+ * values.
+ *
+ * @return the file name of the file containing properties that are used for command default
+ * values
+ */
+ public String getApexPorpertiesFileName() {
+ return apexPropertiesFileName;
+ }
+
+ /**
+ * Sets the file name of the file containing properties that are used for command default
+ * values.
+ *
+ * @param apexPorpertiesFileName the file name of the file containing properties that are used
+ * for command default values
+ */
+ public void setApexPorpertiesFileName(final String apexPorpertiesFileName) {
+ apexPropertiesFileName = apexPorpertiesFileName.trim();
+ }
+
+ /**
+ * Check if the file name of the file containing properties that are used for command default
+ * values is set.
+ *
+ * @return true, if the file name of the file containing properties that are used for command
+ * default values is set
+ */
+ public boolean checkSetApexPropertiesFileName() {
+ return apexPropertiesFileName != null && apexPropertiesFileName.length() > 0;
+ }
+
+ /**
+ * Gets the name of the file containing commands to be streamed into the CLI editor.
+ *
+ * @return the name of the file containing commands to be streamed into the CLI editor
+ */
+ public String getCommandFileName() {
+ return commandFileName;
+ }
+
+ /**
+ * Sets the name of the file containing commands to be streamed into the CLI editor.
+ *
+ * @param commandFileName the name of the file containing commands to be streamed into the CLI
+ * editor
+ */
+ public void setCommandFileName(final String commandFileName) {
+ this.commandFileName = commandFileName.trim();
+ }
+
+ /**
+ * Check if the name of the file containing commands to be streamed into the CLI editor is set.
+ *
+ * @return true, if the name of the file containing commands to be streamed into the CLI editor
+ * is set
+ */
+ public boolean checkSetCommandFileName() {
+ return commandFileName != null && commandFileName.length() > 0;
+ }
+
+ /**
+ * Gets the name of the file containing the Apex model that will be used to initialize the Apex
+ * model in the CLI editor.
+ *
+ * @return the name of the file containing the Apex model that will be used to initialize the
+ * Apex model in the CLI editor
+ */
+ public String getInputModelFileName() {
+ return inputModelFileName;
+ }
+
+ /**
+ * Sets the name of the file containing the Apex model that will be used to initialize the Apex
+ * model in the CLI editor.
+ *
+ * @param inputModelFileName the name of the file containing the Apex model that will be used to
+ * initialize the Apex model in the CLI editor
+ */
+ public void setInputModelFileName(final String inputModelFileName) {
+ this.inputModelFileName = inputModelFileName.trim();
+ }
+
+ /**
+ * Check if the name of the file containing the Apex model that will be used to initialize the
+ * Apex model in the CLI editor is set.
+ *
+ * @return true, if the name of the file containing the Apex model that will be used to
+ * initialize the Apex model in the CLI editor is set
+ */
+ public boolean checkSetInputModelFileName() {
+ return inputModelFileName != null && inputModelFileName.length() > 0;
+ }
+
+ /**
+ * Gets the name of the file that the Apex CLI editor will save the Apex model to when it exits.
+ *
+ * @return the name of the file that the Apex CLI editor will save the Apex model to when it
+ * exits
+ */
+ public String getOutputModelFileName() {
+ return outputModelFileName;
+ }
+
+ /**
+ * Sets the name of the file that the Apex CLI editor will save the Apex model to when it exits.
+ *
+ * @param outputModelFileName the name of the file that the Apex CLI editor will save the Apex
+ * model to when it exits
+ */
+ public void setOutputModelFileName(final String outputModelFileName) {
+ this.outputModelFileName = outputModelFileName.trim();
+ }
+
+ /**
+ * Check if the name of the file that the Apex CLI editor will save the Apex model to when it
+ * exits is set.
+ *
+ * @return true, if the name of the file that the Apex CLI editor will save the Apex model to
+ * when it exits is set
+ */
+ public boolean checkSetOutputModelFileName() {
+ return outputModelFileName != null && outputModelFileName.length() > 0;
+ }
+
+ /**
+ * Gets the working directory that is the root for CLI editor macro includes.
+ *
+ * @return the CLI editor working directory
+ */
+ public String getWorkingDirectory() {
+ return workingDirectory;
+ }
+
+ /**
+ * Sets the working directory that is the root for CLI editor macro includes.
+ *
+ * @param workingDirectory the CLI editor working directory
+ */
+ public void setWorkingDirectory(final String workingDirectory) {
+ this.workingDirectory = workingDirectory.trim();
+ }
+
+ /**
+ * Gets the name of the file to which the Apex CLI editor will log commands and responses.
+ *
+ * @return the name of the file to which the Apex CLI editor will log commands and responses
+ */
+ public String getLogFileName() {
+ return logFileName;
+ }
+
+ /**
+ * Sets the name of the file to which the Apex CLI editor will log commands and responses.
+ *
+ * @param logFileName the name of the file to which the Apex CLI editor will log commands and
+ * responses
+ */
+ public void setLogFileName(final String logFileName) {
+ this.logFileName = logFileName.trim();
+ }
+
+ /**
+ * Check if the name of the file to which the Apex CLI editor will log commands and responses is
+ * set.
+ *
+ * @return true, if the name of the file to which the Apex CLI editor will log commands and
+ * responses is set
+ */
+ public boolean checkSetLogFileName() {
+ return logFileName != null;
+ }
+
+ /**
+ * Checks if the Apex CLI editor is set to echo commands that have been entered.
+ *
+ * @return true, if the Apex CLI editor is set to echo commands that have been entered
+ */
+ public boolean isEchoSet() {
+ return echo;
+ }
+
+ /**
+ * Sets whether the Apex CLI editor should echo commands that have been entered.
+ *
+ * @param echo true, if the Apex CLI editor should echo commands that have been entered
+ */
+ public void setEcho(final boolean echo) {
+ this.echo = echo;
+ }
+
+ /**
+ * Checks whether the Apex CLI editor is set to suppress logging of command output.
+ *
+ * @return true, if the Apex CLI editor is set to suppress logging of command output.
+ */
+ public boolean isSuppressLogSet() {
+ return suppressLog;
+ }
+
+ /**
+ * Sets whether the Apex CLI editor should suppress logging of command output.
+ *
+ * @param suppressLog true, if the Apex CLI editor should suppress logging of command output
+ */
+ public void setSuppressLog(final boolean suppressLog) {
+ this.suppressLog = suppressLog;
+ }
+
+ /**
+ * Checks whether the Apex CLI editor is set to suppress output of its Apex model on exit.
+ *
+ * @return true, if checks if the Apex CLI editor is set to suppress output of its Apex model on
+ * exit
+ */
+ public boolean isSuppressModelOutputSet() {
+ return suppressModelOutput;
+ }
+
+ /**
+ * Sets whether the Apex CLI editor should suppress output of its Apex model on exit.
+ *
+ * @param suppressModelOutput true, if the Apex CLI editor should suppress output of its Apex
+ * model on exit
+ */
+ public void setSuppressModelOutput(final boolean suppressModelOutput) {
+ this.suppressModelOutput = suppressModelOutput;
+ }
+
+ /**
+ * Check if the command failures flag is set
+ *
+ * @return true if the command failures flag has been set
+ */
+ public boolean checkSetIgnoreCommandFailures() {
+ return ignoreCommandFailuresSet;
+ }
+
+ /**
+ * Checks if the command failures flag is set.
+ *
+ * @param ignoreCommandFailuresSet true if the command failures flag has been set
+ */
+ public void setIgnoreCommandFailuresSet(final boolean ignoreCommandFailuresSet) {
+ this.ignoreCommandFailuresSet = ignoreCommandFailuresSet;
+ }
+
+ /**
+ * Checks if command failures should be ignored and command execution continue.
+ *
+ * @return true if command failures should be ignored
+ */
+ public boolean isIgnoreCommandFailures() {
+ return ignoreCommandFailures;
+ }
+
+ /**
+ * Sets if command errors should be ignored and command execution continue.
+ *
+ * @param ignoreCommandFailures true if command errors should be ignored
+ */
+ public void setIgnoreCommandFailures(final boolean ignoreCommandFailures) {
+ this.ignoreCommandFailures = ignoreCommandFailures;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.lang.Object#toString()
+ */
+ @Override
+ public String toString() {
+ return "CLIParameters [helpSet=" + helpSet + ", metadataFileName=" + metadataFileName
+ + ", apexPropertiesFileName=" + apexPropertiesFileName + ", commandFileName=" + commandFileName
+ + ", inputModelFileName=" + inputModelFileName + ", outputModelFileName=" + outputModelFileName
+ + ", logFileName=" + logFileName + ", echo=" + echo + ", suppressLog=" + suppressLog
+ + ", suppressModelOutput=" + suppressModelOutput + "]";
+ }
+}
diff --git a/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/KeywordNode.java b/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/KeywordNode.java
new file mode 100644
index 000000000..06579e073
--- /dev/null
+++ b/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/KeywordNode.java
@@ -0,0 +1,188 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2016-2018 Ericsson. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.apex.auth.clieditor;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+import java.util.TreeMap;
+import java.util.TreeSet;
+
+import org.onap.policy.apex.model.utilities.Assertions;
+
+/**
+ * The Class KeywordNode holds the structure of a command keyword for the Apex CLI editor. The
+ * keyword itself and all its children are held as a recursive tree. This class is used to manage
+ * interactive sub-modes in the Apex CLI editor.
+ */
+public class KeywordNode implements Comparable<KeywordNode> {
+ private final String keyword;
+ private final TreeMap<String, KeywordNode> children;
+ private CLICommand command;
+
+ /**
+ * This Constructor creates a keyword node with the given keyword and no command.
+ *
+ * @param keyword the keyword of the node
+ */
+ public KeywordNode(final String keyword) {
+ this(keyword, null);
+ }
+
+ /**
+ * This Constructor creates a keyword node with the given keyword and command.
+ *
+ * @param keyword the keyword of the keyword node
+ * @param command the command associated with this keyword
+ */
+ public KeywordNode(final String keyword, final CLICommand command) {
+ Assertions.argumentNotNull(keyword, "commands may not be null");
+
+ this.keyword = keyword;
+ children = new TreeMap<>();
+ this.command = command;
+ }
+
+ /**
+ * Process a list of keywords on this keyword node, recursing the keyword node tree, creating
+ * new branches for the keyword list if required. When the end of a branch has been reached,
+ * store the command in that keyword node..
+ *
+ * @param keywordList the list of keywords to process on this keyword node
+ * @param incomingCommand the command
+ */
+ public void processKeywords(final List<String> keywordList, final CLICommand incomingCommand) {
+ if (keywordList.size() <= 0) {
+ this.command = incomingCommand;
+ return;
+ }
+
+ if (!children.containsKey(keywordList.get(0))) {
+ children.put(keywordList.get(0), new KeywordNode(keywordList.get(0)));
+ }
+
+ final ArrayList<String> nextLevelKeywordList = new ArrayList<>(keywordList);
+ nextLevelKeywordList.remove(0);
+ children.get(keywordList.get(0)).processKeywords(nextLevelKeywordList, incomingCommand);
+ }
+
+ /**
+ * Adds the system commands to the keyword node.
+ *
+ * @param systemCommandNodes the system command nodes to add to the keyword node
+ */
+ public void addSystemCommandNodes(final Set<KeywordNode> systemCommandNodes) {
+ if (children.isEmpty()) {
+ return;
+ }
+
+ for (final KeywordNode node : children.values()) {
+ node.addSystemCommandNodes(systemCommandNodes);
+ }
+
+ for (final KeywordNode systemCommandNode : systemCommandNodes) {
+ children.put(systemCommandNode.getKeyword(), systemCommandNode);
+ }
+
+ }
+
+ /**
+ * Gets the keyword of this keyword node.
+ *
+ * @return the keyword of this keyword node
+ */
+ public String getKeyword() {
+ return keyword;
+ }
+
+ /**
+ * Gets the children of this keyword node.
+ *
+ * @return the children of this keyword node
+ */
+ public TreeMap<String, KeywordNode> getChildren() {
+ return children;
+ }
+
+ /**
+ * Gets the command of this keyword node.
+ *
+ * @return the command of this keyword node
+ */
+ public CLICommand getCommand() {
+ return command;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.lang.Object#toString()
+ */
+ @Override
+ public String toString() {
+ return "CommandKeywordNode [keyword=" + keyword + ", children=" + children + ", command=" + command + "]";
+ }
+
+ /**
+ * Gets the commands.
+ *
+ * @return the commands
+ */
+ public Set<CLICommand> getCommands() {
+ final Set<CLICommand> commandSet = new TreeSet<>();
+
+ for (final KeywordNode child : children.values()) {
+ if (child.getCommand() != null) {
+ commandSet.add(child.getCommand());
+ }
+ commandSet.addAll(child.getCommands());
+ }
+
+ return commandSet;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.lang.Comparable#compareTo(java.lang.Object)
+ */
+ @Override
+ public int compareTo(final KeywordNode otherKeywordNode) {
+ Assertions.argumentNotNull(otherKeywordNode, "comparison object may not be null");
+
+ if (this == otherKeywordNode) {
+ return 0;
+ }
+ if (getClass() != otherKeywordNode.getClass()) {
+ return this.hashCode() - otherKeywordNode.hashCode();
+ }
+
+ final KeywordNode other = otherKeywordNode;
+
+ if (!keyword.equals(other.keyword)) {
+ return keyword.compareTo(other.keyword);
+ }
+ if (!children.equals(other.children)) {
+ return (children.hashCode() - other.children.hashCode());
+ }
+ return command.compareTo(otherKeywordNode.command);
+ }
+}
diff --git a/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/package-info.java b/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/package-info.java
new file mode 100644
index 000000000..e0e7f746e
--- /dev/null
+++ b/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/package-info.java
@@ -0,0 +1,28 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2016-2018 Ericsson. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+/**
+ * Provides a command line editor for APEX. The command line editor can be used to interactively
+ * from the command line to edit policy models or it can be executed using scripts containing CLI
+ * commands.
+ *
+ * @author Liam Fallon (liam.fallon@ericsson.com)
+ */
+package org.onap.policy.apex.auth.clieditor;