Export SvcLogicFactory as service
[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 import org.onap.ccsdk.sli.core.sli.ConfigurationException;\r
28 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;\r
29 import org.onap.ccsdk.sli.core.sli.SvcLogicLoader;\r
30 import org.onap.ccsdk.sli.core.sli.SvcLogicRecorder;\r
31 import org.onap.ccsdk.sli.core.sli.SvcLogicStore;\r
32 import org.onap.ccsdk.sli.core.sli.SvcLogicStoreFactory;\r
33 import org.onap.ccsdk.sli.core.sli.provider.base.HashMapResolver;\r
34 import org.onap.ccsdk.sli.core.sli.provider.base.SvcLogicPropertiesProvider;\r
35 import org.onap.ccsdk.sli.core.sli.provider.base.SvcLogicServiceBase;\r
36 import org.onap.ccsdk.sli.core.sli.provider.base.SvcLogicServiceImplBase;\r
37 import org.onap.ccsdk.sli.core.sli.recording.Slf4jRecorder;\r
38 import org.onap.ccsdk.sli.core.slipluginutils.SliPluginUtils;\r
39 import org.onap.ccsdk.sli.core.slipluginutils.SliStringUtils;\r
40 import org.onap.ccsdk.sli.plugins.prop.PropertiesNode;\r
41 import org.onap.ccsdk.sli.plugins.restapicall.RestapiCallNode;\r
42 import org.slf4j.Logger;\r
43 import org.slf4j.LoggerFactory;\r
44 import org.springframework.beans.factory.annotation.Autowired;\r
45 import org.springframework.context.annotation.Bean;\r
46 import org.springframework.context.annotation.Configuration;\r
47 import org.springframework.stereotype.Service;\r
48 \r
49 @Configuration\r
50 @Service\r
51 public class SvcLogicFactory {\r
52   private static final Logger log = LoggerFactory.getLogger(SvcLogicFactory.class);\r
53 \r
54   @Autowired\r
55   List<SvcLogicRecorder> recorders;\r
56 \r
57   @Autowired\r
58   List<SvcLogicJavaPlugin> plugins;\r
59 \r
60   @Bean\r
61   public SvcLogicStore getStore() throws Exception {\r
62     SvcLogicPropertiesProvider propProvider = new SvcLogicPropertiesProvider() {\r
63 \r
64       @Override\r
65       public Properties getProperties() {\r
66         Properties props = new Properties();\r
67 \r
68 \r
69         String propPath = System.getProperty("serviceLogicProperties", "");\r
70 \r
71         if ("".equals(propPath)) {\r
72           propPath = System.getenv("SVCLOGIC_PROPERTIES");\r
73         }\r
74 \r
75 \r
76         if ((propPath == null) || propPath.length() == 0) {\r
77           propPath = "src/main/resources/svclogic.properties";\r
78         }\r
79         System.out.println(propPath);\r
80         try (FileInputStream fileInputStream = new FileInputStream(propPath)) {\r
81           props = new Properties();\r
82           props.load(fileInputStream);\r
83         } catch (final IOException e) {\r
84           log.error("Failed to load properties for file: {}", propPath,\r
85               new ConfigurationException("Failed to load properties for file: " + propPath, e));\r
86         }\r
87         return props;\r
88       }\r
89     };\r
90     SvcLogicStore store = SvcLogicStoreFactory.getSvcLogicStore(propProvider.getProperties());\r
91     return store;\r
92   }\r
93 \r
94   @Bean\r
95   public SvcLogicLoader createLoader() throws Exception {\r
96     String serviceLogicDirectory = System.getProperty("serviceLogicDirectory");\r
97     if (serviceLogicDirectory == null) {\r
98       serviceLogicDirectory = "src/main/resources";\r
99     }\r
100 \r
101     System.out.println("serviceLogicDirectory is " + serviceLogicDirectory);\r
102     SvcLogicLoader loader = new SvcLogicLoader(serviceLogicDirectory, getStore());\r
103 \r
104     try {\r
105       loader.loadAndActivate();\r
106     } catch (IOException e) {\r
107       log.error("Cannot load directed graphs", e);\r
108     }\r
109     return loader;\r
110   }\r
111 \r
112   @Bean\r
113   public SvcLogicServiceBase createService() throws Exception {\r
114     HashMapResolver resolver = new HashMapResolver();\r
115     for (SvcLogicRecorder recorder : recorders) {\r
116       resolver.addSvcLogicRecorder(recorder.getClass().getName(), recorder);\r
117 \r
118     }\r
119     for (SvcLogicJavaPlugin plugin : plugins) {\r
120       resolver.addSvcLogicSvcLogicJavaPlugin(plugin.getClass().getName(), plugin);\r
121 \r
122     }\r
123     return new SvcLogicServiceImplBase(getStore(), resolver);\r
124   }\r
125 \r
126   @Bean\r
127   public Slf4jRecorder slf4jRecorderNode() {\r
128     return new Slf4jRecorder();\r
129   }\r
130 \r
131   @Bean\r
132   public SliPluginUtils sliPluginUtil() {\r
133     return new SliPluginUtils();\r
134   }\r
135 \r
136   @Bean\r
137   public SliStringUtils sliStringUtils() {\r
138     return new SliStringUtils();\r
139   }\r
140   \r
141   @Bean\r
142   public RestapiCallNode restapiCallNode() {\r
143       return new RestapiCallNode();\r
144   }\r
145   \r
146   @Bean\r
147   public PropertiesNode propertiesNode() {\r
148       return new PropertiesNode();\r
149   }\r
150 \r
151 }\r