Updating licenses in all files
[appc.git] / appc-dispatcher / appc-dispatcher-common / ranking-framework-lib / src / main / java / org / openecomp / appc / rankingframework / impl / RankedAttributesTreeBuilder.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * APPC
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
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
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.
21  */
22
23 package org.openecomp.appc.rankingframework.impl;
24
25 import java.util.HashMap;
26
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;
31
32 class RankedAttributesTreeBuilder {
33
34     private static final EELFLogger logger = EELFManager.getInstance().getLogger(RankedAttributesTreeBuilder.class);
35
36     private RankedAttributesTreeBuilder() {
37     }
38
39     static <R> CompositeNode<R> build(ConfigurationSet<R> config) {
40
41         Object[] names = config.getRankedAttributeNames().toArray();
42
43         CompositeNode<R> root = new CompositeNode<>("ROOT", Constants.DEFAULT_MATCH, null,
44                 new HashMap<Object, Node<R>>());
45
46         if (logger.isDebugEnabled()) {
47             logger.debug(String.format("Building decision tree for ranked attributes: %s", config.getRankedAttributeNames()));
48         }
49
50         for (ConfigurationEntry<R> entry : config.getEntries()) {
51             process(entry, names, root);
52         }
53
54         return root;
55     }
56
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++) {
60
61             if (i == 0) {
62                 parentNode = root;
63             }
64
65             final String name = (String) names[i];
66
67             final Object value = value(entry, name);
68
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);
74                 }
75                 parentNode = currentNode;
76             } else {
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);
81
82                     if (logger.isDebugEnabled()) {
83                         logger.debug(String.format("Branch has been created: %s", currentNode));
84                     }
85                 } else {
86                     logger.error(
87                             String.format("Duplicated configuration entry has been detected for attribute '%s' with value '%s' - the node '%s'exists already",
88                                     name,
89                                     value,
90                                     currentNode));
91                     throw new IllegalArgumentException("Duplicated configuration entry: " + currentNode);
92                 }
93             }
94         }
95     }
96
97     private static <R> Object value(ConfigurationEntry<R> entry, String name) {
98         Object value = entry.getAttributeValue(name);
99         return Utils.value(value);
100     }
101 }