2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright (C) 2017 Amdocs
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.openecomp.appc.rankingframework.impl;
25 import java.util.HashMap;
27 import org.openecomp.appc.rankingframework.ConfigurationEntry;
28 import org.openecomp.appc.rankingframework.ConfigurationSet;
29 import com.att.eelf.configuration.EELFLogger;
30 import com.att.eelf.configuration.EELFManager;
32 class RankedAttributesTreeBuilder {
34 private static final EELFLogger logger = EELFManager.getInstance().getLogger(RankedAttributesTreeBuilder.class);
36 private RankedAttributesTreeBuilder() {
39 static <R> CompositeNode<R> build(ConfigurationSet<R> config) {
41 Object[] names = config.getRankedAttributeNames().toArray();
43 CompositeNode<R> root = new CompositeNode<>("ROOT", Constants.DEFAULT_MATCH, null,
44 new HashMap<Object, Node<R>>());
46 if (logger.isDebugEnabled()) {
47 logger.debug(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 = null;
59 for (int i = 0; i < names.length; i++) {
65 final String name = (String) names[i];
67 final Object value = value(entry, name);
69 if (i < names.length - 1) {
70 CompositeNode<R> currentNode = (CompositeNode<R>) parentNode.children().get(value);
71 if (currentNode == null) {
72 currentNode = new CompositeNode<>(name, value, parentNode, new HashMap<Object, Node<R>>());
73 parentNode.children().put(value, currentNode);
75 parentNode = currentNode;
77 LeafNode<R> currentNode = (LeafNode<R>) parentNode.children().get(value);
78 if (currentNode == null) {
79 currentNode = new LeafNode<R>(name, value, parentNode, entry.getResult());
80 parentNode.children().put(value, currentNode);
82 if (logger.isDebugEnabled()) {
83 logger.debug(String.format("Branch has been created: %s", currentNode));
87 String.format("Duplicated configuration entry has been detected for attribute '%s' with value '%s' - the node '%s'exists already",
91 throw new IllegalArgumentException("Duplicated configuration entry: " + currentNode);
97 private static <R> Object value(ConfigurationEntry<R> entry, String name) {
98 Object value = entry.getAttributeValue(name);
99 return Utils.value(value);