[CCSDK-6] Populate seed code
[ccsdk/sli/adaptors.git] / resource-assignment / provider / src / main / java / org / openecomp / sdnc / rm / comp / ReleaseFunction.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.comp;
23
24 import java.util.Collection;
25 import java.util.Iterator;
26 import java.util.List;
27
28 import org.openecomp.sdnc.lock.comp.LockHelper;
29 import org.openecomp.sdnc.lock.comp.ResourceLockedException;
30 import org.openecomp.sdnc.lock.comp.SynchronizedFunction;
31 import org.openecomp.sdnc.rm.dao.ResourceDao;
32 import org.openecomp.sdnc.rm.data.AllocationItem;
33 import org.openecomp.sdnc.rm.data.Resource;
34 import org.openecomp.sdnc.rm.util.ResourceUtil;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 class ReleaseFunction extends SynchronizedFunction {
39
40         @SuppressWarnings("unused")
41         private static final Logger log = LoggerFactory.getLogger(ReleaseFunction.class);
42
43         private ResourceDao resourceDao;
44
45         private String resourceSetId, resourceUnionId;
46
47         public ReleaseFunction(LockHelper lockHelper, ResourceDao resourceDao, String resourceSetId,
48                 String resourceUnionId, Collection<String> lockNames, int lockTimeout) {
49                 super(lockHelper, lockNames, lockTimeout);
50                 this.resourceDao = resourceDao;
51                 this.resourceSetId = resourceSetId;
52                 this.resourceUnionId = resourceUnionId;
53         }
54
55         @Override
56         public void _exec() throws ResourceLockedException {
57                 List<Resource> resourceList =
58                         resourceSetId != null
59                                 ? resourceDao.getResourceSet(resourceSetId) : resourceDao.getResourceUnion(resourceUnionId);
60                 for (Resource r : resourceList) {
61                         boolean updated = false;
62                         if (r.allocationItems != null) {
63                                 Iterator<AllocationItem> i = r.allocationItems.iterator();
64                                 while (i.hasNext()) {
65                                         AllocationItem ai = i.next();
66                                         if (resourceSetId != null) {
67                                                 if (resourceSetId.equals(ai.resourceSetId)) {
68                                                         i.remove();
69                                                         updated = true;
70                                                 }
71
72                                         } else if (resourceUnionId != null) {
73
74                                                 if (resourceUnionId.equals(ai.resourceUnionId)) {
75                                                         i.remove();
76                                                         updated = true;
77                                                 }
78
79                                         }
80                                 }
81                         }
82                         if (updated) {
83                                 ResourceUtil.recalculate(r);
84                                 resourceDao.saveResource(r);
85                         }
86                 }
87         }
88 }