Moving all files to root directory
[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  * openECOMP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
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  */
21
22 package org.openecomp.appc.rankingframework.impl;
23
24 import java.util.HashMap;
25
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;
30
31 class RankedAttributesTreeBuilder {
32
33     private static final EELFLogger logger = EELFManager.getInstance().getLogger(RankedAttributesTreeBuilder.class);
34
35     private RankedAttributesTreeBuilder() {
36     }
37
38     static <R> CompositeNode<R> build(ConfigurationSet<R> config) {
39
40         Object[] names = config.getRankedAttributeNames().toArray();
41
42         CompositeNode<R> root = new CompositeNode<>("ROOT", Constants.DEFAULT_MATCH, null,
43                 new HashMap<Object, Node<R>>());
44
45         if (logger.isDebugEnabled()) {
46             logger.debug(String.format("Building decision tree for ranked attributes: %s", config.getRankedAttributeNames()));
47         }
48
49         for (ConfigurationEntry<R> entry : config.getEntries()) {
50             process(entry, names, root);
51         }
52
53         return root;
54     }
55
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++) {
59
60             if (i == 0) {
61                 parentNode = root;
62             }
63
64             final String name = (String) names[i];
65
66             final Object value = value(entry, name);
67
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);
73                 }
74                 parentNode = currentNode;
75             } else {
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);
80
81                     if (logger.isDebugEnabled()) {
82                         logger.debug(String.format("Branch has been created: %s", currentNode));
83                     }
84                 } else {
85                     logger.error(
86                             String.format("Duplicated configuration entry has been detected for attribute '%s' with value '%s' - the node '%s'exists already",
87                                     name,
88                                     value,
89                                     currentNode));
90                     throw new IllegalArgumentException("Duplicated configuration entry: " + currentNode);
91                 }
92             }
93         }
94     }
95
96     private static <R> Object value(ConfigurationEntry<R> entry, String name) {
97         Object value = entry.getAttributeValue(name);
98         return Utils.value(value);
99     }
100 }