2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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=========================================================
22 package org.openecomp.appc.rankingframework.impl;
24 import java.util.HashMap;
26 import org.openecomp.appc.rankingframework.ConfigurationEntry;
27 import org.openecomp.appc.rankingframework.ConfigurationSet;
28 import com.att.eelf.configuration.EELFLogger;
29 import com.att.eelf.configuration.EELFManager;
31 class RankedAttributesTreeBuilder {
33 private static final EELFLogger logger = EELFManager.getInstance().getLogger(RankedAttributesTreeBuilder.class);
35 private RankedAttributesTreeBuilder() {
38 static <R> CompositeNode<R> build(ConfigurationSet<R> config) {
40 Object[] names = config.getRankedAttributeNames().toArray();
42 CompositeNode<R> root = new CompositeNode<>("ROOT", Constants.DEFAULT_MATCH, null,
43 new HashMap<Object, Node<R>>());
45 if (logger.isDebugEnabled()) {
46 logger.debug(String.format("Building decision tree for ranked attributes: %s", config.getRankedAttributeNames()));
49 for (ConfigurationEntry<R> entry : config.getEntries()) {
50 process(entry, names, root);
56 private static <R> void process(ConfigurationEntry<R> entry, Object[] names, CompositeNode<R> root) {
57 CompositeNode<R> parentNode = null;
58 for (int i = 0; i < names.length; i++) {
64 final String name = (String) names[i];
66 final Object value = value(entry, name);
68 if (i < names.length - 1) {
69 CompositeNode<R> currentNode = (CompositeNode<R>) parentNode.children().get(value);
70 if (currentNode == null) {
71 currentNode = new CompositeNode<>(name, value, parentNode, new HashMap<Object, Node<R>>());
72 parentNode.children().put(value, currentNode);
74 parentNode = currentNode;
76 LeafNode<R> currentNode = (LeafNode<R>) parentNode.children().get(value);
77 if (currentNode == null) {
78 currentNode = new LeafNode<R>(name, value, parentNode, entry.getResult());
79 parentNode.children().put(value, currentNode);
81 if (logger.isDebugEnabled()) {
82 logger.debug(String.format("Branch has been created: %s", currentNode));
86 String.format("Duplicated configuration entry has been detected for attribute '%s' with value '%s' - the node '%s'exists already",
90 throw new IllegalArgumentException("Duplicated configuration entry: " + currentNode);
96 private static <R> Object value(ConfigurationEntry<R> entry, String name) {
97 Object value = entry.getAttributeValue(name);
98 return Utils.value(value);