RA: Fix unnecessary updates to allocation items 89/56789/1
authorsb5356 <sb5356@att.com>
Wed, 18 Jul 2018 20:56:14 +0000 (16:56 -0400)
committersb5356 <sb5356@att.com>
Wed, 18 Jul 2018 20:56:14 +0000 (16:56 -0400)
Change-Id: Ia3569ea5c7afe00624bb5d2368253cc3e5d834f5
Issue-ID: CCSDK-387
Signed-off-by: Stan Bonev <sb5356@att.com>
resource-assignment/provider/src/main/java/org/onap/ccsdk/sli/adaptors/ra/rule/dao/RangeRuleDaoImpl.java
resource-assignment/provider/src/main/java/org/onap/ccsdk/sli/adaptors/rm/dao/jdbc/ResourceDaoImpl.java

index caac1c9..4599314 100644 (file)
@@ -32,7 +32,6 @@ import org.springframework.jdbc.core.RowMapper;
 
 public class RangeRuleDaoImpl implements RangeRuleDao {
 
-    @SuppressWarnings("unused")
     private static final Logger log = LoggerFactory.getLogger(RangeRuleDaoImpl.class);
 
     private static final String GET_SQL = "SELECT * FROM RANGE_RULE WHERE service_model = ? AND equipment_level = ?";
index 58f1036..e202de7 100644 (file)
@@ -8,9 +8,9 @@
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
- * 
+ *
  *      http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -24,7 +24,6 @@ package org.onap.ccsdk.sli.adaptors.rm.dao.jdbc;
 import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.List;
-
 import org.onap.ccsdk.sli.adaptors.rm.dao.ResourceDao;
 import org.onap.ccsdk.sli.adaptors.rm.data.LabelAllocationItem;
 import org.onap.ccsdk.sli.adaptors.rm.data.LabelResource;
@@ -49,14 +48,14 @@ public class ResourceDaoImpl implements ResourceDao {
 
         if (r != null) {
             List<AllocationItem> aiEntityList = allocationItemJdbcDao.getAllocationItems(rEntity.id);
-            r.allocationItems = new ArrayList<org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem>();
+            r.allocationItems = new ArrayList<>();
             for (AllocationItem aiEntity : aiEntityList) {
                 org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem ai = createAllocationItem(r, aiEntity);
                 r.allocationItems.add(ai);
             }
 
             List<ResourceLoad> rlEntityList = resourceLoadJdbcDao.getResourceLoads(rEntity.id);
-            r.resourceLoadList = new ArrayList<org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad>();
+            r.resourceLoadList = new ArrayList<>();
             for (ResourceLoad rlEntity : rlEntityList) {
                 org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad rl = createResourceLoad(r, rlEntity);
                 r.resourceLoadList.add(rl);
@@ -68,66 +67,77 @@ public class ResourceDaoImpl implements ResourceDao {
 
     @Override
     public void saveResource(org.onap.ccsdk.sli.adaptors.rm.data.Resource resource) {
-        if (resource == null)
+        if (resource == null) {
             return;
+        }
 
         org.onap.ccsdk.sli.adaptors.rm.dao.jdbc.Resource resourceEntity =
                 resourceJdbcDao.getResource(resource.resourceKey.assetId, resource.resourceKey.resourceName);
         if (resourceEntity == null) {
             resourceEntity = createResourceEntity(resource);
             resourceJdbcDao.add(resourceEntity);
-            if (resource.allocationItems != null)
+            if (resource.allocationItems != null) {
                 for (org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem ai : resource.allocationItems) {
                     AllocationItem aiEntity = createAllocationItemEntity(resourceEntity.id, ai);
                     allocationItemJdbcDao.add(aiEntity);
                 }
-            if (resource.resourceLoadList != null)
+            }
+            if (resource.resourceLoadList != null) {
                 for (org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad rl : resource.resourceLoadList) {
                     ResourceLoad rlEntity = createResourceLoadEntity(resourceEntity.id, rl);
                     resourceLoadJdbcDao.add(rlEntity);
                 }
+            }
         } else {
             updateResourceEntity(resourceEntity, resource);
             resourceJdbcDao.update(resourceEntity);
 
             List<AllocationItem> oldAiEntityList = allocationItemJdbcDao.getAllocationItems(resourceEntity.id);
-            if (resource.allocationItems != null)
+            if (resource.allocationItems != null) {
                 for (org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem newai : resource.allocationItems) {
                     AllocationItem foundAiEntity = null;
-                    for (AllocationItem oldAiEntity : oldAiEntityList)
+                    for (AllocationItem oldAiEntity : oldAiEntityList) {
                         if (oldAiEntity.resourceSetId.equals(newai.resourceSetId)) {
                             foundAiEntity = oldAiEntity;
                             break;
                         }
+                    }
                     if (foundAiEntity != null) {
-                        updateAllocationItemEntity(foundAiEntity, newai);
-                        allocationItemJdbcDao.update(foundAiEntity);
+                        if (allocationItemChanged(foundAiEntity, newai)) {
+                            updateAllocationItemEntity(foundAiEntity, newai);
+                            allocationItemJdbcDao.update(foundAiEntity);
+                        }
                     } else {
                         AllocationItem newAiEntity = createAllocationItemEntity(resourceEntity.id, newai);
                         allocationItemJdbcDao.add(newAiEntity);
                     }
                 }
+            }
             for (AllocationItem oldAiEntity : oldAiEntityList) {
                 boolean found = false;
-                if (resource.allocationItems != null)
-                    for (org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem newai : resource.allocationItems)
+                if (resource.allocationItems != null) {
+                    for (org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem newai : resource.allocationItems) {
                         if (oldAiEntity.resourceSetId.equals(newai.resourceSetId)) {
                             found = true;
                             break;
                         }
-                if (!found)
+                    }
+                }
+                if (!found) {
                     allocationItemJdbcDao.delete(oldAiEntity.id);
+                }
             }
 
             List<ResourceLoad> oldRlEntityList = resourceLoadJdbcDao.getResourceLoads(resourceEntity.id);
-            if (resource.resourceLoadList != null)
+            if (resource.resourceLoadList != null) {
                 for (org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad newrl : resource.resourceLoadList) {
                     ResourceLoad foundRlEntity = null;
-                    for (ResourceLoad oldRlEntity : oldRlEntityList)
+                    for (ResourceLoad oldRlEntity : oldRlEntityList) {
                         if (oldRlEntity.applicationId.equals(newrl.applicationId)) {
                             foundRlEntity = oldRlEntity;
                             break;
                         }
+                    }
                     if (foundRlEntity != null) {
                         updateResourceLoadEntity(foundRlEntity, newrl);
                         resourceLoadJdbcDao.update(foundRlEntity);
@@ -136,44 +146,76 @@ public class ResourceDaoImpl implements ResourceDao {
                         resourceLoadJdbcDao.add(newRlEntity);
                     }
                 }
+            }
             for (ResourceLoad oldRlEntity : oldRlEntityList) {
                 boolean found = false;
-                if (resource.resourceLoadList != null)
-                    for (org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad newrl : resource.resourceLoadList)
+                if (resource.resourceLoadList != null) {
+                    for (org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad newrl : resource.resourceLoadList) {
                         if (oldRlEntity.applicationId.equals(newrl.applicationId)) {
                             found = true;
                             break;
                         }
-                if (!found)
+                    }
+                }
+                if (!found) {
                     resourceLoadJdbcDao.delete(oldRlEntity.id);
+                }
             }
         }
     }
 
+    private boolean allocationItemChanged(AllocationItem aiEntity,
+            org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem newai) {
+        String newShareGroupList = StrUtil.listStr(newai.resourceShareGroupList);
+        if (!eq(aiEntity.resourceShareGroupList, newShareGroupList)) {
+            return true;
+        }
+
+        if (newai.resourceType == ResourceType.Limit) {
+            if (aiEntity.ltUsed != ((LimitAllocationItem) newai).used) {
+                return true;
+            }
+        } else if (newai.resourceType == ResourceType.Label) {
+            if (!eq(aiEntity.llLabel, ((LabelAllocationItem) newai).label)) {
+                return true;
+            }
+        } else if (newai.resourceType == ResourceType.Range) {
+            String newRrUsed = StrUtil.listInt(((RangeAllocationItem) newai).used);
+            if (!eq(aiEntity.rrUsed, newRrUsed)) {
+                return true;
+            }
+        }
+
+        return false;
+    }
+
     @Override
     public void deleteResource(String assetId, String resourceName) {
-        org.onap.ccsdk.sli.adaptors.rm.dao.jdbc.Resource resourceEntity = resourceJdbcDao.getResource(assetId, resourceName);
-        if (resourceEntity != null)
+        org.onap.ccsdk.sli.adaptors.rm.dao.jdbc.Resource resourceEntity =
+                resourceJdbcDao.getResource(assetId, resourceName);
+        if (resourceEntity != null) {
             resourceJdbcDao.delete(resourceEntity.id);
+        }
     }
 
     @Override
     public List<org.onap.ccsdk.sli.adaptors.rm.data.Resource> getResourceSet(String resourceSetId) {
         List<Resource> rEntityList = resourceJdbcDao.getResourceSet(resourceSetId);
-        List<org.onap.ccsdk.sli.adaptors.rm.data.Resource> rlist = new ArrayList<org.onap.ccsdk.sli.adaptors.rm.data.Resource>();
+        List<org.onap.ccsdk.sli.adaptors.rm.data.Resource> rlist =
+                new ArrayList<>();
         for (Resource rEntity : rEntityList) {
             org.onap.ccsdk.sli.adaptors.rm.data.Resource r = createResource(rEntity);
             rlist.add(r);
 
             List<AllocationItem> aiEntityList = allocationItemJdbcDao.getAllocationItems(rEntity.id);
-            r.allocationItems = new ArrayList<org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem>();
+            r.allocationItems = new ArrayList<>();
             for (AllocationItem aiEntity : aiEntityList) {
                 org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem ai = createAllocationItem(r, aiEntity);
                 r.allocationItems.add(ai);
             }
 
             List<ResourceLoad> rlEntityList = resourceLoadJdbcDao.getResourceLoads(rEntity.id);
-            r.resourceLoadList = new ArrayList<org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad>();
+            r.resourceLoadList = new ArrayList<>();
             for (ResourceLoad rlEntity : rlEntityList) {
                 org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad rl = createResourceLoad(r, rlEntity);
                 r.resourceLoadList.add(rl);
@@ -185,20 +227,21 @@ public class ResourceDaoImpl implements ResourceDao {
     @Override
     public List<org.onap.ccsdk.sli.adaptors.rm.data.Resource> getResourceUnion(String resourceUnionId) {
         List<Resource> rEntityList = resourceJdbcDao.getResourceUnion(resourceUnionId);
-        List<org.onap.ccsdk.sli.adaptors.rm.data.Resource> rlist = new ArrayList<org.onap.ccsdk.sli.adaptors.rm.data.Resource>();
+        List<org.onap.ccsdk.sli.adaptors.rm.data.Resource> rlist =
+                new ArrayList<>();
         for (Resource rEntity : rEntityList) {
             org.onap.ccsdk.sli.adaptors.rm.data.Resource r = createResource(rEntity);
             rlist.add(r);
 
             List<AllocationItem> aiEntityList = allocationItemJdbcDao.getAllocationItems(rEntity.id);
-            r.allocationItems = new ArrayList<org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem>();
+            r.allocationItems = new ArrayList<>();
             for (AllocationItem aiEntity : aiEntityList) {
                 org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem ai = createAllocationItem(r, aiEntity);
                 r.allocationItems.add(ai);
             }
 
             List<ResourceLoad> rlEntityList = resourceLoadJdbcDao.getResourceLoads(rEntity.id);
-            r.resourceLoadList = new ArrayList<org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad>();
+            r.resourceLoadList = new ArrayList<>();
             for (ResourceLoad rlEntity : rlEntityList) {
                 org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad rl = createResourceLoad(r, rlEntity);
                 r.resourceLoadList.add(rl);
@@ -212,18 +255,20 @@ public class ResourceDaoImpl implements ResourceDao {
         resourceEntity.assetId = resource.resourceKey.assetId;
         resourceEntity.name = resource.resourceKey.resourceName;
         resourceEntity.type = resource.resourceType.toString();
-        if (resource.resourceType == ResourceType.Limit)
+        if (resource.resourceType == ResourceType.Limit) {
             resourceEntity.ltUsed = ((LimitResource) resource).used;
-        else if (resource.resourceType == ResourceType.Label) {
+        else if (resource.resourceType == ResourceType.Label) {
             resourceEntity.llLabel = ((LabelResource) resource).label;
             resourceEntity.llReferenceCount = ((LabelResource) resource).referenceCount;
-        } else if (resource.resourceType == ResourceType.Range)
+        } else if (resource.resourceType == ResourceType.Range) {
             resourceEntity.rrUsed = StrUtil.listInt(((RangeResource) resource).used);
+        }
 
         return resourceEntity;
     }
 
-    private ResourceLoad createResourceLoadEntity(long resourceId, org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad rl) {
+    private ResourceLoad createResourceLoadEntity(long resourceId,
+            org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad rl) {
         ResourceLoad rlEntity = new ResourceLoad();
         rlEntity.resourceId = resourceId;
         rlEntity.applicationId = rl.applicationId;
@@ -237,7 +282,8 @@ public class ResourceDaoImpl implements ResourceDao {
         rlEntity.expirationTime = rl.resourceExpirationTime;
     }
 
-    private AllocationItem createAllocationItemEntity(long resourceId, org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem ai) {
+    private AllocationItem createAllocationItemEntity(long resourceId,
+            org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem ai) {
         AllocationItem aiEntity = new AllocationItem();
         aiEntity.resourceId = resourceId;
         aiEntity.resourceSetId = ai.resourceSetId;
@@ -245,39 +291,44 @@ public class ResourceDaoImpl implements ResourceDao {
         aiEntity.resourceShareGroupList = StrUtil.listStr(ai.resourceShareGroupList);
         aiEntity.applicationId = ai.applicationId;
         aiEntity.allocationTime = ai.allocationTime;
-        if (ai.resourceType == ResourceType.Limit)
+        if (ai.resourceType == ResourceType.Limit) {
             aiEntity.ltUsed = ((LimitAllocationItem) ai).used;
-        else if (ai.resourceType == ResourceType.Label)
+        } else if (ai.resourceType == ResourceType.Label) {
             aiEntity.llLabel = ((LabelAllocationItem) ai).label;
-        else if (ai.resourceType == ResourceType.Range)
+        } else if (ai.resourceType == ResourceType.Range) {
             aiEntity.rrUsed = StrUtil.listInt(((RangeAllocationItem) ai).used);
+        }
         return aiEntity;
     }
 
-    private void updateAllocationItemEntity(AllocationItem aiEntity, org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem ai) {
+    private void updateAllocationItemEntity(AllocationItem aiEntity,
+            org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem ai) {
         aiEntity.resourceShareGroupList = StrUtil.listStr(ai.resourceShareGroupList);
         aiEntity.allocationTime = ai.allocationTime;
-        if (ai.resourceType == ResourceType.Limit)
+        if (ai.resourceType == ResourceType.Limit) {
             aiEntity.ltUsed = ((LimitAllocationItem) ai).used;
-        else if (ai.resourceType == ResourceType.Label)
+        } else if (ai.resourceType == ResourceType.Label) {
             aiEntity.llLabel = ((LabelAllocationItem) ai).label;
-        else if (ai.resourceType == ResourceType.Range)
+        } else if (ai.resourceType == ResourceType.Range) {
             aiEntity.rrUsed = StrUtil.listInt(((RangeAllocationItem) ai).used);
+        }
     }
 
     private void updateResourceEntity(Resource resourceEntity, org.onap.ccsdk.sli.adaptors.rm.data.Resource resource) {
-        if (resource.resourceType == ResourceType.Limit)
+        if (resource.resourceType == ResourceType.Limit) {
             resourceEntity.ltUsed = ((LimitResource) resource).used;
-        else if (resource.resourceType == ResourceType.Label) {
+        else if (resource.resourceType == ResourceType.Label) {
             resourceEntity.llLabel = ((LabelResource) resource).label;
             resourceEntity.llReferenceCount = ((LabelResource) resource).referenceCount;
-        } else if (resource.resourceType == ResourceType.Range)
+        } else if (resource.resourceType == ResourceType.Range) {
             resourceEntity.rrUsed = StrUtil.listInt(((RangeResource) resource).used);
+        }
     }
 
     private org.onap.ccsdk.sli.adaptors.rm.data.Resource createResource(Resource resourceEntity) {
-        if (resourceEntity == null)
+        if (resourceEntity == null) {
             return null;
+        }
 
         org.onap.ccsdk.sli.adaptors.rm.data.Resource r = null;
         ResourceType type = ResourceType.valueOf(resourceEntity.type);
@@ -292,9 +343,8 @@ public class ResourceDaoImpl implements ResourceDao {
             r = l;
         } else if (type == ResourceType.Range) {
             RangeResource rr = new RangeResource();
-            rr.used =
-                    StrUtil.listInt(resourceEntity.rrUsed, "Invalid data found in DB in for Resource Id: " +
-                            resourceEntity.id + ": RESOURCE.RR_USED: " + resourceEntity.rrUsed);
+            rr.used = StrUtil.listInt(resourceEntity.rrUsed, "Invalid data found in DB in for Resource Id: "
+                    + resourceEntity.id + ": RESOURCE.RR_USED: " + resourceEntity.rrUsed);
             r = rr;
         }
 
@@ -309,10 +359,10 @@ public class ResourceDaoImpl implements ResourceDao {
     }
 
     private org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem createAllocationItem(
-            org.onap.ccsdk.sli.adaptors.rm.data.Resource r,
-            AllocationItem aiEntity) {
-        if (r == null || aiEntity == null)
+            org.onap.ccsdk.sli.adaptors.rm.data.Resource r, AllocationItem aiEntity) {
+        if (r == null || aiEntity == null) {
             return null;
+        }
 
         org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem ai = null;
         if (r.resourceType == ResourceType.Limit) {
@@ -325,19 +375,19 @@ public class ResourceDaoImpl implements ResourceDao {
             ai = lai;
         } else if (r.resourceType == ResourceType.Range) {
             RangeAllocationItem rai = new RangeAllocationItem();
-            rai.used =
-                    StrUtil.listInt(aiEntity.rrUsed, "Invalid data found in DB in for Allocation Item Id: " +
-                            aiEntity.id + ": ALLOCATION_ITEM.RR_USED: " + aiEntity.rrUsed);
+            rai.used = StrUtil.listInt(aiEntity.rrUsed, "Invalid data found in DB in for Allocation Item Id: "
+                    + aiEntity.id + ": ALLOCATION_ITEM.RR_USED: " + aiEntity.rrUsed);
             ai = rai;
         }
 
-        if (ai!=null) {
+        if (ai != null) {
             ai.resourceType = r.resourceType;
             ai.resourceKey = r.resourceKey;
             ai.resourceSetId = aiEntity.resourceSetId;
             ai.resourceUnionId = aiEntity.resourceUnionId;
-            if (aiEntity.resourceShareGroupList != null)
-                ai.resourceShareGroupList = new HashSet<String>(StrUtil.listStr(aiEntity.resourceShareGroupList));
+            if (aiEntity.resourceShareGroupList != null) {
+                ai.resourceShareGroupList = new HashSet<>(StrUtil.listStr(aiEntity.resourceShareGroupList));
+            }
             ai.applicationId = aiEntity.applicationId;
             ai.allocationTime = aiEntity.allocationTime;
         }
@@ -346,10 +396,10 @@ public class ResourceDaoImpl implements ResourceDao {
     }
 
     private org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad createResourceLoad(
-            org.onap.ccsdk.sli.adaptors.rm.data.Resource r,
-            ResourceLoad rlEntity) {
-        if (rlEntity == null)
+            org.onap.ccsdk.sli.adaptors.rm.data.Resource r, ResourceLoad rlEntity) {
+        if (rlEntity == null) {
             return null;
+        }
 
         org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad rl = new org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad();
         rl.resourceKey = r.resourceKey;
@@ -360,6 +410,10 @@ public class ResourceDaoImpl implements ResourceDao {
         return rl;
     }
 
+    private static boolean eq(Object o1, Object o2) {
+        return o1 == null ? o2 == null : o1.equals(o2);
+    }
+
     public void setResourceJdbcDao(ResourceJdbcDao resourceJdbcDao) {
         this.resourceJdbcDao = resourceJdbcDao;
     }