Released Version 1.4.7
[holmes/common.git] / holmes-actions / src / main / java / org / onap / holmes / common / dmaap / DmaapService.java
1 /*
2  * Copyright 2017-2020 ZTE Corporation.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.onap.holmes.common.dmaap;
17
18 import org.onap.holmes.common.aai.AaiQuery;
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.aai.entity.VnfEntity;
22 import org.onap.holmes.common.api.stat.VesAlarm;
23 import org.onap.holmes.common.dcae.DcaeConfigurationsCache;
24 import org.onap.holmes.common.dmaap.entity.PolicyMsg;
25 import org.onap.holmes.common.dmaap.entity.PolicyMsg.EVENT_STATUS;
26 import org.onap.holmes.common.dmaap.store.ClosedLoopControlNameCache;
27 import org.onap.holmes.common.dmaap.store.UniqueRequestIdCache;
28 import org.onap.holmes.common.exception.CorrelationException;
29 import org.onap.holmes.common.utils.GsonUtil;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.stereotype.Service;
34
35 import java.util.Map.Entry;
36 import java.util.Optional;
37 import java.util.UUID;
38
39 @Service
40 public class DmaapService {
41
42     private static final Logger log = LoggerFactory.getLogger(DmaapService.class);
43
44     private AaiQuery aaiQuery;
45     private ClosedLoopControlNameCache closedLoopControlNameCache;
46     private UniqueRequestIdCache uniqueRequestIdCache;
47
48     @Autowired
49     public void setAaiQuery(AaiQuery aaiQuery) {
50         this.aaiQuery = aaiQuery;
51     }
52
53     @Autowired
54     public void setClosedLoopControlNameCache(ClosedLoopControlNameCache closedLoopControlNameCache) {
55         this.closedLoopControlNameCache = closedLoopControlNameCache;
56     }
57
58     @Autowired
59     public void setUniqueRequestIdCache(UniqueRequestIdCache uniqueRequestIdCache) {
60         this.uniqueRequestIdCache = uniqueRequestIdCache;
61     }
62
63     public void publishPolicyMsg(PolicyMsg policyMsg, String dmaapConfigKey) {
64         try {
65             Publisher publisher = new Publisher();
66             publisher.setUrl(DcaeConfigurationsCache.getPubSecInfo(dmaapConfigKey).getDmaapInfo()
67                     .getTopicUrl());
68             publisher.publish(policyMsg);
69             deleteRequestIdIfNecessary(policyMsg);
70
71             log.info("send policyMsg: " + GsonUtil.beanToJson(policyMsg));
72         } catch (NullPointerException e) {
73             log.error(String.format("DMaaP configurations do not exist!\n DCAE Configurations: \n %s",
74                     DcaeConfigurationsCache.getDcaeConfigurations()), e);
75         } catch (Exception e) {
76             log.error(String.format("An error occurred when publishing a message to Policy: %s",
77                     e.getMessage()), e);
78         }
79     }
80
81     public PolicyMsg getPolicyMsg(VesAlarm rootAlarm, VesAlarm childAlarm, String packgeName) {
82         return Optional.ofNullable(getVmEntity(rootAlarm.getSourceId(), rootAlarm.getSourceName()))
83                 .map(vmEntity -> getEnrichedPolicyMsg(vmEntity, rootAlarm, childAlarm, packgeName))
84                 .orElse(getDefaultPolicyMsg(rootAlarm, packgeName));
85     }
86
87     private PolicyMsg getEnrichedPolicyMsg(VmEntity vmEntity, VesAlarm rootAlarm, VesAlarm childAlarm,
88                                            String packageName) {
89         PolicyMsg policyMsg = new PolicyMsg();
90         policyMsg.setRequestID(getUniqueRequestId(rootAlarm));
91         if (rootAlarm.getAlarmIsCleared() == PolicyMassgeConstant.POLICY_MESSAGE_ONSET) {
92             enrichVnfInfo(vmEntity, childAlarm, policyMsg);
93             policyMsg.setClosedLoopEventStatus(EVENT_STATUS.ONSET);
94             policyMsg.getAai().put("vserver.in-maint", vmEntity.getInMaint());
95             policyMsg.getAai().put("vserver.is-closed-loop-disabled",
96                     vmEntity.getClosedLoopDisable());
97             policyMsg.getAai().put("vserver.prov-status", vmEntity.getProvStatus());
98             policyMsg.getAai().put("vserver.resource-version", vmEntity.getResourceVersion());
99         } else {
100             policyMsg.setClosedLoopAlarmEnd(rootAlarm.getLastEpochMicrosec());
101             policyMsg.setClosedLoopEventStatus(EVENT_STATUS.ABATED);
102         }
103         policyMsg.setClosedLoopControlName(closedLoopControlNameCache.get(packageName));
104         policyMsg.setClosedLoopAlarmStart(rootAlarm.getStartEpochMicrosec());
105         policyMsg.getAai().put("vserver.vserver-id", vmEntity.getVserverId());
106         policyMsg.getAai().put("vserver.vserver-name", vmEntity.getVserverName());
107         policyMsg.getAai().put("vserver.vserver-name2", vmEntity.getVserverName2());
108         policyMsg.getAai().put("vserver.vserver-selflink", vmEntity.getVserverSelflink());
109         policyMsg.setTarget("vserver.vserver-name");
110         return policyMsg;
111     }
112
113     private PolicyMsg getDefaultPolicyMsg(VesAlarm rootAlarm, String packageName) {
114         PolicyMsg policyMsg = new PolicyMsg();
115         policyMsg.setRequestID(getUniqueRequestId(rootAlarm));
116         policyMsg.setClosedLoopControlName(closedLoopControlNameCache.get(packageName));
117         policyMsg.setClosedLoopAlarmStart(rootAlarm.getStartEpochMicrosec());
118         policyMsg.setTarget("vserver.vserver-name");
119         policyMsg.setTargetType("VM");
120         policyMsg.getAai().put("vserver.vserver-name", rootAlarm.getSourceName());
121         if (rootAlarm.getAlarmIsCleared() == PolicyMassgeConstant.POLICY_MESSAGE_ABATED) {
122             policyMsg.setClosedLoopAlarmEnd(rootAlarm.getLastEpochMicrosec());
123             policyMsg.setClosedLoopEventStatus(EVENT_STATUS.ABATED);
124         }
125         return policyMsg;
126     }
127
128     private String getUniqueRequestId(VesAlarm rootAlarm) {
129         String alarmUniqueKey = "";
130         if (rootAlarm.getAlarmIsCleared() == PolicyMassgeConstant.POLICY_MESSAGE_ABATED) {
131             alarmUniqueKey =
132                     rootAlarm.getSourceId() + ":" + rootAlarm.getEventName().replace("Cleared", "");
133         } else {
134             alarmUniqueKey = rootAlarm.getSourceId() + ":" + rootAlarm.getEventName();
135         }
136         if (uniqueRequestIdCache.containsKey(alarmUniqueKey)) {
137             return uniqueRequestIdCache.get(alarmUniqueKey);
138         } else {
139             String requestID = UUID.randomUUID().toString();
140             uniqueRequestIdCache.put(alarmUniqueKey, requestID);
141             return requestID;
142         }
143     }
144
145     private void enrichVnfInfo(VmEntity vmEntity, VesAlarm childAlarm, PolicyMsg policyMsg) {
146         String vnfId = "";
147         String vnfName = "";
148         if (null != childAlarm) {
149             vnfId = childAlarm.getSourceId();
150             vnfName = childAlarm.getSourceName();
151         } else {
152             Relationship relationship = vmEntity.getRelationshipList()
153                     .getRelationship(PolicyMassgeConstant.GENERIC_VNF);
154             if (null != relationship) {
155                 vnfId = relationship.getRelationshipDataValue(PolicyMassgeConstant.GENERIC_VNF_VNF_ID);
156                 vnfName = relationship.getRelatedToPropertyValue(PolicyMassgeConstant.GENERIC_VNF_VNF_NAME);
157             }
158         }
159         VnfEntity vnfEntity = getVnfEntity(vnfId, vnfName);
160         String vserverInstatnceId = getVserverInstanceId(vnfEntity);
161         policyMsg.getAai().put("generic-vnf.vnf-id", vnfId);
162         policyMsg.getAai().put("generic-vnf.service-instance-id", vserverInstatnceId);
163     }
164
165
166     private String getVserverInstanceId(VnfEntity vnfEntity) {
167         String vserverInstanceId = "";
168         if (vnfEntity != null) {
169             Relationship relationship = vnfEntity.getRelationshipList()
170                     .getRelationship(PolicyMassgeConstant.SERVICE_INSTANCE);
171             if (relationship == null) {
172                 return vserverInstanceId;
173             }
174             vserverInstanceId = relationship
175                     .getRelationshipDataValue(PolicyMassgeConstant.SERVICE_INSTANCE_ID);
176         }
177         return vserverInstanceId;
178     }
179
180     private VnfEntity getVnfEntity(String vnfId, String vnfName) {
181         VnfEntity vnfEntity = null;
182         try {
183             vnfEntity = aaiQuery.getAaiVnfData(vnfId, vnfName);
184         } catch (CorrelationException e) {
185             log.error("Failed to get the VNF data.", e);
186         }
187         return vnfEntity;
188     }
189
190     private VmEntity getVmEntity(String sourceId, String sourceName) {
191         VmEntity vmEntity = null;
192         try {
193             vmEntity = aaiQuery.getAaiVmData(sourceId, sourceName);
194         } catch (CorrelationException e) {
195             log.error("Failed to get the VM data.", e);
196         }
197         return vmEntity;
198     }
199
200     private void deleteRequestIdIfNecessary(PolicyMsg policyMsg) {
201         EVENT_STATUS status = policyMsg.getClosedLoopEventStatus();
202         if (EVENT_STATUS.ABATED.equals(status)) {
203             String requestId = policyMsg.getRequestID();
204             for (Entry<String, String> kv : uniqueRequestIdCache.entrySet()) {
205                 if (kv.getValue().equals(requestId)) {
206                     uniqueRequestIdCache.remove(kv.getKey());
207                     break;
208                 }
209             }
210             log.info("An alarm is cleared and the corresponding requestId is deleted successfully");
211         }
212     }
213 }