Updated aai-path.properties with AAI API'S
[ccsdk/sli/adaptors.git] / resource-assignment / provider / src / main / java / org / onap / ccsdk / sli / adaptors / rm / dao / jdbc / ResourceDaoImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                         reserved.
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.ccsdk.sli.adaptors.rm.dao.jdbc;
23
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;
37
38 public class ResourceDaoImpl implements ResourceDao {
39
40     private ResourceJdbcDao resourceJdbcDao;
41     private ResourceLoadJdbcDao resourceLoadJdbcDao;
42     private AllocationItemJdbcDao allocationItemJdbcDao;
43
44     @Override
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);
48     }
49
50     @Override
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);
55
56         if (r != null) {
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);
63             }
64
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);
70             }
71         }
72
73         return r;
74     }
75
76     @Override
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);
82             rlist.add(r);
83         }
84         return rlist;
85     }
86
87     @Override
88     public void saveResource(org.onap.ccsdk.sli.adaptors.rm.data.Resource resource) {
89         if (resource == null) {
90             return;
91         }
92
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);
102                 }
103             }
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);
108                 }
109             }
110         } else {
111             updateResourceEntity(resourceEntity, resource);
112             resourceJdbcDao.update(resourceEntity);
113
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;
121                             break;
122                         }
123                     }
124                     if (foundAiEntity != null) {
125                         if (allocationItemChanged(foundAiEntity, newai)) {
126                             updateAllocationItemEntity(foundAiEntity, newai);
127                             allocationItemJdbcDao.update(foundAiEntity);
128                         }
129                     } else {
130                         AllocationItem newAiEntity = createAllocationItemEntity(resourceEntity.id, newai);
131                         allocationItemJdbcDao.add(newAiEntity);
132                     }
133                 }
134             }
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)) {
140                             found = true;
141                             break;
142                         }
143                     }
144                 }
145                 if (!found) {
146                     allocationItemJdbcDao.delete(oldAiEntity.id);
147                 }
148             }
149
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;
157                             break;
158                         }
159                     }
160                     if (foundRlEntity != null) {
161                         updateResourceLoadEntity(foundRlEntity, newrl);
162                         resourceLoadJdbcDao.update(foundRlEntity);
163                     } else {
164                         ResourceLoad newRlEntity = createResourceLoadEntity(resourceEntity.id, newrl);
165                         resourceLoadJdbcDao.add(newRlEntity);
166                     }
167                 }
168             }
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)) {
174                             found = true;
175                             break;
176                         }
177                     }
178                 }
179                 if (!found) {
180                     resourceLoadJdbcDao.delete(oldRlEntity.id);
181                 }
182             }
183         }
184     }
185
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)) {
190             return true;
191         }
192
193         if (newai.resourceType == ResourceType.Limit) {
194             if (aiEntity.ltUsed != ((LimitAllocationItem) newai).used) {
195                 return true;
196             }
197         } else if (newai.resourceType == ResourceType.Label) {
198             if (!eq(aiEntity.llLabel, ((LabelAllocationItem) newai).label)) {
199                 return true;
200             }
201         } else if (newai.resourceType == ResourceType.Range) {
202             String newRrUsed = StrUtil.listInt(((RangeAllocationItem) newai).used);
203             if (!eq(aiEntity.rrUsed, newRrUsed)) {
204                 return true;
205             }
206         }
207
208         return false;
209     }
210
211     @Override
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);
217         }
218     }
219
220     @Override
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);
226             rlist.add(r);
227         }
228         return rlist;
229     }
230
231     @Override
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);
237             rlist.add(r);
238         }
239         return rlist;
240     }
241
242     @Override
243     public List<org.onap.ccsdk.sli.adaptors.rm.data.Resource> getResourceSetForAsset(String resourceSetId,
244             String assetId) {
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);
249             rlist.add(r);
250         }
251         return rlist;
252     }
253
254     @Override
255     public List<org.onap.ccsdk.sli.adaptors.rm.data.Resource> getResourceUnionForAsset(String resourceUnionId,
256             String assetId) {
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);
261             rlist.add(r);
262         }
263         return rlist;
264     }
265
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);
278         }
279
280         return resourceEntity;
281     }
282
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;
290         return rlEntity;
291     }
292
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;
296     }
297
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);
313         }
314         return aiEntity;
315     }
316
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);
327         }
328     }
329
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);
338         }
339     }
340
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);
343
344         if (r != null) {
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);
350             }
351
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);
357             }
358         }
359         return r;
360     }
361
362     private org.onap.ccsdk.sli.adaptors.rm.data.Resource createResource(Resource resourceEntity) {
363         if (resourceEntity == null) {
364             return null;
365         }
366
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;
372             r = l;
373         } else if (type == ResourceType.Label) {
374             LabelResource l = new LabelResource();
375             l.label = resourceEntity.llLabel;
376             l.referenceCount = resourceEntity.llReferenceCount;
377             r = l;
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);
382             r = rr;
383         }
384
385         if (r != null) {
386             r.resourceType = type;
387             r.resourceKey = new ResourceKey();
388             r.resourceKey.assetId = resourceEntity.assetId;
389             r.resourceKey.resourceName = resourceEntity.name;
390         }
391
392         return r;
393     }
394
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) {
398             return null;
399         }
400
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;
405             ai = lai;
406         } else if (r.resourceType == ResourceType.Label) {
407             LabelAllocationItem lai = new LabelAllocationItem();
408             lai.label = aiEntity.llLabel;
409             ai = lai;
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);
414             ai = rai;
415         }
416
417         if (ai != null) {
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));
424             }
425             ai.applicationId = aiEntity.applicationId;
426             ai.allocationTime = aiEntity.allocationTime;
427         }
428
429         return ai;
430     }
431
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) {
435             return null;
436         }
437
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;
443
444         return rl;
445     }
446
447     private static boolean eq(Object o1, Object o2) {
448         return o1 == null ? o2 == null : o1.equals(o2);
449     }
450
451     public void setResourceJdbcDao(ResourceJdbcDao resourceJdbcDao) {
452         this.resourceJdbcDao = resourceJdbcDao;
453     }
454
455     public void setResourceLoadJdbcDao(ResourceLoadJdbcDao resourceLoadJdbcDao) {
456         this.resourceLoadJdbcDao = resourceLoadJdbcDao;
457     }
458
459     public void setAllocationItemJdbcDao(AllocationItemJdbcDao allocationItemJdbcDao) {
460         this.allocationItemJdbcDao = allocationItemJdbcDao;
461     }
462 }