2 * ============LICENSE_START=======================================================
\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
20 * ECOMP and OpenECOMP are trademarks
\r
21 * and service marks of AT&T Intellectual Property.
\r
23 package org.openecomp.modelloader.service;
\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
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
36 import java.io.FileInputStream;
\r
37 import java.io.IOException;
\r
38 import java.util.Properties;
\r
40 import javax.annotation.PreDestroy;
\r
41 import javax.servlet.http.HttpServletRequest;
\r
42 import javax.ws.rs.core.Response;
\r
45 * Service class in charge of managing the negotiating model loading
\r
46 * capabilities between AAI and an ASDC.
\r
48 public class ModelLoaderService implements ModelLoaderInterface {
\r
50 protected static final String FILESEP = (System.getProperty("file.separator") == null) ? "/"
\r
51 : System.getProperty("file.separator");
\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
57 private IDistributionClient client;
\r
58 private ModelLoaderConfig config;
\r
60 static Logger logger = LoggerFactory.getInstance().getLogger(ModelLoaderService.class.getName());
\r
63 * Responsible for loading configuration files and calling initialization.
\r
65 public ModelLoaderService() {
\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
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
81 config = new ModelLoaderConfig(configProperties, CONFIG_AUTH_LOCATION);
\r
86 public void finalize() {
\r
87 preShutdownOperations();
\r
91 * Responsible for stopping the connection to the distribution client before
\r
92 * the resource is destroyed.
\r
94 protected void preShutdownOperations() {
\r
95 logger.info(ModelLoaderMsgs.STOPPING_CLIENT);
\r
96 if (client != null) {
\r
102 * Responsible for loading configuration files, initializing model
\r
103 * distribution clients, and starting them.
\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
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
127 logger.debug(ModelLoaderMsgs.INITIALIZING,
\r
128 "Succcessfully loaded service: " + this.getClass().getSimpleName());
\r
132 * Shut down the process.
\r
134 private void shutdown() {
\r
135 preShutdownOperations();
\r
137 // TODO: Find a better way to shut down the model loader.
\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
145 Runtime.getRuntime().halt(1);
\r
149 * @see org.openecomp.modelloader.service.ModelLoaderInterface#loadModel(java.lang.String)
\r
152 public Response loadModel(String modelid) {
\r
153 Response response = Response.ok("{\"model_loaded\":\"" + modelid + "\"}").build();
\r
159 * @see org.openecomp.modelloader.service.ModelLoaderInterface#saveModel(java.lang.String, java.lang.String)
\r
162 public Response saveModel(String modelid, String modelname) {
\r
163 Response response = Response.ok("{\"model_saved\":\"" + modelid + "-" + modelname + "\"}")
\r
170 public Response ingestModel(String modelid, HttpServletRequest req, String payload)
\r
171 throws IOException {
\r
174 if (config.getIngestSimulatorEnabled()) {
\r
175 logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Received test artifact");
\r
177 ModelArtifactHandler handler = new ModelArtifactHandler(config);
\r
178 handler.loadModelTest(payload.getBytes());
\r
180 response = Response.ok().build();
\r
182 logger.debug("Simulation interface disabled");
\r
183 response = Response.serverError().build();
\r