2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 European Software Marketing Ltd.
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;
32 import javax.annotation.PostConstruct;
33 import javax.ws.rs.core.Response;
34 import org.onap.aai.cl.api.Logger;
35 import org.onap.aai.cl.eelf.LoggerFactory;
36 import org.onap.aai.modelloader.config.ModelLoaderConfig;
37 import org.onap.aai.modelloader.entity.Artifact;
38 import org.onap.aai.modelloader.notification.ArtifactDeploymentManager;
39 import org.onap.aai.modelloader.notification.ArtifactDownloadManager;
40 import org.onap.aai.modelloader.notification.EventCallback;
41 import org.onap.aai.modelloader.restclient.BabelServiceClientFactory;
42 import org.onap.sdc.api.IDistributionClient;
43 import org.onap.sdc.api.notification.IArtifactInfo;
44 import org.onap.sdc.api.notification.INotificationData;
45 import org.onap.sdc.api.results.IDistributionClientResult;
46 import org.onap.sdc.impl.DistributionClientFactory;
47 import org.onap.sdc.utils.DistributionActionResultEnum;
48 import org.springframework.beans.factory.annotation.Value;
49 import org.springframework.web.bind.annotation.PathVariable;
50 import org.springframework.web.bind.annotation.RequestBody;
51 import org.springframework.web.bind.annotation.RequestMapping;
52 import org.springframework.web.bind.annotation.RestController;
55 * Service class in charge of managing the negotiating model loading capabilities between AAI and an ASDC.
58 @RequestMapping("/services/model-loader/v1/model-service")
59 public class ModelLoaderService implements ModelLoaderInterface {
61 static Logger logger = LoggerFactory.getInstance().getLogger(ModelLoaderService.class.getName());
63 protected static final String FILESEP =
64 (System.getProperty("file.separator") == null) ? "/" : System.getProperty("file.separator");
66 @Value("${CONFIG_HOME}")
67 private String configDir;
68 private IDistributionClient client;
69 private ModelLoaderConfig config;
72 * Responsible for loading configuration files and calling initialization.
75 protected void start() {
76 // Load model loader system configuration
77 logger.info(ModelLoaderMsgs.LOADING_CONFIGURATION);
78 ModelLoaderConfig.setConfigHome(configDir);
79 Properties configProperties = new Properties();
81 configProperties.load(new FileInputStream(configDir + FILESEP + "model-loader.properties"));
82 config = new ModelLoaderConfig(configProperties);
83 if (!config.getASDCConnectionDisabled()) {
86 } catch (IOException e) {
87 String errorMsg = "Failed to load configuration: " + e.getMessage();
88 logger.error(ModelLoaderMsgs.ASDC_CONNECTION_ERROR, errorMsg);
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 initSdcClient() {
106 // Initialize distribution client
107 logger.debug(ModelLoaderMsgs.INITIALIZING, "Initializing distribution client...");
108 client = DistributionClientFactory.createDistributionClient();
109 EventCallback callback = new EventCallback(client, config);
111 IDistributionClientResult initResult = client.init(config, callback);
113 if (initResult.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) {
114 String errorMsg = "Failed to initialize distribution client: " + initResult.getDistributionMessageResult();
115 logger.error(ModelLoaderMsgs.ASDC_CONNECTION_ERROR, errorMsg);
117 // Kick off a timer to retry the SDC connection
118 Timer timer = new Timer();
119 TimerTask task = new SdcConnectionJob(client, config, callback, timer);
120 timer.schedule(task, new Date(), 60000);
122 // Start distribution client
123 logger.debug(ModelLoaderMsgs.INITIALIZING, "Starting distribution client...");
124 IDistributionClientResult startResult = client.start();
125 if (startResult.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) {
126 String errorMsg = "Failed to start distribution client: " + startResult.getDistributionMessageResult();
127 logger.error(ModelLoaderMsgs.ASDC_CONNECTION_ERROR, errorMsg);
129 // Kick off a timer to retry the SDC connection
130 Timer timer = new Timer();
131 TimerTask task = new SdcConnectionJob(client, config, callback, timer);
132 timer.schedule(task, new Date(), 60000);
134 logger.info(ModelLoaderMsgs.INITIALIZING, "Connection to SDC established");
137 Runtime.getRuntime().addShutdownHook(new Thread(this::preShutdownOperations));
143 * @see org.onap.aai.modelloader.service.ModelLoaderInterface#loadModel(java.lang.String)
146 public Response loadModel(@PathVariable String modelid) {
147 return Response.ok("{\"model_loaded\":\"" + modelid + "\"}").build();
153 * @see org.onap.aai.modelloader.service.ModelLoaderInterface#saveModel(java.lang.String, java.lang.String)
156 public Response saveModel(@PathVariable String modelid, @PathVariable String modelname) {
157 return Response.ok("{\"model_saved\":\"" + modelid + "-" + modelname + "\"}").build();
161 public Response ingestModel(@PathVariable String modelName, @PathVariable String modelVersion,
162 @RequestBody String payload) throws IOException {
165 if (config.getIngestSimulatorEnabled()) {
167 logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Received test artifact");
169 List<Artifact> catalogArtifacts = new ArrayList<>();
170 List<Artifact> modelArtifacts = new ArrayList<>();
172 IArtifactInfo artifactInfo = new ArtifactInfoImpl();
173 ((ArtifactInfoImpl) artifactInfo).setArtifactName(modelName);
174 ((ArtifactInfoImpl) artifactInfo).setArtifactVersion(modelVersion);
176 byte[] csarFile = Base64.getDecoder().decode(payload);
178 logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Generating xml models from test artifact");
180 new ArtifactDownloadManager(client, config, new BabelServiceClientFactory()).processToscaArtifacts(
181 modelArtifacts, catalogArtifacts, csarFile, artifactInfo, "test-transaction-id", modelVersion);
183 List<IArtifactInfo> artifacts = new ArrayList<>();
184 artifacts.add(artifactInfo);
185 INotificationData notificationData = new NotificationDataImpl();
186 ((NotificationDataImpl) notificationData).setDistributionID("TestDistributionID");
188 logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Loading xml models from test artifact");
190 success = new ArtifactDeploymentManager(client, config).deploy(notificationData, artifacts,
191 modelArtifacts, catalogArtifacts);
193 } catch (Exception e) {
194 return Response.serverError().entity(e).build();
197 logger.debug("Simulation interface disabled");
203 response = Response.ok().build();
205 response = Response.serverError().build();