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 if (logger.isDebugEnabled()) {
49 logger.debug(String.format("Building decision tree for ranked attributes: %s", config.getRankedAttributeNames()));
52 for (ConfigurationEntry<R> entry : config.getEntries()) {
53 process(entry, names, root);
59 private static <R> void process(ConfigurationEntry<R> entry, Object[] names, CompositeNode<R> root) {
60 CompositeNode<R> parentNode = null;
61 for (int i = 0; i < names.length; i++) {
67 final String name = (String) names[i];
69 final Object value = value(entry, name);
71 if (i < names.length - 1) {
72 CompositeNode<R> currentNode = (CompositeNode<R>) parentNode.children().get(value);
73 if (currentNode == null) {
74 currentNode = new CompositeNode<>(name, value, parentNode, new HashMap<Object, Node<R>>());
75 parentNode.children().put(value, currentNode);
77 parentNode = currentNode;
79 LeafNode<R> currentNode = (LeafNode<R>) parentNode.children().get(value);
80 if (currentNode == null) {
81 currentNode = new LeafNode<R>(name, value, parentNode, entry.getResult());
82 parentNode.children().put(value, currentNode);
84 if (logger.isDebugEnabled()) {
85 logger.debug(String.format("Branch has been created: %s", currentNode));
89 String.format("Duplicated configuration entry has been detected for attribute '%s' with value '%s' - the node '%s'exists already",
93 throw new IllegalArgumentException("Duplicated configuration entry: " + currentNode);
99 private static <R> Object value(ConfigurationEntry<R> entry, String name) {
100 Object value = entry.getAttributeValue(name);
101 return Utils.value(value);