3c845329879db69410c7bacfb0e6aa656ec195b3
[ccsdk/apps.git] /
1 /*******************************************************************************\r
2  * Copyright © 2017-2018 AT&T Intellectual Property.\r
3  * \r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  * \r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  * \r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  ******************************************************************************/\r
16 package org.onap.ccsdk.apps.ms.vlantagapi.core;\r
17 \r
18 import java.util.Arrays;\r
19 import java.util.Collections;\r
20 import java.util.HashMap;\r
21 import java.util.List;\r
22 import java.util.Map;\r
23 \r
24 import javax.sql.DataSource;\r
25 \r
26 import org.onap.ccsdk.sli.adaptors.lock.comp.LockHelper;\r
27 import org.onap.ccsdk.sli.adaptors.lock.comp.LockHelperImpl;\r
28 import org.onap.ccsdk.sli.adaptors.lock.dao.ResourceLockDao;\r
29 import org.onap.ccsdk.sli.adaptors.lock.dao.ResourceLockDaoImpl;\r
30 import org.onap.ccsdk.sli.adaptors.ra.ResourceAllocator;\r
31 import org.onap.ccsdk.sli.adaptors.ra.alloc.DbAllocationRule;\r
32 import org.onap.ccsdk.sli.adaptors.ra.comp.AllocationRule;\r
33 import org.onap.ccsdk.sli.adaptors.ra.comp.EndPointAllocator;\r
34 import org.onap.ccsdk.sli.adaptors.ra.comp.EndPointAllocatorImpl;\r
35 import org.onap.ccsdk.sli.adaptors.ra.rule.dao.RangeRuleDao;\r
36 import org.onap.ccsdk.sli.adaptors.ra.rule.dao.RangeRuleDaoImpl;\r
37 import org.onap.ccsdk.sli.adaptors.ra.rule.dao.ResourceRuleDao;\r
38 import org.onap.ccsdk.sli.adaptors.ra.rule.dao.ResourceRuleDaoImpl;\r
39 import org.onap.ccsdk.sli.adaptors.rm.comp.ResourceManager;\r
40 import org.onap.ccsdk.sli.adaptors.rm.comp.ResourceManagerImpl;\r
41 import org.onap.ccsdk.sli.adaptors.rm.dao.ResourceDao;\r
42 import org.onap.ccsdk.sli.adaptors.rm.dao.jdbc.AllocationItemJdbcDao;\r
43 import org.onap.ccsdk.sli.adaptors.rm.dao.jdbc.AllocationItemJdbcDaoImpl;\r
44 import org.onap.ccsdk.sli.adaptors.rm.dao.jdbc.ResourceDaoImpl;\r
45 import org.onap.ccsdk.sli.adaptors.rm.dao.jdbc.ResourceJdbcDao;\r
46 import org.onap.ccsdk.sli.adaptors.rm.dao.jdbc.ResourceJdbcDaoImpl;\r
47 import org.onap.ccsdk.sli.adaptors.rm.dao.jdbc.ResourceLoadJdbcDao;\r
48 import org.onap.ccsdk.sli.adaptors.rm.dao.jdbc.ResourceLoadJdbcDaoImpl;\r
49 import org.onap.ccsdk.sli.adaptors.util.db.CachedDataSourceWrap;\r
50 import org.onap.ccsdk.sli.adaptors.util.db.DataSourceWrap;\r
51 import org.onap.ccsdk.sli.adaptors.util.speed.SpeedUtil;\r
52 import org.slf4j.Logger;\r
53 import org.slf4j.LoggerFactory;\r
54 import org.springframework.context.annotation.Bean;\r
55 import org.springframework.context.annotation.Configuration;\r
56 import org.springframework.jdbc.core.JdbcTemplate;\r
57 import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;\r
58 import org.springframework.stereotype.Component;\r
59 \r
60 /**\r
61  * ResourceAssignmentInitializer.java Purpose: Represents and Initializes\r
62  * Resource Assignment Spring Module Beans\r
63  * \r
64  * @author Saurav Paira\r
65  * @version 1.0\r
66  */\r
67 \r
68 @Component\r
69 @Configuration\r
70 public class ResourceAssignmentInitializer {\r
71 \r
72         private static final Logger log = LoggerFactory.getLogger(ResourceAssignmentInitializer.class);\r
73         \r
74         \r
75         @Bean\r
76     public JdbcTemplate getJdbcTemplate(DataSource dataSource) {\r
77         return new JdbcTemplate(dataSource);\r
78     }\r
79         \r
80         @Bean\r
81         public ResourceLockDaoImpl getResourceLockDaoImpl(JdbcTemplate jdbcTemplate) {\r
82                 ResourceLockDaoImpl resourceLockDaoImpl = new ResourceLockDaoImpl();\r
83                 resourceLockDaoImpl.setJdbcTemplate(jdbcTemplate);\r
84                 \r
85                 return resourceLockDaoImpl;\r
86         }\r
87         \r
88         @Bean\r
89         public LockHelperImpl getLockHelperImpl(ResourceLockDao resourceLockDao) {\r
90                 LockHelperImpl lockHelperImpl = new LockHelperImpl();\r
91                 lockHelperImpl.setLockWait(5);\r
92                 lockHelperImpl.setRetryCount(10);\r
93                 lockHelperImpl.setResourceLockDao(resourceLockDao);\r
94                 \r
95                 return lockHelperImpl;\r
96         }\r
97         \r
98         @Bean\r
99         public ResourceJdbcDaoImpl getResourceJdbcDaoImpl(JdbcTemplate jdbcTemplate) {\r
100                 ResourceJdbcDaoImpl resourceJdbcDaoImpl = new ResourceJdbcDaoImpl();\r
101                 resourceJdbcDaoImpl.setJdbcTemplate(jdbcTemplate);\r
102                 return resourceJdbcDaoImpl;\r
103         }\r
104         \r
105         @Bean\r
106         public AllocationItemJdbcDaoImpl getAllocationItemJdbcDaoImpl(JdbcTemplate jdbcTemplate) {\r
107                 AllocationItemJdbcDaoImpl allocationItemJdbcDaoImpl = new AllocationItemJdbcDaoImpl();\r
108                 allocationItemJdbcDaoImpl.setJdbcTemplate(jdbcTemplate);\r
109                 return allocationItemJdbcDaoImpl;\r
110         }\r
111         \r
112         @Bean\r
113         public ResourceLoadJdbcDaoImpl getResourceLoadJdbcDaoImpl(JdbcTemplate jdbcTemplate) {\r
114                 ResourceLoadJdbcDaoImpl resourceLoadJdbcDaoImpl = new ResourceLoadJdbcDaoImpl();\r
115                 resourceLoadJdbcDaoImpl.setJdbcTemplate(jdbcTemplate);\r
116                 return resourceLoadJdbcDaoImpl;\r
117         }\r
118         \r
119         \r
120         @Bean\r
121         public ResourceDaoImpl getResourceDaoImpl(ResourceJdbcDao resourceJdbcDao, AllocationItemJdbcDao allocationItemJdbcDao, ResourceLoadJdbcDao resourceLoadJdbcDao) {\r
122                 ResourceDaoImpl resourceDaoImpl = new ResourceDaoImpl();\r
123                 resourceDaoImpl.setAllocationItemJdbcDao(allocationItemJdbcDao);\r
124                 resourceDaoImpl.setResourceJdbcDao(resourceJdbcDao);\r
125                 resourceDaoImpl.setResourceLoadJdbcDao(resourceLoadJdbcDao);\r
126                 \r
127                 return resourceDaoImpl;\r
128         }\r
129         \r
130         @Bean\r
131         public ResourceManagerImpl getResourceManagerImpl(LockHelper lockHelper, ResourceDao resourceDao) {\r
132                 ResourceManagerImpl resourceManagerImpl = new ResourceManagerImpl();\r
133                 resourceManagerImpl.setLockHelper(lockHelper);\r
134                 resourceManagerImpl.setLockTimeout(600);\r
135                 resourceManagerImpl.setResourceDao(resourceDao);\r
136                 \r
137                 return resourceManagerImpl;\r
138         }\r
139         \r
140         @Bean\r
141         public ResourceRuleDaoImpl getResourceRuleDaoImpl(JdbcTemplate jdbcTemplate) {\r
142                 ResourceRuleDaoImpl resourceRuleDaoImpl = new ResourceRuleDaoImpl();\r
143                 resourceRuleDaoImpl.setJdbcTemplate(jdbcTemplate);\r
144                                 \r
145                 return resourceRuleDaoImpl;\r
146         }\r
147         \r
148         @Bean\r
149         public RangeRuleDaoImpl getRangeRuleDaoImpl(JdbcTemplate jdbcTemplate) {\r
150                 RangeRuleDaoImpl rangeRuleDaoImpl = new RangeRuleDaoImpl();\r
151                 rangeRuleDaoImpl.setJdbcTemplate(jdbcTemplate);\r
152                                 \r
153                 return rangeRuleDaoImpl;\r
154         }\r
155         \r
156         @Bean\r
157         public ResourceAllocator getResourceAllocator(ResourceManager resourceManager, EndPointAllocator endPointAllocator, SpeedUtil speedUtil) {\r
158                 ResourceAllocator resourceAllocator = new ResourceAllocator();\r
159                 resourceAllocator.setEndPointAllocator(endPointAllocator);\r
160                 resourceAllocator.setResourceManager(resourceManager);\r
161                 resourceAllocator.setSpeedUtil(speedUtil);\r
162                                 \r
163                 return resourceAllocator;\r
164         }\r
165         \r
166         @Bean\r
167         public SpeedUtil getSpeedUtil() {\r
168                 return new SpeedUtil();\r
169         }\r
170         \r
171         @Bean\r
172         public EndPointAllocatorImpl getEndPointAllocatorImpl(ResourceManager resourceManager, DbAllocationRule dbAllocationRule) {\r
173                 EndPointAllocatorImpl endPointAllocatorImpl = new EndPointAllocatorImpl();\r
174                 \r
175                 Map<String, List<AllocationRule>> allocationRuleMap = new HashMap<>();\r
176                 allocationRuleMap.put("DEFAULT", Arrays.asList(dbAllocationRule));\r
177                                 \r
178                 endPointAllocatorImpl.setResourceManager(resourceManager);\r
179                 endPointAllocatorImpl.setAllocationRuleMap(allocationRuleMap);\r
180 \r
181                 return endPointAllocatorImpl;\r
182         }\r
183         \r
184         @Bean\r
185         public DbAllocationRule getDbAllocationRule(ResourceRuleDao resourceRuleDao, RangeRuleDao rangeRuleDao) {\r
186                 DbAllocationRule dbAllocationRule = new DbAllocationRule();\r
187                 dbAllocationRule.setRangeRuleDao(rangeRuleDao);\r
188                 dbAllocationRule.setResourceRuleDao(resourceRuleDao);\r
189                 \r
190                 return dbAllocationRule;\r
191         }\r
192 }\r