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 void saveResource(org.onap.ccsdk.sli.adaptors.rm.data.Resource resource) {
 
  70         if (resource == null) {
 
  74         org.onap.ccsdk.sli.adaptors.rm.dao.jdbc.Resource resourceEntity =
 
  75                 resourceJdbcDao.getResource(resource.resourceKey.assetId, resource.resourceKey.resourceName);
 
  76         if (resourceEntity == null) {
 
  77             resourceEntity = createResourceEntity(resource);
 
  78             resourceJdbcDao.add(resourceEntity);
 
  79             if (resource.allocationItems != null) {
 
  80                 for (org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem ai : resource.allocationItems) {
 
  81                     AllocationItem aiEntity = createAllocationItemEntity(resourceEntity.id, ai);
 
  82                     allocationItemJdbcDao.add(aiEntity);
 
  85             if (resource.resourceLoadList != null) {
 
  86                 for (org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad rl : resource.resourceLoadList) {
 
  87                     ResourceLoad rlEntity = createResourceLoadEntity(resourceEntity.id, rl);
 
  88                     resourceLoadJdbcDao.add(rlEntity);
 
  92             updateResourceEntity(resourceEntity, resource);
 
  93             resourceJdbcDao.update(resourceEntity);
 
  95             List<AllocationItem> oldAiEntityList = allocationItemJdbcDao.getAllocationItems(resourceEntity.id);
 
  96             if (resource.allocationItems != null) {
 
  97                 for (org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem newai : resource.allocationItems) {
 
  98                     AllocationItem foundAiEntity = null;
 
  99                     for (AllocationItem oldAiEntity : oldAiEntityList) {
 
 100                         if (oldAiEntity.resourceSetId.equals(newai.resourceSetId)) {
 
 101                             foundAiEntity = oldAiEntity;
 
 105                     if (foundAiEntity != null) {
 
 106                         if (allocationItemChanged(foundAiEntity, newai)) {
 
 107                             updateAllocationItemEntity(foundAiEntity, newai);
 
 108                             allocationItemJdbcDao.update(foundAiEntity);
 
 111                         AllocationItem newAiEntity = createAllocationItemEntity(resourceEntity.id, newai);
 
 112                         allocationItemJdbcDao.add(newAiEntity);
 
 116             for (AllocationItem oldAiEntity : oldAiEntityList) {
 
 117                 boolean found = false;
 
 118                 if (resource.allocationItems != null) {
 
 119                     for (org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem newai : resource.allocationItems) {
 
 120                         if (oldAiEntity.resourceSetId.equals(newai.resourceSetId)) {
 
 127                     allocationItemJdbcDao.delete(oldAiEntity.id);
 
 131             List<ResourceLoad> oldRlEntityList = resourceLoadJdbcDao.getResourceLoads(resourceEntity.id);
 
 132             if (resource.resourceLoadList != null) {
 
 133                 for (org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad newrl : resource.resourceLoadList) {
 
 134                     ResourceLoad foundRlEntity = null;
 
 135                     for (ResourceLoad oldRlEntity : oldRlEntityList) {
 
 136                         if (oldRlEntity.applicationId.equals(newrl.applicationId)) {
 
 137                             foundRlEntity = oldRlEntity;
 
 141                     if (foundRlEntity != null) {
 
 142                         updateResourceLoadEntity(foundRlEntity, newrl);
 
 143                         resourceLoadJdbcDao.update(foundRlEntity);
 
 145                         ResourceLoad newRlEntity = createResourceLoadEntity(resourceEntity.id, newrl);
 
 146                         resourceLoadJdbcDao.add(newRlEntity);
 
 150             for (ResourceLoad oldRlEntity : oldRlEntityList) {
 
 151                 boolean found = false;
 
 152                 if (resource.resourceLoadList != null) {
 
 153                     for (org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad newrl : resource.resourceLoadList) {
 
 154                         if (oldRlEntity.applicationId.equals(newrl.applicationId)) {
 
 161                     resourceLoadJdbcDao.delete(oldRlEntity.id);
 
 167     private boolean allocationItemChanged(AllocationItem aiEntity,
 
 168             org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem newai) {
 
 169         String newShareGroupList = StrUtil.listStr(newai.resourceShareGroupList);
 
 170         if (!eq(aiEntity.resourceShareGroupList, newShareGroupList)) {
 
 174         if (newai.resourceType == ResourceType.Limit) {
 
 175             if (aiEntity.ltUsed != ((LimitAllocationItem) newai).used) {
 
 178         } else if (newai.resourceType == ResourceType.Label) {
 
 179             if (!eq(aiEntity.llLabel, ((LabelAllocationItem) newai).label)) {
 
 182         } else if (newai.resourceType == ResourceType.Range) {
 
 183             String newRrUsed = StrUtil.listInt(((RangeAllocationItem) newai).used);
 
 184             if (!eq(aiEntity.rrUsed, newRrUsed)) {
 
 193     public void deleteResource(String assetId, String resourceName) {
 
 194         org.onap.ccsdk.sli.adaptors.rm.dao.jdbc.Resource resourceEntity =
 
 195                 resourceJdbcDao.getResource(assetId, resourceName);
 
 196         if (resourceEntity != null) {
 
 197             resourceJdbcDao.delete(resourceEntity.id);
 
 202     public List<org.onap.ccsdk.sli.adaptors.rm.data.Resource> getResourceSet(String resourceSetId) {
 
 203         List<Resource> rEntityList = resourceJdbcDao.getResourceSet(resourceSetId);
 
 204         List<org.onap.ccsdk.sli.adaptors.rm.data.Resource> rlist =
 
 206         for (Resource rEntity : rEntityList) {
 
 207             org.onap.ccsdk.sli.adaptors.rm.data.Resource r = createResource(rEntity);
 
 210             List<AllocationItem> aiEntityList = allocationItemJdbcDao.getAllocationItems(rEntity.id);
 
 211             r.allocationItems = new ArrayList<>();
 
 212             for (AllocationItem aiEntity : aiEntityList) {
 
 213                 org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem ai = createAllocationItem(r, aiEntity);
 
 214                 r.allocationItems.add(ai);
 
 217             List<ResourceLoad> rlEntityList = resourceLoadJdbcDao.getResourceLoads(rEntity.id);
 
 218             r.resourceLoadList = new ArrayList<>();
 
 219             for (ResourceLoad rlEntity : rlEntityList) {
 
 220                 org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad rl = createResourceLoad(r, rlEntity);
 
 221                 r.resourceLoadList.add(rl);
 
 228     public List<org.onap.ccsdk.sli.adaptors.rm.data.Resource> getResourceUnion(String resourceUnionId) {
 
 229         List<Resource> rEntityList = resourceJdbcDao.getResourceUnion(resourceUnionId);
 
 230         List<org.onap.ccsdk.sli.adaptors.rm.data.Resource> rlist =
 
 232         for (Resource rEntity : rEntityList) {
 
 233             org.onap.ccsdk.sli.adaptors.rm.data.Resource r = createResource(rEntity);
 
 236             List<AllocationItem> aiEntityList = allocationItemJdbcDao.getAllocationItems(rEntity.id);
 
 237             r.allocationItems = new ArrayList<>();
 
 238             for (AllocationItem aiEntity : aiEntityList) {
 
 239                 org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem ai = createAllocationItem(r, aiEntity);
 
 240                 r.allocationItems.add(ai);
 
 243             List<ResourceLoad> rlEntityList = resourceLoadJdbcDao.getResourceLoads(rEntity.id);
 
 244             r.resourceLoadList = new ArrayList<>();
 
 245             for (ResourceLoad rlEntity : rlEntityList) {
 
 246                 org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad rl = createResourceLoad(r, rlEntity);
 
 247                 r.resourceLoadList.add(rl);
 
 253     private Resource createResourceEntity(org.onap.ccsdk.sli.adaptors.rm.data.Resource resource) {
 
 254         Resource resourceEntity = new Resource();
 
 255         resourceEntity.assetId = resource.resourceKey.assetId;
 
 256         resourceEntity.name = resource.resourceKey.resourceName;
 
 257         resourceEntity.type = resource.resourceType.toString();
 
 258         if (resource.resourceType == ResourceType.Limit) {
 
 259             resourceEntity.ltUsed = ((LimitResource) resource).used;
 
 260         } else if (resource.resourceType == ResourceType.Label) {
 
 261             resourceEntity.llLabel = ((LabelResource) resource).label;
 
 262             resourceEntity.llReferenceCount = ((LabelResource) resource).referenceCount;
 
 263         } else if (resource.resourceType == ResourceType.Range) {
 
 264             resourceEntity.rrUsed = StrUtil.listInt(((RangeResource) resource).used);
 
 267         return resourceEntity;
 
 270     private ResourceLoad createResourceLoadEntity(long resourceId,
 
 271             org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad rl) {
 
 272         ResourceLoad rlEntity = new ResourceLoad();
 
 273         rlEntity.resourceId = resourceId;
 
 274         rlEntity.applicationId = rl.applicationId;
 
 275         rlEntity.loadTime = rl.resourceLoadTime;
 
 276         rlEntity.expirationTime = rl.resourceExpirationTime;
 
 280     private void updateResourceLoadEntity(ResourceLoad rlEntity, org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad rl) {
 
 281         rlEntity.loadTime = rl.resourceLoadTime;
 
 282         rlEntity.expirationTime = rl.resourceExpirationTime;
 
 285     private AllocationItem createAllocationItemEntity(long resourceId,
 
 286             org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem ai) {
 
 287         AllocationItem aiEntity = new AllocationItem();
 
 288         aiEntity.resourceId = resourceId;
 
 289         aiEntity.resourceSetId = ai.resourceSetId;
 
 290         aiEntity.resourceUnionId = ai.resourceUnionId;
 
 291         aiEntity.resourceShareGroupList = StrUtil.listStr(ai.resourceShareGroupList);
 
 292         aiEntity.applicationId = ai.applicationId;
 
 293         aiEntity.allocationTime = ai.allocationTime;
 
 294         if (ai.resourceType == ResourceType.Limit) {
 
 295             aiEntity.ltUsed = ((LimitAllocationItem) ai).used;
 
 296         } else if (ai.resourceType == ResourceType.Label) {
 
 297             aiEntity.llLabel = ((LabelAllocationItem) ai).label;
 
 298         } else if (ai.resourceType == ResourceType.Range) {
 
 299             aiEntity.rrUsed = StrUtil.listInt(((RangeAllocationItem) ai).used);
 
 304     private void updateAllocationItemEntity(AllocationItem aiEntity,
 
 305             org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem ai) {
 
 306         aiEntity.resourceShareGroupList = StrUtil.listStr(ai.resourceShareGroupList);
 
 307         aiEntity.allocationTime = ai.allocationTime;
 
 308         if (ai.resourceType == ResourceType.Limit) {
 
 309             aiEntity.ltUsed = ((LimitAllocationItem) ai).used;
 
 310         } else if (ai.resourceType == ResourceType.Label) {
 
 311             aiEntity.llLabel = ((LabelAllocationItem) ai).label;
 
 312         } else if (ai.resourceType == ResourceType.Range) {
 
 313             aiEntity.rrUsed = StrUtil.listInt(((RangeAllocationItem) ai).used);
 
 317     private void updateResourceEntity(Resource resourceEntity, org.onap.ccsdk.sli.adaptors.rm.data.Resource resource) {
 
 318         if (resource.resourceType == ResourceType.Limit) {
 
 319             resourceEntity.ltUsed = ((LimitResource) resource).used;
 
 320         } else if (resource.resourceType == ResourceType.Label) {
 
 321             resourceEntity.llLabel = ((LabelResource) resource).label;
 
 322             resourceEntity.llReferenceCount = ((LabelResource) resource).referenceCount;
 
 323         } else if (resource.resourceType == ResourceType.Range) {
 
 324             resourceEntity.rrUsed = StrUtil.listInt(((RangeResource) resource).used);
 
 328     private org.onap.ccsdk.sli.adaptors.rm.data.Resource createResource(Resource resourceEntity) {
 
 329         if (resourceEntity == null) {
 
 333         org.onap.ccsdk.sli.adaptors.rm.data.Resource r = null;
 
 334         ResourceType type = ResourceType.valueOf(resourceEntity.type);
 
 335         if (type == ResourceType.Limit) {
 
 336             LimitResource l = new LimitResource();
 
 337             l.used = resourceEntity.ltUsed;
 
 339         } else if (type == ResourceType.Label) {
 
 340             LabelResource l = new LabelResource();
 
 341             l.label = resourceEntity.llLabel;
 
 342             l.referenceCount = resourceEntity.llReferenceCount;
 
 344         } else if (type == ResourceType.Range) {
 
 345             RangeResource rr = new RangeResource();
 
 346             rr.used = StrUtil.listInt(resourceEntity.rrUsed, "Invalid data found in DB in for Resource Id: "
 
 347                     + resourceEntity.id + ": RESOURCE.RR_USED: " + resourceEntity.rrUsed);
 
 352             r.resourceType = type;
 
 353             r.resourceKey = new ResourceKey();
 
 354             r.resourceKey.assetId = resourceEntity.assetId;
 
 355             r.resourceKey.resourceName = resourceEntity.name;
 
 361     private org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem createAllocationItem(
 
 362             org.onap.ccsdk.sli.adaptors.rm.data.Resource r, AllocationItem aiEntity) {
 
 363         if (r == null || aiEntity == null) {
 
 367         org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem ai = null;
 
 368         if (r.resourceType == ResourceType.Limit) {
 
 369             LimitAllocationItem lai = new LimitAllocationItem();
 
 370             lai.used = aiEntity.ltUsed;
 
 372         } else if (r.resourceType == ResourceType.Label) {
 
 373             LabelAllocationItem lai = new LabelAllocationItem();
 
 374             lai.label = aiEntity.llLabel;
 
 376         } else if (r.resourceType == ResourceType.Range) {
 
 377             RangeAllocationItem rai = new RangeAllocationItem();
 
 378             rai.used = StrUtil.listInt(aiEntity.rrUsed, "Invalid data found in DB in for Allocation Item Id: "
 
 379                     + aiEntity.id + ": ALLOCATION_ITEM.RR_USED: " + aiEntity.rrUsed);
 
 384             ai.resourceType = r.resourceType;
 
 385             ai.resourceKey = r.resourceKey;
 
 386             ai.resourceSetId = aiEntity.resourceSetId;
 
 387             ai.resourceUnionId = aiEntity.resourceUnionId;
 
 388             if (aiEntity.resourceShareGroupList != null) {
 
 389                 ai.resourceShareGroupList = new HashSet<>(StrUtil.listStr(aiEntity.resourceShareGroupList));
 
 391             ai.applicationId = aiEntity.applicationId;
 
 392             ai.allocationTime = aiEntity.allocationTime;
 
 398     private org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad createResourceLoad(
 
 399             org.onap.ccsdk.sli.adaptors.rm.data.Resource r, ResourceLoad rlEntity) {
 
 400         if (rlEntity == null) {
 
 404         org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad rl = new org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad();
 
 405         rl.resourceKey = r.resourceKey;
 
 406         rl.applicationId = rlEntity.applicationId;
 
 407         rl.resourceLoadTime = rlEntity.loadTime;
 
 408         rl.resourceExpirationTime = rlEntity.expirationTime;
 
 413     private static boolean eq(Object o1, Object o2) {
 
 414         return o1 == null ? o2 == null : o1.equals(o2);
 
 417     public void setResourceJdbcDao(ResourceJdbcDao resourceJdbcDao) {
 
 418         this.resourceJdbcDao = resourceJdbcDao;
 
 421     public void setResourceLoadJdbcDao(ResourceLoadJdbcDao resourceLoadJdbcDao) {
 
 422         this.resourceLoadJdbcDao = resourceLoadJdbcDao;
 
 425     public void setAllocationItemJdbcDao(AllocationItemJdbcDao allocationItemJdbcDao) {
 
 426         this.allocationItemJdbcDao = allocationItemJdbcDao;