Second part of onap rename
[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         if (logger.isDebugEnabled()) {
49             logger.debug(String.format("Building decision tree for ranked attributes: %s", config.getRankedAttributeNames()));
50         }
51
52         for (ConfigurationEntry<R> entry : config.getEntries()) {
53             process(entry, names, root);
54         }
55
56         return root;
57     }
58
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++) {
62
63             if (i == 0) {
64                 parentNode = root;
65             }
66
67             final String name = (String) names[i];
68
69             final Object value = value(entry, name);
70
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);
76                 }
77                 parentNode = currentNode;
78             } else {
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);
83
84                     if (logger.isDebugEnabled()) {
85                         logger.debug(String.format("Branch has been created: %s", currentNode));
86                     }
87                 } else {
88                     logger.error(
89                             String.format("Duplicated configuration entry has been detected for attribute '%s' with value '%s' - the node '%s'exists already",
90                                     name,
91                                     value,
92                                     currentNode));
93                     throw new IllegalArgumentException("Duplicated configuration entry: " + currentNode);
94                 }
95             }
96         }
97     }
98
99     private static <R> Object value(ConfigurationEntry<R> entry, String name) {
100         Object value = entry.getAttributeValue(name);
101         return Utils.value(value);
102     }
103 }