2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
 
   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=========================================================
 
  22 package org.openecomp.appc.rankingframework.impl;
 
  24 import java.util.HashSet;
 
  25 import java.util.List;
 
  28 import org.openecomp.appc.rankingframework.RankedAttributesContext;
 
  29 import com.att.eelf.configuration.EELFLogger;
 
  30 import com.att.eelf.configuration.EELFManager;
 
  32 class BacktraceStrategy implements Strategy {
 
  34     private static final EELFLogger logger = EELFManager.getInstance().getLogger(BacktraceStrategy.class);
 
  37     public <R> R resolve(CompositeNode<R> rootNode, List<String> rankedNames, RankedAttributesContext context) {
 
  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('}');
 
  44             logger.debug(String.format("Trying to resolve path: %s", buff));
 
  47         Set<String> visited = new HashSet<>();
 
  49         CompositeNode<R> parentNode = rootNode;
 
  54         String attribute = null;
 
  59                 attribute = rankedNames.get(depth);
 
  60                 value = Utils.value(context.getAttributeValue(attribute));
 
  63             Node<R> childNode = parentNode.children().get(value);
 
  65             if (childNode != null) {
 
  66                 if (logger.isDebugEnabled()) {
 
  67                     logger.debug(String.format("Found matching node '%s' - checking it out", childNode));
 
  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));
 
  77                 if (logger.isDebugEnabled()) {
 
  78                     logger.debug(String.format("Node '%s/{%s = %s}' not found  - falling back", parentNode, attribute, value != null ? value : "NULL"));
 
  82             if (childNode != null) {
 
  83                 switch (childNode.type()) {
 
  87                         parentNode = (CompositeNode<R>) childNode;
 
  90                         if (logger.isDebugEnabled()) {
 
  91                             logger.debug( String.format("Result node has been resolved succesfully - '%s'", childNode));
 
  93                         result = ((LeafNode<R>) childNode).result();
 
  97                         throw new IllegalStateException(childNode.type().name());
 
 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));
 
 109                     parentNode = parentNode.parent();
 
 111                     logger.debug("Didn't success to resolve the path - stopping without result");