Change nexus values to properties
[appc.git] / appc-dispatcher / appc-dispatcher-common / ranking-framework-lib / src / main / java / org / openecomp / appc / rankingframework / impl / BacktraceStrategy.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.HashSet;
25 import java.util.List;
26 import java.util.Set;
27
28 import org.openecomp.appc.rankingframework.RankedAttributesContext;
29 import com.att.eelf.configuration.EELFLogger;
30 import com.att.eelf.configuration.EELFManager;
31
32 class BacktraceStrategy implements Strategy {
33
34     private static final EELFLogger logger = EELFManager.getInstance().getLogger(BacktraceStrategy.class);
35
36     @Override
37     public <R> R resolve(CompositeNode<R> rootNode, List<String> rankedNames, RankedAttributesContext context) {
38
39         if (logger.isDebugEnabled()) {
40             StringBuilder buff = new StringBuilder(128);
41             for (String name : rankedNames) {
42                 buff.append("/{").append(name).append(" = ").append(Utils.value(context.getAttributeValue(name))).append('}');
43             }
44             logger.debug(String.format("Trying to resolve path: %s", buff));
45         }
46
47         Set<String> visited = new HashSet<>();
48
49         CompositeNode<R> parentNode = rootNode;
50         int depth = 0;
51         boolean stop = false;
52         R result = null;
53
54         String attribute = null;
55         Object value = null;
56
57         do {
58             if (value == null) {
59                 attribute = rankedNames.get(depth);
60                 value = Utils.value(context.getAttributeValue(attribute));
61             }
62
63             Node<R> childNode = parentNode.children().get(value);
64
65             if (childNode != null) {
66                 if (logger.isDebugEnabled()) {
67                     logger.debug(String.format("Found matching node '%s' - checking it out", childNode));
68                 }
69
70                 if (!visited.add(childNode.id())) {
71                     if (logger.isDebugEnabled()) {
72                         logger.debug(String.format("The matching node '%s' was checked before - ignoring it", childNode));
73                     }
74                     childNode = null;
75                 }
76             } else {
77                 if (logger.isDebugEnabled()) {
78                     logger.debug(String.format("Node '%s/{%s = %s}' not found  - falling back", parentNode, attribute, value != null ? value : "NULL"));
79                 }
80             }
81
82             if (childNode != null) {
83                 switch (childNode.type()) {
84                     case COMPOSITE:
85                         depth++;
86                         value = null;
87                         parentNode = (CompositeNode<R>) childNode;
88                         break;
89                     case LEAF:
90                         if (logger.isDebugEnabled()) {
91                             logger.debug( String.format("Result node has been resolved succesfully - '%s'", childNode));
92                         }
93                         result = ((LeafNode<R>) childNode).result();
94                         stop = true;
95                         break;
96                     default:
97                         throw new IllegalStateException(childNode.type().name());
98                 }
99             } else {
100                 if (!value.equals(Constants.DEFAULT_MATCH)) {
101                     logger.debug("Exact match didn't work, trying the default option, if any");
102                     value = Constants.DEFAULT_MATCH;
103                 } else if (depth > 0) {
104                     if (logger.isDebugEnabled()) {
105                         logger.debug(String.format("Exact match didn't work and no default option available beneath '%s' - moving out", parentNode));
106                     }
107                     depth--;
108                     value = null;
109                     parentNode = parentNode.parent();
110                 } else {
111                     logger.debug("Didn't success to resolve the path - stopping without result");
112                     stop = true;
113                 }
114             }
115         } while (!stop);
116
117         return result;
118     }
119 }