2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 package org.openecomp.appc.rankingframework.impl;
25 import java.util.HashSet;
26 import java.util.List;
29 import org.openecomp.appc.rankingframework.RankedAttributesContext;
30 import com.att.eelf.configuration.EELFLogger;
31 import com.att.eelf.configuration.EELFManager;
33 class BacktraceStrategy implements Strategy {
35 private static final EELFLogger logger = EELFManager.getInstance().getLogger(BacktraceStrategy.class);
38 public <R> R resolve(CompositeNode<R> rootNode, List<String> rankedNames, RankedAttributesContext context) {
40 if (logger.isDebugEnabled()) {
41 StringBuilder buff = new StringBuilder(128);
42 for (String name : rankedNames) {
43 buff.append("/{").append(name).append(" = ").append(Utils.value(context.getAttributeValue(name))).append('}');
45 logger.debug(String.format("Trying to resolve path: %s", buff));
48 Set<String> visited = new HashSet<>();
50 CompositeNode<R> parentNode = rootNode;
55 String attribute = null;
60 attribute = rankedNames.get(depth);
61 value = Utils.value(context.getAttributeValue(attribute));
64 Node<R> childNode = parentNode.children().get(value);
66 if (childNode != null) {
67 if (logger.isDebugEnabled()) {
68 logger.debug(String.format("Found matching node '%s' - checking it out", childNode));
71 if (!visited.add(childNode.id())) {
72 if (logger.isDebugEnabled()) {
73 logger.debug(String.format("The matching node '%s' was checked before - ignoring it", childNode));
78 if (logger.isDebugEnabled()) {
79 logger.debug(String.format("Node '%s/{%s = %s}' not found - falling back", parentNode, attribute, value != null ? value : "NULL"));
83 if (childNode != null) {
84 switch (childNode.type()) {
88 parentNode = (CompositeNode<R>) childNode;
91 if (logger.isDebugEnabled()) {
92 logger.debug( String.format("Result node has been resolved succesfully - '%s'", childNode));
94 result = ((LeafNode<R>) childNode).result();
98 throw new IllegalStateException(childNode.type().name());
101 if (!value.equals(Constants.DEFAULT_MATCH)) {
102 logger.debug("Exact match didn't work, trying the default option, if any");
103 value = Constants.DEFAULT_MATCH;
104 } else if (depth > 0) {
105 if (logger.isDebugEnabled()) {
106 logger.debug(String.format("Exact match didn't work and no default option available beneath '%s' - moving out", parentNode));
110 parentNode = parentNode.parent();
112 logger.debug("Didn't success to resolve the path - stopping without result");