[CCSDK-6] Populate seed code
[ccsdk/sli/adaptors.git] / resource-assignment / provider / src / main / java / org / openecomp / sdnc / rm / comp / ResourceManagerImpl.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.HashSet;
25 import java.util.List;
26 import java.util.Set;
27
28 import org.openecomp.sdnc.lock.comp.LockHelper;
29 import org.openecomp.sdnc.rm.dao.ResourceDao;
30 import org.openecomp.sdnc.rm.data.AllocationOutcome;
31 import org.openecomp.sdnc.rm.data.AllocationRequest;
32 import org.openecomp.sdnc.rm.data.Resource;
33 import org.openecomp.sdnc.rm.util.ResourceUtil;
34 import org.openecomp.sdnc.util.str.StrUtil;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 public class ResourceManagerImpl implements ResourceManager {
39
40         private static final Logger log = LoggerFactory.getLogger(ResourceManagerImpl.class);
41
42         private LockHelper lockHelper;
43         private ResourceDao resourceDao;
44
45         private String applicationId;
46         private int lockTimeout = 10 * 60; // Default 10 min
47
48         public ResourceManagerImpl() {
49                 log.info("ResourceManager created.");
50         }
51
52         @Override
53         public Resource getResource(String resourceName, String assetId) {
54                 Resource r = resourceDao.getResource(assetId, resourceName);
55                 ResourceUtil.recalculate(r);
56                 return r;
57         }
58
59         @Override
60         public List<Resource> getResourceUnion(String resourceUnionId) {
61                 List<Resource> rlist = resourceDao.getResourceUnion(resourceUnionId);
62                 for (Resource r : rlist)
63                         ResourceUtil.recalculate(r);
64                 return rlist;
65         }
66
67         @Override
68         public AllocationOutcome allocateResources(AllocationRequest allocationRequest) {
69                 if (allocationRequest == null)
70                         throw new IllegalArgumentException("allocateResources called with null argument");
71
72                 AllocationFunction allocationFunction =
73                         new AllocationFunction(lockHelper, resourceDao, applicationId, allocationRequest, lockTimeout);
74                 allocationFunction.exec();
75                 AllocationOutcome allocationOutcome = allocationFunction.getAllocationOutcome();
76
77                 StrUtil.info(log, allocationOutcome);
78
79                 return allocationOutcome;
80         }
81
82         @Override
83         public void releaseResourceSet(String resourceSetId) {
84                 List<Resource> resourceList = resourceDao.getResourceSet(resourceSetId);
85                 if (resourceList == null || resourceList.isEmpty())
86                         return;
87
88                 Set<String> lockNames = getLockNames(resourceList);
89                 ReleaseFunction releaseFunction =
90                         new ReleaseFunction(lockHelper, resourceDao, resourceSetId, null, lockNames, lockTimeout);
91                 releaseFunction.exec();
92         }
93
94         @Override
95         public void releaseResourceUnion(String resourceUnionId) {
96                 List<Resource> resourceList = resourceDao.getResourceUnion(resourceUnionId);
97                 if (resourceList == null || resourceList.isEmpty())
98                         return;
99
100                 Set<String> lockNames = getLockNames(resourceList);
101                 ReleaseFunction releaseFunction =
102                         new ReleaseFunction(lockHelper, resourceDao, null, resourceUnionId, lockNames, lockTimeout);
103                 releaseFunction.exec();
104         }
105
106         private Set<String> getLockNames(List<Resource> resourceList) {
107                 Set<String> lockNames = new HashSet<String>();
108                 for (Resource r : resourceList)
109                         lockNames.add(r.resourceKey.assetId);
110                 return lockNames;
111         }
112
113         public void setResourceDao(ResourceDao resourceDao) {
114                 this.resourceDao = resourceDao;
115         }
116
117         public void setLockTimeout(int lockTimeout) {
118                 this.lockTimeout = lockTimeout;
119         }
120
121         public void setApplicationId(String applicationId) {
122                 this.applicationId = applicationId;
123         }
124
125         public void setLockHelper(LockHelper lockHelper) {
126                 this.lockHelper = lockHelper;
127         }
128 }