2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * 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=========================================================
21 package org.openecomp.modelloader.service;
23 import org.openecomp.sdc.api.IDistributionClient;
24 import org.openecomp.sdc.api.results.IDistributionClientResult;
25 import org.openecomp.sdc.impl.DistributionClientFactory;
26 import org.openecomp.sdc.utils.DistributionActionResultEnum;
28 import org.openecomp.cl.api.Logger;
29 import org.openecomp.cl.eelf.LoggerFactory;
30 import org.openecomp.modelloader.config.ModelLoaderConfig;
31 import org.openecomp.modelloader.entity.model.ModelArtifactHandler;
32 import org.openecomp.modelloader.notification.EventCallback;
34 import java.io.IOException;
35 import java.util.Properties;
37 import javax.annotation.PostConstruct;
38 import javax.annotation.PreDestroy;
39 import javax.servlet.http.HttpServletRequest;
40 import javax.ws.rs.core.Response;
43 * Service class in charge of managing the negotiating model loading
44 * capabilities between AAI and an ASDC.
46 public class ModelLoaderService implements ModelLoaderInterface {
48 protected static String CONFIG_FILE = "model-loader.properties";
50 private IDistributionClient client;
51 private ModelLoaderConfig config;
53 static Logger logger = LoggerFactory.getInstance().getLogger(ModelLoaderService.class.getName());
56 * Responsible for loading configuration files and calling initialization.
59 protected void start() {
60 // Load model loader system configuration
61 logger.info(ModelLoaderMsgs.LOADING_CONFIGURATION);
62 Properties configProperties = new Properties();
64 configProperties.load(this.getClass().getClassLoader().getResourceAsStream(CONFIG_FILE));
65 } catch (IOException e) {
66 String errorMsg = "Failed to load configuration: " + e.getMessage();
67 logger.error(ModelLoaderMsgs.ASDC_CONNECTION_ERROR, errorMsg);
71 config = new ModelLoaderConfig(configProperties);
76 * Responsible for stopping the connection to the distribution client before
77 * the resource is destroyed.
80 protected void preShutdownOperations() {
81 logger.info(ModelLoaderMsgs.STOPPING_CLIENT);
88 * Responsible for loading configuration files, initializing model
89 * distribution clients, and starting them.
91 protected void init() {
92 // Initialize distribution client
93 logger.debug(ModelLoaderMsgs.INITIALIZING, "Initializing distribution client...");
94 client = DistributionClientFactory.createDistributionClient();
95 IDistributionClientResult initResult = client.init(config, new EventCallback(client, config));
96 if (initResult.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) {
97 String errorMsg = "Failed to initialize distribution client: "
98 + initResult.getDistributionMessageResult();
99 logger.error(ModelLoaderMsgs.ASDC_CONNECTION_ERROR, errorMsg);
103 // Start distribution client
104 logger.debug(ModelLoaderMsgs.INITIALIZING, "Starting distribution client...");
105 IDistributionClientResult startResult = client.start();
106 if (startResult.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) {
107 String errorMsg = "Failed to start distribution client: "
108 + startResult.getDistributionMessageResult();
109 logger.error(ModelLoaderMsgs.ASDC_CONNECTION_ERROR, errorMsg);
113 logger.debug(ModelLoaderMsgs.INITIALIZING,
114 "Succcessfully loaded service: " + this.getClass().getSimpleName());
118 * Shut down the process.
120 private void shutdown() {
121 preShutdownOperations();
123 // TODO: Find a better way to shut down the model loader.
125 // Give logs time to write to file
127 } catch (InterruptedException e) {
128 // Nothing we can do at this point
131 Runtime.getRuntime().halt(1);
135 * @see org.openecomp.modelloader.service.ModelLoaderInterface#loadModel(java.lang.String)
138 public Response loadModel(String modelid) {
139 Response response = Response.ok("{\"model_loaded\":\"" + modelid + "\"}").build();
145 * @see org.openecomp.modelloader.service.ModelLoaderInterface#saveModel(java.lang.String, java.lang.String)
148 public Response saveModel(String modelid, String modelname) {
149 Response response = Response.ok("{\"model_saved\":\"" + modelid + "-" + modelname + "\"}")
156 public Response ingestModel(String modelid, HttpServletRequest req, String payload)
160 if (config.getIngestSimulatorEnabled()) {
161 logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Received test artifact");
163 ModelArtifactHandler handler = new ModelArtifactHandler(config);
164 handler.loadModelTest(payload.getBytes());
166 response = Response.ok().build();
168 logger.debug("Simulation interface disabled");
169 response = Response.serverError().build();