2 * Copyright 2017-2021 ZTE Corporation.
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
14 package org.onap.holmes.common.aai;
16 import lombok.extern.slf4j.Slf4j;
17 import org.onap.holmes.common.aai.entity.RelationshipList.Relationship;
18 import org.onap.holmes.common.aai.entity.VmEntity;
19 import org.onap.holmes.common.exception.CorrelationException;
20 import org.onap.holmes.common.utils.SpringContextUtil;
22 import java.util.List;
23 import java.util.Optional;
26 public class CorrelationUtil {
28 private static AaiQuery aaiQuery;
30 private static class LazyHolder {
31 private static final CorrelationUtil INSTANCE = new CorrelationUtil();
33 private CorrelationUtil (){}
35 public static final CorrelationUtil getInstance() {
36 if (aaiQuery == null) {
37 aaiQuery = SpringContextUtil.getBean(AaiQuery.class);
39 return LazyHolder.INSTANCE;
42 public boolean isTopologicallyRelated(String sourceId, String rootSourceId, String rootSourceName) {
43 return Optional.ofNullable(getVmEntity(rootSourceId, rootSourceName)).map(vmEntity ->
44 getIsRelated(sourceId, vmEntity)).orElse(false);
47 private boolean getIsRelated(String sourceId, VmEntity vmEntity) {
48 List<Relationship> relationships = vmEntity.getRelationshipList().getRelationships();
49 for (Relationship relationship : relationships) {
50 boolean isRelated = relationship.getRelationshipDataList().stream().anyMatch(
51 relationshipData -> relationshipData.getRelationshipValue().equals(sourceId));
59 private VmEntity getVmEntity(String rootSourceId, String rootSourceName) {
60 VmEntity vmEntity = null;
62 vmEntity = aaiQuery.getAaiVmData(rootSourceId, rootSourceName);
63 } catch (CorrelationException e) {
64 log.error("Failed to get the VM data.", e);