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 org.onap.aai.cl.api.Logger;
29 import org.onap.aai.cl.eelf.LoggerFactory;
30 import org.onap.aai.modelloader.config.ModelLoaderConfig;
31 import org.onap.aai.modelloader.entity.Artifact;
32 import org.onap.aai.modelloader.entity.ArtifactType;
33 import org.onap.aai.modelloader.notification.ArtifactDownloadManager;
34 import org.onap.aai.modelloader.notification.NotificationDataImpl;
35 import org.onap.aai.modelloader.notification.NotificationPublisher;
36 import org.onap.sdc.api.IDistributionClient;
37 import org.onap.sdc.api.notification.IArtifactInfo;
38 import org.springframework.http.HttpStatus;
39 import org.springframework.http.ResponseEntity;
40 import org.springframework.web.bind.annotation.GetMapping;
41 import org.springframework.web.bind.annotation.PathVariable;
42 import org.springframework.web.bind.annotation.PostMapping;
43 import org.springframework.web.bind.annotation.PutMapping;
44 import org.springframework.web.bind.annotation.RequestBody;
45 import org.springframework.web.bind.annotation.RequestMapping;
46 import org.springframework.web.bind.annotation.RestController;
49 * Service class in charge of managing the negotiating model loading capabilities between AAI and an ASDC.
52 @RequestMapping("/services/model-loader/v1/model-service")
53 public class ModelController {
55 private static final Logger logger = LoggerFactory.getInstance().getLogger(ModelController.class);
57 private final IDistributionClient client;
58 private final ModelLoaderConfig config;
59 private final ArtifactDeploymentManager artifactDeploymentManager;
60 private final ArtifactDownloadManager artifactDownloadManager;
62 public ModelController(IDistributionClient client, ModelLoaderConfig config, ArtifactDeploymentManager artifactDeploymentManager, ArtifactDownloadManager artifactDownloadManager) {
65 this.artifactDeploymentManager = artifactDeploymentManager;
66 this.artifactDownloadManager = artifactDownloadManager;
69 @GetMapping(value = "/loadModel/{modelid}", produces = "application/json")
70 public ResponseEntity<String> loadModel(@PathVariable String modelid) {
71 return ResponseEntity.ok("{\"model_loaded\":\"" + modelid + "\"}");
74 @PutMapping(value = "/saveModel/{modelid}/{modelname}", produces = "application/json")
75 public ResponseEntity<String> saveModel(@PathVariable String modelid, @PathVariable String modelname) {
76 return ResponseEntity.ok("{\"model_saved\":\"" + modelid + "-" + modelname + "\"}");
79 @PostMapping(value = "/ingestModel/{modelName}/{modelVersion}", produces = "application/json")
80 public ResponseEntity<String> ingestModel(@PathVariable String modelName, @PathVariable String modelVersion,
81 @RequestBody String payload) throws IOException {
82 ResponseEntity<String> response;
84 if (config.getIngestSimulatorEnabled()) {
85 response = processTestArtifact(modelName, modelVersion, payload);
87 logger.debug("Simulation interface disabled");
88 response = ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
94 private ResponseEntity<String> processTestArtifact(String modelName, String modelVersion, String payload) {
95 IArtifactInfo artifactInfo = new ArtifactInfoImpl();
96 ((ArtifactInfoImpl) artifactInfo).setArtifactName(modelName);
97 ((ArtifactInfoImpl) artifactInfo).setArtifactVersion(modelVersion);
99 ResponseEntity<String> response;
101 logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Received test artifact " + modelName + " " + modelVersion);
103 byte[] csarFile = Base64.getDecoder().decode(payload);
105 logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Generating XML models from test artifact");
107 List<Artifact> artifacts = artifactDownloadManager.processToscaArtifacts(csarFile, artifactInfo, "test-transaction-id", modelVersion);
108 List<Artifact> modelArtifacts = new ArrayList<>();
109 List<Artifact> catalogArtifacts = new ArrayList<>();
110 for(Artifact artifact : artifacts) {
111 if(artifact.getType().equals(ArtifactType.MODEL)) {
112 modelArtifacts.add(artifact);
114 catalogArtifacts.add(artifact);
118 logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Loading xml models from test artifacts: "
119 + modelArtifacts.size() + " model(s) and " + catalogArtifacts.size() + " catalog(s)");
121 NotificationDataImpl notificationData = new NotificationDataImpl();
122 notificationData.setDistributionID("TestDistributionID");
124 artifactDeploymentManager.deploy(notificationData.getDistributionID(), modelArtifacts, catalogArtifacts);
125 logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Deployment success was " + success);
126 response = success ? ResponseEntity.ok().build() : ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
127 } catch (Exception e) {
128 String responseMessage = e.getMessage();
129 logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Exception handled: " + responseMessage);
130 if (config.getASDCConnectionDisabled()) {
131 // Make sure the NotificationPublisher logger is invoked as per the standard processing flow.
132 new NotificationPublisher().publishDeployFailure(client, new NotificationDataImpl(), artifactInfo);
134 responseMessage += "\nSDC publishing is enabled but has been bypassed";
136 response = ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(responseMessage);