2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.rankingframework.impl;
27 import java.util.HashSet;
28 import java.util.List;
31 import org.onap.appc.rankingframework.RankedAttributesContext;
32 import com.att.eelf.configuration.EELFLogger;
33 import com.att.eelf.configuration.EELFManager;
35 class BacktraceStrategy implements Strategy {
37 private static final EELFLogger logger = EELFManager.getInstance().getLogger(BacktraceStrategy.class);
40 public <R> R resolve(CompositeNode<R> rootNode, List<String> rankedNames, RankedAttributesContext context) {
42 if (logger.isDebugEnabled()) {
43 StringBuilder buff = new StringBuilder(128);
44 for (String name : rankedNames) {
45 buff.append("/{").append(name).append(" = ").append(Utils.value(context.getAttributeValue(name))).append('}');
47 logger.debug(String.format("Trying to resolve path: %s", buff));
50 Set<String> visited = new HashSet<>();
52 CompositeNode<R> parentNode = rootNode;
57 String attribute = null;
62 attribute = rankedNames.get(depth);
63 value = Utils.value(context.getAttributeValue(attribute));
66 Node<R> childNode = parentNode.children().get(value);
68 if (childNode != null) {
69 if (logger.isDebugEnabled()) {
70 logger.debug(String.format("Found matching node '%s' - checking it out", childNode));
73 if (!visited.add(childNode.id())) {
74 if (logger.isDebugEnabled()) {
75 logger.debug(String.format("The matching node '%s' was checked before - ignoring it", childNode));
80 if (logger.isDebugEnabled()) {
81 logger.debug(String.format("Node '%s/{%s = %s}' not found - falling back", parentNode, attribute, value != null ? value : "NULL"));
85 if (childNode != null) {
86 switch (childNode.type()) {
90 parentNode = (CompositeNode<R>) childNode;
93 if (logger.isDebugEnabled()) {
94 logger.debug( String.format("Result node has been resolved succesfully - '%s'", childNode));
96 result = ((LeafNode<R>) childNode).result();
100 throw new IllegalStateException(childNode.type().name());
103 if (!value.equals(Constants.DEFAULT_MATCH)) {
104 logger.debug("Exact match didn't work, trying the default option, if any");
105 value = Constants.DEFAULT_MATCH;
106 } else if (depth > 0) {
107 if (logger.isDebugEnabled()) {
108 logger.debug(String.format("Exact match didn't work and no default option available beneath '%s' - moving out", parentNode));
112 parentNode = parentNode.parent();
114 logger.debug("Didn't success to resolve the path - stopping without result");