add correlation analysis
[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.exception.CorrelationException;
22
23 @Slf4j
24 public class CorrelationUtil {
25
26     private AaiQuery aaiQuery;
27
28     private static class LazyHolder {
29         private static final CorrelationUtil INSTANCE = new CorrelationUtil();
30     }
31     private CorrelationUtil (){}
32
33     public static final CorrelationUtil getInstance() {
34         return LazyHolder.INSTANCE;
35     }
36
37     public boolean isTopologicallyRelated(String childId, String rootId) {
38
39         return Optional.ofNullable(getVmEntity(rootId)).map(vmEntity ->
40                 getIsRelated(childId, vmEntity)).orElse(false);
41     }
42
43     private boolean getIsRelated(String childId, VmEntity vmEntity) {
44         List<Relationship> relationships = vmEntity.getRelationshipList().getRelationships();
45         for (Relationship relationship : relationships) {
46             boolean isRelated = relationship.getRelationshipDataList().stream().anyMatch(
47                     relationshipData -> relationshipData.getRelationshipValue().equals(childId));
48             if (isRelated) {
49                 return true;
50             }
51         }
52         return false;
53     }
54
55     private VmEntity getVmEntity(String rootId) {
56         VmEntity vmEntity = null;
57         try {
58             vmEntity = aaiQuery.getAaiVmData("", rootId);
59         } catch (CorrelationException e) {
60             log.error("Failed to get vm data", e.getMessage());
61         }
62         return vmEntity;
63     }
64 }