Support env variables in svclogic.properties
[ccsdk/apps.git] / services / src / main / java / org / onap / ccsdk / apps / services / SvcLogicFactory.java
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * ONAP - CCSDK\r
4  * ================================================================================\r
5  * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * \r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ============LICENSE_END=========================================================\r
19  */\r
20 \r
21 package org.onap.ccsdk.apps.services;\r
22 \r
23 import java.io.FileInputStream;\r
24 import java.io.IOException;\r
25 import java.util.List;\r
26 import java.util.Properties;\r
27 \r
28 import org.onap.ccsdk.sli.adaptors.aai.AAIService;\r
29 import org.onap.ccsdk.sli.adaptors.aai.AAIServiceProvider;\r
30 import org.onap.ccsdk.sli.adaptors.messagerouter.publisher.api.PublisherApi;\r
31 import org.onap.ccsdk.sli.adaptors.messagerouter.publisher.provider.impl.PublisherApiImpl;\r
32 import org.onap.ccsdk.sli.adaptors.netbox.api.NetboxClient;\r
33 import org.onap.ccsdk.sli.adaptors.netbox.impl.NetboxClientImpl;\r
34 import org.onap.ccsdk.sli.adaptors.netbox.impl.NetboxHttpClient;\r
35 import org.onap.ccsdk.sli.adaptors.netbox.property.NetboxProperties;\r
36 import org.onap.ccsdk.sli.adaptors.resource.mdsal.ConfigResource;\r
37 import org.onap.ccsdk.sli.adaptors.resource.mdsal.MdsalResourcePropertiesProviderImpl;\r
38 import org.onap.ccsdk.sli.adaptors.resource.mdsal.OperationalResource;\r
39 import org.onap.ccsdk.sli.adaptors.resource.sql.SqlResource;\r
40 import org.onap.ccsdk.sli.core.dblib.DBLIBResourceProvider;\r
41 import org.onap.ccsdk.sli.core.dblib.DBResourceManager;\r
42 import org.onap.ccsdk.sli.core.dblib.DbLibService;\r
43 import org.onap.ccsdk.sli.core.sli.ConfigurationException;\r
44 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;\r
45 import org.onap.ccsdk.sli.core.sli.SvcLogicLoader;\r
46 import org.onap.ccsdk.sli.core.sli.SvcLogicRecorder;\r
47 import org.onap.ccsdk.sli.core.sli.SvcLogicResource;\r
48 import org.onap.ccsdk.sli.core.sli.SvcLogicStore;\r
49 import org.onap.ccsdk.sli.core.sli.SvcLogicStoreFactory;\r
50 import org.onap.ccsdk.sli.core.sli.provider.base.HashMapResolver;\r
51 import org.onap.ccsdk.sli.core.sli.provider.base.SvcLogicPropertiesProvider;\r
52 import org.onap.ccsdk.sli.core.sli.provider.base.SvcLogicServiceBase;\r
53 import org.onap.ccsdk.sli.core.sli.provider.base.SvcLogicServiceImplBase;\r
54 import org.onap.ccsdk.sli.core.sli.recording.Slf4jRecorder;\r
55 import org.onap.ccsdk.sli.core.slipluginutils.SliPluginUtils;\r
56 import org.onap.ccsdk.sli.core.slipluginutils.SliStringUtils;\r
57 import org.onap.ccsdk.sli.core.utils.common.EnvProperties;\r
58 import org.onap.ccsdk.sli.plugins.prop.PropertiesNode;\r
59 import org.onap.ccsdk.sli.plugins.restapicall.RestapiCallNode;\r
60 import org.slf4j.Logger;\r
61 import org.slf4j.LoggerFactory;\r
62 import org.springframework.beans.factory.annotation.Autowired;\r
63 import org.springframework.context.annotation.Bean;\r
64 import org.springframework.context.annotation.Configuration;\r
65 import org.springframework.stereotype.Service;\r
66 \r
67 @Configuration\r
68 @Service\r
69 public class SvcLogicFactory {\r
70   private static final Logger log = LoggerFactory.getLogger(SvcLogicFactory.class);\r
71 \r
72   @Autowired\r
73   List<SvcLogicRecorder> recorders;\r
74 \r
75   @Autowired\r
76   List<SvcLogicJavaPlugin> plugins;\r
77 \r
78   @Autowired\r
79   List<SvcLogicResource> svcLogicResources;\r
80 \r
81   @Bean\r
82   public SvcLogicStore getStore() throws Exception {\r
83     SvcLogicPropertiesProvider propProvider = new SvcLogicPropertiesProvider() {\r
84 \r
85       @Override\r
86       public Properties getProperties() {\r
87         Properties props = new Properties();\r
88 \r
89 \r
90         String propPath = System.getProperty("serviceLogicProperties", "");\r
91 \r
92         if ("".equals(propPath)) {\r
93           propPath = System.getenv("SVCLOGIC_PROPERTIES");\r
94         }\r
95 \r
96 \r
97         if ((propPath == null) || propPath.length() == 0) {\r
98           propPath = "src/main/resources/svclogic.properties";\r
99         }\r
100         System.out.println(propPath);\r
101         try (FileInputStream fileInputStream = new FileInputStream(propPath)) {\r
102           props = new EnvProperties();\r
103           props.load(fileInputStream);\r
104         } catch (final IOException e) {\r
105           log.error("Failed to load properties for file: {}", propPath,\r
106               new ConfigurationException("Failed to load properties for file: " + propPath, e));\r
107         }\r
108         return props;\r
109       }\r
110     };\r
111     SvcLogicStore store = SvcLogicStoreFactory.getSvcLogicStore(propProvider.getProperties());\r
112     return store;\r
113   }\r
114 \r
115   @Bean\r
116   public SvcLogicLoader createLoader() throws Exception {\r
117     String serviceLogicDirectory = System.getProperty("serviceLogicDirectory");\r
118     if (serviceLogicDirectory == null) {\r
119       serviceLogicDirectory = "src/main/resources";\r
120     }\r
121 \r
122     System.out.println("serviceLogicDirectory is " + serviceLogicDirectory);\r
123     SvcLogicLoader loader = new SvcLogicLoader(serviceLogicDirectory, getStore());\r
124 \r
125     try {\r
126       loader.loadAndActivate();\r
127     } catch (IOException e) {\r
128       log.error("Cannot load directed graphs", e);\r
129     }\r
130     return loader;\r
131   }\r
132 \r
133   @Bean\r
134   public SvcLogicServiceBase createService() throws Exception {\r
135     HashMapResolver resolver = new HashMapResolver();\r
136     for (SvcLogicRecorder recorder : recorders) {\r
137       resolver.addSvcLogicRecorder(recorder.getClass().getName(), recorder);\r
138 \r
139     }\r
140     for (SvcLogicJavaPlugin plugin : plugins) {\r
141       resolver.addSvcLogicSvcLogicJavaPlugin(plugin.getClass().getName(), plugin);\r
142 \r
143     }\r
144     for (SvcLogicResource svcLogicResource : svcLogicResources) {\r
145       resolver.addSvcLogicResource(svcLogicResource.getClass().getName(), svcLogicResource);\r
146     }\r
147 \r
148     return new SvcLogicServiceImplBase(getStore(), resolver);\r
149   }\r
150 \r
151   @Bean\r
152   public Slf4jRecorder slf4jRecorderNode() {\r
153     return new Slf4jRecorder();\r
154   }\r
155 \r
156   // Beans from sli/core\r
157 \r
158   @Bean\r
159   public SliPluginUtils sliPluginUtil() {\r
160     return new SliPluginUtils();\r
161   }\r
162 \r
163   @Bean\r
164   public SliStringUtils sliStringUtils() {\r
165     return new SliStringUtils();\r
166   }\r
167 \r
168   // Beans from sli/adaptors\r
169 \r
170   @Bean AAIService aaiService() {\r
171     return new AAIService(new AAIServiceProvider());\r
172   }\r
173   \r
174   @Bean\r
175   public ConfigResource configResource() {\r
176     return new ConfigResource(new MdsalResourcePropertiesProviderImpl());\r
177   }\r
178 \r
179   @Bean\r
180   public OperationalResource operationalResource() {\r
181     return new OperationalResource(new MdsalResourcePropertiesProviderImpl());\r
182   }\r
183 \r
184   @Bean \r
185   public PublisherApi publisherApi() {\r
186     return new PublisherApiImpl();\r
187   }\r
188   \r
189   \r
190   @Bean \r
191   public NetboxClient netboxClient() {\r
192     return new NetboxClientImpl();\r
193   }\r
194   \r
195   \r
196   @Bean\r
197   public SqlResource sqlResource() {\r
198     return new SqlResource();\r
199   }\r
200 \r
201   \r
202   @Bean\r
203   public RestapiCallNode restapiCallNode() {\r
204       return new RestapiCallNode();\r
205   }\r
206   \r
207   @Bean\r
208   public PropertiesNode propertiesNode() {\r
209       return new PropertiesNode();\r
210   }\r
211 \r
212   \r
213 \r
214 }\r