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;
 
  28 import org.onap.ccsdk.sli.adaptors.rm.dao.ResourceDao;
 
  29 import org.onap.ccsdk.sli.adaptors.rm.data.LabelAllocationItem;
 
  30 import org.onap.ccsdk.sli.adaptors.rm.data.LabelResource;
 
  31 import org.onap.ccsdk.sli.adaptors.rm.data.LimitAllocationItem;
 
  32 import org.onap.ccsdk.sli.adaptors.rm.data.LimitResource;
 
  33 import org.onap.ccsdk.sli.adaptors.rm.data.RangeAllocationItem;
 
  34 import org.onap.ccsdk.sli.adaptors.rm.data.RangeResource;
 
  35 import org.onap.ccsdk.sli.adaptors.rm.data.ResourceKey;
 
  36 import org.onap.ccsdk.sli.adaptors.rm.data.ResourceType;
 
  37 import org.onap.ccsdk.sli.adaptors.util.str.StrUtil;
 
  39 public class ResourceDaoImpl implements ResourceDao {
 
  41     private ResourceJdbcDao resourceJdbcDao;
 
  42     private ResourceLoadJdbcDao resourceLoadJdbcDao;
 
  43     private AllocationItemJdbcDao allocationItemJdbcDao;
 
  46     public org.onap.ccsdk.sli.adaptors.rm.data.Resource getResource(String assetId, String resourceName) {
 
  47         Resource rEntity = resourceJdbcDao.getResource(assetId, resourceName);
 
  48         org.onap.ccsdk.sli.adaptors.rm.data.Resource r = createResource(rEntity);
 
  51             List<AllocationItem> aiEntityList = allocationItemJdbcDao.getAllocationItems(rEntity.id);
 
  52             r.allocationItems = new ArrayList<org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem>();
 
  53             for (AllocationItem aiEntity : aiEntityList) {
 
  54                 org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem ai = createAllocationItem(r, aiEntity);
 
  55                 r.allocationItems.add(ai);
 
  58             List<ResourceLoad> rlEntityList = resourceLoadJdbcDao.getResourceLoads(rEntity.id);
 
  59             r.resourceLoadList = new ArrayList<org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad>();
 
  60             for (ResourceLoad rlEntity : rlEntityList) {
 
  61                 org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad rl = createResourceLoad(r, rlEntity);
 
  62                 r.resourceLoadList.add(rl);
 
  70     public void saveResource(org.onap.ccsdk.sli.adaptors.rm.data.Resource resource) {
 
  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);
 
  84             if (resource.resourceLoadList != null)
 
  85                 for (org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad rl : resource.resourceLoadList) {
 
  86                     ResourceLoad rlEntity = createResourceLoadEntity(resourceEntity.id, rl);
 
  87                     resourceLoadJdbcDao.add(rlEntity);
 
  90             updateResourceEntity(resourceEntity, resource);
 
  91             resourceJdbcDao.update(resourceEntity);
 
  93             List<AllocationItem> oldAiEntityList = allocationItemJdbcDao.getAllocationItems(resourceEntity.id);
 
  94             if (resource.allocationItems != null)
 
  95                 for (org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem newai : resource.allocationItems) {
 
  96                     AllocationItem foundAiEntity = null;
 
  97                     for (AllocationItem oldAiEntity : oldAiEntityList)
 
  98                         if (oldAiEntity.resourceSetId.equals(newai.resourceSetId)) {
 
  99                             foundAiEntity = oldAiEntity;
 
 102                     if (foundAiEntity != null) {
 
 103                         updateAllocationItemEntity(foundAiEntity, newai);
 
 104                         allocationItemJdbcDao.update(foundAiEntity);
 
 106                         AllocationItem newAiEntity = createAllocationItemEntity(resourceEntity.id, newai);
 
 107                         allocationItemJdbcDao.add(newAiEntity);
 
 110             for (AllocationItem oldAiEntity : oldAiEntityList) {
 
 111                 boolean found = false;
 
 112                 if (resource.allocationItems != null)
 
 113                     for (org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem newai : resource.allocationItems)
 
 114                         if (oldAiEntity.resourceSetId.equals(newai.resourceSetId)) {
 
 119                     allocationItemJdbcDao.delete(oldAiEntity.id);
 
 122             List<ResourceLoad> oldRlEntityList = resourceLoadJdbcDao.getResourceLoads(resourceEntity.id);
 
 123             if (resource.resourceLoadList != null)
 
 124                 for (org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad newrl : resource.resourceLoadList) {
 
 125                     ResourceLoad foundRlEntity = null;
 
 126                     for (ResourceLoad oldRlEntity : oldRlEntityList)
 
 127                         if (oldRlEntity.applicationId.equals(newrl.applicationId)) {
 
 128                             foundRlEntity = oldRlEntity;
 
 131                     if (foundRlEntity != null) {
 
 132                         updateResourceLoadEntity(foundRlEntity, newrl);
 
 133                         resourceLoadJdbcDao.update(foundRlEntity);
 
 135                         ResourceLoad newRlEntity = createResourceLoadEntity(resourceEntity.id, newrl);
 
 136                         resourceLoadJdbcDao.add(newRlEntity);
 
 139             for (ResourceLoad oldRlEntity : oldRlEntityList) {
 
 140                 boolean found = false;
 
 141                 if (resource.resourceLoadList != null)
 
 142                     for (org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad newrl : resource.resourceLoadList)
 
 143                         if (oldRlEntity.applicationId.equals(newrl.applicationId)) {
 
 148                     resourceLoadJdbcDao.delete(oldRlEntity.id);
 
 154     public void deleteResource(String assetId, String resourceName) {
 
 155         org.onap.ccsdk.sli.adaptors.rm.dao.jdbc.Resource resourceEntity = resourceJdbcDao.getResource(assetId, resourceName);
 
 156         if (resourceEntity != null)
 
 157             resourceJdbcDao.delete(resourceEntity.id);
 
 161     public List<org.onap.ccsdk.sli.adaptors.rm.data.Resource> getResourceSet(String resourceSetId) {
 
 162         List<Resource> rEntityList = resourceJdbcDao.getResourceSet(resourceSetId);
 
 163         List<org.onap.ccsdk.sli.adaptors.rm.data.Resource> rlist = new ArrayList<org.onap.ccsdk.sli.adaptors.rm.data.Resource>();
 
 164         for (Resource rEntity : rEntityList) {
 
 165             org.onap.ccsdk.sli.adaptors.rm.data.Resource r = createResource(rEntity);
 
 168             List<AllocationItem> aiEntityList = allocationItemJdbcDao.getAllocationItems(rEntity.id);
 
 169             r.allocationItems = new ArrayList<org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem>();
 
 170             for (AllocationItem aiEntity : aiEntityList) {
 
 171                 org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem ai = createAllocationItem(r, aiEntity);
 
 172                 r.allocationItems.add(ai);
 
 175             List<ResourceLoad> rlEntityList = resourceLoadJdbcDao.getResourceLoads(rEntity.id);
 
 176             r.resourceLoadList = new ArrayList<org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad>();
 
 177             for (ResourceLoad rlEntity : rlEntityList) {
 
 178                 org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad rl = createResourceLoad(r, rlEntity);
 
 179                 r.resourceLoadList.add(rl);
 
 186     public List<org.onap.ccsdk.sli.adaptors.rm.data.Resource> getResourceUnion(String resourceUnionId) {
 
 187         List<Resource> rEntityList = resourceJdbcDao.getResourceUnion(resourceUnionId);
 
 188         List<org.onap.ccsdk.sli.adaptors.rm.data.Resource> rlist = new ArrayList<org.onap.ccsdk.sli.adaptors.rm.data.Resource>();
 
 189         for (Resource rEntity : rEntityList) {
 
 190             org.onap.ccsdk.sli.adaptors.rm.data.Resource r = createResource(rEntity);
 
 193             List<AllocationItem> aiEntityList = allocationItemJdbcDao.getAllocationItems(rEntity.id);
 
 194             r.allocationItems = new ArrayList<org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem>();
 
 195             for (AllocationItem aiEntity : aiEntityList) {
 
 196                 org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem ai = createAllocationItem(r, aiEntity);
 
 197                 r.allocationItems.add(ai);
 
 200             List<ResourceLoad> rlEntityList = resourceLoadJdbcDao.getResourceLoads(rEntity.id);
 
 201             r.resourceLoadList = new ArrayList<org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad>();
 
 202             for (ResourceLoad rlEntity : rlEntityList) {
 
 203                 org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad rl = createResourceLoad(r, rlEntity);
 
 204                 r.resourceLoadList.add(rl);
 
 210     private Resource createResourceEntity(org.onap.ccsdk.sli.adaptors.rm.data.Resource resource) {
 
 211         Resource resourceEntity = new Resource();
 
 212         resourceEntity.assetId = resource.resourceKey.assetId;
 
 213         resourceEntity.name = resource.resourceKey.resourceName;
 
 214         resourceEntity.type = resource.resourceType.toString();
 
 215         if (resource.resourceType == ResourceType.Limit)
 
 216             resourceEntity.ltUsed = ((LimitResource) resource).used;
 
 217         else if (resource.resourceType == ResourceType.Label) {
 
 218             resourceEntity.llLabel = ((LabelResource) resource).label;
 
 219             resourceEntity.llReferenceCount = ((LabelResource) resource).referenceCount;
 
 220         } else if (resource.resourceType == ResourceType.Range)
 
 221             resourceEntity.rrUsed = StrUtil.listInt(((RangeResource) resource).used);
 
 223         return resourceEntity;
 
 226     private ResourceLoad createResourceLoadEntity(long resourceId, org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad rl) {
 
 227         ResourceLoad rlEntity = new ResourceLoad();
 
 228         rlEntity.resourceId = resourceId;
 
 229         rlEntity.applicationId = rl.applicationId;
 
 230         rlEntity.loadTime = rl.resourceLoadTime;
 
 231         rlEntity.expirationTime = rl.resourceExpirationTime;
 
 235     private void updateResourceLoadEntity(ResourceLoad rlEntity, org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad rl) {
 
 236         rlEntity.loadTime = rl.resourceLoadTime;
 
 237         rlEntity.expirationTime = rl.resourceExpirationTime;
 
 240     private AllocationItem createAllocationItemEntity(long resourceId, org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem ai) {
 
 241         AllocationItem aiEntity = new AllocationItem();
 
 242         aiEntity.resourceId = resourceId;
 
 243         aiEntity.resourceSetId = ai.resourceSetId;
 
 244         aiEntity.resourceUnionId = ai.resourceUnionId;
 
 245         aiEntity.resourceShareGroupList = StrUtil.listStr(ai.resourceShareGroupList);
 
 246         aiEntity.applicationId = ai.applicationId;
 
 247         aiEntity.allocationTime = ai.allocationTime;
 
 248         if (ai.resourceType == ResourceType.Limit)
 
 249             aiEntity.ltUsed = ((LimitAllocationItem) ai).used;
 
 250         else if (ai.resourceType == ResourceType.Label)
 
 251             aiEntity.llLabel = ((LabelAllocationItem) ai).label;
 
 252         else if (ai.resourceType == ResourceType.Range)
 
 253             aiEntity.rrUsed = StrUtil.listInt(((RangeAllocationItem) ai).used);
 
 257     private void updateAllocationItemEntity(AllocationItem aiEntity, org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem ai) {
 
 258         aiEntity.resourceShareGroupList = StrUtil.listStr(ai.resourceShareGroupList);
 
 259         aiEntity.allocationTime = ai.allocationTime;
 
 260         if (ai.resourceType == ResourceType.Limit)
 
 261             aiEntity.ltUsed = ((LimitAllocationItem) ai).used;
 
 262         else if (ai.resourceType == ResourceType.Label)
 
 263             aiEntity.llLabel = ((LabelAllocationItem) ai).label;
 
 264         else if (ai.resourceType == ResourceType.Range)
 
 265             aiEntity.rrUsed = StrUtil.listInt(((RangeAllocationItem) ai).used);
 
 268     private void updateResourceEntity(Resource resourceEntity, org.onap.ccsdk.sli.adaptors.rm.data.Resource resource) {
 
 269         if (resource.resourceType == ResourceType.Limit)
 
 270             resourceEntity.ltUsed = ((LimitResource) resource).used;
 
 271         else if (resource.resourceType == ResourceType.Label) {
 
 272             resourceEntity.llLabel = ((LabelResource) resource).label;
 
 273             resourceEntity.llReferenceCount = ((LabelResource) resource).referenceCount;
 
 274         } else if (resource.resourceType == ResourceType.Range)
 
 275             resourceEntity.rrUsed = StrUtil.listInt(((RangeResource) resource).used);
 
 278     private org.onap.ccsdk.sli.adaptors.rm.data.Resource createResource(Resource resourceEntity) {
 
 279         if (resourceEntity == null)
 
 282         org.onap.ccsdk.sli.adaptors.rm.data.Resource r = null;
 
 283         ResourceType type = ResourceType.valueOf(resourceEntity.type);
 
 284         if (type == ResourceType.Limit) {
 
 285             LimitResource l = new LimitResource();
 
 286             l.used = resourceEntity.ltUsed;
 
 288         } else if (type == ResourceType.Label) {
 
 289             LabelResource l = new LabelResource();
 
 290             l.label = resourceEntity.llLabel;
 
 291             l.referenceCount = resourceEntity.llReferenceCount;
 
 293         } else if (type == ResourceType.Range) {
 
 294             RangeResource rr = new RangeResource();
 
 296                     StrUtil.listInt(resourceEntity.rrUsed, "Invalid data found in DB in for Resource Id: " +
 
 297                             resourceEntity.id + ": RESOURCE.RR_USED: " + resourceEntity.rrUsed);
 
 302             r.resourceType = type;
 
 303             r.resourceKey = new ResourceKey();
 
 304             r.resourceKey.assetId = resourceEntity.assetId;
 
 305             r.resourceKey.resourceName = resourceEntity.name;
 
 311     private org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem createAllocationItem(
 
 312             org.onap.ccsdk.sli.adaptors.rm.data.Resource r,
 
 313             AllocationItem aiEntity) {
 
 314         if (r == null || aiEntity == null)
 
 317         org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem ai = null;
 
 318         if (r.resourceType == ResourceType.Limit) {
 
 319             LimitAllocationItem lai = new LimitAllocationItem();
 
 320             lai.used = aiEntity.ltUsed;
 
 322         } else if (r.resourceType == ResourceType.Label) {
 
 323             LabelAllocationItem lai = new LabelAllocationItem();
 
 324             lai.label = aiEntity.llLabel;
 
 326         } else if (r.resourceType == ResourceType.Range) {
 
 327             RangeAllocationItem rai = new RangeAllocationItem();
 
 329                     StrUtil.listInt(aiEntity.rrUsed, "Invalid data found in DB in for Allocation Item Id: " +
 
 330                             aiEntity.id + ": ALLOCATION_ITEM.RR_USED: " + aiEntity.rrUsed);
 
 335             ai.resourceType = r.resourceType;
 
 336             ai.resourceKey = r.resourceKey;
 
 337             ai.resourceSetId = aiEntity.resourceSetId;
 
 338             ai.resourceUnionId = aiEntity.resourceUnionId;
 
 339             if (aiEntity.resourceShareGroupList != null)
 
 340                 ai.resourceShareGroupList = new HashSet<String>(StrUtil.listStr(aiEntity.resourceShareGroupList));
 
 341             ai.applicationId = aiEntity.applicationId;
 
 342             ai.allocationTime = aiEntity.allocationTime;
 
 348     private org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad createResourceLoad(
 
 349             org.onap.ccsdk.sli.adaptors.rm.data.Resource r,
 
 350             ResourceLoad rlEntity) {
 
 351         if (rlEntity == null)
 
 354         org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad rl = new org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad();
 
 355         rl.resourceKey = r.resourceKey;
 
 356         rl.applicationId = rlEntity.applicationId;
 
 357         rl.resourceLoadTime = rlEntity.loadTime;
 
 358         rl.resourceExpirationTime = rlEntity.expirationTime;
 
 363     public void setResourceJdbcDao(ResourceJdbcDao resourceJdbcDao) {
 
 364         this.resourceJdbcDao = resourceJdbcDao;
 
 367     public void setResourceLoadJdbcDao(ResourceLoadJdbcDao resourceLoadJdbcDao) {
 
 368         this.resourceLoadJdbcDao = resourceLoadJdbcDao;
 
 371     public void setAllocationItemJdbcDao(AllocationItemJdbcDao allocationItemJdbcDao) {
 
 372         this.allocationItemJdbcDao = allocationItemJdbcDao;