2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.rankingframework.impl;
27 import java.util.HashMap;
29 import org.onap.appc.rankingframework.ConfigurationEntry;
30 import org.onap.appc.rankingframework.ConfigurationSet;
31 import com.att.eelf.configuration.EELFLogger;
32 import com.att.eelf.configuration.EELFManager;
34 class RankedAttributesTreeBuilder {
36 private static final EELFLogger logger = EELFManager.getInstance().getLogger(RankedAttributesTreeBuilder.class);
38 private RankedAttributesTreeBuilder() {
41 static <R> CompositeNode<R> build(ConfigurationSet<R> config) {
43 Object[] names = config.getRankedAttributeNames().toArray();
45 CompositeNode<R> root = new CompositeNode<>("ROOT", Constants.DEFAULT_MATCH, null,
46 new HashMap<Object, Node<R>>());
48 logDebugWhenEnabled(String.format("Building decision tree for ranked attributes: %s", config.getRankedAttributeNames()));
50 for (ConfigurationEntry<R> entry : config.getEntries()) {
51 process(entry, names, root);
57 private static <R> void process(ConfigurationEntry<R> entry, Object[] names, CompositeNode<R> root) {
58 CompositeNode<R> parentNode = root;
59 for (int i = 0; i < names.length; i++) {
61 final String name = (String) names[i];
63 final Object value = value(entry, name);
65 if (i < names.length - 1) {
66 CompositeNode<R> currentNode = (CompositeNode<R>) parentNode.children().get(value);
67 if (currentNode == null) {
68 currentNode = new CompositeNode<>(name, value, parentNode, new HashMap<Object, Node<R>>());
69 parentNode.children().put(value, currentNode);
71 parentNode = currentNode;
73 LeafNode<R> currentNode = (LeafNode<R>) parentNode.children().get(value);
74 if (currentNode == null) {
75 currentNode = new LeafNode<>(name, value, parentNode, entry.getResult());
76 parentNode.children().put(value, currentNode);
78 logDebugWhenEnabled(String.format("Branch has been created: %s", currentNode));
81 String.format("Duplicated configuration entry has been detected for attribute '%s' with value '%s' - the node '%s'exists already",
85 throw new IllegalArgumentException("Duplicated configuration entry: " + currentNode);
91 private static void logDebugWhenEnabled(String message) {
92 if(logger.isDebugEnabled()) {
93 logger.debug(message);
97 private static <R> Object value(ConfigurationEntry<R> entry, String name) {
98 Object value = entry.getAttributeValue(name);
99 return Utils.value(value);