[CCSDK-6] Populate seed code
[ccsdk/sli/adaptors.git] / resource-assignment / provider / src / main / java / org / openecomp / sdnc / rm / dao / jdbc / ResourceDaoImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 ONAP 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.openecomp.sdnc.rm.dao.jdbc;
23
24 import java.util.ArrayList;
25 import java.util.HashSet;
26 import java.util.List;
27
28 import org.openecomp.sdnc.rm.dao.ResourceDao;
29 import org.openecomp.sdnc.rm.data.LabelAllocationItem;
30 import org.openecomp.sdnc.rm.data.LabelResource;
31 import org.openecomp.sdnc.rm.data.LimitAllocationItem;
32 import org.openecomp.sdnc.rm.data.LimitResource;
33 import org.openecomp.sdnc.rm.data.RangeAllocationItem;
34 import org.openecomp.sdnc.rm.data.RangeResource;
35 import org.openecomp.sdnc.rm.data.ResourceKey;
36 import org.openecomp.sdnc.rm.data.ResourceType;
37 import org.openecomp.sdnc.util.str.StrUtil;
38
39 public class ResourceDaoImpl implements ResourceDao {
40
41         private ResourceJdbcDao resourceJdbcDao;
42         private ResourceLoadJdbcDao resourceLoadJdbcDao;
43         private AllocationItemJdbcDao allocationItemJdbcDao;
44
45         @Override
46         public org.openecomp.sdnc.rm.data.Resource getResource(String assetId, String resourceName) {
47                 Resource rEntity = resourceJdbcDao.getResource(assetId, resourceName);
48                 org.openecomp.sdnc.rm.data.Resource r = createResource(rEntity);
49
50                 if (r != null) {
51                         List<AllocationItem> aiEntityList = allocationItemJdbcDao.getAllocationItems(rEntity.id);
52                         r.allocationItems = new ArrayList<org.openecomp.sdnc.rm.data.AllocationItem>();
53                         for (AllocationItem aiEntity : aiEntityList) {
54                                 org.openecomp.sdnc.rm.data.AllocationItem ai = createAllocationItem(r, aiEntity);
55                                 r.allocationItems.add(ai);
56                         }
57
58                         List<ResourceLoad> rlEntityList = resourceLoadJdbcDao.getResourceLoads(rEntity.id);
59                         r.resourceLoadList = new ArrayList<org.openecomp.sdnc.rm.data.ResourceLoad>();
60                         for (ResourceLoad rlEntity : rlEntityList) {
61                                 org.openecomp.sdnc.rm.data.ResourceLoad rl = createResourceLoad(r, rlEntity);
62                                 r.resourceLoadList.add(rl);
63                         }
64                 }
65
66                 return r;
67         }
68
69         @Override
70         public void saveResource(org.openecomp.sdnc.rm.data.Resource resource) {
71                 if (resource == null)
72                         return;
73
74                 org.openecomp.sdnc.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.openecomp.sdnc.rm.data.AllocationItem ai : resource.allocationItems) {
81                                         AllocationItem aiEntity = createAllocationItemEntity(resourceEntity.id, ai);
82                                         allocationItemJdbcDao.add(aiEntity);
83                                 }
84                         if (resource.resourceLoadList != null)
85                                 for (org.openecomp.sdnc.rm.data.ResourceLoad rl : resource.resourceLoadList) {
86                                         ResourceLoad rlEntity = createResourceLoadEntity(resourceEntity.id, rl);
87                                         resourceLoadJdbcDao.add(rlEntity);
88                                 }
89                 } else {
90                         updateResourceEntity(resourceEntity, resource);
91                         resourceJdbcDao.update(resourceEntity);
92
93                         List<AllocationItem> oldAiEntityList = allocationItemJdbcDao.getAllocationItems(resourceEntity.id);
94                         if (resource.allocationItems != null)
95                                 for (org.openecomp.sdnc.rm.data.AllocationItem newai : resource.allocationItems) {
96                                         AllocationItem foundAiEntity = null;
97                                         for (AllocationItem oldAiEntity : oldAiEntityList)
98                                                 if (oldAiEntity.resourceSetId.equals(newai.resourceSetId)) {
99                                                         foundAiEntity = oldAiEntity;
100                                                         break;
101                                                 }
102                                         if (foundAiEntity != null) {
103                                                 updateAllocationItemEntity(foundAiEntity, newai);
104                                                 allocationItemJdbcDao.update(foundAiEntity);
105                                         } else {
106                                                 AllocationItem newAiEntity = createAllocationItemEntity(resourceEntity.id, newai);
107                                                 allocationItemJdbcDao.add(newAiEntity);
108                                         }
109                                 }
110                         for (AllocationItem oldAiEntity : oldAiEntityList) {
111                                 boolean found = false;
112                                 if (resource.allocationItems != null)
113                                         for (org.openecomp.sdnc.rm.data.AllocationItem newai : resource.allocationItems)
114                                                 if (oldAiEntity.resourceSetId.equals(newai.resourceSetId)) {
115                                                         found = true;
116                                                         break;
117                                                 }
118                                 if (!found)
119                                         allocationItemJdbcDao.delete(oldAiEntity.id);
120                         }
121
122                         List<ResourceLoad> oldRlEntityList = resourceLoadJdbcDao.getResourceLoads(resourceEntity.id);
123                         if (resource.resourceLoadList != null)
124                                 for (org.openecomp.sdnc.rm.data.ResourceLoad newrl : resource.resourceLoadList) {
125                                         ResourceLoad foundRlEntity = null;
126                                         for (ResourceLoad oldRlEntity : oldRlEntityList)
127                                                 if (oldRlEntity.applicationId.equals(newrl.applicationId)) {
128                                                         foundRlEntity = oldRlEntity;
129                                                         break;
130                                                 }
131                                         if (foundRlEntity != null) {
132                                                 updateResourceLoadEntity(foundRlEntity, newrl);
133                                                 resourceLoadJdbcDao.update(foundRlEntity);
134                                         } else {
135                                                 ResourceLoad newRlEntity = createResourceLoadEntity(resourceEntity.id, newrl);
136                                                 resourceLoadJdbcDao.add(newRlEntity);
137                                         }
138                                 }
139                         for (ResourceLoad oldRlEntity : oldRlEntityList) {
140                                 boolean found = false;
141                                 if (resource.resourceLoadList != null)
142                                         for (org.openecomp.sdnc.rm.data.ResourceLoad newrl : resource.resourceLoadList)
143                                                 if (oldRlEntity.applicationId.equals(newrl.applicationId)) {
144                                                         found = true;
145                                                         break;
146                                                 }
147                                 if (!found)
148                                         resourceLoadJdbcDao.delete(oldRlEntity.id);
149                         }
150                 }
151         }
152
153         @Override
154         public void deleteResource(String assetId, String resourceName) {
155                 org.openecomp.sdnc.rm.dao.jdbc.Resource resourceEntity = resourceJdbcDao.getResource(assetId, resourceName);
156                 if (resourceEntity != null)
157                         resourceJdbcDao.delete(resourceEntity.id);
158         }
159
160         @Override
161         public List<org.openecomp.sdnc.rm.data.Resource> getResourceSet(String resourceSetId) {
162                 List<Resource> rEntityList = resourceJdbcDao.getResourceSet(resourceSetId);
163                 List<org.openecomp.sdnc.rm.data.Resource> rlist = new ArrayList<org.openecomp.sdnc.rm.data.Resource>();
164                 for (Resource rEntity : rEntityList) {
165                         org.openecomp.sdnc.rm.data.Resource r = createResource(rEntity);
166                         rlist.add(r);
167
168                         List<AllocationItem> aiEntityList = allocationItemJdbcDao.getAllocationItems(rEntity.id);
169                         r.allocationItems = new ArrayList<org.openecomp.sdnc.rm.data.AllocationItem>();
170                         for (AllocationItem aiEntity : aiEntityList) {
171                                 org.openecomp.sdnc.rm.data.AllocationItem ai = createAllocationItem(r, aiEntity);
172                                 r.allocationItems.add(ai);
173                         }
174
175                         List<ResourceLoad> rlEntityList = resourceLoadJdbcDao.getResourceLoads(rEntity.id);
176                         r.resourceLoadList = new ArrayList<org.openecomp.sdnc.rm.data.ResourceLoad>();
177                         for (ResourceLoad rlEntity : rlEntityList) {
178                                 org.openecomp.sdnc.rm.data.ResourceLoad rl = createResourceLoad(r, rlEntity);
179                                 r.resourceLoadList.add(rl);
180                         }
181                 }
182                 return rlist;
183         }
184
185         @Override
186         public List<org.openecomp.sdnc.rm.data.Resource> getResourceUnion(String resourceUnionId) {
187                 List<Resource> rEntityList = resourceJdbcDao.getResourceUnion(resourceUnionId);
188                 List<org.openecomp.sdnc.rm.data.Resource> rlist = new ArrayList<org.openecomp.sdnc.rm.data.Resource>();
189                 for (Resource rEntity : rEntityList) {
190                         org.openecomp.sdnc.rm.data.Resource r = createResource(rEntity);
191                         rlist.add(r);
192
193                         List<AllocationItem> aiEntityList = allocationItemJdbcDao.getAllocationItems(rEntity.id);
194                         r.allocationItems = new ArrayList<org.openecomp.sdnc.rm.data.AllocationItem>();
195                         for (AllocationItem aiEntity : aiEntityList) {
196                                 org.openecomp.sdnc.rm.data.AllocationItem ai = createAllocationItem(r, aiEntity);
197                                 r.allocationItems.add(ai);
198                         }
199
200                         List<ResourceLoad> rlEntityList = resourceLoadJdbcDao.getResourceLoads(rEntity.id);
201                         r.resourceLoadList = new ArrayList<org.openecomp.sdnc.rm.data.ResourceLoad>();
202                         for (ResourceLoad rlEntity : rlEntityList) {
203                                 org.openecomp.sdnc.rm.data.ResourceLoad rl = createResourceLoad(r, rlEntity);
204                                 r.resourceLoadList.add(rl);
205                         }
206                 }
207                 return rlist;
208         }
209
210         private Resource createResourceEntity(org.openecomp.sdnc.rm.data.Resource resource) {
211                 Resource resourceEntity = new Resource();
212                 resourceEntity.assetId = resource.resourceKey.assetId;
213                 resourceEntity.name = resource.resourceKey.resourceName;
214                 resourceEntity.type = resource.resourceType.toString();
215                 if (resource.resourceType == ResourceType.Limit)
216                         resourceEntity.ltUsed = ((LimitResource) resource).used;
217                 else if (resource.resourceType == ResourceType.Label) {
218                         resourceEntity.llLabel = ((LabelResource) resource).label;
219                         resourceEntity.llReferenceCount = ((LabelResource) resource).referenceCount;
220                 } else if (resource.resourceType == ResourceType.Range)
221                         resourceEntity.rrUsed = StrUtil.listInt(((RangeResource) resource).used);
222
223                 return resourceEntity;
224         }
225
226         private ResourceLoad createResourceLoadEntity(long resourceId, org.openecomp.sdnc.rm.data.ResourceLoad rl) {
227                 ResourceLoad rlEntity = new ResourceLoad();
228                 rlEntity.resourceId = resourceId;
229                 rlEntity.applicationId = rl.applicationId;
230                 rlEntity.loadTime = rl.resourceLoadTime;
231                 rlEntity.expirationTime = rl.resourceExpirationTime;
232                 return rlEntity;
233         }
234
235         private void updateResourceLoadEntity(ResourceLoad rlEntity, org.openecomp.sdnc.rm.data.ResourceLoad rl) {
236                 rlEntity.loadTime = rl.resourceLoadTime;
237                 rlEntity.expirationTime = rl.resourceExpirationTime;
238         }
239
240         private AllocationItem createAllocationItemEntity(long resourceId, org.openecomp.sdnc.rm.data.AllocationItem ai) {
241                 AllocationItem aiEntity = new AllocationItem();
242                 aiEntity.resourceId = resourceId;
243                 aiEntity.resourceSetId = ai.resourceSetId;
244                 aiEntity.resourceUnionId = ai.resourceUnionId;
245                 aiEntity.resourceShareGroupList = StrUtil.listStr(ai.resourceShareGroupList);
246                 aiEntity.applicationId = ai.applicationId;
247                 aiEntity.allocationTime = ai.allocationTime;
248                 if (ai.resourceType == ResourceType.Limit)
249                         aiEntity.ltUsed = ((LimitAllocationItem) ai).used;
250                 else if (ai.resourceType == ResourceType.Label)
251                         aiEntity.llLabel = ((LabelAllocationItem) ai).label;
252                 else if (ai.resourceType == ResourceType.Range)
253                         aiEntity.rrUsed = StrUtil.listInt(((RangeAllocationItem) ai).used);
254                 return aiEntity;
255         }
256
257         private void updateAllocationItemEntity(AllocationItem aiEntity, org.openecomp.sdnc.rm.data.AllocationItem ai) {
258                 aiEntity.resourceShareGroupList = StrUtil.listStr(ai.resourceShareGroupList);
259                 aiEntity.allocationTime = ai.allocationTime;
260                 if (ai.resourceType == ResourceType.Limit)
261                         aiEntity.ltUsed = ((LimitAllocationItem) ai).used;
262                 else if (ai.resourceType == ResourceType.Label)
263                         aiEntity.llLabel = ((LabelAllocationItem) ai).label;
264                 else if (ai.resourceType == ResourceType.Range)
265                         aiEntity.rrUsed = StrUtil.listInt(((RangeAllocationItem) ai).used);
266         }
267
268         private void updateResourceEntity(Resource resourceEntity, org.openecomp.sdnc.rm.data.Resource resource) {
269                 if (resource.resourceType == ResourceType.Limit)
270                         resourceEntity.ltUsed = ((LimitResource) resource).used;
271                 else if (resource.resourceType == ResourceType.Label) {
272                         resourceEntity.llLabel = ((LabelResource) resource).label;
273                         resourceEntity.llReferenceCount = ((LabelResource) resource).referenceCount;
274                 } else if (resource.resourceType == ResourceType.Range)
275                         resourceEntity.rrUsed = StrUtil.listInt(((RangeResource) resource).used);
276         }
277
278         private org.openecomp.sdnc.rm.data.Resource createResource(Resource resourceEntity) {
279                 if (resourceEntity == null)
280                         return null;
281
282                 org.openecomp.sdnc.rm.data.Resource r = null;
283                 ResourceType type = ResourceType.valueOf(resourceEntity.type);
284                 if (type == ResourceType.Limit) {
285                         LimitResource l = new LimitResource();
286                         l.used = resourceEntity.ltUsed;
287                         r = l;
288                 } else if (type == ResourceType.Label) {
289                         LabelResource l = new LabelResource();
290                         l.label = resourceEntity.llLabel;
291                         l.referenceCount = resourceEntity.llReferenceCount;
292                         r = l;
293                 } else if (type == ResourceType.Range) {
294                         RangeResource rr = new RangeResource();
295                         rr.used =
296                                 StrUtil.listInt(resourceEntity.rrUsed, "Invalid data found in DB in for Resource Id: " +
297                                         resourceEntity.id + ": RESOURCE.RR_USED: " + resourceEntity.rrUsed);
298                         r = rr;
299                 }
300
301                 r.resourceType = type;
302                 r.resourceKey = new ResourceKey();
303                 r.resourceKey.assetId = resourceEntity.assetId;
304                 r.resourceKey.resourceName = resourceEntity.name;
305
306                 return r;
307         }
308
309         private org.openecomp.sdnc.rm.data.AllocationItem createAllocationItem(
310                 org.openecomp.sdnc.rm.data.Resource r,
311                 AllocationItem aiEntity) {
312                 if (r == null || aiEntity == null)
313                         return null;
314
315                 org.openecomp.sdnc.rm.data.AllocationItem ai = null;
316                 if (r.resourceType == ResourceType.Limit) {
317                         LimitAllocationItem lai = new LimitAllocationItem();
318                         lai.used = aiEntity.ltUsed;
319                         ai = lai;
320                 } else if (r.resourceType == ResourceType.Label) {
321                         LabelAllocationItem lai = new LabelAllocationItem();
322                         lai.label = aiEntity.llLabel;
323                         ai = lai;
324                 } else if (r.resourceType == ResourceType.Range) {
325                         RangeAllocationItem rai = new RangeAllocationItem();
326                         rai.used =
327                                 StrUtil.listInt(aiEntity.rrUsed, "Invalid data found in DB in for Allocation Item Id: " +
328                                         aiEntity.id + ": ALLOCATION_ITEM.RR_USED: " + aiEntity.rrUsed);
329                         ai = rai;
330                 }
331
332                 ai.resourceType = r.resourceType;
333                 ai.resourceKey = r.resourceKey;
334                 ai.resourceSetId = aiEntity.resourceSetId;
335                 ai.resourceUnionId = aiEntity.resourceUnionId;
336                 if (aiEntity.resourceShareGroupList != null)
337                         ai.resourceShareGroupList = new HashSet<String>(StrUtil.listStr(aiEntity.resourceShareGroupList));
338                 ai.applicationId = aiEntity.applicationId;
339                 ai.allocationTime = aiEntity.allocationTime;
340
341                 return ai;
342         }
343
344         private org.openecomp.sdnc.rm.data.ResourceLoad createResourceLoad(
345                 org.openecomp.sdnc.rm.data.Resource r,
346                 ResourceLoad rlEntity) {
347                 if (rlEntity == null)
348                         return null;
349
350                 org.openecomp.sdnc.rm.data.ResourceLoad rl = new org.openecomp.sdnc.rm.data.ResourceLoad();
351                 rl.resourceKey = r.resourceKey;
352                 rl.applicationId = rlEntity.applicationId;
353                 rl.resourceLoadTime = rlEntity.loadTime;
354                 rl.resourceExpirationTime = rlEntity.expirationTime;
355
356                 return rl;
357         }
358
359         public void setResourceJdbcDao(ResourceJdbcDao resourceJdbcDao) {
360                 this.resourceJdbcDao = resourceJdbcDao;
361         }
362
363         public void setResourceLoadJdbcDao(ResourceLoadJdbcDao resourceLoadJdbcDao) {
364                 this.resourceLoadJdbcDao = resourceLoadJdbcDao;
365         }
366
367         public void setAllocationItemJdbcDao(AllocationItemJdbcDao allocationItemJdbcDao) {
368                 this.allocationItemJdbcDao = allocationItemJdbcDao;
369         }
370 }