Improve handling 'empty'/null string in Service fields
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / tosca / ToscaDefaultImportHelper.java
1 /*
2  * -
3  *  ============LICENSE_START=======================================================
4  *  Copyright (C) 2021 Nordix Foundation.
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *       http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.openecomp.sdc.be.tosca;
23
24 import java.nio.file.Path;
25 import lombok.AccessLevel;
26 import lombok.NoArgsConstructor;
27 import org.apache.commons.lang.StringUtils;
28
29 /**
30  * Helper class for TOSCA default imports.
31  */
32 @NoArgsConstructor(access = AccessLevel.PRIVATE)
33 public class ToscaDefaultImportHelper {
34
35     /**
36      * Add the model as a file prefix in the given path, e.g.: "path/to/entry.yaml -> path/to/modelId-entry.yaml".
37      *
38      * @param originalPath the entry original path
39      * @param modelId      the model id to add as prefix
40      * @return the modified file path with a model prefix.
41      */
42     public static Path addModelAsFilePrefix(final Path originalPath, final String modelId) {
43         if (StringUtils.isEmpty(modelId)) {
44             return originalPath;
45         }
46         final var fileName = originalPath.getFileName().toString();
47         final var newFileName = String.format("%s-%s", modelId, fileName);
48         if (originalPath.getParent() == null) {
49             return Path.of(newFileName);
50         }
51         return originalPath.getParent().resolve(newFileName);
52     }
53
54 }