Adding apex auth modules
[policy/apex-pdp.git] / auth / cli-editor / src / main / java / org / onap / policy / apex / auth / clieditor / KeywordNode.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.auth.clieditor;
22
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Set;
26 import java.util.TreeMap;
27 import java.util.TreeSet;
28
29 import org.onap.policy.apex.model.utilities.Assertions;
30
31 /**
32  * The Class KeywordNode holds the structure of a command keyword for the Apex CLI editor. The
33  * keyword itself and all its children are held as a recursive tree. This class is used to manage
34  * interactive sub-modes in the Apex CLI editor.
35  */
36 public class KeywordNode implements Comparable<KeywordNode> {
37     private final String keyword;
38     private final TreeMap<String, KeywordNode> children;
39     private CLICommand command;
40
41     /**
42      * This Constructor creates a keyword node with the given keyword and no command.
43      *
44      * @param keyword the keyword of the node
45      */
46     public KeywordNode(final String keyword) {
47         this(keyword, null);
48     }
49
50     /**
51      * This Constructor creates a keyword node with the given keyword and command.
52      *
53      * @param keyword the keyword of the keyword node
54      * @param command the command associated with this keyword
55      */
56     public KeywordNode(final String keyword, final CLICommand command) {
57         Assertions.argumentNotNull(keyword, "commands may not be null");
58
59         this.keyword = keyword;
60         children = new TreeMap<>();
61         this.command = command;
62     }
63
64     /**
65      * Process a list of keywords on this keyword node, recursing the keyword node tree, creating
66      * new branches for the keyword list if required. When the end of a branch has been reached,
67      * store the command in that keyword node..
68      *
69      * @param keywordList the list of keywords to process on this keyword node
70      * @param incomingCommand the command
71      */
72     public void processKeywords(final List<String> keywordList, final CLICommand incomingCommand) {
73         if (keywordList.size() <= 0) {
74             this.command = incomingCommand;
75             return;
76         }
77
78         if (!children.containsKey(keywordList.get(0))) {
79             children.put(keywordList.get(0), new KeywordNode(keywordList.get(0)));
80         }
81
82         final ArrayList<String> nextLevelKeywordList = new ArrayList<>(keywordList);
83         nextLevelKeywordList.remove(0);
84         children.get(keywordList.get(0)).processKeywords(nextLevelKeywordList, incomingCommand);
85     }
86
87     /**
88      * Adds the system commands to the keyword node.
89      *
90      * @param systemCommandNodes the system command nodes to add to the keyword node
91      */
92     public void addSystemCommandNodes(final Set<KeywordNode> systemCommandNodes) {
93         if (children.isEmpty()) {
94             return;
95         }
96
97         for (final KeywordNode node : children.values()) {
98             node.addSystemCommandNodes(systemCommandNodes);
99         }
100
101         for (final KeywordNode systemCommandNode : systemCommandNodes) {
102             children.put(systemCommandNode.getKeyword(), systemCommandNode);
103         }
104
105     }
106
107     /**
108      * Gets the keyword of this keyword node.
109      *
110      * @return the keyword of this keyword node
111      */
112     public String getKeyword() {
113         return keyword;
114     }
115
116     /**
117      * Gets the children of this keyword node.
118      *
119      * @return the children of this keyword node
120      */
121     public TreeMap<String, KeywordNode> getChildren() {
122         return children;
123     }
124
125     /**
126      * Gets the command of this keyword node.
127      *
128      * @return the command of this keyword node
129      */
130     public CLICommand getCommand() {
131         return command;
132     }
133
134     /*
135      * (non-Javadoc)
136      *
137      * @see java.lang.Object#toString()
138      */
139     @Override
140     public String toString() {
141         return "CommandKeywordNode [keyword=" + keyword + ", children=" + children + ", command=" + command + "]";
142     }
143
144     /**
145      * Gets the commands.
146      *
147      * @return the commands
148      */
149     public Set<CLICommand> getCommands() {
150         final Set<CLICommand> commandSet = new TreeSet<>();
151
152         for (final KeywordNode child : children.values()) {
153             if (child.getCommand() != null) {
154                 commandSet.add(child.getCommand());
155             }
156             commandSet.addAll(child.getCommands());
157         }
158
159         return commandSet;
160     }
161
162     /*
163      * (non-Javadoc)
164      *
165      * @see java.lang.Comparable#compareTo(java.lang.Object)
166      */
167     @Override
168     public int compareTo(final KeywordNode otherKeywordNode) {
169         Assertions.argumentNotNull(otherKeywordNode, "comparison object may not be null");
170
171         if (this == otherKeywordNode) {
172             return 0;
173         }
174         if (getClass() != otherKeywordNode.getClass()) {
175             return this.hashCode() - otherKeywordNode.hashCode();
176         }
177
178         final KeywordNode other = otherKeywordNode;
179
180         if (!keyword.equals(other.keyword)) {
181             return keyword.compareTo(other.keyword);
182         }
183         if (!children.equals(other.children)) {
184             return (children.hashCode() - other.children.hashCode());
185         }
186         return command.compareTo(otherKeywordNode.command);
187     }
188 }