summaryrefslogtreecommitdiffstats
path: root/holmes-actions/src/main/java/org/onap/holmes/common/dmaap/DmaapService.java
blob: de7d74f34df38cb2a10b27bcfa5415299003416d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
 * Copyright 2017-2020 ZTE Corporation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.onap.holmes.common.dmaap;

import org.onap.holmes.common.aai.AaiQuery;
import org.onap.holmes.common.aai.entity.RelationshipList.Relationship;
import org.onap.holmes.common.aai.entity.VmEntity;
import org.onap.holmes.common.aai.entity.VnfEntity;
import org.onap.holmes.common.api.stat.VesAlarm;
import org.onap.holmes.common.dcae.DcaeConfigurationsCache;
import org.onap.holmes.common.dmaap.entity.PolicyMsg;
import org.onap.holmes.common.dmaap.entity.PolicyMsg.EVENT_STATUS;
import org.onap.holmes.common.dmaap.store.ClosedLoopControlNameCache;
import org.onap.holmes.common.dmaap.store.UniqueRequestIdCache;
import org.onap.holmes.common.exception.CorrelationException;
import org.onap.holmes.common.utils.GsonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map.Entry;
import java.util.Optional;
import java.util.UUID;

@Service
public class DmaapService {

    private static final Logger log = LoggerFactory.getLogger(DmaapService.class);

    private AaiQuery aaiQuery;
    private ClosedLoopControlNameCache closedLoopControlNameCache;
    private UniqueRequestIdCache uniqueRequestIdCache;

    @Autowired
    public void setAaiQuery(AaiQuery aaiQuery) {
        this.aaiQuery = aaiQuery;
    }

    @Autowired
    public void setClosedLoopControlNameCache(ClosedLoopControlNameCache closedLoopControlNameCache) {
        this.closedLoopControlNameCache = closedLoopControlNameCache;
    }

    @Autowired
    public void setUniqueRequestIdCache(UniqueRequestIdCache uniqueRequestIdCache) {
        this.uniqueRequestIdCache = uniqueRequestIdCache;
    }

    public void publishPolicyMsg(PolicyMsg policyMsg, String dmaapConfigKey) {
        try {
            Publisher publisher = new Publisher();
            publisher.setUrl(DcaeConfigurationsCache.getPubSecInfo(dmaapConfigKey).getDmaapInfo()
                    .getTopicUrl());
            publisher.publish(policyMsg);
            deleteRequestIdIfNecessary(policyMsg);

            log.info("send policyMsg: " + GsonUtil.beanToJson(policyMsg));
        } catch (NullPointerException e) {
            log.error(String.format("DMaaP configurations do not exist!\n DCAE Configurations: \n %s",
                    DcaeConfigurationsCache.getDcaeConfigurations()), e);
        } catch (Exception e) {
            log.error(String.format("An error occurred when publishing a message to Policy: %s",
                    e.getMessage()), e);
        }
    }

    public PolicyMsg getPolicyMsg(VesAlarm rootAlarm, VesAlarm childAlarm, String packgeName) {
        return Optional.ofNullable(getVmEntity(rootAlarm.getSourceId(), rootAlarm.getSourceName()))
                .map(vmEntity -> getEnrichedPolicyMsg(vmEntity, rootAlarm, childAlarm, packgeName))
                .orElse(getDefaultPolicyMsg(rootAlarm, packgeName));
    }

    private PolicyMsg getEnrichedPolicyMsg(VmEntity vmEntity, VesAlarm rootAlarm, VesAlarm childAlarm,
                                           String packageName) {
        PolicyMsg policyMsg = new PolicyMsg();
        policyMsg.setRequestID(getUniqueRequestId(rootAlarm));
        if (rootAlarm.getAlarmIsCleared() == PolicyMassgeConstant.POLICY_MESSAGE_ONSET) {
            enrichVnfInfo(vmEntity, childAlarm, policyMsg);
            policyMsg.setClosedLoopEventStatus(EVENT_STATUS.ONSET);
            policyMsg.getAai().put("vserver.in-maint", vmEntity.getInMaint());
            policyMsg.getAai().put("vserver.is-closed-loop-disabled",
                    vmEntity.getClosedLoopDisable());
            policyMsg.getAai().put("vserver.prov-status", vmEntity.getProvStatus());
            policyMsg.getAai().put("vserver.resource-version", vmEntity.getResourceVersion());
        } else {
            policyMsg.setClosedLoopAlarmEnd(rootAlarm.getLastEpochMicrosec());
            policyMsg.setClosedLoopEventStatus(EVENT_STATUS.ABATED);
        }
        policyMsg.setClosedLoopControlName(closedLoopControlNameCache.get(packageName));
        policyMsg.setClosedLoopAlarmStart(rootAlarm.getStartEpochMicrosec());
        policyMsg.getAai().put("vserver.vserver-id", vmEntity.getVserverId());
        policyMsg.getAai().put("vserver.vserver-name", vmEntity.getVserverName());
        policyMsg.getAai().put("vserver.vserver-name2", vmEntity.getVserverName2());
        policyMsg.getAai().put("vserver.vserver-selflink", vmEntity.getVserverSelflink());
        policyMsg.setTarget("vserver.vserver-name");
        return policyMsg;
    }

    private PolicyMsg getDefaultPolicyMsg(VesAlarm rootAlarm, String packageName) {
        PolicyMsg policyMsg = new PolicyMsg();
        policyMsg.setRequestID(getUniqueRequestId(rootAlarm));
        policyMsg.setClosedLoopControlName(closedLoopControlNameCache.get(packageName));
        policyMsg.setClosedLoopAlarmStart(rootAlarm.getStartEpochMicrosec());
        policyMsg.setTarget("vserver.vserver-name");
        policyMsg.setTargetType("VM");
        policyMsg.getAai().put("vserver.vserver-name", rootAlarm.getSourceName());
        if (rootAlarm.getAlarmIsCleared() == PolicyMassgeConstant.POLICY_MESSAGE_ABATED) {
            policyMsg.setClosedLoopAlarmEnd(rootAlarm.getLastEpochMicrosec());
            policyMsg.setClosedLoopEventStatus(EVENT_STATUS.ABATED);
        }
        return policyMsg;
    }

    private String getUniqueRequestId(VesAlarm rootAlarm) {
        String alarmUniqueKey = "";
        if (rootAlarm.getAlarmIsCleared() == PolicyMassgeConstant.POLICY_MESSAGE_ABATED) {
            alarmUniqueKey =
                    rootAlarm.getSourceId() + ":" + rootAlarm.getEventName().replace("Cleared", "");
        } else {
            alarmUniqueKey = rootAlarm.getSourceId() + ":" + rootAlarm.getEventName();
        }
        if (uniqueRequestIdCache.containsKey(alarmUniqueKey)) {
            return uniqueRequestIdCache.get(alarmUniqueKey);
        } else {
            String requestID = UUID.randomUUID().toString();
            uniqueRequestIdCache.put(alarmUniqueKey, requestID);
            return requestID;
        }
    }

    private void enrichVnfInfo(VmEntity vmEntity, VesAlarm childAlarm, PolicyMsg policyMsg) {
        String vnfId = "";
        String vnfName = "";
        if (null != childAlarm) {
            vnfId = childAlarm.getSourceId();
            vnfName = childAlarm.getSourceName();
        } else {
            Relationship relationship = vmEntity.getRelationshipList()
                    .getRelationship(PolicyMassgeConstant.GENERIC_VNF);
            if (null != relationship) {
                vnfId = relationship.getRelationshipDataValue(PolicyMassgeConstant.GENERIC_VNF_VNF_ID);
                vnfName = relationship.getRelatedToPropertyValue(PolicyMassgeConstant.GENERIC_VNF_VNF_NAME);
            }
        }
        VnfEntity vnfEntity = getVnfEntity(vnfId, vnfName);
        String vserverInstatnceId = getVserverInstanceId(vnfEntity);
        policyMsg.getAai().put("generic-vnf.vnf-id", vnfId);
        policyMsg.getAai().put("generic-vnf.service-instance-id", vserverInstatnceId);
    }


    private String getVserverInstanceId(VnfEntity vnfEntity) {
        String vserverInstanceId = "";
        if (vnfEntity != null) {
            Relationship relationship = vnfEntity.getRelationshipList()
                    .getRelationship(PolicyMassgeConstant.SERVICE_INSTANCE);
            if (relationship == null) {
                return vserverInstanceId;
            }
            vserverInstanceId = relationship
                    .getRelationshipDataValue(PolicyMassgeConstant.SERVICE_INSTANCE_ID);
        }
        return vserverInstanceId;
    }

    private VnfEntity getVnfEntity(String vnfId, String vnfName) {
        VnfEntity vnfEntity = null;
        try {
            vnfEntity = aaiQuery.getAaiVnfData(vnfId, vnfName);
        } catch (CorrelationException e) {
            log.error("Failed to get the VNF data.", e);
        }
        return vnfEntity;
    }

    private VmEntity getVmEntity(String sourceId, String sourceName) {
        VmEntity vmEntity = null;
        try {
            vmEntity = aaiQuery.getAaiVmData(sourceId, sourceName);
        } catch (CorrelationException e) {
            log.error("Failed to get the VM data.", e);
        }
        return vmEntity;
    }

    private void deleteRequestIdIfNecessary(PolicyMsg policyMsg) {
        EVENT_STATUS status = policyMsg.getClosedLoopEventStatus();
        if (EVENT_STATUS.ABATED.equals(status)) {
            String requestId = policyMsg.getRequestID();
            for (Entry<String, String> kv : uniqueRequestIdCache.entrySet()) {
                if (kv.getValue().equals(requestId)) {
                    uniqueRequestIdCache.remove(kv.getKey());
                    break;
                }
            }
            log.info("An alarm is cleared and the corresponding requestId is deleted successfully");
        }
    }
}