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 return createResourceWithItems(rEntity);
51 public org.onap.ccsdk.sli.adaptors.rm.data.Resource query(String assetId, String resourceName,
52 String resourceUnionFilter, String resourceShareGroupFilter) {
53 Resource rEntity = resourceJdbcDao.getResource(assetId, resourceName);
54 org.onap.ccsdk.sli.adaptors.rm.data.Resource r = createResource(rEntity);
57 List<AllocationItem> aiEntityList = allocationItemJdbcDao.queryAllocationItems(rEntity.id,
58 resourceUnionFilter, resourceShareGroupFilter);
59 r.allocationItems = new ArrayList<>();
60 for (AllocationItem aiEntity : aiEntityList) {
61 org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem ai = createAllocationItem(r, aiEntity);
62 r.allocationItems.add(ai);
65 List<ResourceLoad> rlEntityList = resourceLoadJdbcDao.getResourceLoads(rEntity.id);
66 r.resourceLoadList = new ArrayList<>();
67 for (ResourceLoad rlEntity : rlEntityList) {
68 org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad rl = createResourceLoad(r, rlEntity);
69 r.resourceLoadList.add(rl);
77 public List<org.onap.ccsdk.sli.adaptors.rm.data.Resource> query(String assetIdFilter, String resourceName) {
78 List<Resource> rEntityList = resourceJdbcDao.queryResources(assetIdFilter, resourceName);
79 List<org.onap.ccsdk.sli.adaptors.rm.data.Resource> rlist = new ArrayList<>();
80 for (Resource rEntity : rEntityList) {
81 org.onap.ccsdk.sli.adaptors.rm.data.Resource r = createResourceWithItems(rEntity);
88 public void saveResource(org.onap.ccsdk.sli.adaptors.rm.data.Resource resource) {
89 if (resource == null) {
93 org.onap.ccsdk.sli.adaptors.rm.dao.jdbc.Resource resourceEntity =
94 resourceJdbcDao.getResource(resource.resourceKey.assetId, resource.resourceKey.resourceName);
95 if (resourceEntity == null) {
96 resourceEntity = createResourceEntity(resource);
97 resourceJdbcDao.add(resourceEntity);
98 if (resource.allocationItems != null) {
99 for (org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem ai : resource.allocationItems) {
100 AllocationItem aiEntity = createAllocationItemEntity(resourceEntity.id, ai);
101 allocationItemJdbcDao.add(aiEntity);
104 if (resource.resourceLoadList != null) {
105 for (org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad rl : resource.resourceLoadList) {
106 ResourceLoad rlEntity = createResourceLoadEntity(resourceEntity.id, rl);
107 resourceLoadJdbcDao.add(rlEntity);
111 updateResourceEntity(resourceEntity, resource);
112 resourceJdbcDao.update(resourceEntity);
114 List<AllocationItem> oldAiEntityList = allocationItemJdbcDao.getAllocationItems(resourceEntity.id);
115 if (resource.allocationItems != null) {
116 for (org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem newai : resource.allocationItems) {
117 AllocationItem foundAiEntity = null;
118 for (AllocationItem oldAiEntity : oldAiEntityList) {
119 if (oldAiEntity.resourceSetId.equals(newai.resourceSetId)) {
120 foundAiEntity = oldAiEntity;
124 if (foundAiEntity != null) {
125 if (allocationItemChanged(foundAiEntity, newai)) {
126 updateAllocationItemEntity(foundAiEntity, newai);
127 allocationItemJdbcDao.update(foundAiEntity);
130 AllocationItem newAiEntity = createAllocationItemEntity(resourceEntity.id, newai);
131 allocationItemJdbcDao.add(newAiEntity);
135 for (AllocationItem oldAiEntity : oldAiEntityList) {
136 boolean found = false;
137 if (resource.allocationItems != null) {
138 for (org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem newai : resource.allocationItems) {
139 if (oldAiEntity.resourceSetId.equals(newai.resourceSetId)) {
146 allocationItemJdbcDao.delete(oldAiEntity.id);
150 List<ResourceLoad> oldRlEntityList = resourceLoadJdbcDao.getResourceLoads(resourceEntity.id);
151 if (resource.resourceLoadList != null) {
152 for (org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad newrl : resource.resourceLoadList) {
153 ResourceLoad foundRlEntity = null;
154 for (ResourceLoad oldRlEntity : oldRlEntityList) {
155 if (oldRlEntity.applicationId.equals(newrl.applicationId)) {
156 foundRlEntity = oldRlEntity;
160 if (foundRlEntity != null) {
161 updateResourceLoadEntity(foundRlEntity, newrl);
162 resourceLoadJdbcDao.update(foundRlEntity);
164 ResourceLoad newRlEntity = createResourceLoadEntity(resourceEntity.id, newrl);
165 resourceLoadJdbcDao.add(newRlEntity);
169 for (ResourceLoad oldRlEntity : oldRlEntityList) {
170 boolean found = false;
171 if (resource.resourceLoadList != null) {
172 for (org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad newrl : resource.resourceLoadList) {
173 if (oldRlEntity.applicationId.equals(newrl.applicationId)) {
180 resourceLoadJdbcDao.delete(oldRlEntity.id);
186 private boolean allocationItemChanged(AllocationItem aiEntity,
187 org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem newai) {
188 String newShareGroupList = StrUtil.listStr(newai.resourceShareGroupList);
189 if (!eq(aiEntity.resourceShareGroupList, newShareGroupList)) {
193 if (newai.resourceType == ResourceType.Limit) {
194 if (aiEntity.ltUsed != ((LimitAllocationItem) newai).used) {
197 } else if (newai.resourceType == ResourceType.Label) {
198 if (!eq(aiEntity.llLabel, ((LabelAllocationItem) newai).label)) {
201 } else if (newai.resourceType == ResourceType.Range) {
202 String newRrUsed = StrUtil.listInt(((RangeAllocationItem) newai).used);
203 if (!eq(aiEntity.rrUsed, newRrUsed)) {
212 public void deleteResource(String assetId, String resourceName) {
213 org.onap.ccsdk.sli.adaptors.rm.dao.jdbc.Resource resourceEntity =
214 resourceJdbcDao.getResource(assetId, resourceName);
215 if (resourceEntity != null) {
216 resourceJdbcDao.delete(resourceEntity.id);
221 public List<org.onap.ccsdk.sli.adaptors.rm.data.Resource> getResourceSet(String resourceSetId) {
222 List<Resource> rEntityList = resourceJdbcDao.getResourceSet(resourceSetId);
223 List<org.onap.ccsdk.sli.adaptors.rm.data.Resource> rlist = new ArrayList<>();
224 for (Resource rEntity : rEntityList) {
225 org.onap.ccsdk.sli.adaptors.rm.data.Resource r = createResourceWithItems(rEntity);
232 public List<org.onap.ccsdk.sli.adaptors.rm.data.Resource> getResourceUnion(String resourceUnionId) {
233 List<Resource> rEntityList = resourceJdbcDao.getResourceUnion(resourceUnionId);
234 List<org.onap.ccsdk.sli.adaptors.rm.data.Resource> rlist = new ArrayList<>();
235 for (Resource rEntity : rEntityList) {
236 org.onap.ccsdk.sli.adaptors.rm.data.Resource r = createResourceWithItems(rEntity);
243 public List<org.onap.ccsdk.sli.adaptors.rm.data.Resource> getResourceSetForAsset(String resourceSetId,
245 List<Resource> rEntityList = resourceJdbcDao.getResourceSetForAsset(resourceSetId, assetId);
246 List<org.onap.ccsdk.sli.adaptors.rm.data.Resource> rlist = new ArrayList<>();
247 for (Resource rEntity : rEntityList) {
248 org.onap.ccsdk.sli.adaptors.rm.data.Resource r = createResourceWithItems(rEntity);
255 public List<org.onap.ccsdk.sli.adaptors.rm.data.Resource> getResourceUnionForAsset(String resourceUnionId,
257 List<Resource> rEntityList = resourceJdbcDao.getResourceUnionForAsset(resourceUnionId, assetId);
258 List<org.onap.ccsdk.sli.adaptors.rm.data.Resource> rlist = new ArrayList<>();
259 for (Resource rEntity : rEntityList) {
260 org.onap.ccsdk.sli.adaptors.rm.data.Resource r = createResourceWithItems(rEntity);
266 private Resource createResourceEntity(org.onap.ccsdk.sli.adaptors.rm.data.Resource resource) {
267 Resource resourceEntity = new Resource();
268 resourceEntity.assetId = resource.resourceKey.assetId;
269 resourceEntity.name = resource.resourceKey.resourceName;
270 resourceEntity.type = resource.resourceType.toString();
271 if (resource.resourceType == ResourceType.Limit) {
272 resourceEntity.ltUsed = ((LimitResource) resource).used;
273 } else if (resource.resourceType == ResourceType.Label) {
274 resourceEntity.llLabel = ((LabelResource) resource).label;
275 resourceEntity.llReferenceCount = ((LabelResource) resource).referenceCount;
276 } else if (resource.resourceType == ResourceType.Range) {
277 resourceEntity.rrUsed = StrUtil.listInt(((RangeResource) resource).used);
280 return resourceEntity;
283 private ResourceLoad createResourceLoadEntity(long resourceId,
284 org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad rl) {
285 ResourceLoad rlEntity = new ResourceLoad();
286 rlEntity.resourceId = resourceId;
287 rlEntity.applicationId = rl.applicationId;
288 rlEntity.loadTime = rl.resourceLoadTime;
289 rlEntity.expirationTime = rl.resourceExpirationTime;
293 private void updateResourceLoadEntity(ResourceLoad rlEntity, org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad rl) {
294 rlEntity.loadTime = rl.resourceLoadTime;
295 rlEntity.expirationTime = rl.resourceExpirationTime;
298 private AllocationItem createAllocationItemEntity(long resourceId,
299 org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem ai) {
300 AllocationItem aiEntity = new AllocationItem();
301 aiEntity.resourceId = resourceId;
302 aiEntity.resourceSetId = ai.resourceSetId;
303 aiEntity.resourceUnionId = ai.resourceUnionId;
304 aiEntity.resourceShareGroupList = StrUtil.listStr(ai.resourceShareGroupList);
305 aiEntity.applicationId = ai.applicationId;
306 aiEntity.allocationTime = ai.allocationTime;
307 if (ai.resourceType == ResourceType.Limit) {
308 aiEntity.ltUsed = ((LimitAllocationItem) ai).used;
309 } else if (ai.resourceType == ResourceType.Label) {
310 aiEntity.llLabel = ((LabelAllocationItem) ai).label;
311 } else if (ai.resourceType == ResourceType.Range) {
312 aiEntity.rrUsed = StrUtil.listInt(((RangeAllocationItem) ai).used);
317 private void updateAllocationItemEntity(AllocationItem aiEntity,
318 org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem ai) {
319 aiEntity.resourceShareGroupList = StrUtil.listStr(ai.resourceShareGroupList);
320 aiEntity.allocationTime = ai.allocationTime;
321 if (ai.resourceType == ResourceType.Limit) {
322 aiEntity.ltUsed = ((LimitAllocationItem) ai).used;
323 } else if (ai.resourceType == ResourceType.Label) {
324 aiEntity.llLabel = ((LabelAllocationItem) ai).label;
325 } else if (ai.resourceType == ResourceType.Range) {
326 aiEntity.rrUsed = StrUtil.listInt(((RangeAllocationItem) ai).used);
330 private void updateResourceEntity(Resource resourceEntity, org.onap.ccsdk.sli.adaptors.rm.data.Resource resource) {
331 if (resource.resourceType == ResourceType.Limit) {
332 resourceEntity.ltUsed = ((LimitResource) resource).used;
333 } else if (resource.resourceType == ResourceType.Label) {
334 resourceEntity.llLabel = ((LabelResource) resource).label;
335 resourceEntity.llReferenceCount = ((LabelResource) resource).referenceCount;
336 } else if (resource.resourceType == ResourceType.Range) {
337 resourceEntity.rrUsed = StrUtil.listInt(((RangeResource) resource).used);
341 private org.onap.ccsdk.sli.adaptors.rm.data.Resource createResourceWithItems(Resource rEntity) {
342 org.onap.ccsdk.sli.adaptors.rm.data.Resource r = createResource(rEntity);
345 List<AllocationItem> aiEntityList = allocationItemJdbcDao.getAllocationItems(rEntity.id);
346 r.allocationItems = new ArrayList<>();
347 for (AllocationItem aiEntity : aiEntityList) {
348 org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem ai = createAllocationItem(r, aiEntity);
349 r.allocationItems.add(ai);
352 List<ResourceLoad> rlEntityList = resourceLoadJdbcDao.getResourceLoads(rEntity.id);
353 r.resourceLoadList = new ArrayList<>();
354 for (ResourceLoad rlEntity : rlEntityList) {
355 org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad rl = createResourceLoad(r, rlEntity);
356 r.resourceLoadList.add(rl);
362 private org.onap.ccsdk.sli.adaptors.rm.data.Resource createResource(Resource resourceEntity) {
363 if (resourceEntity == null) {
367 org.onap.ccsdk.sli.adaptors.rm.data.Resource r = null;
368 ResourceType type = ResourceType.valueOf(resourceEntity.type);
369 if (type == ResourceType.Limit) {
370 LimitResource l = new LimitResource();
371 l.used = resourceEntity.ltUsed;
373 } else if (type == ResourceType.Label) {
374 LabelResource l = new LabelResource();
375 l.label = resourceEntity.llLabel;
376 l.referenceCount = resourceEntity.llReferenceCount;
378 } else if (type == ResourceType.Range) {
379 RangeResource rr = new RangeResource();
380 rr.used = StrUtil.listInt(resourceEntity.rrUsed, "Invalid data found in DB in for Resource Id: "
381 + resourceEntity.id + ": RESOURCE.RR_USED: " + resourceEntity.rrUsed);
386 r.resourceType = type;
387 r.resourceKey = new ResourceKey();
388 r.resourceKey.assetId = resourceEntity.assetId;
389 r.resourceKey.resourceName = resourceEntity.name;
395 private org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem createAllocationItem(
396 org.onap.ccsdk.sli.adaptors.rm.data.Resource r, AllocationItem aiEntity) {
397 if (r == null || aiEntity == null) {
401 org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem ai = null;
402 if (r.resourceType == ResourceType.Limit) {
403 LimitAllocationItem lai = new LimitAllocationItem();
404 lai.used = aiEntity.ltUsed;
406 } else if (r.resourceType == ResourceType.Label) {
407 LabelAllocationItem lai = new LabelAllocationItem();
408 lai.label = aiEntity.llLabel;
410 } else if (r.resourceType == ResourceType.Range) {
411 RangeAllocationItem rai = new RangeAllocationItem();
412 rai.used = StrUtil.listInt(aiEntity.rrUsed, "Invalid data found in DB in for Allocation Item Id: "
413 + aiEntity.id + ": ALLOCATION_ITEM.RR_USED: " + aiEntity.rrUsed);
418 ai.resourceType = r.resourceType;
419 ai.resourceKey = r.resourceKey;
420 ai.resourceSetId = aiEntity.resourceSetId;
421 ai.resourceUnionId = aiEntity.resourceUnionId;
422 if (aiEntity.resourceShareGroupList != null) {
423 ai.resourceShareGroupList = new HashSet<>(StrUtil.listStr(aiEntity.resourceShareGroupList));
425 ai.applicationId = aiEntity.applicationId;
426 ai.allocationTime = aiEntity.allocationTime;
432 private org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad createResourceLoad(
433 org.onap.ccsdk.sli.adaptors.rm.data.Resource r, ResourceLoad rlEntity) {
434 if (rlEntity == null) {
438 org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad rl = new org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad();
439 rl.resourceKey = r.resourceKey;
440 rl.applicationId = rlEntity.applicationId;
441 rl.resourceLoadTime = rlEntity.loadTime;
442 rl.resourceExpirationTime = rlEntity.expirationTime;
447 private static boolean eq(Object o1, Object o2) {
448 return o1 == null ? o2 == null : o1.equals(o2);
451 public void setResourceJdbcDao(ResourceJdbcDao resourceJdbcDao) {
452 this.resourceJdbcDao = resourceJdbcDao;
455 public void setResourceLoadJdbcDao(ResourceLoadJdbcDao resourceLoadJdbcDao) {
456 this.resourceLoadJdbcDao = resourceLoadJdbcDao;
459 public void setAllocationItemJdbcDao(AllocationItemJdbcDao allocationItemJdbcDao) {
460 this.allocationItemJdbcDao = allocationItemJdbcDao;