2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 Amdocs
7 * ===================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END============================================
21 package org.onap.aai.modelloader.service;
23 import java.io.FileInputStream;
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.Base64;
27 import java.util.Date;
28 import java.util.List;
29 import java.util.Properties;
30 import java.util.Timer;
31 import java.util.TimerTask;
33 import javax.ws.rs.core.Response;
35 import org.onap.aai.cl.api.Logger;
36 import org.onap.aai.cl.eelf.LoggerFactory;
37 import org.onap.aai.modelloader.config.ModelLoaderConfig;
38 import org.onap.aai.modelloader.entity.Artifact;
39 import org.onap.aai.modelloader.notification.ArtifactDeploymentManager;
40 import org.onap.aai.modelloader.notification.ArtifactDownloadManager;
41 import org.onap.aai.modelloader.notification.EventCallback;
42 import org.onap.aai.modelloader.restclient.BabelServiceClientFactory;
43 import org.openecomp.sdc.api.IDistributionClient;
44 import org.openecomp.sdc.api.notification.IArtifactInfo;
45 import org.openecomp.sdc.api.notification.INotificationData;
46 import org.openecomp.sdc.api.results.IDistributionClientResult;
47 import org.openecomp.sdc.impl.DistributionClientFactory;
48 import org.openecomp.sdc.utils.DistributionActionResultEnum;
51 * Service class in charge of managing the negotiating model loading capabilities between AAI and an ASDC.
53 public class ModelLoaderService implements ModelLoaderInterface {
55 protected static final String FILESEP =
56 (System.getProperty("file.separator") == null) ? "/" : System.getProperty("file.separator");
58 protected static final String CONFIG_DIR = System.getProperty("CONFIG_HOME") + FILESEP;
59 protected static final String CONFIG_AUTH_LOCATION = CONFIG_DIR + "auth" + FILESEP;
60 protected static final String CONFIG_FILE = CONFIG_DIR + "model-loader.properties";
62 private IDistributionClient client;
63 private ModelLoaderConfig config;
65 static Logger logger = LoggerFactory.getInstance().getLogger(ModelLoaderService.class.getName());
68 * Responsible for loading configuration files and calling initialization.
70 public ModelLoaderService() {
74 protected void start() {
75 // Load model loader system configuration
76 logger.info(ModelLoaderMsgs.LOADING_CONFIGURATION);
77 Properties configProperties = new Properties();
79 configProperties.load(new FileInputStream(CONFIG_FILE));
80 } catch (IOException e) {
81 String errorMsg = "Failed to load configuration: " + e.getMessage();
82 logger.error(ModelLoaderMsgs.ASDC_CONNECTION_ERROR, errorMsg);
86 config = new ModelLoaderConfig(configProperties, CONFIG_AUTH_LOCATION);
89 Runtime.getRuntime().addShutdownHook(new Thread(this::preShutdownOperations));
93 * Responsible for stopping the connection to the distribution client before the resource is destroyed.
95 protected void preShutdownOperations() {
96 logger.info(ModelLoaderMsgs.STOPPING_CLIENT);
103 * Responsible for loading configuration files, initializing model distribution clients, and starting them.
105 protected void init() {
106 if (!config.getASDCConnectionDisabled()) {
107 // Initialize distribution client
108 logger.debug(ModelLoaderMsgs.INITIALIZING, "Initializing distribution client...");
109 client = DistributionClientFactory.createDistributionClient();
110 EventCallback callback = new EventCallback(client, config);
112 IDistributionClientResult initResult = client.init(config, callback);
114 if (initResult.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) {
116 "Failed to initialize distribution client: " + initResult.getDistributionMessageResult();
117 logger.error(ModelLoaderMsgs.ASDC_CONNECTION_ERROR, errorMsg);
119 // Kick off a timer to retry the SDC connection
120 Timer timer = new Timer();
121 TimerTask task = new SdcConnectionJob(client, config, callback, timer);
122 timer.schedule(task, new Date(), 60000);
124 // Start distribution client
125 logger.debug(ModelLoaderMsgs.INITIALIZING, "Starting distribution client...");
126 IDistributionClientResult startResult = client.start();
127 if (startResult.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) {
129 "Failed to start distribution client: " + startResult.getDistributionMessageResult();
130 logger.error(ModelLoaderMsgs.ASDC_CONNECTION_ERROR, errorMsg);
132 // Kick off a timer to retry the SDC connection
133 Timer timer = new Timer();
134 TimerTask task = new SdcConnectionJob(client, config, callback, timer);
135 timer.schedule(task, new Date(), 60000);
137 logger.info(ModelLoaderMsgs.INITIALIZING, "Connection to SDC established");
144 * Shut down the process.
146 private void shutdown() {
147 preShutdownOperations();
149 // TODO: Find a better way to shut down the model loader.
151 // Give logs time to write to file
153 } catch (InterruptedException e) { // NOSONAR
154 // Nothing we can do at this point
155 logger.debug(e.getMessage());
158 Runtime.getRuntime().halt(1);
164 * @see org.onap.aai.modelloader.service.ModelLoaderInterface#loadModel(java.lang.String)
167 public Response loadModel(String modelid) {
168 return Response.ok("{\"model_loaded\":\"" + modelid + "\"}").build();
174 * @see org.onap.aai.modelloader.service.ModelLoaderInterface#saveModel(java.lang.String, java.lang.String)
177 public Response saveModel(String modelid, String modelname) {
178 return Response.ok("{\"model_saved\":\"" + modelid + "-" + modelname + "\"}").build();
182 public Response ingestModel(String modelName, String modelVersion, String payload) throws IOException {
185 if (config.getIngestSimulatorEnabled()) {
187 logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Received test artifact");
189 List<Artifact> catalogArtifacts = new ArrayList<>();
190 List<Artifact> modelArtifacts = new ArrayList<>();
192 IArtifactInfo artifactInfo = new ArtifactInfoImpl();
193 ((ArtifactInfoImpl) artifactInfo).setArtifactName(modelName);
194 ((ArtifactInfoImpl) artifactInfo).setArtifactVersion(modelVersion);
196 byte[] csarFile = Base64.getDecoder().decode(payload);
198 logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Generating xml models from test artifact");
200 new ArtifactDownloadManager(client, config, new BabelServiceClientFactory()).processToscaArtifacts(
201 modelArtifacts, catalogArtifacts, csarFile, artifactInfo, "test-transaction-id", modelVersion);
203 List<IArtifactInfo> artifacts = new ArrayList<>();
204 artifacts.add(artifactInfo);
205 INotificationData notificationData = new NotificationDataImpl();
206 ((NotificationDataImpl) notificationData).setDistributionID("TestDistributionID");
208 logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Loading xml models from test artifact");
210 success = new ArtifactDeploymentManager(client, config).deploy(notificationData, artifacts,
211 modelArtifacts, catalogArtifacts);
213 } catch (Exception e) {
214 return Response.serverError().entity(e).build();
217 logger.debug("Simulation interface disabled");
223 response = Response.ok().build();
225 response = Response.serverError().build();