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
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
22 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
24 package org.openecomp.modelloader.service;
26 import org.openecomp.sdc.api.IDistributionClient;
27 import org.openecomp.sdc.api.results.IDistributionClientResult;
28 import org.openecomp.sdc.impl.DistributionClientFactory;
29 import org.openecomp.sdc.utils.DistributionActionResultEnum;
31 import org.openecomp.cl.api.Logger;
32 import org.openecomp.cl.eelf.LoggerFactory;
33 import org.openecomp.modelloader.config.ModelLoaderConfig;
34 import org.openecomp.modelloader.entity.model.ModelArtifactHandler;
35 import org.openecomp.modelloader.notification.EventCallback;
37 import java.io.FileInputStream;
38 import java.io.IOException;
39 import java.util.Date;
40 import java.util.Properties;
41 import java.util.Timer;
42 import java.util.TimerTask;
44 import javax.servlet.http.HttpServletRequest;
45 import javax.ws.rs.core.Response;
48 * Service class in charge of managing the negotiating model loading
49 * capabilities between AAI and an ASDC.
51 public class ModelLoaderService implements ModelLoaderInterface {
53 protected static final String FILESEP = (System.getProperty("file.separator") == null) ? "/"
54 : System.getProperty("file.separator");
56 protected static final String CONFIG_DIR = System.getProperty("CONFIG_HOME") + FILESEP;
57 protected static final String CONFIG_AUTH_LOCATION = CONFIG_DIR + "auth" + FILESEP;
58 protected static final String CONFIG_FILE = CONFIG_DIR + "model-loader.properties";
60 private IDistributionClient client;
61 private ModelLoaderConfig config;
62 private Timer timer = null;
64 static Logger logger = LoggerFactory.getInstance().getLogger(ModelLoaderService.class.getName());
67 * Responsible for loading configuration files and calling initialization.
69 public ModelLoaderService() {
73 protected void start() {
74 // Load model loader system configuration
75 logger.info(ModelLoaderMsgs.LOADING_CONFIGURATION);
76 Properties configProperties = new Properties();
78 configProperties.load(new FileInputStream(CONFIG_FILE));
79 } catch (IOException e) {
80 String errorMsg = "Failed to load configuration: " + e.getMessage();
81 logger.error(ModelLoaderMsgs.ASDC_CONNECTION_ERROR, errorMsg);
85 config = new ModelLoaderConfig(configProperties, CONFIG_AUTH_LOCATION);
88 Runtime.getRuntime().addShutdownHook(new Thread(new Runnable(){
90 preShutdownOperations();
96 * Responsible for stopping the connection to the distribution client before
97 * the resource is destroyed.
99 protected void preShutdownOperations() {
100 logger.info(ModelLoaderMsgs.STOPPING_CLIENT);
101 if (client != null) {
107 * Responsible for loading configuration files, initializing model
108 * distribution clients, and starting them.
110 protected void init() {
111 // Initialize distribution client
112 logger.debug(ModelLoaderMsgs.INITIALIZING, "Initializing distribution client...");
113 client = DistributionClientFactory.createDistributionClient();
114 EventCallback callback = new EventCallback(client, config);
116 IDistributionClientResult initResult = client.init(config, callback);
118 if (initResult.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) {
119 String errorMsg = "Failed to initialize distribution client: "
120 + initResult.getDistributionMessageResult();
121 logger.error(ModelLoaderMsgs.ASDC_CONNECTION_ERROR, errorMsg);
123 // Kick off a timer to retry the SDC connection
125 TimerTask task = new SdcConnectionJob(client, config, callback, timer);
126 timer.schedule(task, new Date(), 60000);
129 // Start distribution client
130 logger.debug(ModelLoaderMsgs.INITIALIZING, "Starting distribution client...");
131 IDistributionClientResult startResult = client.start();
132 if (startResult.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) {
133 String errorMsg = "Failed to start distribution client: "
134 + startResult.getDistributionMessageResult();
135 logger.error(ModelLoaderMsgs.ASDC_CONNECTION_ERROR, errorMsg);
137 // Kick off a timer to retry the SDC connection
139 TimerTask task = new SdcConnectionJob(client, config, callback, timer);
140 timer.schedule(task, new Date(), 60000);
143 logger.info(ModelLoaderMsgs.INITIALIZING, "Connection to SDC established");
149 * Shut down the process.
151 private void shutdown() {
152 preShutdownOperations();
154 // TODO: Find a better way to shut down the model loader.
156 // Give logs time to write to file
158 } catch (InterruptedException e) {
159 // Nothing we can do at this point
162 Runtime.getRuntime().halt(1);
166 * @see org.openecomp.modelloader.service.ModelLoaderInterface#loadModel(java.lang.String)
169 public Response loadModel(String modelid) {
170 Response response = Response.ok("{\"model_loaded\":\"" + modelid + "\"}").build();
176 * @see org.openecomp.modelloader.service.ModelLoaderInterface#saveModel(java.lang.String, java.lang.String)
179 public Response saveModel(String modelid, String modelname) {
180 Response response = Response.ok("{\"model_saved\":\"" + modelid + "-" + modelname + "\"}")
187 public Response ingestModel(String modelid, HttpServletRequest req, String payload)
191 if (config.getIngestSimulatorEnabled()) {
192 logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Received test artifact");
194 ModelArtifactHandler handler = new ModelArtifactHandler(config);
195 handler.loadModelTest(payload.getBytes());
197 response = Response.ok().build();
199 logger.debug("Simulation interface disabled");
200 response = Response.serverError().build();