aboutsummaryrefslogtreecommitdiffstats
path: root/resource-assignment/provider/src/main/java/org/openecomp/sdnc/rm/dao/jdbc/ResourceDaoImpl.java
blob: 98d0f57c170dc9d7a44ff28dec864ae38a8dd787 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*-
 * ============LICENSE_START=======================================================
 * openECOMP : SDN-C
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights
 * 							reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.sdnc.rm.dao.jdbc;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import org.openecomp.sdnc.rm.dao.ResourceDao;
import org.openecomp.sdnc.rm.data.LabelAllocationItem;
import org.openecomp.sdnc.rm.data.LabelResource;
import org.openecomp.sdnc.rm.data.LimitAllocationItem;
import org.openecomp.sdnc.rm.data.LimitResource;
import org.openecomp.sdnc.rm.data.RangeAllocationItem;
import org.openecomp.sdnc.rm.data.RangeResource;
import org.openecomp.sdnc.rm.data.ResourceKey;
import org.openecomp.sdnc.rm.data.ResourceType;
import org.openecomp.sdnc.util.str.StrUtil;

public class ResourceDaoImpl implements ResourceDao {

	private ResourceJdbcDao resourceJdbcDao;
	private ResourceLoadJdbcDao resourceLoadJdbcDao;
	private AllocationItemJdbcDao allocationItemJdbcDao;

	@Override
	public org.openecomp.sdnc.rm.data.Resource getResource(String assetId, String resourceName) {
		Resource rEntity = resourceJdbcDao.getResource(assetId, resourceName);
		org.openecomp.sdnc.rm.data.Resource r = createResource(rEntity);

		if (r != null) {
			List<AllocationItem> aiEntityList = allocationItemJdbcDao.getAllocationItems(rEntity.id);
			r.allocationItems = new ArrayList<org.openecomp.sdnc.rm.data.AllocationItem>();
			for (AllocationItem aiEntity : aiEntityList) {
				org.openecomp.sdnc.rm.data.AllocationItem ai = createAllocationItem(r, aiEntity);
				r.allocationItems.add(ai);
			}

			List<ResourceLoad> rlEntityList = resourceLoadJdbcDao.getResourceLoads(rEntity.id);
			r.resourceLoadList = new ArrayList<org.openecomp.sdnc.rm.data.ResourceLoad>();
			for (ResourceLoad rlEntity : rlEntityList) {
				org.openecomp.sdnc.rm.data.ResourceLoad rl = createResourceLoad(r, rlEntity);
				r.resourceLoadList.add(rl);
			}
		}

		return r;
	}

	@Override
	public void saveResource(org.openecomp.sdnc.rm.data.Resource resource) {
		if (resource == null)
			return;

		org.openecomp.sdnc.rm.dao.jdbc.Resource resourceEntity =
		        resourceJdbcDao.getResource(resource.resourceKey.assetId, resource.resourceKey.resourceName);
		if (resourceEntity == null) {
			resourceEntity = createResourceEntity(resource);
			resourceJdbcDao.add(resourceEntity);
			if (resource.allocationItems != null)
				for (org.openecomp.sdnc.rm.data.AllocationItem ai : resource.allocationItems) {
					AllocationItem aiEntity = createAllocationItemEntity(resourceEntity.id, ai);
					allocationItemJdbcDao.add(aiEntity);
				}
			if (resource.resourceLoadList != null)
				for (org.openecomp.sdnc.rm.data.ResourceLoad rl : resource.resourceLoadList) {
					ResourceLoad rlEntity = createResourceLoadEntity(resourceEntity.id, rl);
					resourceLoadJdbcDao.add(rlEntity);
				}
		} else {
			updateResourceEntity(resourceEntity, resource);
			resourceJdbcDao.update(resourceEntity);

			List<AllocationItem> oldAiEntityList = allocationItemJdbcDao.getAllocationItems(resourceEntity.id);
			if (resource.allocationItems != null)
				for (org.openecomp.sdnc.rm.data.AllocationItem newai : resource.allocationItems) {
					AllocationItem foundAiEntity = null;
					for (AllocationItem oldAiEntity : oldAiEntityList)
						if (oldAiEntity.resourceSetId.equals(newai.resourceSetId)) {
							foundAiEntity = oldAiEntity;
							break;
						}
					if (foundAiEntity != null) {
						updateAllocationItemEntity(foundAiEntity, newai);
						allocationItemJdbcDao.update(foundAiEntity);
					} else {
						AllocationItem newAiEntity = createAllocationItemEntity(resourceEntity.id, newai);
						allocationItemJdbcDao.add(newAiEntity);
					}
				}
			for (AllocationItem oldAiEntity : oldAiEntityList) {
				boolean found = false;
				if (resource.allocationItems != null)
					for (org.openecomp.sdnc.rm.data.AllocationItem newai : resource.allocationItems)
						if (oldAiEntity.resourceSetId.equals(newai.resourceSetId)) {
							found = true;
							break;
						}
				if (!found)
					allocationItemJdbcDao.delete(oldAiEntity.id);
			}

			List<ResourceLoad> oldRlEntityList = resourceLoadJdbcDao.getResourceLoads(resourceEntity.id);
			if (resource.resourceLoadList != null)
				for (org.openecomp.sdnc.rm.data.ResourceLoad newrl : resource.resourceLoadList) {
					ResourceLoad foundRlEntity = null;
					for (ResourceLoad oldRlEntity : oldRlEntityList)
						if (oldRlEntity.applicationId.equals(newrl.applicationId)) {
							foundRlEntity = oldRlEntity;
							break;
						}
					if (foundRlEntity != null) {
						updateResourceLoadEntity(foundRlEntity, newrl);
						resourceLoadJdbcDao.update(foundRlEntity);
					} else {
						ResourceLoad newRlEntity = createResourceLoadEntity(resourceEntity.id, newrl);
						resourceLoadJdbcDao.add(newRlEntity);
					}
				}
			for (ResourceLoad oldRlEntity : oldRlEntityList) {
				boolean found = false;
				if (resource.resourceLoadList != null)
					for (org.openecomp.sdnc.rm.data.ResourceLoad newrl : resource.resourceLoadList)
						if (oldRlEntity.applicationId.equals(newrl.applicationId)) {
							found = true;
							break;
						}
				if (!found)
					resourceLoadJdbcDao.delete(oldRlEntity.id);
			}
		}
	}

	@Override
	public void deleteResource(String assetId, String resourceName) {
		org.openecomp.sdnc.rm.dao.jdbc.Resource resourceEntity = resourceJdbcDao.getResource(assetId, resourceName);
		if (resourceEntity != null)
			resourceJdbcDao.delete(resourceEntity.id);
	}

	@Override
	public List<org.openecomp.sdnc.rm.data.Resource> getResourceSet(String resourceSetId) {
		List<Resource> rEntityList = resourceJdbcDao.getResourceSet(resourceSetId);
		List<org.openecomp.sdnc.rm.data.Resource> rlist = new ArrayList<org.openecomp.sdnc.rm.data.Resource>();
		for (Resource rEntity : rEntityList) {
			org.openecomp.sdnc.rm.data.Resource r = createResource(rEntity);
			rlist.add(r);

			List<AllocationItem> aiEntityList = allocationItemJdbcDao.getAllocationItems(rEntity.id);
			r.allocationItems = new ArrayList<org.openecomp.sdnc.rm.data.AllocationItem>();
			for (AllocationItem aiEntity : aiEntityList) {
				org.openecomp.sdnc.rm.data.AllocationItem ai = createAllocationItem(r, aiEntity);
				r.allocationItems.add(ai);
			}

			List<ResourceLoad> rlEntityList = resourceLoadJdbcDao.getResourceLoads(rEntity.id);
			r.resourceLoadList = new ArrayList<org.openecomp.sdnc.rm.data.ResourceLoad>();
			for (ResourceLoad rlEntity : rlEntityList) {
				org.openecomp.sdnc.rm.data.ResourceLoad rl = createResourceLoad(r, rlEntity);
				r.resourceLoadList.add(rl);
			}
		}
		return rlist;
	}

	@Override
	public List<org.openecomp.sdnc.rm.data.Resource> getResourceUnion(String resourceUnionId) {
		List<Resource> rEntityList = resourceJdbcDao.getResourceUnion(resourceUnionId);
		List<org.openecomp.sdnc.rm.data.Resource> rlist = new ArrayList<org.openecomp.sdnc.rm.data.Resource>();
		for (Resource rEntity : rEntityList) {
			org.openecomp.sdnc.rm.data.Resource r = createResource(rEntity);
			rlist.add(r);

			List<AllocationItem> aiEntityList = allocationItemJdbcDao.getAllocationItems(rEntity.id);
			r.allocationItems = new ArrayList<org.openecomp.sdnc.rm.data.AllocationItem>();
			for (AllocationItem aiEntity : aiEntityList) {
				org.openecomp.sdnc.rm.data.AllocationItem ai = createAllocationItem(r, aiEntity);
				r.allocationItems.add(ai);
			}

			List<ResourceLoad> rlEntityList = resourceLoadJdbcDao.getResourceLoads(rEntity.id);
			r.resourceLoadList = new ArrayList<org.openecomp.sdnc.rm.data.ResourceLoad>();
			for (ResourceLoad rlEntity : rlEntityList) {
				org.openecomp.sdnc.rm.data.ResourceLoad rl = createResourceLoad(r, rlEntity);
				r.resourceLoadList.add(rl);
			}
		}
		return rlist;
	}

	private Resource createResourceEntity(org.openecomp.sdnc.rm.data.Resource resource) {
		Resource resourceEntity = new Resource();
		resourceEntity.assetId = resource.resourceKey.assetId;
		resourceEntity.name = resource.resourceKey.resourceName;
		resourceEntity.type = resource.resourceType.toString();
		if (resource.resourceType == ResourceType.Limit)
			resourceEntity.ltUsed = ((LimitResource) resource).used;
		else if (resource.resourceType == ResourceType.Label) {
			resourceEntity.llLabel = ((LabelResource) resource).label;
			resourceEntity.llReferenceCount = ((LabelResource) resource).referenceCount;
		} else if (resource.resourceType == ResourceType.Range)
			resourceEntity.rrUsed = StrUtil.listInt(((RangeResource) resource).used);

		return resourceEntity;
	}

	private ResourceLoad createResourceLoadEntity(long resourceId, org.openecomp.sdnc.rm.data.ResourceLoad rl) {
		ResourceLoad rlEntity = new ResourceLoad();
		rlEntity.resourceId = resourceId;
		rlEntity.applicationId = rl.applicationId;
		rlEntity.loadTime = rl.resourceLoadTime;
		rlEntity.expirationTime = rl.resourceExpirationTime;
		return rlEntity;
	}

	private void updateResourceLoadEntity(ResourceLoad rlEntity, org.openecomp.sdnc.rm.data.ResourceLoad rl) {
		rlEntity.loadTime = rl.resourceLoadTime;
		rlEntity.expirationTime = rl.resourceExpirationTime;
	}

	private AllocationItem createAllocationItemEntity(long resourceId, org.openecomp.sdnc.rm.data.AllocationItem ai) {
		AllocationItem aiEntity = new AllocationItem();
		aiEntity.resourceId = resourceId;
		aiEntity.resourceSetId = ai.resourceSetId;
		aiEntity.resourceUnionId = ai.resourceUnionId;
		aiEntity.resourceShareGroupList = StrUtil.listStr(ai.resourceShareGroupList);
		aiEntity.applicationId = ai.applicationId;
		aiEntity.allocationTime = ai.allocationTime;
		if (ai.resourceType == ResourceType.Limit)
			aiEntity.ltUsed = ((LimitAllocationItem) ai).used;
		else if (ai.resourceType == ResourceType.Label)
			aiEntity.llLabel = ((LabelAllocationItem) ai).label;
		else if (ai.resourceType == ResourceType.Range)
			aiEntity.rrUsed = StrUtil.listInt(((RangeAllocationItem) ai).used);
		return aiEntity;
	}

	private void updateAllocationItemEntity(AllocationItem aiEntity, org.openecomp.sdnc.rm.data.AllocationItem ai) {
		aiEntity.resourceShareGroupList = StrUtil.listStr(ai.resourceShareGroupList);
		aiEntity.allocationTime = ai.allocationTime;
		if (ai.resourceType == ResourceType.Limit)
			aiEntity.ltUsed = ((LimitAllocationItem) ai).used;
		else if (ai.resourceType == ResourceType.Label)
			aiEntity.llLabel = ((LabelAllocationItem) ai).label;
		else if (ai.resourceType == ResourceType.Range)
			aiEntity.rrUsed = StrUtil.listInt(((RangeAllocationItem) ai).used);
	}

	private void updateResourceEntity(Resource resourceEntity, org.openecomp.sdnc.rm.data.Resource resource) {
		if (resource.resourceType == ResourceType.Limit)
			resourceEntity.ltUsed = ((LimitResource) resource).used;
		else if (resource.resourceType == ResourceType.Label) {
			resourceEntity.llLabel = ((LabelResource) resource).label;
			resourceEntity.llReferenceCount = ((LabelResource) resource).referenceCount;
		} else if (resource.resourceType == ResourceType.Range)
			resourceEntity.rrUsed = StrUtil.listInt(((RangeResource) resource).used);
	}

	private org.openecomp.sdnc.rm.data.Resource createResource(Resource resourceEntity) {
		if (resourceEntity == null)
			return null;

		org.openecomp.sdnc.rm.data.Resource r = null;
		ResourceType type = ResourceType.valueOf(resourceEntity.type);
		if (type == ResourceType.Limit) {
			LimitResource l = new LimitResource();
			l.used = resourceEntity.ltUsed;
			r = l;
		} else if (type == ResourceType.Label) {
			LabelResource l = new LabelResource();
			l.label = resourceEntity.llLabel;
			l.referenceCount = resourceEntity.llReferenceCount;
			r = l;
		} else if (type == ResourceType.Range) {
			RangeResource rr = new RangeResource();
			rr.used =
			        StrUtil.listInt(resourceEntity.rrUsed, "Invalid data found in DB in for Resource Id: " +
			                resourceEntity.id + ": RESOURCE.RR_USED: " + resourceEntity.rrUsed);
			r = rr;
		}

		r.resourceType = type;
		r.resourceKey = new ResourceKey();
		r.resourceKey.assetId = resourceEntity.assetId;
		r.resourceKey.resourceName = resourceEntity.name;

		return r;
	}

	private org.openecomp.sdnc.rm.data.AllocationItem createAllocationItem(
	        org.openecomp.sdnc.rm.data.Resource r,
	        AllocationItem aiEntity) {
		if (r == null || aiEntity == null)
			return null;

		org.openecomp.sdnc.rm.data.AllocationItem ai = null;
		if (r.resourceType == ResourceType.Limit) {
			LimitAllocationItem lai = new LimitAllocationItem();
			lai.used = aiEntity.ltUsed;
			ai = lai;
		} else if (r.resourceType == ResourceType.Label) {
			LabelAllocationItem lai = new LabelAllocationItem();
			lai.label = aiEntity.llLabel;
			ai = lai;
		} else if (r.resourceType == ResourceType.Range) {
			RangeAllocationItem rai = new RangeAllocationItem();
			rai.used =
			        StrUtil.listInt(aiEntity.rrUsed, "Invalid data found in DB in for Allocation Item Id: " +
			                aiEntity.id + ": ALLOCATION_ITEM.RR_USED: " + aiEntity.rrUsed);
			ai = rai;
		}

		ai.resourceType = r.resourceType;
		ai.resourceKey = r.resourceKey;
		ai.resourceSetId = aiEntity.resourceSetId;
		ai.resourceUnionId = aiEntity.resourceUnionId;
		if (aiEntity.resourceShareGroupList != null)
			ai.resourceShareGroupList = new HashSet<String>(StrUtil.listStr(aiEntity.resourceShareGroupList));
		ai.applicationId = aiEntity.applicationId;
		ai.allocationTime = aiEntity.allocationTime;

		return ai;
	}

	private org.openecomp.sdnc.rm.data.ResourceLoad createResourceLoad(
	        org.openecomp.sdnc.rm.data.Resource r,
	        ResourceLoad rlEntity) {
		if (rlEntity == null)
			return null;

		org.openecomp.sdnc.rm.data.ResourceLoad rl = new org.openecomp.sdnc.rm.data.ResourceLoad();
		rl.resourceKey = r.resourceKey;
		rl.applicationId = rlEntity.applicationId;
		rl.resourceLoadTime = rlEntity.loadTime;
		rl.resourceExpirationTime = rlEntity.expirationTime;

		return rl;
	}

	public void setResourceJdbcDao(ResourceJdbcDao resourceJdbcDao) {
		this.resourceJdbcDao = resourceJdbcDao;
	}

	public void setResourceLoadJdbcDao(ResourceLoadJdbcDao resourceLoadJdbcDao) {
		this.resourceLoadJdbcDao = resourceLoadJdbcDao;
	}

	public void setAllocationItemJdbcDao(AllocationItemJdbcDao allocationItemJdbcDao) {
		this.allocationItemJdbcDao = allocationItemJdbcDao;
	}
}