118cd1813c8819937d6d6a06fac84f13b1fc43ca
[aai/model-loader.git] / src / main / java / org / openecomp / modelloader / service / ModelLoaderService.java
1 /**\r
2  * ============LICENSE_START=======================================================\r
3  * Model Loader\r
4  * ================================================================================\r
5  * Copyright © 2017 AT&T Intellectual Property.\r
6  * Copyright © 2017 Amdocs\r
7  * All rights reserved.\r
8  * ================================================================================\r
9  * Licensed under the Apache License, Version 2.0 (the "License");\r
10  * you may not use this file except in compliance with the License.\r
11  * You may obtain a copy of the License at\r
12  * http://www.apache.org/licenses/LICENSE-2.0\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  * ECOMP and OpenECOMP are trademarks\r
21  * and service marks of AT&T Intellectual Property.\r
22  */\r
23 package org.openecomp.modelloader.service;\r
24 \r
25 import org.openecomp.sdc.api.IDistributionClient;\r
26 import org.openecomp.sdc.api.results.IDistributionClientResult;\r
27 import org.openecomp.sdc.impl.DistributionClientFactory;\r
28 import org.openecomp.sdc.utils.DistributionActionResultEnum;\r
29 \r
30 import org.openecomp.cl.api.Logger;\r
31 import org.openecomp.cl.eelf.LoggerFactory;\r
32 import org.openecomp.modelloader.config.ModelLoaderConfig;\r
33 import org.openecomp.modelloader.entity.model.ModelArtifactHandler;\r
34 import org.openecomp.modelloader.notification.EventCallback;\r
35 \r
36 import java.io.FileInputStream;\r
37 import java.io.IOException;\r
38 import java.util.Properties;\r
39 \r
40 import javax.annotation.PreDestroy;\r
41 import javax.servlet.http.HttpServletRequest;\r
42 import javax.ws.rs.core.Response;\r
43 \r
44 /**\r
45  * Service class in charge of managing the negotiating model loading\r
46  * capabilities between AAI and an ASDC.\r
47  */\r
48 public class ModelLoaderService implements ModelLoaderInterface {\r
49         \r
50         protected static final String FILESEP = (System.getProperty("file.separator") == null) ? "/"\r
51             : System.getProperty("file.separator");\r
52 \r
53         protected static final String CONFIG_DIR = System.getProperty("CONFIG_HOME") + FILESEP;\r
54         protected static final String CONFIG_AUTH_LOCATION = CONFIG_DIR + "auth" + FILESEP;\r
55         protected static final String CONFIG_FILE = CONFIG_DIR + "model-loader.properties";\r
56 \r
57         private IDistributionClient client;\r
58         private ModelLoaderConfig config;\r
59 \r
60         static Logger logger = LoggerFactory.getInstance().getLogger(ModelLoaderService.class.getName());\r
61 \r
62         /**\r
63          * Responsible for loading configuration files and calling initialization.\r
64          */\r
65         public ModelLoaderService() {\r
66                 start();\r
67         }\r
68 \r
69         protected void start() {\r
70                 // Load model loader system configuration\r
71                 logger.info(ModelLoaderMsgs.LOADING_CONFIGURATION);\r
72                 Properties configProperties = new Properties();\r
73                 try {\r
74                         configProperties.load(new FileInputStream(CONFIG_FILE));\r
75                 } catch (IOException e) {\r
76                         String errorMsg = "Failed to load configuration: " + e.getMessage();\r
77                         logger.error(ModelLoaderMsgs.ASDC_CONNECTION_ERROR, errorMsg);\r
78                         shutdown();\r
79                 }\r
80 \r
81                 config = new ModelLoaderConfig(configProperties, CONFIG_AUTH_LOCATION);\r
82                 init();\r
83         }\r
84 \r
85         @Override\r
86         public void finalize() {\r
87                 preShutdownOperations();\r
88         }\r
89         \r
90         /**\r
91          * Responsible for stopping the connection to the distribution client before\r
92          * the resource is destroyed.\r
93          */\r
94         protected void preShutdownOperations() {\r
95                 logger.info(ModelLoaderMsgs.STOPPING_CLIENT);\r
96                 if (client != null) {\r
97                         client.stop();\r
98                 }\r
99         }\r
100 \r
101         /**\r
102          * Responsible for loading configuration files, initializing model\r
103          * distribution clients, and starting them.\r
104          */\r
105         protected void init() {\r
106                 // Initialize distribution client\r
107                 logger.debug(ModelLoaderMsgs.INITIALIZING, "Initializing distribution client...");\r
108                 client = DistributionClientFactory.createDistributionClient();\r
109                 IDistributionClientResult initResult = client.init(config, new EventCallback(client, config));\r
110                 if (initResult.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) {\r
111                         String errorMsg = "Failed to initialize distribution client: "\r
112                                         + initResult.getDistributionMessageResult();\r
113                         logger.error(ModelLoaderMsgs.ASDC_CONNECTION_ERROR, errorMsg);\r
114                         shutdown();\r
115                 }\r
116 \r
117                 // Start distribution client\r
118                 logger.debug(ModelLoaderMsgs.INITIALIZING, "Starting distribution client...");\r
119                 IDistributionClientResult startResult = client.start();\r
120                 if (startResult.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) {\r
121                         String errorMsg = "Failed to start distribution client: "\r
122                                         + startResult.getDistributionMessageResult();\r
123                         logger.error(ModelLoaderMsgs.ASDC_CONNECTION_ERROR, errorMsg);\r
124                         shutdown();\r
125                 }\r
126 \r
127                 logger.debug(ModelLoaderMsgs.INITIALIZING,\r
128                                 "Succcessfully loaded service: " + this.getClass().getSimpleName());\r
129         }\r
130 \r
131         /**\r
132          * Shut down the process.\r
133          */\r
134         private void shutdown() {\r
135                 preShutdownOperations();\r
136 \r
137                 // TODO: Find a better way to shut down the model loader.\r
138                 try {\r
139                         // Give logs time to write to file\r
140                         Thread.sleep(2000);\r
141                 } catch (InterruptedException e) {\r
142                         // Nothing we can do at this point\r
143                 }\r
144 \r
145                 Runtime.getRuntime().halt(1);\r
146         }\r
147 \r
148         /** (non-Javadoc)\r
149          * @see org.openecomp.modelloader.service.ModelLoaderInterface#loadModel(java.lang.String)\r
150          */\r
151         @Override\r
152         public Response loadModel(String modelid) {\r
153                 Response response = Response.ok("{\"model_loaded\":\"" + modelid + "\"}").build();\r
154 \r
155                 return response;\r
156         }\r
157 \r
158         /** (non-Javadoc)\r
159          * @see org.openecomp.modelloader.service.ModelLoaderInterface#saveModel(java.lang.String, java.lang.String)\r
160          */\r
161         @Override\r
162         public Response saveModel(String modelid, String modelname) {\r
163                 Response response = Response.ok("{\"model_saved\":\"" + modelid + "-" + modelname + "\"}")\r
164                                 .build();\r
165 \r
166                 return response;\r
167         }\r
168 \r
169         @Override\r
170         public Response ingestModel(String modelid, HttpServletRequest req, String payload)\r
171                         throws IOException {\r
172                 Response response;\r
173 \r
174                 if (config.getIngestSimulatorEnabled()) {\r
175                         logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Received test artifact");\r
176 \r
177                         ModelArtifactHandler handler = new ModelArtifactHandler(config);\r
178                         handler.loadModelTest(payload.getBytes());\r
179 \r
180                         response = Response.ok().build();\r
181                 } else {\r
182                         logger.debug("Simulation interface disabled");\r
183                         response = Response.serverError().build();\r
184                 }\r
185 \r
186                 return response;\r
187         }\r
188 }\r