Fix sonar blockers
[appc.git] / appc-dispatcher / appc-dispatcher-common / ranking-framework-lib / src / main / java / org / onap / appc / rankingframework / impl / RankedAttributesTreeBuilder.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.rankingframework.impl;
26
27 import java.util.HashMap;
28
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;
33
34 class RankedAttributesTreeBuilder {
35
36     private static final EELFLogger logger = EELFManager.getInstance().getLogger(RankedAttributesTreeBuilder.class);
37
38     private RankedAttributesTreeBuilder() {
39     }
40
41     static <R> CompositeNode<R> build(ConfigurationSet<R> config) {
42
43         Object[] names = config.getRankedAttributeNames().toArray();
44
45         CompositeNode<R> root = new CompositeNode<>("ROOT", Constants.DEFAULT_MATCH, null,
46                 new HashMap<Object, Node<R>>());
47
48         logDebugWhenEnabled(String.format("Building decision tree for ranked attributes: %s", config.getRankedAttributeNames()));
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 = root;
59         for (int i = 0; i < names.length; i++) {
60
61             final String name = (String) names[i];
62
63             final Object value = value(entry, name);
64
65             if (i < names.length - 1) {
66                 CompositeNode<R> currentNode = (CompositeNode<R>) parentNode.children().get(value);
67                 if (currentNode == null) {
68                     currentNode = new CompositeNode<>(name, value, parentNode, new HashMap<Object, Node<R>>());
69                     parentNode.children().put(value, currentNode);
70                 }
71                 parentNode = currentNode;
72             } else {
73                 LeafNode<R> currentNode = (LeafNode<R>) parentNode.children().get(value);
74                 if (currentNode == null) {
75                     currentNode = new LeafNode<>(name, value, parentNode, entry.getResult());
76                     parentNode.children().put(value, currentNode);
77
78                     logDebugWhenEnabled(String.format("Branch has been created: %s", currentNode));
79                 } else {
80                     logger.error(
81                             String.format("Duplicated configuration entry has been detected for attribute '%s' with value '%s' - the node '%s'exists already",
82                                     name,
83                                     value,
84                                     currentNode));
85                     throw new IllegalArgumentException("Duplicated configuration entry: " + currentNode);
86                 }
87             }
88         }
89     }
90
91     private static void logDebugWhenEnabled(String message) {
92         if(logger.isDebugEnabled()) {
93             logger.debug(message);
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 }