2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 * ECOMP and OpenECOMP are trademarks
21 * and service marks of AT&T Intellectual Property.
23 package org.openecomp.modelloader.service;
25 import org.openecomp.sdc.api.IDistributionClient;
26 import org.openecomp.sdc.api.results.IDistributionClientResult;
27 import org.openecomp.sdc.impl.DistributionClientFactory;
28 import org.openecomp.sdc.utils.DistributionActionResultEnum;
30 import org.openecomp.cl.api.Logger;
31 import org.openecomp.cl.eelf.LoggerFactory;
32 import org.openecomp.modelloader.config.ModelLoaderConfig;
33 import org.openecomp.modelloader.entity.model.ModelArtifactHandler;
34 import org.openecomp.modelloader.notification.EventCallback;
36 import java.io.FileInputStream;
37 import java.io.IOException;
38 import java.util.Date;
39 import java.util.Properties;
40 import java.util.Timer;
41 import java.util.TimerTask;
43 import javax.servlet.http.HttpServletRequest;
44 import javax.ws.rs.core.Response;
47 * Service class in charge of managing the negotiating model loading
48 * capabilities between AAI and an ASDC.
50 public class ModelLoaderService implements ModelLoaderInterface {
52 protected static final String FILESEP = (System.getProperty("file.separator") == null) ? "/"
53 : System.getProperty("file.separator");
55 protected static final String CONFIG_DIR = System.getProperty("CONFIG_HOME") + FILESEP;
56 protected static final String CONFIG_AUTH_LOCATION = CONFIG_DIR + "auth" + FILESEP;
57 protected static final String CONFIG_FILE = CONFIG_DIR + "model-loader.properties";
59 private IDistributionClient client;
60 private ModelLoaderConfig config;
61 private Timer timer = null;
63 static Logger logger = LoggerFactory.getInstance().getLogger(ModelLoaderService.class.getName());
66 * Responsible for loading configuration files and calling initialization.
68 public ModelLoaderService() {
72 protected void start() {
73 // Load model loader system configuration
74 logger.info(ModelLoaderMsgs.LOADING_CONFIGURATION);
75 Properties configProperties = new Properties();
77 configProperties.load(new FileInputStream(CONFIG_FILE));
78 } catch (IOException e) {
79 String errorMsg = "Failed to load configuration: " + e.getMessage();
80 logger.error(ModelLoaderMsgs.ASDC_CONNECTION_ERROR, errorMsg);
84 config = new ModelLoaderConfig(configProperties, CONFIG_AUTH_LOCATION);
87 Runtime.getRuntime().addShutdownHook(new Thread(new Runnable(){
89 preShutdownOperations();
95 * Responsible for stopping the connection to the distribution client before
96 * the resource is destroyed.
98 protected void preShutdownOperations() {
99 logger.info(ModelLoaderMsgs.STOPPING_CLIENT);
100 if (client != null) {
106 * Responsible for loading configuration files, initializing model
107 * distribution clients, and starting them.
109 protected void init() {
110 // Initialize distribution client
111 logger.debug(ModelLoaderMsgs.INITIALIZING, "Initializing distribution client...");
112 client = DistributionClientFactory.createDistributionClient();
113 EventCallback callback = new EventCallback(client, config);
115 IDistributionClientResult initResult = client.init(config, callback);
117 if (initResult.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) {
118 String errorMsg = "Failed to initialize distribution client: "
119 + initResult.getDistributionMessageResult();
120 logger.error(ModelLoaderMsgs.ASDC_CONNECTION_ERROR, errorMsg);
122 // Kick off a timer to retry the SDC connection
124 TimerTask task = new SdcConnectionJob(client, config, callback, timer);
125 timer.schedule(task, new Date(), 60000);
128 // Start distribution client
129 logger.debug(ModelLoaderMsgs.INITIALIZING, "Starting distribution client...");
130 IDistributionClientResult startResult = client.start();
131 if (startResult.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) {
132 String errorMsg = "Failed to start distribution client: "
133 + startResult.getDistributionMessageResult();
134 logger.error(ModelLoaderMsgs.ASDC_CONNECTION_ERROR, errorMsg);
136 // Kick off a timer to retry the SDC connection
138 TimerTask task = new SdcConnectionJob(client, config, callback, timer);
139 timer.schedule(task, new Date(), 60000);
142 logger.info(ModelLoaderMsgs.INITIALIZING, "Connection to SDC established");
148 * Shut down the process.
150 private void shutdown() {
151 preShutdownOperations();
153 // TODO: Find a better way to shut down the model loader.
155 // Give logs time to write to file
157 } catch (InterruptedException e) {
158 // Nothing we can do at this point
161 Runtime.getRuntime().halt(1);
165 * @see org.openecomp.modelloader.service.ModelLoaderInterface#loadModel(java.lang.String)
168 public Response loadModel(String modelid) {
169 Response response = Response.ok("{\"model_loaded\":\"" + modelid + "\"}").build();
175 * @see org.openecomp.modelloader.service.ModelLoaderInterface#saveModel(java.lang.String, java.lang.String)
178 public Response saveModel(String modelid, String modelname) {
179 Response response = Response.ok("{\"model_saved\":\"" + modelid + "-" + modelname + "\"}")
186 public Response ingestModel(String modelid, HttpServletRequest req, String payload)
190 if (config.getIngestSimulatorEnabled()) {
191 logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Received test artifact");
193 ModelArtifactHandler handler = new ModelArtifactHandler(config);
194 handler.loadModelTest(payload.getBytes());
196 response = Response.ok().build();
198 logger.debug("Simulation interface disabled");
199 response = Response.serverError().build();