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