summaryrefslogtreecommitdiffstats
path: root/common
diff options
context:
space:
mode:
authorhetengjiao <hetengjiao@chinamobile.com>2020-10-26 17:47:25 +0800
committerhetengjiao <hetengjiao@chinamobile.com>2020-10-26 18:24:48 +0800
commit2510a11d6404db223e8b185797ab67c6da3d721d (patch)
tree5663d62adcf1a184d4144abd91741ab7f74a6d35 /common
parent7d14d2aee6dcd10313dfa44c59f9d1ce7c97100c (diff)
fixed type of subnetCapabilityQuery to Object
Issue-ID: SO-2963 Signed-off-by: hetengjiao <hetengjiao@chinamobile.com> Change-Id: I6c1005fb93c4d99b88057000a840ef5479b63da1
Diffstat (limited to 'common')
-rw-r--r--common/src/main/java/org/onap/so/beans/nsmf/NssmfAdapterNBIRequest.java2
1 files changed, 1 insertions, 1 deletions
diff --git a/common/src/main/java/org/onap/so/beans/nsmf/NssmfAdapterNBIRequest.java b/common/src/main/java/org/onap/so/beans/nsmf/NssmfAdapterNBIRequest.java
index a44dbd636f..e4989059a3 100644
--- a/common/src/main/java/org/onap/so/beans/nsmf/NssmfAdapterNBIRequest.java
+++ b/common/src/main/java/org/onap/so/beans/nsmf/NssmfAdapterNBIRequest.java
@@ -44,7 +44,7 @@ public class NssmfAdapterNBIRequest implements Serializable {
private DeAllocateNssi deAllocateNssi;
- private String subnetCapabilityQuery;
+ private Object subnetCapabilityQuery;
private String responseId;
}
id='n97' href='#n97'>97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
/*
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2019 Nordix Foundation
 *  ================================================================================
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *  SPDX-License-Identifier: Apache-2.0
 *  ============LICENSE_END=========================================================
 */

package org.openecomp.core.impl;

import static org.openecomp.sdc.tosca.csar.CSARConstants.NON_FILE_IMPORT_ATTRIBUTES;

import java.net.URI;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.openecomp.core.converter.ServiceTemplateReaderService;
import org.openecomp.core.impl.services.ServiceTemplateReaderServiceImpl;
import org.openecomp.sdc.common.errors.Messages;
import org.openecomp.sdc.datatypes.error.ErrorLevel;
import org.openecomp.sdc.datatypes.error.ErrorMessage;

/**
 * Handles TOSCA definition imports, checking for import definition errors.
 */
public class ToscaDefinitionImportHandler {

    private final Map<String, byte[]> fileMap;
    private final Set<String> handledDefinitionFilesList = new LinkedHashSet<>();
    private final List<ErrorMessage> validationErrorList = new ArrayList<>();
    private String currentFile;

    /**
     * Reads the provided package structure starting from a main definition yaml file.
     * @param fileStructureMap      The package structure with file path and respective file byte
     * @param mainDefinitionFilePath    The main descriptor yaml file to start the reading
     */
    public ToscaDefinitionImportHandler(final Map<String, byte[]> fileStructureMap, final String mainDefinitionFilePath) {
        this.fileMap = fileStructureMap;
        handleImports(mainDefinitionFilePath);
    }

    /**
     * Reads and validates the descriptor imports recursively.
     * Starts from the provided descriptor and goes until the end of the import tree.
     * Processes each file just once.
     *
     * @param fileName      the descriptor file path
     */
    private void handleImports(final String fileName) {
        currentFile = fileName;
        if (!checkImportExists(fileName)) {
            return;
        }
        final ServiceTemplateReaderService readerService;
        try {
            readerService = new ServiceTemplateReaderServiceImpl(fileMap.get(fileName));
        } catch (final Exception ex) {
            reportError(ErrorLevel.ERROR,
                String.format(Messages.INVALID_YAML_FORMAT.getErrorMessage(), ex.getMessage()));
            return;
        }
        handledDefinitionFilesList.add(fileName);
        final List<Object> imports = readerService.getImports();
        final List<String> extractImportFiles = extractFileImports(imports);
        for (final String importedFile : extractImportFiles) {
            final String resolvedPath = resolveImportPath(FilenameUtils.getPath(fileName), importedFile);
            if (!handledDefinitionFilesList.contains(resolvedPath)) {
                handleImports(resolvedPath);
            }
        }
    }

    /**
     * Iterates reads each import statement in the given list.
     * <pre>
     * example of a descriptor.yaml import statement
     * imports:
     * - /Artifacts/anImportedDescriptor.yaml
     * - anotherDescriptor: anotherImportedDescriptor.yaml
     * - yetAnotherDescriptor:
     *     yetAnotherDescriptor: ../Definitions/yetAnotherDescriptor.yaml
     * </pre>
     * @param imports   the import statements
     * @return
     *  The list of import file paths found
     */
    private List<String> extractFileImports(final List<Object> imports) {
        final List<String> importedFileList = new ArrayList<>();
        imports.forEach(importObject -> importedFileList.addAll(readImportStatement(importObject)));

        return importedFileList;
    }

    /**
     * Reads an import statement which can be a value, a [key:value] or a [key:[key:value]].
     * Ignores entries which contains the same keys as
     * {@link org.openecomp.sdc.tosca.csar.CSARConstants#NON_FILE_IMPORT_ATTRIBUTES}.
     * Reports invalid import statements.
     * <pre>
     * example of yaml imports statements:
     * - /Artifacts/anImportedDescriptor.yaml
     * - anotherDescriptor: anotherImportedDescriptor.yaml
     * - yetAnotherDescriptor:
     *     yetAnotherDescriptor: ../Definitions/yetAnotherDescriptor.yaml
     * </pre>
     * @param importObject      the object representing the yaml import statement
     * @return
     *  The list of import file paths found
     */
    private List<String> readImportStatement(final Object importObject) {
        final List<String> importedFileList = new ArrayList<>();
        if (importObject instanceof String) {
            importedFileList.add((String) importObject);
        } else if (importObject instanceof Map) {
            final Map<String, Object> importObjectMap = (Map) importObject;
            for (final Map.Entry entry : importObjectMap.entrySet()) {
                if (NON_FILE_IMPORT_ATTRIBUTES.stream().noneMatch(attr -> entry.getKey().equals(attr))) {
                    importedFileList.addAll(readImportStatement(entry.getValue()));
                }
            }
        } else {
            reportError(ErrorLevel.ERROR,
                String.format(Messages.INVALID_IMPORT_STATEMENT.getErrorMessage(), currentFile, importObject));
        }

        return importedFileList;
    }

    /**
     * Given a directory path, resolves the import path.
     * @param directoryPath     A directory path to resolve the import path
     * @param importPath        An import statement path
     * @return
     *  The resolved path of the import, using as base the directory path
     */
    private String resolveImportPath(final String directoryPath, final String importPath) {
        final String fixedParentDir;
        if (StringUtils.isEmpty(directoryPath)) {
            fixedParentDir = "/";
        } else {
            fixedParentDir = String.format("%s%s%s",
                directoryPath.startsWith("/") ? "" : "/"
                , directoryPath
                , directoryPath.endsWith("/") ? "" : "/");
        }

        final URI parentDirUri = URI.create(fixedParentDir);

        String resolvedImportPath = parentDirUri.resolve(importPath).toString();
        if (resolvedImportPath.contains("../")) {
            reportError(ErrorLevel.ERROR,
                Messages.INVALID_IMPORT_STATEMENT.formatMessage(currentFile, importPath));
            return null;
        }
        if (resolvedImportPath.startsWith("/")) {
            resolvedImportPath = resolvedImportPath.substring(1);
        }

        return resolvedImportPath;
    }

    /**
     * Checks if the given file path exists inside the file structure.
     * Reports an error if the file was not found.
     *
     * @param filePath  file path to check inside the file structure
     * @return
     *  {@code true} if the file exists, {@code false} otherwise
     */
    private boolean checkImportExists(final String filePath) {
        if (!fileMap.keySet().contains(filePath)) {
            reportError(ErrorLevel.ERROR, Messages.MISSING_IMPORT_FILE.formatMessage(filePath));
            return false;
        }

        return true;
    }

    /**
     * Gets all processed files during the import handling.
     * @return
     *  A list containing the processed files paths
     */
    public Set<String> getHandledDefinitionFilesList() {
        return handledDefinitionFilesList;
    }

    /**
     * Adds an error to the validation error list.
     *
     * @param errorLevel        the error level
     * @param errorMessage      the error message
     */
    private void reportError(final ErrorLevel errorLevel, final String errorMessage) {
        validationErrorList.add(new ErrorMessage(errorLevel, errorMessage));
    }

    /**
     * Gets the list of errors.
     * @return
     *  The import validation errors detected
     */
    public List<ErrorMessage> getErrors() {
        return validationErrorList;
    }

    /**
     * Checks if the handler detected a import error.
     * @return
     *  {@code true} if the handler detected any error, {@code false} otherwise.
     */
    public boolean hasError() {
        return !validationErrorList.isEmpty();
    }
}