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