7deec92234634db68a2f1adf92a637c2e6ad987d
[ccsdk/sli/adaptors.git] /
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         org.onap.ccsdk.sli.adaptors.rm.data.Resource r = createResource(rEntity);
48
49         if (r != null) {
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);
55             }
56
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);
62             }
63         }
64
65         return r;
66     }
67
68     @Override
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);
73
74         if (r != null) {
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);
81             }
82
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);
88             }
89         }
90
91         return r;
92     }
93
94     @Override
95     public void saveResource(org.onap.ccsdk.sli.adaptors.rm.data.Resource resource) {
96         if (resource == null) {
97             return;
98         }
99
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);
109                 }
110             }
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);
115                 }
116             }
117         } else {
118             updateResourceEntity(resourceEntity, resource);
119             resourceJdbcDao.update(resourceEntity);
120
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;
128                             break;
129                         }
130                     }
131                     if (foundAiEntity != null) {
132                         if (allocationItemChanged(foundAiEntity, newai)) {
133                             updateAllocationItemEntity(foundAiEntity, newai);
134                             allocationItemJdbcDao.update(foundAiEntity);
135                         }
136                     } else {
137                         AllocationItem newAiEntity = createAllocationItemEntity(resourceEntity.id, newai);
138                         allocationItemJdbcDao.add(newAiEntity);
139                     }
140                 }
141             }
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)) {
147                             found = true;
148                             break;
149                         }
150                     }
151                 }
152                 if (!found) {
153                     allocationItemJdbcDao.delete(oldAiEntity.id);
154                 }
155             }
156
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;
164                             break;
165                         }
166                     }
167                     if (foundRlEntity != null) {
168                         updateResourceLoadEntity(foundRlEntity, newrl);
169                         resourceLoadJdbcDao.update(foundRlEntity);
170                     } else {
171                         ResourceLoad newRlEntity = createResourceLoadEntity(resourceEntity.id, newrl);
172                         resourceLoadJdbcDao.add(newRlEntity);
173                     }
174                 }
175             }
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)) {
181                             found = true;
182                             break;
183                         }
184                     }
185                 }
186                 if (!found) {
187                     resourceLoadJdbcDao.delete(oldRlEntity.id);
188                 }
189             }
190         }
191     }
192
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)) {
197             return true;
198         }
199
200         if (newai.resourceType == ResourceType.Limit) {
201             if (aiEntity.ltUsed != ((LimitAllocationItem) newai).used) {
202                 return true;
203             }
204         } else if (newai.resourceType == ResourceType.Label) {
205             if (!eq(aiEntity.llLabel, ((LabelAllocationItem) newai).label)) {
206                 return true;
207             }
208         } else if (newai.resourceType == ResourceType.Range) {
209             String newRrUsed = StrUtil.listInt(((RangeAllocationItem) newai).used);
210             if (!eq(aiEntity.rrUsed, newRrUsed)) {
211                 return true;
212             }
213         }
214
215         return false;
216     }
217
218     @Override
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);
224         }
225     }
226
227     @Override
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);
233             rlist.add(r);
234
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);
240             }
241
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);
247             }
248         }
249         return rlist;
250     }
251
252     @Override
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);
258             rlist.add(r);
259
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);
265             }
266
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);
272             }
273         }
274         return rlist;
275     }
276
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);
289         }
290
291         return resourceEntity;
292     }
293
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;
301         return rlEntity;
302     }
303
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;
307     }
308
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);
324         }
325         return aiEntity;
326     }
327
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);
338         }
339     }
340
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);
349         }
350     }
351
352     private org.onap.ccsdk.sli.adaptors.rm.data.Resource createResource(Resource resourceEntity) {
353         if (resourceEntity == null) {
354             return null;
355         }
356
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;
362             r = l;
363         } else if (type == ResourceType.Label) {
364             LabelResource l = new LabelResource();
365             l.label = resourceEntity.llLabel;
366             l.referenceCount = resourceEntity.llReferenceCount;
367             r = l;
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);
372             r = rr;
373         }
374
375         if (r != null) {
376             r.resourceType = type;
377             r.resourceKey = new ResourceKey();
378             r.resourceKey.assetId = resourceEntity.assetId;
379             r.resourceKey.resourceName = resourceEntity.name;
380         }
381
382         return r;
383     }
384
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) {
388             return null;
389         }
390
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;
395             ai = lai;
396         } else if (r.resourceType == ResourceType.Label) {
397             LabelAllocationItem lai = new LabelAllocationItem();
398             lai.label = aiEntity.llLabel;
399             ai = lai;
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);
404             ai = rai;
405         }
406
407         if (ai != null) {
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));
414             }
415             ai.applicationId = aiEntity.applicationId;
416             ai.allocationTime = aiEntity.allocationTime;
417         }
418
419         return ai;
420     }
421
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) {
425             return null;
426         }
427
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;
433
434         return rl;
435     }
436
437     private static boolean eq(Object o1, Object o2) {
438         return o1 == null ? o2 == null : o1.equals(o2);
439     }
440
441     public void setResourceJdbcDao(ResourceJdbcDao resourceJdbcDao) {
442         this.resourceJdbcDao = resourceJdbcDao;
443     }
444
445     public void setResourceLoadJdbcDao(ResourceLoadJdbcDao resourceLoadJdbcDao) {
446         this.resourceLoadJdbcDao = resourceLoadJdbcDao;
447     }
448
449     public void setAllocationItemJdbcDao(AllocationItemJdbcDao allocationItemJdbcDao) {
450         this.allocationItemJdbcDao = allocationItemJdbcDao;
451     }
452 }