-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathUsageEventUtils.java
More file actions
281 lines (230 loc) · 14.6 KB
/
UsageEventUtils.java
File metadata and controls
281 lines (230 loc) · 14.6 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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 com.cloud.event;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import com.cloud.network.Network;
import org.apache.cloudstack.context.CallContext;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
import org.apache.cloudstack.framework.events.Event;
import org.apache.cloudstack.framework.events.EventBus;
import org.apache.cloudstack.framework.events.EventDistributor;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.cloud.dc.DataCenterVO;
import com.cloud.dc.dao.DataCenterDao;
import com.cloud.event.dao.UsageEventDao;
import com.cloud.user.Account;
import com.cloud.user.dao.AccountDao;
import com.cloud.utils.component.ComponentContext;
public class UsageEventUtils {
private static UsageEventDao s_usageEventDao;
private static AccountDao s_accountDao;
private static DataCenterDao s_dcDao;
protected static Logger LOGGER = LogManager.getLogger(UsageEventUtils.class);
protected static EventBus s_eventBus = null;
protected static ConfigurationDao s_configDao;
private static EventDistributor eventDistributor;
@Inject
UsageEventDao usageEventDao;
@Inject
AccountDao accountDao;
@Inject
DataCenterDao dcDao;
@Inject
ConfigurationDao configDao;
public UsageEventUtils() {
}
@PostConstruct
void init() {
s_usageEventDao = usageEventDao;
s_accountDao = accountDao;
s_dcDao = dcDao;
s_configDao = configDao;
}
public static void publishUsageEvent(String usageType, long accountId, long zoneId, long resourceId, String resourceName, Long offeringId, Long templateId,
Long size, String entityType, String entityUUID, Map<String, String> details) {
saveUsageEvent(usageType, accountId, zoneId, resourceId, resourceName, offeringId, templateId, size, details);
publishUsageEvent(usageType, accountId, zoneId, entityType, entityUUID);
}
public static void publishUsageEvent(String usageType, long accountId, long zoneId, long resourceId, String resourceName, Long offeringId, Long templateId,
Long size, String entityType, String entityUUID) {
saveUsageEvent(usageType, accountId, zoneId, resourceId, resourceName, offeringId, templateId, size);
publishUsageEvent(usageType, accountId, zoneId, entityType, entityUUID);
}
public static void publishUsageEvent(String usageType, long accountId, long zoneId, long resourceId, String resourceName, Long offeringId, Long templateId,
Long size, String entityType, String entityUUID, boolean displayResource) {
if(displayResource){
saveUsageEvent(usageType, accountId, zoneId, resourceId, resourceName, offeringId, templateId, size);
}
publishUsageEvent(usageType, accountId, zoneId, entityType, entityUUID);
}
public static void publishUsageEvent(String usageType, long accountId, long zoneId, long resourceId, String resourceName, Long offeringId, Long templateId,
Long size, String entityType, String entityUUID, Long vmId, boolean displayResource) {
if (displayResource) {
saveUsageEvent(usageType, accountId, zoneId, resourceId, offeringId, templateId, size, vmId, resourceName);
}
publishUsageEvent(usageType, accountId, zoneId, entityType, entityUUID);
}
public static void publishUsageEvent(String usageType, long accountId, long zoneId, long resourceId, String resourceName, Long offeringId, Long templateId,
Long size, Long virtualSize, String entityType, String entityUUID, Map<String, String> details) {
saveUsageEvent(usageType, accountId, zoneId, resourceId, resourceName, offeringId, templateId, size, virtualSize, details);
publishUsageEvent(usageType, accountId, zoneId, entityType, entityUUID);
}
public static void publishUsageEvent(String usageType, long accountId, long zoneId, long resourceId, String resourceName, Long offeringId, Long templateId,
Long size, Long virtualSize, String entityType, String entityUUID) {
saveUsageEvent(usageType, accountId, zoneId, resourceId, resourceName, offeringId, templateId, size, virtualSize);
publishUsageEvent(usageType, accountId, zoneId, entityType, entityUUID);
}
public static void publishUsageEvent(String usageType, long accountId, long zoneId, long resourceId, String resourceName, String entityType, String entityUUID) {
saveUsageEvent(usageType, accountId, zoneId, resourceId, resourceName);
publishUsageEvent(usageType, accountId, zoneId, entityType, entityUUID);
}
public static void publishUsageEvent(String usageType, long accountId, long zoneId, long resourceId, String resourceName, String entityType, String entityUUID, boolean diplayResource) {
if (diplayResource){
saveUsageEvent(usageType, accountId, zoneId, resourceId, resourceName);
publishUsageEvent(usageType, accountId, zoneId, entityType, entityUUID);
}
}
public static void publishUsageEvent(String usageType, long accountId, long zoneId, long ipAddressId, String ipAddress, boolean isSourceNat, String guestType,
boolean isSystem, boolean usageHidden, String entityType, String entityUUID) {
saveUsageEvent(usageType, accountId, zoneId, ipAddressId, ipAddress, isSourceNat, guestType, isSystem, usageHidden);
publishUsageEvent(usageType, accountId, zoneId, entityType, entityUUID);
}
public static void publishUsageEvent(String usageType, long accountId, long zoneId, long resourceId, String resourceName, Long offeringId, Long templateId,
String resourceType, String entityType, String entityUUID, boolean displayResource) {
if(displayResource){
saveUsageEvent(usageType, accountId, zoneId, resourceId, resourceName, offeringId, templateId, resourceType);
}
publishUsageEvent(usageType, accountId, zoneId, entityType, entityUUID);
}
public static void publishUsageEvent(String usageType, long accountId, long zoneId, long vmId, long securityGroupId, String entityType, String entityUUID) {
saveUsageEvent(usageType, accountId, zoneId, vmId, securityGroupId);
publishUsageEvent(usageType, accountId, zoneId, entityType, entityUUID);
}
public static void publishUsageEvent(String usageType, long accountId, long zoneId, long resourceId, String resourceName, Long offeringId, Long templateId,
String resourceType, String entityType, String entityUUID, Map<String, String> details, boolean displayResource) {
if(displayResource){
saveUsageEvent(usageType, accountId, zoneId, resourceId, resourceName, offeringId, templateId, resourceType, details);
}
publishUsageEvent(usageType, accountId, zoneId, entityType, entityUUID);
}
private static void saveUsageEvent(String usageType, long accountId, long zoneId, long resourceId, String resourceName, Long offeringId, Long templateId,
String resourceType, Map<String, String> details) {
UsageEventVO usageEvent = new UsageEventVO(usageType, accountId, zoneId, resourceId, resourceName, offeringId, templateId, resourceType);
s_usageEventDao.persist(usageEvent);
s_usageEventDao.saveDetails(usageEvent.getId(), details);
}
public static void saveUsageEvent(String usageType, long accountId, long zoneId, long resourceId, String resourceName, Long offeringId, Long templateId, Long size) {
s_usageEventDao.persist(new UsageEventVO(usageType, accountId, zoneId, resourceId, resourceName, offeringId, templateId, size));
}
public static void saveUsageEvent(String usageType, long accountId, long zoneId, long resourceId, String resourceName, Long offeringId, Long templateId, Long size, Map<String, String> details) {
UsageEventVO usageEventVO = new UsageEventVO(usageType, accountId, zoneId, resourceId, resourceName, offeringId, templateId, size);
s_usageEventDao.persist(usageEventVO);
if (MapUtils.isNotEmpty(details)) {
s_usageEventDao.saveDetails(usageEventVO.getId(), details);
}
}
public static void saveUsageEvent(String usageType, long accountId, long zoneId, long resourceId, String resourceName, Long offeringId, Long templateId, Long size,
Long virtualSize) {
s_usageEventDao.persist(new UsageEventVO(usageType, accountId, zoneId, resourceId, resourceName, offeringId, templateId, size, virtualSize));
}
public static void saveUsageEvent(String usageType, long accountId, long zoneId, long resourceId, String resourceName, Long offeringId, Long templateId, Long size,
Long virtualSize, Map<String, String> details) {
UsageEventVO usageEventVO = new UsageEventVO(usageType, accountId, zoneId, resourceId, resourceName, offeringId, templateId, size, virtualSize);
s_usageEventDao.persist(usageEventVO);
if (MapUtils.isNotEmpty(details)) {
s_usageEventDao.saveDetails(usageEventVO.getId(), details);
}
}
public static void saveUsageEvent(String usageType, long accountId, long zoneId, long resourceId, String resourceName) {
s_usageEventDao.persist(new UsageEventVO(usageType, accountId, zoneId, resourceId, resourceName));
}
public static void saveUsageEvent(String usageType, long accountId, long zoneId, long ipAddressId, String ipAddress, boolean isSourceNat, String guestType,
boolean isSystem, boolean usageHidden) {
final UsageEventVO usageEventVO = new UsageEventVO(usageType, accountId, zoneId, ipAddressId, ipAddress, isSourceNat, guestType, isSystem);
s_usageEventDao.persist(usageEventVO);
if (usageHidden) {
s_usageEventDao.saveDetails(usageEventVO.getId(), Map.of("hidden", "true"));
}
}
public static void saveUsageEvent(String usageType, long accountId, long zoneId, long resourceId, String resourceName, Long offeringId, Long templateId,
String resourceType) {
s_usageEventDao.persist(new UsageEventVO(usageType, accountId, zoneId, resourceId, resourceName, offeringId, templateId, resourceType));
}
public static void saveUsageEvent(String usageType, long accountId, long zoneId, long vmId, long securityGroupId) {
s_usageEventDao.persist(new UsageEventVO(usageType, accountId, zoneId, vmId, securityGroupId));
}
public static void saveUsageEvent(String usageType, long accountId, long zoneId, long resourceId, Long offeringId, Long templateId, Long size, Long vmId, String resourceName) {
s_usageEventDao.persist(new UsageEventVO(usageType, accountId, zoneId, resourceId, offeringId, templateId, size, vmId, resourceName));
}
private static void publishUsageEvent(String usageEventType, Long accountId, Long zoneId, String resourceType, String resourceUUID) {
String configKey = "publish.usage.events";
String value = s_configDao.getValue(configKey);
boolean configValue = Boolean.parseBoolean(value);
if( !configValue)
return;
try {
eventDistributor = ComponentContext.getComponent(EventDistributor.class);
} catch (NoSuchBeanDefinitionException nbe) {
return; // no provider is configured to provide events distributor, so just return
}
Account account = s_accountDao.findById(accountId);
DataCenterVO dc = CallContext.current().getRequestEntityCache().get(DataCenterVO.class, zoneId,
() -> s_dcDao.findById(zoneId));
// if account has been deleted, this might be called during cleanup of resources and results in null pointer
if (account == null)
return;
// if an invalid zone is passed in, create event without zone UUID
String zoneUuid = null;
if (dc != null)
zoneUuid = dc.getUuid();
Event event = new Event(Name, EventCategory.USAGE_EVENT.getName(), usageEventType, resourceType, resourceUUID);
Map<String, String> eventDescription = new HashMap<String, String>();
eventDescription.put("account", account.getUuid());
eventDescription.put("zone", zoneUuid);
eventDescription.put("event", usageEventType);
eventDescription.put("resource", resourceType);
eventDescription.put("id", resourceUUID);
String eventDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z").format(new Date());
eventDescription.put("eventDateTime", eventDate);
event.setDescription(eventDescription);
eventDistributor.publish(event);
}
static final String Name = "management-server";
public static void publishNetworkCreation(Network network) {
publishUsageEvent(EventTypes.EVENT_NETWORK_CREATE, network.getAccountId(), network.getDataCenterId(),
network.getId(), network.getName(), network.getNetworkOfferingId(), null, null, null, network.getState().name(),
network.getUuid());
}
public static void publishNetworkUpdate(Network network) {
publishUsageEvent(EventTypes.EVENT_NETWORK_UPDATE, network.getAccountId(), network.getDataCenterId(),
network.getId(), network.getName(), network.getNetworkOfferingId(), null, network.getState().name(),
Network.class.getName(), network.getUuid(), true);
}
public static void publishNetworkDeletion(Network network) {
publishUsageEvent(EventTypes.EVENT_NETWORK_DELETE, network.getAccountId(), network.getDataCenterId(),
network.getId(), network.getName(), network.getNetworkOfferingId(), null, null, null,
Network.class.getName(), network.getUuid());
}
}