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);
89 public void finalize() {
90 preShutdownOperations();
94 * Responsible for stopping the connection to the distribution client before
95 * the resource is destroyed.
97 protected void preShutdownOperations() {
98 logger.info(ModelLoaderMsgs.STOPPING_CLIENT);
105 * Responsible for loading configuration files, initializing model
106 * distribution clients, and starting them.
108 protected void init() {
109 // Initialize distribution client
110 logger.debug(ModelLoaderMsgs.INITIALIZING, "Initializing distribution client...");
111 client = DistributionClientFactory.createDistributionClient();
112 EventCallback callback = new EventCallback(client, config);
114 IDistributionClientResult initResult = client.init(config, callback);
116 if (initResult.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) {
117 String errorMsg = "Failed to initialize distribution client: "
118 + initResult.getDistributionMessageResult();
119 logger.error(ModelLoaderMsgs.ASDC_CONNECTION_ERROR, errorMsg);
121 // Kick off a timer to retry the SDC connection
123 TimerTask task = new SdcConnectionJob(client, config, callback, timer);
124 timer.schedule(task, new Date(), 60000);
127 // Start distribution client
128 logger.debug(ModelLoaderMsgs.INITIALIZING, "Starting distribution client...");
129 IDistributionClientResult startResult = client.start();
130 if (startResult.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) {
131 String errorMsg = "Failed to start distribution client: "
132 + startResult.getDistributionMessageResult();
133 logger.error(ModelLoaderMsgs.ASDC_CONNECTION_ERROR, errorMsg);
135 // Kick off a timer to retry the SDC connection
137 TimerTask task = new SdcConnectionJob(client, config, callback, timer);
138 timer.schedule(task, new Date(), 60000);
141 logger.info(ModelLoaderMsgs.INITIALIZING, "Connection to SDC established");
147 * Shut down the process.
149 private void shutdown() {
150 preShutdownOperations();
152 // TODO: Find a better way to shut down the model loader.
154 // Give logs time to write to file
156 } catch (InterruptedException e) {
157 // Nothing we can do at this point
160 Runtime.getRuntime().halt(1);
164 * @see org.openecomp.modelloader.service.ModelLoaderInterface#loadModel(java.lang.String)
167 public Response loadModel(String modelid) {
168 Response response = Response.ok("{\"model_loaded\":\"" + modelid + "\"}").build();
174 * @see org.openecomp.modelloader.service.ModelLoaderInterface#saveModel(java.lang.String, java.lang.String)
177 public Response saveModel(String modelid, String modelname) {
178 Response response = Response.ok("{\"model_saved\":\"" + modelid + "-" + modelname + "\"}")
185 public Response ingestModel(String modelid, HttpServletRequest req, String payload)
189 if (config.getIngestSimulatorEnabled()) {
190 logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Received test artifact");
192 ModelArtifactHandler handler = new ModelArtifactHandler(config);
193 handler.loadModelTest(payload.getBytes());
195 response = Response.ok().build();
197 logger.debug("Simulation interface disabled");
198 response = Response.serverError().build();