172651631b89ffa59e2eab65c8373c362c9d6084
[ccsdk/sli.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 List<org.onap.ccsdk.sli.adaptors.rm.data.Resource> query(String assetIdFilter, String resourceName) {
96         List<Resource> rEntityList = resourceJdbcDao.queryResources(assetIdFilter, resourceName);
97         List<org.onap.ccsdk.sli.adaptors.rm.data.Resource> rlist = new ArrayList<>();
98         for (Resource rEntity : rEntityList) {
99             org.onap.ccsdk.sli.adaptors.rm.data.Resource r = createResource(rEntity);
100             rlist.add(r);
101
102             List<AllocationItem> aiEntityList = allocationItemJdbcDao.getAllocationItems(rEntity.id);
103             r.allocationItems = new ArrayList<>();
104             for (AllocationItem aiEntity : aiEntityList) {
105                 org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem ai = createAllocationItem(r, aiEntity);
106                 r.allocationItems.add(ai);
107             }
108
109             List<ResourceLoad> rlEntityList = resourceLoadJdbcDao.getResourceLoads(rEntity.id);
110             r.resourceLoadList = new ArrayList<>();
111             for (ResourceLoad rlEntity : rlEntityList) {
112                 org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad rl = createResourceLoad(r, rlEntity);
113                 r.resourceLoadList.add(rl);
114             }
115         }
116         return rlist;
117     }
118
119     @Override
120     public void saveResource(org.onap.ccsdk.sli.adaptors.rm.data.Resource resource) {
121         if (resource == null) {
122             return;
123         }
124
125         org.onap.ccsdk.sli.adaptors.rm.dao.jdbc.Resource resourceEntity =
126                 resourceJdbcDao.getResource(resource.resourceKey.assetId, resource.resourceKey.resourceName);
127         if (resourceEntity == null) {
128             resourceEntity = createResourceEntity(resource);
129             resourceJdbcDao.add(resourceEntity);
130             if (resource.allocationItems != null) {
131                 for (org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem ai : resource.allocationItems) {
132                     AllocationItem aiEntity = createAllocationItemEntity(resourceEntity.id, ai);
133                     allocationItemJdbcDao.add(aiEntity);
134                 }
135             }
136             if (resource.resourceLoadList != null) {
137                 for (org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad rl : resource.resourceLoadList) {
138                     ResourceLoad rlEntity = createResourceLoadEntity(resourceEntity.id, rl);
139                     resourceLoadJdbcDao.add(rlEntity);
140                 }
141             }
142         } else {
143             updateResourceEntity(resourceEntity, resource);
144             resourceJdbcDao.update(resourceEntity);
145
146             List<AllocationItem> oldAiEntityList = allocationItemJdbcDao.getAllocationItems(resourceEntity.id);
147             if (resource.allocationItems != null) {
148                 for (org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem newai : resource.allocationItems) {
149                     AllocationItem foundAiEntity = null;
150                     for (AllocationItem oldAiEntity : oldAiEntityList) {
151                         if (oldAiEntity.resourceSetId.equals(newai.resourceSetId)) {
152                             foundAiEntity = oldAiEntity;
153                             break;
154                         }
155                     }
156                     if (foundAiEntity != null) {
157                         if (allocationItemChanged(foundAiEntity, newai)) {
158                             updateAllocationItemEntity(foundAiEntity, newai);
159                             allocationItemJdbcDao.update(foundAiEntity);
160                         }
161                     } else {
162                         AllocationItem newAiEntity = createAllocationItemEntity(resourceEntity.id, newai);
163                         allocationItemJdbcDao.add(newAiEntity);
164                     }
165                 }
166             }
167             for (AllocationItem oldAiEntity : oldAiEntityList) {
168                 boolean found = false;
169                 if (resource.allocationItems != null) {
170                     for (org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem newai : resource.allocationItems) {
171                         if (oldAiEntity.resourceSetId.equals(newai.resourceSetId)) {
172                             found = true;
173                             break;
174                         }
175                     }
176                 }
177                 if (!found) {
178                     allocationItemJdbcDao.delete(oldAiEntity.id);
179                 }
180             }
181
182             List<ResourceLoad> oldRlEntityList = resourceLoadJdbcDao.getResourceLoads(resourceEntity.id);
183             if (resource.resourceLoadList != null) {
184                 for (org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad newrl : resource.resourceLoadList) {
185                     ResourceLoad foundRlEntity = null;
186                     for (ResourceLoad oldRlEntity : oldRlEntityList) {
187                         if (oldRlEntity.applicationId.equals(newrl.applicationId)) {
188                             foundRlEntity = oldRlEntity;
189                             break;
190                         }
191                     }
192                     if (foundRlEntity != null) {
193                         updateResourceLoadEntity(foundRlEntity, newrl);
194                         resourceLoadJdbcDao.update(foundRlEntity);
195                     } else {
196                         ResourceLoad newRlEntity = createResourceLoadEntity(resourceEntity.id, newrl);
197                         resourceLoadJdbcDao.add(newRlEntity);
198                     }
199                 }
200             }
201             for (ResourceLoad oldRlEntity : oldRlEntityList) {
202                 boolean found = false;
203                 if (resource.resourceLoadList != null) {
204                     for (org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad newrl : resource.resourceLoadList) {
205                         if (oldRlEntity.applicationId.equals(newrl.applicationId)) {
206                             found = true;
207                             break;
208                         }
209                     }
210                 }
211                 if (!found) {
212                     resourceLoadJdbcDao.delete(oldRlEntity.id);
213                 }
214             }
215         }
216     }
217
218     private boolean allocationItemChanged(AllocationItem aiEntity,
219             org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem newai) {
220         String newShareGroupList = StrUtil.listStr(newai.resourceShareGroupList);
221         if (!eq(aiEntity.resourceShareGroupList, newShareGroupList)) {
222             return true;
223         }
224
225         if (newai.resourceType == ResourceType.Limit) {
226             if (aiEntity.ltUsed != ((LimitAllocationItem) newai).used) {
227                 return true;
228             }
229         } else if (newai.resourceType == ResourceType.Label) {
230             if (!eq(aiEntity.llLabel, ((LabelAllocationItem) newai).label)) {
231                 return true;
232             }
233         } else if (newai.resourceType == ResourceType.Range) {
234             String newRrUsed = StrUtil.listInt(((RangeAllocationItem) newai).used);
235             if (!eq(aiEntity.rrUsed, newRrUsed)) {
236                 return true;
237             }
238         }
239
240         return false;
241     }
242
243     @Override
244     public void deleteResource(String assetId, String resourceName) {
245         org.onap.ccsdk.sli.adaptors.rm.dao.jdbc.Resource resourceEntity =
246                 resourceJdbcDao.getResource(assetId, resourceName);
247         if (resourceEntity != null) {
248             resourceJdbcDao.delete(resourceEntity.id);
249         }
250     }
251
252     @Override
253     public List<org.onap.ccsdk.sli.adaptors.rm.data.Resource> getResourceSet(String resourceSetId) {
254         List<Resource> rEntityList = resourceJdbcDao.getResourceSet(resourceSetId);
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     @Override
278     public List<org.onap.ccsdk.sli.adaptors.rm.data.Resource> getResourceUnion(String resourceUnionId) {
279         List<Resource> rEntityList = resourceJdbcDao.getResourceUnion(resourceUnionId);
280         List<org.onap.ccsdk.sli.adaptors.rm.data.Resource> rlist = new ArrayList<>();
281         for (Resource rEntity : rEntityList) {
282             org.onap.ccsdk.sli.adaptors.rm.data.Resource r = createResource(rEntity);
283             rlist.add(r);
284
285             List<AllocationItem> aiEntityList = allocationItemJdbcDao.getAllocationItems(rEntity.id);
286             r.allocationItems = new ArrayList<>();
287             for (AllocationItem aiEntity : aiEntityList) {
288                 org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem ai = createAllocationItem(r, aiEntity);
289                 r.allocationItems.add(ai);
290             }
291
292             List<ResourceLoad> rlEntityList = resourceLoadJdbcDao.getResourceLoads(rEntity.id);
293             r.resourceLoadList = new ArrayList<>();
294             for (ResourceLoad rlEntity : rlEntityList) {
295                 org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad rl = createResourceLoad(r, rlEntity);
296                 r.resourceLoadList.add(rl);
297             }
298         }
299         return rlist;
300     }
301
302     private Resource createResourceEntity(org.onap.ccsdk.sli.adaptors.rm.data.Resource resource) {
303         Resource resourceEntity = new Resource();
304         resourceEntity.assetId = resource.resourceKey.assetId;
305         resourceEntity.name = resource.resourceKey.resourceName;
306         resourceEntity.type = resource.resourceType.toString();
307         if (resource.resourceType == ResourceType.Limit) {
308             resourceEntity.ltUsed = ((LimitResource) resource).used;
309         } else if (resource.resourceType == ResourceType.Label) {
310             resourceEntity.llLabel = ((LabelResource) resource).label;
311             resourceEntity.llReferenceCount = ((LabelResource) resource).referenceCount;
312         } else if (resource.resourceType == ResourceType.Range) {
313             resourceEntity.rrUsed = StrUtil.listInt(((RangeResource) resource).used);
314         }
315
316         return resourceEntity;
317     }
318
319     private ResourceLoad createResourceLoadEntity(long resourceId,
320             org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad rl) {
321         ResourceLoad rlEntity = new ResourceLoad();
322         rlEntity.resourceId = resourceId;
323         rlEntity.applicationId = rl.applicationId;
324         rlEntity.loadTime = rl.resourceLoadTime;
325         rlEntity.expirationTime = rl.resourceExpirationTime;
326         return rlEntity;
327     }
328
329     private void updateResourceLoadEntity(ResourceLoad rlEntity, org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad rl) {
330         rlEntity.loadTime = rl.resourceLoadTime;
331         rlEntity.expirationTime = rl.resourceExpirationTime;
332     }
333
334     private AllocationItem createAllocationItemEntity(long resourceId,
335             org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem ai) {
336         AllocationItem aiEntity = new AllocationItem();
337         aiEntity.resourceId = resourceId;
338         aiEntity.resourceSetId = ai.resourceSetId;
339         aiEntity.resourceUnionId = ai.resourceUnionId;
340         aiEntity.resourceShareGroupList = StrUtil.listStr(ai.resourceShareGroupList);
341         aiEntity.applicationId = ai.applicationId;
342         aiEntity.allocationTime = ai.allocationTime;
343         if (ai.resourceType == ResourceType.Limit) {
344             aiEntity.ltUsed = ((LimitAllocationItem) ai).used;
345         } else if (ai.resourceType == ResourceType.Label) {
346             aiEntity.llLabel = ((LabelAllocationItem) ai).label;
347         } else if (ai.resourceType == ResourceType.Range) {
348             aiEntity.rrUsed = StrUtil.listInt(((RangeAllocationItem) ai).used);
349         }
350         return aiEntity;
351     }
352
353     private void updateAllocationItemEntity(AllocationItem aiEntity,
354             org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem ai) {
355         aiEntity.resourceShareGroupList = StrUtil.listStr(ai.resourceShareGroupList);
356         aiEntity.allocationTime = ai.allocationTime;
357         if (ai.resourceType == ResourceType.Limit) {
358             aiEntity.ltUsed = ((LimitAllocationItem) ai).used;
359         } else if (ai.resourceType == ResourceType.Label) {
360             aiEntity.llLabel = ((LabelAllocationItem) ai).label;
361         } else if (ai.resourceType == ResourceType.Range) {
362             aiEntity.rrUsed = StrUtil.listInt(((RangeAllocationItem) ai).used);
363         }
364     }
365
366     private void updateResourceEntity(Resource resourceEntity, org.onap.ccsdk.sli.adaptors.rm.data.Resource resource) {
367         if (resource.resourceType == ResourceType.Limit) {
368             resourceEntity.ltUsed = ((LimitResource) resource).used;
369         } else if (resource.resourceType == ResourceType.Label) {
370             resourceEntity.llLabel = ((LabelResource) resource).label;
371             resourceEntity.llReferenceCount = ((LabelResource) resource).referenceCount;
372         } else if (resource.resourceType == ResourceType.Range) {
373             resourceEntity.rrUsed = StrUtil.listInt(((RangeResource) resource).used);
374         }
375     }
376
377     private org.onap.ccsdk.sli.adaptors.rm.data.Resource createResource(Resource resourceEntity) {
378         if (resourceEntity == null) {
379             return null;
380         }
381
382         org.onap.ccsdk.sli.adaptors.rm.data.Resource r = null;
383         ResourceType type = ResourceType.valueOf(resourceEntity.type);
384         if (type == ResourceType.Limit) {
385             LimitResource l = new LimitResource();
386             l.used = resourceEntity.ltUsed;
387             r = l;
388         } else if (type == ResourceType.Label) {
389             LabelResource l = new LabelResource();
390             l.label = resourceEntity.llLabel;
391             l.referenceCount = resourceEntity.llReferenceCount;
392             r = l;
393         } else if (type == ResourceType.Range) {
394             RangeResource rr = new RangeResource();
395             rr.used = StrUtil.listInt(resourceEntity.rrUsed, "Invalid data found in DB in for Resource Id: "
396                     + resourceEntity.id + ": RESOURCE.RR_USED: " + resourceEntity.rrUsed);
397             r = rr;
398         }
399
400         if (r != null) {
401             r.resourceType = type;
402             r.resourceKey = new ResourceKey();
403             r.resourceKey.assetId = resourceEntity.assetId;
404             r.resourceKey.resourceName = resourceEntity.name;
405         }
406
407         return r;
408     }
409
410     private org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem createAllocationItem(
411             org.onap.ccsdk.sli.adaptors.rm.data.Resource r, AllocationItem aiEntity) {
412         if (r == null || aiEntity == null) {
413             return null;
414         }
415
416         org.onap.ccsdk.sli.adaptors.rm.data.AllocationItem ai = null;
417         if (r.resourceType == ResourceType.Limit) {
418             LimitAllocationItem lai = new LimitAllocationItem();
419             lai.used = aiEntity.ltUsed;
420             ai = lai;
421         } else if (r.resourceType == ResourceType.Label) {
422             LabelAllocationItem lai = new LabelAllocationItem();
423             lai.label = aiEntity.llLabel;
424             ai = lai;
425         } else if (r.resourceType == ResourceType.Range) {
426             RangeAllocationItem rai = new RangeAllocationItem();
427             rai.used = StrUtil.listInt(aiEntity.rrUsed, "Invalid data found in DB in for Allocation Item Id: "
428                     + aiEntity.id + ": ALLOCATION_ITEM.RR_USED: " + aiEntity.rrUsed);
429             ai = rai;
430         }
431
432         if (ai != null) {
433             ai.resourceType = r.resourceType;
434             ai.resourceKey = r.resourceKey;
435             ai.resourceSetId = aiEntity.resourceSetId;
436             ai.resourceUnionId = aiEntity.resourceUnionId;
437             if (aiEntity.resourceShareGroupList != null) {
438                 ai.resourceShareGroupList = new HashSet<>(StrUtil.listStr(aiEntity.resourceShareGroupList));
439             }
440             ai.applicationId = aiEntity.applicationId;
441             ai.allocationTime = aiEntity.allocationTime;
442         }
443
444         return ai;
445     }
446
447     private org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad createResourceLoad(
448             org.onap.ccsdk.sli.adaptors.rm.data.Resource r, ResourceLoad rlEntity) {
449         if (rlEntity == null) {
450             return null;
451         }
452
453         org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad rl = new org.onap.ccsdk.sli.adaptors.rm.data.ResourceLoad();
454         rl.resourceKey = r.resourceKey;
455         rl.applicationId = rlEntity.applicationId;
456         rl.resourceLoadTime = rlEntity.loadTime;
457         rl.resourceExpirationTime = rlEntity.expirationTime;
458
459         return rl;
460     }
461
462     private static boolean eq(Object o1, Object o2) {
463         return o1 == null ? o2 == null : o1.equals(o2);
464     }
465
466     public void setResourceJdbcDao(ResourceJdbcDao resourceJdbcDao) {
467         this.resourceJdbcDao = resourceJdbcDao;
468     }
469
470     public void setResourceLoadJdbcDao(ResourceLoadJdbcDao resourceLoadJdbcDao) {
471         this.resourceLoadJdbcDao = resourceLoadJdbcDao;
472     }
473
474     public void setAllocationItemJdbcDao(AllocationItemJdbcDao allocationItemJdbcDao) {
475         this.allocationItemJdbcDao = allocationItemJdbcDao;
476     }
477 }