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.IOException;
24 import java.util.ArrayList;
25 import java.util.Base64;
26 import java.util.List;
28 import javax.ws.rs.core.MediaType;
29 import javax.ws.rs.core.Response;
31 import org.onap.aai.cl.api.Logger;
32 import org.onap.aai.cl.eelf.LoggerFactory;
33 import org.onap.aai.modelloader.config.ModelLoaderConfig;
34 import org.onap.aai.modelloader.entity.Artifact;
35 import org.onap.aai.modelloader.notification.ArtifactDownloadManager;
36 import org.onap.aai.modelloader.notification.NotificationDataImpl;
37 import org.onap.aai.modelloader.notification.NotificationPublisher;
38 import org.onap.sdc.api.IDistributionClient;
39 import org.onap.sdc.api.notification.IArtifactInfo;
40 import org.springframework.web.bind.annotation.PathVariable;
41 import org.springframework.web.bind.annotation.RequestBody;
42 import org.springframework.web.bind.annotation.RequestMapping;
43 import org.springframework.web.bind.annotation.RestController;
46 * Service class in charge of managing the negotiating model loading capabilities between AAI and an ASDC.
49 @RequestMapping("/services/model-loader/v1/model-service")
50 public class ModelController implements ModelLoaderInterface {
52 private static final Logger logger = LoggerFactory.getInstance().getLogger(ModelController.class);
54 private final IDistributionClient client;
55 private final ModelLoaderConfig config;
56 private final ArtifactDeploymentManager artifactDeploymentManager;
57 private final ArtifactDownloadManager artifactDownloadManager;
59 public ModelController(IDistributionClient client, ModelLoaderConfig config, ArtifactDeploymentManager artifactDeploymentManager, ArtifactDownloadManager artifactDownloadManager) {
62 this.artifactDeploymentManager = artifactDeploymentManager;
63 this.artifactDownloadManager = artifactDownloadManager;
69 * @see org.onap.aai.modelloader.service.ModelLoaderInterface#loadModel(java.lang.String)
72 public Response loadModel(@PathVariable String modelid) {
73 return Response.ok("{\"model_loaded\":\"" + modelid + "\"}").build();
79 * @see org.onap.aai.modelloader.service.ModelLoaderInterface#saveModel(java.lang.String, java.lang.String)
82 public Response saveModel(@PathVariable String modelid, @PathVariable String modelname) {
83 return Response.ok("{\"model_saved\":\"" + modelid + "-" + modelname + "\"}").build();
87 public Response ingestModel(@PathVariable String modelName, @PathVariable String modelVersion,
88 @RequestBody String payload) throws IOException {
91 if (config.getIngestSimulatorEnabled()) {
92 response = processTestArtifact(modelName, modelVersion, payload);
94 logger.debug("Simulation interface disabled");
95 response = Response.serverError().build();
101 private Response processTestArtifact(String modelName, String modelVersion, String payload) {
102 IArtifactInfo artifactInfo = new ArtifactInfoImpl();
103 ((ArtifactInfoImpl) artifactInfo).setArtifactName(modelName);
104 ((ArtifactInfoImpl) artifactInfo).setArtifactVersion(modelVersion);
108 logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Received test artifact " + modelName + " " + modelVersion);
110 byte[] csarFile = Base64.getDecoder().decode(payload);
112 logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Generating XML models from test artifact");
114 List<Artifact> modelArtifacts = new ArrayList<>();
115 List<Artifact> catalogArtifacts = new ArrayList<>();
117 artifactDownloadManager.processToscaArtifacts(modelArtifacts,
118 catalogArtifacts, csarFile, artifactInfo, "test-transaction-id", modelVersion);
120 logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Loading xml models from test artifacts: "
121 + modelArtifacts.size() + " model(s) and " + catalogArtifacts.size() + " catalog(s)");
123 NotificationDataImpl notificationData = new NotificationDataImpl();
124 notificationData.setDistributionID("TestDistributionID");
126 artifactDeploymentManager.deploy(notificationData, modelArtifacts, catalogArtifacts);
127 logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Deployment success was " + success);
128 response = success ? Response.ok().build() : Response.serverError().build();
129 } catch (Exception e) {
130 String responseMessage = e.getMessage();
131 logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Exception handled: " + responseMessage);
132 if (config.getASDCConnectionDisabled()) {
133 // Make sure the NotificationPublisher logger is invoked as per the standard processing flow.
134 new NotificationPublisher().publishDeployFailure(client, new NotificationDataImpl(), artifactInfo);
136 responseMessage += "\nSDC publishing is enabled but has been bypassed";
138 response = Response.serverError().entity(responseMessage).type(MediaType.APPLICATION_XML).build();