Add the Stacktrace into the Log
[holmes/common.git] / holmes-actions / src / main / java / org / onap / holmes / common / aai / CorrelationUtil.java
1 /**
2  * Copyright 2017 ZTE Corporation.
3  *
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
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
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
12  * the License.
13  */
14 package org.onap.holmes.common.aai;
15
16 import java.util.List;
17 import java.util.Optional;
18 import lombok.extern.slf4j.Slf4j;
19 import org.onap.holmes.common.aai.entity.RelationshipList.Relationship;
20 import org.onap.holmes.common.aai.entity.VmEntity;
21 import org.onap.holmes.common.dropwizard.ioc.utils.ServiceLocatorHolder;
22 import org.onap.holmes.common.exception.CorrelationException;
23
24 @Slf4j
25 public class CorrelationUtil {
26
27     private static AaiQuery aaiQuery;
28
29     private static class LazyHolder {
30         private static final CorrelationUtil INSTANCE = new CorrelationUtil();
31     }
32     private CorrelationUtil (){}
33
34     public static final CorrelationUtil getInstance() {
35         if (aaiQuery == null) {
36             aaiQuery = ServiceLocatorHolder.getLocator().getService(AaiQuery.class);
37         }
38         return LazyHolder.INSTANCE;
39     }
40
41     public boolean isTopologicallyRelated(String sourceId, String rootSourceId, String rootSourceName) {
42         return Optional.ofNullable(getVmEntity(rootSourceId, rootSourceName)).map(vmEntity ->
43                 getIsRelated(sourceId, vmEntity)).orElse(false);
44     }
45
46     private boolean getIsRelated(String sourceId, VmEntity vmEntity) {
47         List<Relationship> relationships = vmEntity.getRelationshipList().getRelationships();
48         for (Relationship relationship : relationships) {
49             boolean isRelated = relationship.getRelationshipDataList().stream().anyMatch(
50                     relationshipData -> relationshipData.getRelationshipValue().equals(sourceId));
51             if (isRelated) {
52                 return true;
53             }
54         }
55         return  false;
56     }
57
58     private VmEntity getVmEntity(String rootSourceId, String rootSourceName) {
59         VmEntity vmEntity = null;
60         try {
61             vmEntity = aaiQuery.getAaiVmData(rootSourceId, rootSourceName);
62         } catch (CorrelationException e) {
63             log.error("Failed to get the VM data.", e);
64         }
65         return vmEntity;
66     }
67 }