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 void saveResource(org.onap.ccsdk.sli.adaptors.rm.data.Resource resource) {
96 if (resource == null) {
100 org.onap.ccsdk.sli.adaptors.rm.dao.jdbc.Resource resourceEntity =
101 resourceJdbcDao.getResource(resource.resourceKey.assetId, resource.resourceKey.resourceName);
102 if (resourceEntity == null) {
103 resourceEntity = createResourceEntity(resource);
104 resourceJdbcDao.add(resourceEntity);
105 if (resource.allocationItems != null) {
106 for (org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem ai : resource.allocationItems) {
107 AllocationItem aiEntity = createAllocationItemEntity(resourceEntity.id, ai);
108 allocationItemJdbcDao.add(aiEntity);
111 if (resource.resourceLoadList != null) {
112 for (org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad rl : resource.resourceLoadList) {
113 ResourceLoad rlEntity = createResourceLoadEntity(resourceEntity.id, rl);
114 resourceLoadJdbcDao.add(rlEntity);
118 updateResourceEntity(resourceEntity, resource);
119 resourceJdbcDao.update(resourceEntity);
121 List<AllocationItem> oldAiEntityList = allocationItemJdbcDao.getAllocationItems(resourceEntity.id);
122 if (resource.allocationItems != null) {
123 for (org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem newai : resource.allocationItems) {
124 AllocationItem foundAiEntity = null;
125 for (AllocationItem oldAiEntity : oldAiEntityList) {
126 if (oldAiEntity.resourceSetId.equals(newai.resourceSetId)) {
127 foundAiEntity = oldAiEntity;
131 if (foundAiEntity != null) {
132 if (allocationItemChanged(foundAiEntity, newai)) {
133 updateAllocationItemEntity(foundAiEntity, newai);
134 allocationItemJdbcDao.update(foundAiEntity);
137 AllocationItem newAiEntity = createAllocationItemEntity(resourceEntity.id, newai);
138 allocationItemJdbcDao.add(newAiEntity);
142 for (AllocationItem oldAiEntity : oldAiEntityList) {
143 boolean found = false;
144 if (resource.allocationItems != null) {
145 for (org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem newai : resource.allocationItems) {
146 if (oldAiEntity.resourceSetId.equals(newai.resourceSetId)) {
153 allocationItemJdbcDao.delete(oldAiEntity.id);
157 List<ResourceLoad> oldRlEntityList = resourceLoadJdbcDao.getResourceLoads(resourceEntity.id);
158 if (resource.resourceLoadList != null) {
159 for (org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad newrl : resource.resourceLoadList) {
160 ResourceLoad foundRlEntity = null;
161 for (ResourceLoad oldRlEntity : oldRlEntityList) {
162 if (oldRlEntity.applicationId.equals(newrl.applicationId)) {
163 foundRlEntity = oldRlEntity;
167 if (foundRlEntity != null) {
168 updateResourceLoadEntity(foundRlEntity, newrl);
169 resourceLoadJdbcDao.update(foundRlEntity);
171 ResourceLoad newRlEntity = createResourceLoadEntity(resourceEntity.id, newrl);
172 resourceLoadJdbcDao.add(newRlEntity);
176 for (ResourceLoad oldRlEntity : oldRlEntityList) {
177 boolean found = false;
178 if (resource.resourceLoadList != null) {
179 for (org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad newrl : resource.resourceLoadList) {
180 if (oldRlEntity.applicationId.equals(newrl.applicationId)) {
187 resourceLoadJdbcDao.delete(oldRlEntity.id);
193 private boolean allocationItemChanged(AllocationItem aiEntity,
194 org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem newai) {
195 String newShareGroupList = StrUtil.listStr(newai.resourceShareGroupList);
196 if (!eq(aiEntity.resourceShareGroupList, newShareGroupList)) {
200 if (newai.resourceType == ResourceType.Limit) {
201 if (aiEntity.ltUsed != ((LimitAllocationItem) newai).used) {
204 } else if (newai.resourceType == ResourceType.Label) {
205 if (!eq(aiEntity.llLabel, ((LabelAllocationItem) newai).label)) {
208 } else if (newai.resourceType == ResourceType.Range) {
209 String newRrUsed = StrUtil.listInt(((RangeAllocationItem) newai).used);
210 if (!eq(aiEntity.rrUsed, newRrUsed)) {
219 public void deleteResource(String assetId, String resourceName) {
220 org.onap.ccsdk.sli.adaptors.rm.dao.jdbc.Resource resourceEntity =
221 resourceJdbcDao.getResource(assetId, resourceName);
222 if (resourceEntity != null) {
223 resourceJdbcDao.delete(resourceEntity.id);
228 public List<org.onap.ccsdk.sli.adaptors.rm.data.Resource> getResourceSet(String resourceSetId) {
229 List<Resource> rEntityList = resourceJdbcDao.getResourceSet(resourceSetId);
230 List<org.onap.ccsdk.sli.adaptors.rm.data.Resource> rlist = new ArrayList<>();
231 for (Resource rEntity : rEntityList) {
232 org.onap.ccsdk.sli.adaptors.rm.data.Resource r = createResource(rEntity);
235 List<AllocationItem> aiEntityList = allocationItemJdbcDao.getAllocationItems(rEntity.id);
236 r.allocationItems = new ArrayList<>();
237 for (AllocationItem aiEntity : aiEntityList) {
238 org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem ai = createAllocationItem(r, aiEntity);
239 r.allocationItems.add(ai);
242 List<ResourceLoad> rlEntityList = resourceLoadJdbcDao.getResourceLoads(rEntity.id);
243 r.resourceLoadList = new ArrayList<>();
244 for (ResourceLoad rlEntity : rlEntityList) {
245 org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad rl = createResourceLoad(r, rlEntity);
246 r.resourceLoadList.add(rl);
253 public List<org.onap.ccsdk.sli.adaptors.rm.data.Resource> getResourceUnion(String resourceUnionId) {
254 List<Resource> rEntityList = resourceJdbcDao.getResourceUnion(resourceUnionId);
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);
277 private Resource createResourceEntity(org.onap.ccsdk.sli.adaptors.rm.data.Resource resource) {
278 Resource resourceEntity = new Resource();
279 resourceEntity.assetId = resource.resourceKey.assetId;
280 resourceEntity.name = resource.resourceKey.resourceName;
281 resourceEntity.type = resource.resourceType.toString();
282 if (resource.resourceType == ResourceType.Limit) {
283 resourceEntity.ltUsed = ((LimitResource) resource).used;
284 } else if (resource.resourceType == ResourceType.Label) {
285 resourceEntity.llLabel = ((LabelResource) resource).label;
286 resourceEntity.llReferenceCount = ((LabelResource) resource).referenceCount;
287 } else if (resource.resourceType == ResourceType.Range) {
288 resourceEntity.rrUsed = StrUtil.listInt(((RangeResource) resource).used);
291 return resourceEntity;
294 private ResourceLoad createResourceLoadEntity(long resourceId,
295 org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad rl) {
296 ResourceLoad rlEntity = new ResourceLoad();
297 rlEntity.resourceId = resourceId;
298 rlEntity.applicationId = rl.applicationId;
299 rlEntity.loadTime = rl.resourceLoadTime;
300 rlEntity.expirationTime = rl.resourceExpirationTime;
304 private void updateResourceLoadEntity(ResourceLoad rlEntity, org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad rl) {
305 rlEntity.loadTime = rl.resourceLoadTime;
306 rlEntity.expirationTime = rl.resourceExpirationTime;
309 private AllocationItem createAllocationItemEntity(long resourceId,
310 org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem ai) {
311 AllocationItem aiEntity = new AllocationItem();
312 aiEntity.resourceId = resourceId;
313 aiEntity.resourceSetId = ai.resourceSetId;
314 aiEntity.resourceUnionId = ai.resourceUnionId;
315 aiEntity.resourceShareGroupList = StrUtil.listStr(ai.resourceShareGroupList);
316 aiEntity.applicationId = ai.applicationId;
317 aiEntity.allocationTime = ai.allocationTime;
318 if (ai.resourceType == ResourceType.Limit) {
319 aiEntity.ltUsed = ((LimitAllocationItem) ai).used;
320 } else if (ai.resourceType == ResourceType.Label) {
321 aiEntity.llLabel = ((LabelAllocationItem) ai).label;
322 } else if (ai.resourceType == ResourceType.Range) {
323 aiEntity.rrUsed = StrUtil.listInt(((RangeAllocationItem) ai).used);
328 private void updateAllocationItemEntity(AllocationItem aiEntity,
329 org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem ai) {
330 aiEntity.resourceShareGroupList = StrUtil.listStr(ai.resourceShareGroupList);
331 aiEntity.allocationTime = ai.allocationTime;
332 if (ai.resourceType == ResourceType.Limit) {
333 aiEntity.ltUsed = ((LimitAllocationItem) ai).used;
334 } else if (ai.resourceType == ResourceType.Label) {
335 aiEntity.llLabel = ((LabelAllocationItem) ai).label;
336 } else if (ai.resourceType == ResourceType.Range) {
337 aiEntity.rrUsed = StrUtil.listInt(((RangeAllocationItem) ai).used);
341 private void updateResourceEntity(Resource resourceEntity, org.onap.ccsdk.sli.adaptors.rm.data.Resource resource) {
342 if (resource.resourceType == ResourceType.Limit) {
343 resourceEntity.ltUsed = ((LimitResource) resource).used;
344 } else if (resource.resourceType == ResourceType.Label) {
345 resourceEntity.llLabel = ((LabelResource) resource).label;
346 resourceEntity.llReferenceCount = ((LabelResource) resource).referenceCount;
347 } else if (resource.resourceType == ResourceType.Range) {
348 resourceEntity.rrUsed = StrUtil.listInt(((RangeResource) resource).used);
352 private org.onap.ccsdk.sli.adaptors.rm.data.Resource createResource(Resource resourceEntity) {
353 if (resourceEntity == null) {
357 org.onap.ccsdk.sli.adaptors.rm.data.Resource r = null;
358 ResourceType type = ResourceType.valueOf(resourceEntity.type);
359 if (type == ResourceType.Limit) {
360 LimitResource l = new LimitResource();
361 l.used = resourceEntity.ltUsed;
363 } else if (type == ResourceType.Label) {
364 LabelResource l = new LabelResource();
365 l.label = resourceEntity.llLabel;
366 l.referenceCount = resourceEntity.llReferenceCount;
368 } else if (type == ResourceType.Range) {
369 RangeResource rr = new RangeResource();
370 rr.used = StrUtil.listInt(resourceEntity.rrUsed, "Invalid data found in DB in for Resource Id: "
371 + resourceEntity.id + ": RESOURCE.RR_USED: " + resourceEntity.rrUsed);
376 r.resourceType = type;
377 r.resourceKey = new ResourceKey();
378 r.resourceKey.assetId = resourceEntity.assetId;
379 r.resourceKey.resourceName = resourceEntity.name;
385 private org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem createAllocationItem(
386 org.onap.ccsdk.sli.adaptors.rm.data.Resource r, AllocationItem aiEntity) {
387 if (r == null || aiEntity == null) {
391 org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem ai = null;
392 if (r.resourceType == ResourceType.Limit) {
393 LimitAllocationItem lai = new LimitAllocationItem();
394 lai.used = aiEntity.ltUsed;
396 } else if (r.resourceType == ResourceType.Label) {
397 LabelAllocationItem lai = new LabelAllocationItem();
398 lai.label = aiEntity.llLabel;
400 } else if (r.resourceType == ResourceType.Range) {
401 RangeAllocationItem rai = new RangeAllocationItem();
402 rai.used = StrUtil.listInt(aiEntity.rrUsed, "Invalid data found in DB in for Allocation Item Id: "
403 + aiEntity.id + ": ALLOCATION_ITEM.RR_USED: " + aiEntity.rrUsed);
408 ai.resourceType = r.resourceType;
409 ai.resourceKey = r.resourceKey;
410 ai.resourceSetId = aiEntity.resourceSetId;
411 ai.resourceUnionId = aiEntity.resourceUnionId;
412 if (aiEntity.resourceShareGroupList != null) {
413 ai.resourceShareGroupList = new HashSet<>(StrUtil.listStr(aiEntity.resourceShareGroupList));
415 ai.applicationId = aiEntity.applicationId;
416 ai.allocationTime = aiEntity.allocationTime;
422 private org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad createResourceLoad(
423 org.onap.ccsdk.sli.adaptors.rm.data.Resource r, ResourceLoad rlEntity) {
424 if (rlEntity == null) {
428 org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad rl = new org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad();
429 rl.resourceKey = r.resourceKey;
430 rl.applicationId = rlEntity.applicationId;
431 rl.resourceLoadTime = rlEntity.loadTime;
432 rl.resourceExpirationTime = rlEntity.expirationTime;
437 private static boolean eq(Object o1, Object o2) {
438 return o1 == null ? o2 == null : o1.equals(o2);
441 public void setResourceJdbcDao(ResourceJdbcDao resourceJdbcDao) {
442 this.resourceJdbcDao = resourceJdbcDao;
445 public void setResourceLoadJdbcDao(ResourceLoadJdbcDao resourceLoadJdbcDao) {
446 this.resourceLoadJdbcDao = resourceLoadJdbcDao;
449 public void setAllocationItemJdbcDao(AllocationItemJdbcDao allocationItemJdbcDao) {
450 this.allocationItemJdbcDao = allocationItemJdbcDao;