forked from AnythingLinux/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResourceMetaDataManagerImpl.java
More file actions
256 lines (229 loc) · 11.1 KB
/
ResourceMetaDataManagerImpl.java
File metadata and controls
256 lines (229 loc) · 11.1 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
// 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.metadata;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.ejb.Local;
import javax.inject.Inject;
import javax.naming.ConfigurationException;
import org.apache.cloudstack.api.ResourceDetail;
import org.apache.cloudstack.resourcedetail.ResourceDetailsDao;
import org.apache.cloudstack.resourcedetail.dao.AutoScaleVmGroupDetailsDao;
import org.apache.cloudstack.resourcedetail.dao.AutoScaleVmProfileDetailsDao;
import org.apache.cloudstack.resourcedetail.dao.DiskOfferingDetailsDao;
import org.apache.cloudstack.resourcedetail.dao.FirewallRuleDetailsDao;
import org.apache.cloudstack.resourcedetail.dao.NetworkACLItemDetailsDao;
import org.apache.cloudstack.resourcedetail.dao.NetworkACLListDetailsDao;
import org.apache.cloudstack.resourcedetail.dao.RemoteAccessVpnDetailsDao;
import org.apache.cloudstack.resourcedetail.dao.Site2SiteCustomerGatewayDetailsDao;
import org.apache.cloudstack.resourcedetail.dao.Site2SiteVpnConnectionDetailsDao;
import org.apache.cloudstack.resourcedetail.dao.Site2SiteVpnGatewayDetailsDao;
import org.apache.cloudstack.resourcedetail.dao.UserDetailsDao;
import org.apache.cloudstack.resourcedetail.dao.UserIpAddressDetailsDao;
import org.apache.cloudstack.resourcedetail.dao.VpcDetailsDao;
import org.apache.cloudstack.resourcedetail.dao.VpcGatewayDetailsDao;
import org.apache.cloudstack.storage.datastore.db.StoragePoolDetailsDao;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import com.cloud.dc.dao.DataCenterDetailsDao;
import com.cloud.event.ActionEvent;
import com.cloud.event.EventTypes;
import com.cloud.exception.InvalidParameterValueException;
import com.cloud.network.dao.NetworkDetailsDao;
import com.cloud.server.ResourceMetaDataService;
import com.cloud.server.ResourceTag.ResourceObjectType;
import com.cloud.server.TaggedResourceService;
import com.cloud.service.dao.ServiceOfferingDetailsDao;
import com.cloud.storage.dao.VMTemplateDetailsDao;
import com.cloud.storage.dao.VolumeDetailsDao;
import com.cloud.utils.component.ManagerBase;
import com.cloud.utils.db.DB;
import com.cloud.utils.db.Transaction;
import com.cloud.utils.db.TransactionCallback;
import com.cloud.utils.db.TransactionStatus;
import com.cloud.vm.dao.NicDetailsDao;
import com.cloud.vm.dao.UserVmDetailsDao;
@Component
@Local(value = {ResourceMetaDataService.class, ResourceMetaDataManager.class})
public class ResourceMetaDataManagerImpl extends ManagerBase implements ResourceMetaDataService, ResourceMetaDataManager {
public static final Logger s_logger = Logger.getLogger(ResourceMetaDataManagerImpl.class);
@Inject
VolumeDetailsDao _volumeDetailDao;
@Inject
NicDetailsDao _nicDetailDao;
@Inject
UserVmDetailsDao _userVmDetailDao;
@Inject
DataCenterDetailsDao _dcDetailsDao;
@Inject
NetworkDetailsDao _networkDetailsDao;
@Inject
TaggedResourceService _taggedResourceMgr;
@Inject
VMTemplateDetailsDao _templateDetailsDao;
@Inject
ServiceOfferingDetailsDao _serviceOfferingDetailsDao;
@Inject
StoragePoolDetailsDao _storageDetailsDao;
@Inject
FirewallRuleDetailsDao _firewallRuleDetailsDao;
@Inject
UserIpAddressDetailsDao _userIpAddressDetailsDao;
@Inject
RemoteAccessVpnDetailsDao _vpnDetailsDao;
@Inject
VpcDetailsDao _vpcDetailsDao;
@Inject
VpcGatewayDetailsDao _vpcGatewayDetailsDao;
@Inject
NetworkACLListDetailsDao _networkACLListDetailsDao;
@Inject
NetworkACLItemDetailsDao _networkACLDetailsDao;
@Inject
Site2SiteVpnGatewayDetailsDao _vpnGatewayDetailsDao;
@Inject
Site2SiteCustomerGatewayDetailsDao _customerGatewayDetailsDao;
@Inject
Site2SiteVpnConnectionDetailsDao _vpnConnectionDetailsDao;
@Inject
DiskOfferingDetailsDao _diskOfferingDetailsDao;
@Inject
UserDetailsDao _userDetailsDao;
@Inject
AutoScaleVmProfileDetailsDao _autoScaleVmProfileDetailsDao;
@Inject
AutoScaleVmGroupDetailsDao _autoScaleVmGroupDetailsDao;
private static Map<ResourceObjectType, ResourceDetailsDao<? extends ResourceDetail>> s_daoMap = new HashMap<ResourceObjectType, ResourceDetailsDao<? extends ResourceDetail>>();
@Override
public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
s_daoMap.put(ResourceObjectType.UserVm, _userVmDetailDao);
s_daoMap.put(ResourceObjectType.Volume, _volumeDetailDao);
s_daoMap.put(ResourceObjectType.Template, _templateDetailsDao);
s_daoMap.put(ResourceObjectType.Network, _networkDetailsDao);
s_daoMap.put(ResourceObjectType.Nic, _nicDetailDao);
s_daoMap.put(ResourceObjectType.ServiceOffering, _serviceOfferingDetailsDao);
s_daoMap.put(ResourceObjectType.Zone, _dcDetailsDao);
s_daoMap.put(ResourceObjectType.Storage, _storageDetailsDao);
s_daoMap.put(ResourceObjectType.FirewallRule, _firewallRuleDetailsDao);
s_daoMap.put(ResourceObjectType.PublicIpAddress, _userIpAddressDetailsDao);
s_daoMap.put(ResourceObjectType.PortForwardingRule, _firewallRuleDetailsDao);
s_daoMap.put(ResourceObjectType.LoadBalancer, _firewallRuleDetailsDao);
s_daoMap.put(ResourceObjectType.RemoteAccessVpn, _vpnDetailsDao);
s_daoMap.put(ResourceObjectType.Vpc, _vpcDetailsDao);
s_daoMap.put(ResourceObjectType.PrivateGateway, _vpcGatewayDetailsDao);
s_daoMap.put(ResourceObjectType.NetworkACLList, _networkACLListDetailsDao);
s_daoMap.put(ResourceObjectType.NetworkACL, _networkACLDetailsDao);
s_daoMap.put(ResourceObjectType.VpnGateway, _vpnGatewayDetailsDao);
s_daoMap.put(ResourceObjectType.CustomerGateway, _customerGatewayDetailsDao);
s_daoMap.put(ResourceObjectType.VpnConnection, _vpnConnectionDetailsDao);
s_daoMap.put(ResourceObjectType.DiskOffering, _diskOfferingDetailsDao);
s_daoMap.put(ResourceObjectType.User, _userDetailsDao);
s_daoMap.put(ResourceObjectType.AutoScaleVmProfile, _autoScaleVmProfileDetailsDao);
s_daoMap.put(ResourceObjectType.AutoScaleVmGroup, _autoScaleVmGroupDetailsDao);
return true;
}
@Override
public boolean start() {
return true;
}
@Override
public boolean stop() {
return true;
}
@Override
@DB
@ActionEvent(eventType = EventTypes.EVENT_RESOURCE_DETAILS_CREATE, eventDescription = "creating resource meta data")
public boolean addResourceMetaData(final String resourceId, final ResourceObjectType resourceType, final Map<String, String> details, final boolean forDisplay) {
return Transaction.execute(new TransactionCallback<Boolean>() {
@Override
public Boolean doInTransaction(TransactionStatus status) {
for (String key : details.keySet()) {
String value = details.get(key);
if (value == null || value.isEmpty()) {
throw new InvalidParameterValueException("Value for the key " + key + " is either null or empty");
}
DetailDaoHelper newDetailDaoHelper = new DetailDaoHelper(resourceType);
newDetailDaoHelper.addDetail(_taggedResourceMgr.getResourceId(resourceId, resourceType), key, value, forDisplay);
}
return true;
}
});
}
@Override
@DB
@ActionEvent(eventType = EventTypes.EVENT_RESOURCE_DETAILS_DELETE, eventDescription = "deleting resource meta data")
public boolean deleteResourceMetaData(String resourceId, ResourceObjectType resourceType, String key) {
long id = _taggedResourceMgr.getResourceId(resourceId, resourceType);
DetailDaoHelper newDetailDaoHelper = new DetailDaoHelper(resourceType);
newDetailDaoHelper.removeDetail(id, key);
return true;
}
private class DetailDaoHelper {
private ResourceObjectType resourceType;
private ResourceDetailsDao<? super ResourceDetail> dao;
private DetailDaoHelper(ResourceObjectType resourceType) {
if (!resourceType.resourceMetadataSupport()) {
throw new UnsupportedOperationException("ResourceType " + resourceType + " doesn't support metadata");
}
this.resourceType = resourceType;
ResourceDetailsDao<?> dao = s_daoMap.get(resourceType);
if (dao == null) {
throw new UnsupportedOperationException("ResourceType " + resourceType + " doesn't support metadata");
}
this.dao = (ResourceDetailsDao)s_daoMap.get(resourceType);
}
private void removeDetail(long resourceId, String key) {
dao.removeDetail(resourceId, key);
}
private ResourceDetail getDetail(long resourceId, String key) {
return dao.findDetail(resourceId, key);
}
private void addDetail(long resourceId, String key, String value, boolean forDisplay) {
dao.addDetail(resourceId, key, value, forDisplay);
}
private Map<String, String> getDetailsMap(long resourceId, Boolean forDisplay) {
if (forDisplay == null) {
return dao.listDetailsKeyPairs(resourceId);
} else {
return dao.listDetailsKeyPairs(resourceId, forDisplay);
}
}
private List<? extends ResourceDetail> getDetailsList(long resourceId, Boolean forDisplay) {
if (forDisplay == null) {
return dao.listDetails(resourceId);
} else {
return dao.listDetails(resourceId, forDisplay);
}
}
}
@Override
public List<? extends ResourceDetail> getDetailsList(long resourceId, ResourceObjectType resourceType, Boolean forDisplay) {
DetailDaoHelper newDetailDaoHelper = new DetailDaoHelper(resourceType);
return newDetailDaoHelper.getDetailsList(resourceId, forDisplay);
}
@Override
public ResourceDetail getDetail(long resourceId, ResourceObjectType resourceType, String key) {
DetailDaoHelper newDetailDaoHelper = new DetailDaoHelper(resourceType);
return newDetailDaoHelper.getDetail(resourceId, key);
}
@Override
public Map<String, String> getDetailsMap(long resourceId, ResourceObjectType resourceType, Boolean forDisplay) {
DetailDaoHelper newDetailDaoHelper = new DetailDaoHelper(resourceType);
return newDetailDaoHelper.getDetailsMap(resourceId, forDisplay);
}
}