2 * Copyright © 2016-2018 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.openecomp.sdc.be.model.jsontitan.utils;
18 import org.apache.commons.collections.CollectionUtils;
19 import org.openecomp.sdc.be.model.InterfaceDefinition;
21 import java.util.Collection;
22 import java.util.Formatter;
23 import java.util.Optional;
24 import java.util.stream.Collectors;
25 import org.openecomp.sdc.common.util.ValidationUtils;
27 public class InterfaceUtils {
29 public static final String INTERFACE_TOSCA_RESOURCE_NAME = "org.openecomp.interfaces.node.lifecycle.%s";
31 public static final Optional<InterfaceDefinition> getInterfaceDefinitionFromToscaName(
32 Collection<InterfaceDefinition> interfaces,
33 String componentName) {
34 if (CollectionUtils.isEmpty(interfaces)) {
35 return Optional.empty();
38 String toscaName = createInterfaceToscaResourceName(componentName);
39 return interfaces.stream().filter(
40 interfaceDefinition -> interfaceDefinition.getToscaResourceName() != null && interfaceDefinition
41 .getToscaResourceName().equals(toscaName)).findAny();
44 public static Collection<InterfaceDefinition> getInterfaceDefinitionListFromToscaName(Collection<InterfaceDefinition> interfaces,
45 String componentName) {
46 if (CollectionUtils.isEmpty(interfaces)) {
47 return CollectionUtils.EMPTY_COLLECTION;
50 String toscaName = createInterfaceToscaResourceName(componentName);
51 return interfaces.stream().filter(
52 interfaceDefinition -> interfaceDefinition.getToscaResourceName() != null && interfaceDefinition
53 .getToscaResourceName().equals(toscaName)).collect(Collectors.toList());
56 public static String createInterfaceToscaResourceName(String componentName) {
57 StringBuilder sb = new StringBuilder();
58 try (Formatter formatter = new Formatter(sb)) {
59 return formatter.format(INTERFACE_TOSCA_RESOURCE_NAME, ValidationUtils.convertToSystemName(componentName)).toString();