Catalog alignment
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / utils / ComponentUtilities.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.model.utils;
22
23 import org.apache.commons.lang3.StringUtils;
24 import org.openecomp.sdc.be.datatypes.elements.Annotation;
25 import org.openecomp.sdc.be.model.Component;
26 import org.openecomp.sdc.be.model.ComponentInstance;
27 import org.openecomp.sdc.be.model.InputDefinition;
28
29 import java.util.List;
30 import java.util.Optional;
31
32 import static java.util.Collections.emptyList;
33
34 public class ComponentUtilities {
35     private ComponentUtilities() {
36     }
37
38     public static Optional<String> getComponentInstanceNameByInstanceId(Component component, String id) {
39         return component.getComponentInstanceById(id)
40                 .flatMap(instance -> component.getComponentInstanceByName(instance.getName()))
41                 .map(ComponentInstance::getName);
42     }
43
44     public static List<Annotation> getInputAnnotations(Component component, String inputName) {
45         return getInputByName(component, inputName)
46                 .map(InputDefinition::getAnnotations)
47                 .orElse(emptyList());
48     }
49
50     private static Optional<InputDefinition> getInputByName(Component component, String inputName) {
51         return component.safeGetInputs().stream()
52                 .filter(input -> input.getName().equals(inputName))
53                 .findFirst();
54     }
55
56     public static boolean isNotUpdatedCapReqName(String prefix, String currName, String previousName) {
57         return StringUtils.isEmpty(previousName) || StringUtils.isEmpty(currName) || !currName.equals(prefix + previousName);
58     }
59 }