2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.ccsdk.sli.adaptors.rm.dao.jdbc;
24 import java.util.ArrayList;
25 import java.util.HashSet;
26 import java.util.List;
27 import org.onap.ccsdk.sli.adaptors.rm.dao.ResourceDao;
28 import org.onap.ccsdk.sli.adaptors.rm.data.LabelAllocationItem;
29 import org.onap.ccsdk.sli.adaptors.rm.data.LabelResource;
30 import org.onap.ccsdk.sli.adaptors.rm.data.LimitAllocationItem;
31 import org.onap.ccsdk.sli.adaptors.rm.data.LimitResource;
32 import org.onap.ccsdk.sli.adaptors.rm.data.RangeAllocationItem;
33 import org.onap.ccsdk.sli.adaptors.rm.data.RangeResource;
34 import org.onap.ccsdk.sli.adaptors.rm.data.ResourceKey;
35 import org.onap.ccsdk.sli.adaptors.rm.data.ResourceType;
36 import org.onap.ccsdk.sli.adaptors.util.str.StrUtil;
38 public class ResourceDaoImpl implements ResourceDao {
40 private ResourceJdbcDao resourceJdbcDao;
41 private ResourceLoadJdbcDao resourceLoadJdbcDao;
42 private AllocationItemJdbcDao allocationItemJdbcDao;
45 public org.onap.ccsdk.sli.adaptors.rm.data.Resource getResource(String assetId, String resourceName) {
46 Resource rEntity = resourceJdbcDao.getResource(assetId, resourceName);
47 org.onap.ccsdk.sli.adaptors.rm.data.Resource r = createResource(rEntity);
50 List<AllocationItem> aiEntityList = allocationItemJdbcDao.getAllocationItems(rEntity.id);
51 r.allocationItems = new ArrayList<>();
52 for (AllocationItem aiEntity : aiEntityList) {
53 org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem ai = createAllocationItem(r, aiEntity);
54 r.allocationItems.add(ai);
57 List<ResourceLoad> rlEntityList = resourceLoadJdbcDao.getResourceLoads(rEntity.id);
58 r.resourceLoadList = new ArrayList<>();
59 for (ResourceLoad rlEntity : rlEntityList) {
60 org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad rl = createResourceLoad(r, rlEntity);
61 r.resourceLoadList.add(rl);
69 public org.onap.ccsdk.sli.adaptors.rm.data.Resource query(String assetId, String resourceName,
70 String resourceUnionFilter, String resourceShareGroupFilter) {
71 Resource rEntity = resourceJdbcDao.getResource(assetId, resourceName);
72 org.onap.ccsdk.sli.adaptors.rm.data.Resource r = createResource(rEntity);
75 List<AllocationItem> aiEntityList = allocationItemJdbcDao.queryAllocationItems(rEntity.id,
76 resourceUnionFilter, resourceShareGroupFilter);
77 r.allocationItems = new ArrayList<>();
78 for (AllocationItem aiEntity : aiEntityList) {
79 org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem ai = createAllocationItem(r, aiEntity);
80 r.allocationItems.add(ai);
83 List<ResourceLoad> rlEntityList = resourceLoadJdbcDao.getResourceLoads(rEntity.id);
84 r.resourceLoadList = new ArrayList<>();
85 for (ResourceLoad rlEntity : rlEntityList) {
86 org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad rl = createResourceLoad(r, rlEntity);
87 r.resourceLoadList.add(rl);
95 public List<org.onap.ccsdk.sli.adaptors.rm.data.Resource> query(String assetIdFilter, String resourceName) {
96 List<Resource> rEntityList = resourceJdbcDao.queryResources(assetIdFilter, resourceName);
97 List<org.onap.ccsdk.sli.adaptors.rm.data.Resource> rlist = new ArrayList<>();
98 for (Resource rEntity : rEntityList) {
99 org.onap.ccsdk.sli.adaptors.rm.data.Resource r = createResource(rEntity);
102 List<AllocationItem> aiEntityList = allocationItemJdbcDao.getAllocationItems(rEntity.id);
103 r.allocationItems = new ArrayList<>();
104 for (AllocationItem aiEntity : aiEntityList) {
105 org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem ai = createAllocationItem(r, aiEntity);
106 r.allocationItems.add(ai);
109 List<ResourceLoad> rlEntityList = resourceLoadJdbcDao.getResourceLoads(rEntity.id);
110 r.resourceLoadList = new ArrayList<>();
111 for (ResourceLoad rlEntity : rlEntityList) {
112 org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad rl = createResourceLoad(r, rlEntity);
113 r.resourceLoadList.add(rl);
120 public void saveResource(org.onap.ccsdk.sli.adaptors.rm.data.Resource resource) {
121 if (resource == null) {
125 org.onap.ccsdk.sli.adaptors.rm.dao.jdbc.Resource resourceEntity =
126 resourceJdbcDao.getResource(resource.resourceKey.assetId, resource.resourceKey.resourceName);
127 if (resourceEntity == null) {
128 resourceEntity = createResourceEntity(resource);
129 resourceJdbcDao.add(resourceEntity);
130 if (resource.allocationItems != null) {
131 for (org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem ai : resource.allocationItems) {
132 AllocationItem aiEntity = createAllocationItemEntity(resourceEntity.id, ai);
133 allocationItemJdbcDao.add(aiEntity);
136 if (resource.resourceLoadList != null) {
137 for (org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad rl : resource.resourceLoadList) {
138 ResourceLoad rlEntity = createResourceLoadEntity(resourceEntity.id, rl);
139 resourceLoadJdbcDao.add(rlEntity);
143 updateResourceEntity(resourceEntity, resource);
144 resourceJdbcDao.update(resourceEntity);
146 List<AllocationItem> oldAiEntityList = allocationItemJdbcDao.getAllocationItems(resourceEntity.id);
147 if (resource.allocationItems != null) {
148 for (org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem newai : resource.allocationItems) {
149 AllocationItem foundAiEntity = null;
150 for (AllocationItem oldAiEntity : oldAiEntityList) {
151 if (oldAiEntity.resourceSetId.equals(newai.resourceSetId)) {
152 foundAiEntity = oldAiEntity;
156 if (foundAiEntity != null) {
157 if (allocationItemChanged(foundAiEntity, newai)) {
158 updateAllocationItemEntity(foundAiEntity, newai);
159 allocationItemJdbcDao.update(foundAiEntity);
162 AllocationItem newAiEntity = createAllocationItemEntity(resourceEntity.id, newai);
163 allocationItemJdbcDao.add(newAiEntity);
167 for (AllocationItem oldAiEntity : oldAiEntityList) {
168 boolean found = false;
169 if (resource.allocationItems != null) {
170 for (org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem newai : resource.allocationItems) {
171 if (oldAiEntity.resourceSetId.equals(newai.resourceSetId)) {
178 allocationItemJdbcDao.delete(oldAiEntity.id);
182 List<ResourceLoad> oldRlEntityList = resourceLoadJdbcDao.getResourceLoads(resourceEntity.id);
183 if (resource.resourceLoadList != null) {
184 for (org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad newrl : resource.resourceLoadList) {
185 ResourceLoad foundRlEntity = null;
186 for (ResourceLoad oldRlEntity : oldRlEntityList) {
187 if (oldRlEntity.applicationId.equals(newrl.applicationId)) {
188 foundRlEntity = oldRlEntity;
192 if (foundRlEntity != null) {
193 updateResourceLoadEntity(foundRlEntity, newrl);
194 resourceLoadJdbcDao.update(foundRlEntity);
196 ResourceLoad newRlEntity = createResourceLoadEntity(resourceEntity.id, newrl);
197 resourceLoadJdbcDao.add(newRlEntity);
201 for (ResourceLoad oldRlEntity : oldRlEntityList) {
202 boolean found = false;
203 if (resource.resourceLoadList != null) {
204 for (org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad newrl : resource.resourceLoadList) {
205 if (oldRlEntity.applicationId.equals(newrl.applicationId)) {
212 resourceLoadJdbcDao.delete(oldRlEntity.id);
218 private boolean allocationItemChanged(AllocationItem aiEntity,
219 org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem newai) {
220 String newShareGroupList = StrUtil.listStr(newai.resourceShareGroupList);
221 if (!eq(aiEntity.resourceShareGroupList, newShareGroupList)) {
225 if (newai.resourceType == ResourceType.Limit) {
226 if (aiEntity.ltUsed != ((LimitAllocationItem) newai).used) {
229 } else if (newai.resourceType == ResourceType.Label) {
230 if (!eq(aiEntity.llLabel, ((LabelAllocationItem) newai).label)) {
233 } else if (newai.resourceType == ResourceType.Range) {
234 String newRrUsed = StrUtil.listInt(((RangeAllocationItem) newai).used);
235 if (!eq(aiEntity.rrUsed, newRrUsed)) {
244 public void deleteResource(String assetId, String resourceName) {
245 org.onap.ccsdk.sli.adaptors.rm.dao.jdbc.Resource resourceEntity =
246 resourceJdbcDao.getResource(assetId, resourceName);
247 if (resourceEntity != null) {
248 resourceJdbcDao.delete(resourceEntity.id);
253 public List<org.onap.ccsdk.sli.adaptors.rm.data.Resource> getResourceSet(String resourceSetId) {
254 List<Resource> rEntityList = resourceJdbcDao.getResourceSet(resourceSetId);
255 List<org.onap.ccsdk.sli.adaptors.rm.data.Resource> rlist = new ArrayList<>();
256 for (Resource rEntity : rEntityList) {
257 org.onap.ccsdk.sli.adaptors.rm.data.Resource r = createResource(rEntity);
260 List<AllocationItem> aiEntityList = allocationItemJdbcDao.getAllocationItems(rEntity.id);
261 r.allocationItems = new ArrayList<>();
262 for (AllocationItem aiEntity : aiEntityList) {
263 org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem ai = createAllocationItem(r, aiEntity);
264 r.allocationItems.add(ai);
267 List<ResourceLoad> rlEntityList = resourceLoadJdbcDao.getResourceLoads(rEntity.id);
268 r.resourceLoadList = new ArrayList<>();
269 for (ResourceLoad rlEntity : rlEntityList) {
270 org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad rl = createResourceLoad(r, rlEntity);
271 r.resourceLoadList.add(rl);
278 public List<org.onap.ccsdk.sli.adaptors.rm.data.Resource> getResourceUnion(String resourceUnionId) {
279 List<Resource> rEntityList = resourceJdbcDao.getResourceUnion(resourceUnionId);
280 List<org.onap.ccsdk.sli.adaptors.rm.data.Resource> rlist = new ArrayList<>();
281 for (Resource rEntity : rEntityList) {
282 org.onap.ccsdk.sli.adaptors.rm.data.Resource r = createResource(rEntity);
285 List<AllocationItem> aiEntityList = allocationItemJdbcDao.getAllocationItems(rEntity.id);
286 r.allocationItems = new ArrayList<>();
287 for (AllocationItem aiEntity : aiEntityList) {
288 org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem ai = createAllocationItem(r, aiEntity);
289 r.allocationItems.add(ai);
292 List<ResourceLoad> rlEntityList = resourceLoadJdbcDao.getResourceLoads(rEntity.id);
293 r.resourceLoadList = new ArrayList<>();
294 for (ResourceLoad rlEntity : rlEntityList) {
295 org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad rl = createResourceLoad(r, rlEntity);
296 r.resourceLoadList.add(rl);
302 private Resource createResourceEntity(org.onap.ccsdk.sli.adaptors.rm.data.Resource resource) {
303 Resource resourceEntity = new Resource();
304 resourceEntity.assetId = resource.resourceKey.assetId;
305 resourceEntity.name = resource.resourceKey.resourceName;
306 resourceEntity.type = resource.resourceType.toString();
307 if (resource.resourceType == ResourceType.Limit) {
308 resourceEntity.ltUsed = ((LimitResource) resource).used;
309 } else if (resource.resourceType == ResourceType.Label) {
310 resourceEntity.llLabel = ((LabelResource) resource).label;
311 resourceEntity.llReferenceCount = ((LabelResource) resource).referenceCount;
312 } else if (resource.resourceType == ResourceType.Range) {
313 resourceEntity.rrUsed = StrUtil.listInt(((RangeResource) resource).used);
316 return resourceEntity;
319 private ResourceLoad createResourceLoadEntity(long resourceId,
320 org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad rl) {
321 ResourceLoad rlEntity = new ResourceLoad();
322 rlEntity.resourceId = resourceId;
323 rlEntity.applicationId = rl.applicationId;
324 rlEntity.loadTime = rl.resourceLoadTime;
325 rlEntity.expirationTime = rl.resourceExpirationTime;
329 private void updateResourceLoadEntity(ResourceLoad rlEntity, org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad rl) {
330 rlEntity.loadTime = rl.resourceLoadTime;
331 rlEntity.expirationTime = rl.resourceExpirationTime;
334 private AllocationItem createAllocationItemEntity(long resourceId,
335 org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem ai) {
336 AllocationItem aiEntity = new AllocationItem();
337 aiEntity.resourceId = resourceId;
338 aiEntity.resourceSetId = ai.resourceSetId;
339 aiEntity.resourceUnionId = ai.resourceUnionId;
340 aiEntity.resourceShareGroupList = StrUtil.listStr(ai.resourceShareGroupList);
341 aiEntity.applicationId = ai.applicationId;
342 aiEntity.allocationTime = ai.allocationTime;
343 if (ai.resourceType == ResourceType.Limit) {
344 aiEntity.ltUsed = ((LimitAllocationItem) ai).used;
345 } else if (ai.resourceType == ResourceType.Label) {
346 aiEntity.llLabel = ((LabelAllocationItem) ai).label;
347 } else if (ai.resourceType == ResourceType.Range) {
348 aiEntity.rrUsed = StrUtil.listInt(((RangeAllocationItem) ai).used);
353 private void updateAllocationItemEntity(AllocationItem aiEntity,
354 org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem ai) {
355 aiEntity.resourceShareGroupList = StrUtil.listStr(ai.resourceShareGroupList);
356 aiEntity.allocationTime = ai.allocationTime;
357 if (ai.resourceType == ResourceType.Limit) {
358 aiEntity.ltUsed = ((LimitAllocationItem) ai).used;
359 } else if (ai.resourceType == ResourceType.Label) {
360 aiEntity.llLabel = ((LabelAllocationItem) ai).label;
361 } else if (ai.resourceType == ResourceType.Range) {
362 aiEntity.rrUsed = StrUtil.listInt(((RangeAllocationItem) ai).used);
366 private void updateResourceEntity(Resource resourceEntity, org.onap.ccsdk.sli.adaptors.rm.data.Resource resource) {
367 if (resource.resourceType == ResourceType.Limit) {
368 resourceEntity.ltUsed = ((LimitResource) resource).used;
369 } else if (resource.resourceType == ResourceType.Label) {
370 resourceEntity.llLabel = ((LabelResource) resource).label;
371 resourceEntity.llReferenceCount = ((LabelResource) resource).referenceCount;
372 } else if (resource.resourceType == ResourceType.Range) {
373 resourceEntity.rrUsed = StrUtil.listInt(((RangeResource) resource).used);
377 private org.onap.ccsdk.sli.adaptors.rm.data.Resource createResource(Resource resourceEntity) {
378 if (resourceEntity == null) {
382 org.onap.ccsdk.sli.adaptors.rm.data.Resource r = null;
383 ResourceType type = ResourceType.valueOf(resourceEntity.type);
384 if (type == ResourceType.Limit) {
385 LimitResource l = new LimitResource();
386 l.used = resourceEntity.ltUsed;
388 } else if (type == ResourceType.Label) {
389 LabelResource l = new LabelResource();
390 l.label = resourceEntity.llLabel;
391 l.referenceCount = resourceEntity.llReferenceCount;
393 } else if (type == ResourceType.Range) {
394 RangeResource rr = new RangeResource();
395 rr.used = StrUtil.listInt(resourceEntity.rrUsed, "Invalid data found in DB in for Resource Id: "
396 + resourceEntity.id + ": RESOURCE.RR_USED: " + resourceEntity.rrUsed);
401 r.resourceType = type;
402 r.resourceKey = new ResourceKey();
403 r.resourceKey.assetId = resourceEntity.assetId;
404 r.resourceKey.resourceName = resourceEntity.name;
410 private org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem createAllocationItem(
411 org.onap.ccsdk.sli.adaptors.rm.data.Resource r, AllocationItem aiEntity) {
412 if (r == null || aiEntity == null) {
416 org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem ai = null;
417 if (r.resourceType == ResourceType.Limit) {
418 LimitAllocationItem lai = new LimitAllocationItem();
419 lai.used = aiEntity.ltUsed;
421 } else if (r.resourceType == ResourceType.Label) {
422 LabelAllocationItem lai = new LabelAllocationItem();
423 lai.label = aiEntity.llLabel;
425 } else if (r.resourceType == ResourceType.Range) {
426 RangeAllocationItem rai = new RangeAllocationItem();
427 rai.used = StrUtil.listInt(aiEntity.rrUsed, "Invalid data found in DB in for Allocation Item Id: "
428 + aiEntity.id + ": ALLOCATION_ITEM.RR_USED: " + aiEntity.rrUsed);
433 ai.resourceType = r.resourceType;
434 ai.resourceKey = r.resourceKey;
435 ai.resourceSetId = aiEntity.resourceSetId;
436 ai.resourceUnionId = aiEntity.resourceUnionId;
437 if (aiEntity.resourceShareGroupList != null) {
438 ai.resourceShareGroupList = new HashSet<>(StrUtil.listStr(aiEntity.resourceShareGroupList));
440 ai.applicationId = aiEntity.applicationId;
441 ai.allocationTime = aiEntity.allocationTime;
447 private org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad createResourceLoad(
448 org.onap.ccsdk.sli.adaptors.rm.data.Resource r, ResourceLoad rlEntity) {
449 if (rlEntity == null) {
453 org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad rl = new org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad();
454 rl.resourceKey = r.resourceKey;
455 rl.applicationId = rlEntity.applicationId;
456 rl.resourceLoadTime = rlEntity.loadTime;
457 rl.resourceExpirationTime = rlEntity.expirationTime;
462 private static boolean eq(Object o1, Object o2) {
463 return o1 == null ? o2 == null : o1.equals(o2);
466 public void setResourceJdbcDao(ResourceJdbcDao resourceJdbcDao) {
467 this.resourceJdbcDao = resourceJdbcDao;
470 public void setResourceLoadJdbcDao(ResourceLoadJdbcDao resourceLoadJdbcDao) {
471 this.resourceLoadJdbcDao = resourceLoadJdbcDao;
474 public void setAllocationItemJdbcDao(AllocationItemJdbcDao allocationItemJdbcDao) {
475 this.allocationItemJdbcDao = allocationItemJdbcDao;