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.notification.ArtifactDownloadManager;
33 import org.onap.aai.modelloader.notification.NotificationDataImpl;
34 import org.onap.aai.modelloader.notification.NotificationPublisher;
35 import org.onap.sdc.api.IDistributionClient;
36 import org.onap.sdc.api.notification.IArtifactInfo;
37 import org.springframework.http.ResponseEntity;
38 import org.springframework.web.bind.annotation.GetMapping;
39 import org.springframework.web.bind.annotation.PathVariable;
40 import org.springframework.web.bind.annotation.PostMapping;
41 import org.springframework.web.bind.annotation.PutMapping;
42 import org.springframework.web.bind.annotation.RequestBody;
43 import org.springframework.web.bind.annotation.RequestMapping;
44 import org.springframework.web.bind.annotation.RestController;
47 * Service class in charge of managing the negotiating model loading capabilities between AAI and an ASDC.
50 @RequestMapping("/services/model-loader/v1/model-service")
51 public class ModelController {
53 private static final Logger logger = LoggerFactory.getInstance().getLogger(ModelController.class);
55 private final IDistributionClient client;
56 private final ModelLoaderConfig config;
57 private final ArtifactDeploymentManager artifactDeploymentManager;
58 private final ArtifactDownloadManager artifactDownloadManager;
60 public ModelController(IDistributionClient client, ModelLoaderConfig config, ArtifactDeploymentManager artifactDeploymentManager, ArtifactDownloadManager artifactDownloadManager) {
63 this.artifactDeploymentManager = artifactDeploymentManager;
64 this.artifactDownloadManager = artifactDownloadManager;
67 @GetMapping(value = "/loadModel/{modelid}", produces = "application/json")
68 public ResponseEntity<String> loadModel(@PathVariable String modelid) {
69 return ResponseEntity.ok("{\"model_loaded\":\"" + modelid + "\"}");
72 @PutMapping(value = "/saveModel/{modelid}/{modelname}", produces = "application/json")
73 public ResponseEntity<String> saveModel(@PathVariable String modelid, @PathVariable String modelname) {
74 return ResponseEntity.ok("{\"model_saved\":\"" + modelid + "-" + modelname + "\"}");
77 @PostMapping(value = "/ingestModel/{modelName}/{modelVersion}", produces = "application/json")
78 public ResponseEntity<String> ingestModel(@PathVariable String modelName, @PathVariable String modelVersion,
79 @RequestBody String payload) throws IOException {
80 ResponseEntity<String> response;
82 if (config.getIngestSimulatorEnabled()) {
83 response = processTestArtifact(modelName, modelVersion, payload);
85 logger.debug("Simulation interface disabled");
86 response = ResponseEntity.internalServerError().build();
92 private ResponseEntity<String> processTestArtifact(String modelName, String modelVersion, String payload) {
93 IArtifactInfo artifactInfo = new ArtifactInfoImpl();
94 ((ArtifactInfoImpl) artifactInfo).setArtifactName(modelName);
95 ((ArtifactInfoImpl) artifactInfo).setArtifactVersion(modelVersion);
97 ResponseEntity<String> response;
99 logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Received test artifact " + modelName + " " + modelVersion);
101 byte[] csarFile = Base64.getDecoder().decode(payload);
103 logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Generating XML models from test artifact");
105 List<Artifact> modelArtifacts = new ArrayList<>();
106 List<Artifact> catalogArtifacts = new ArrayList<>();
108 artifactDownloadManager.processToscaArtifacts(modelArtifacts,
109 catalogArtifacts, csarFile, artifactInfo, "test-transaction-id", modelVersion);
111 logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Loading xml models from test artifacts: "
112 + modelArtifacts.size() + " model(s) and " + catalogArtifacts.size() + " catalog(s)");
114 NotificationDataImpl notificationData = new NotificationDataImpl();
115 notificationData.setDistributionID("TestDistributionID");
117 artifactDeploymentManager.deploy(notificationData, modelArtifacts, catalogArtifacts);
118 logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Deployment success was " + success);
119 response = success ? ResponseEntity.ok().build() : ResponseEntity.internalServerError().build();
120 } catch (Exception e) {
121 String responseMessage = e.getMessage();
122 logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Exception handled: " + responseMessage);
123 if (config.getASDCConnectionDisabled()) {
124 // Make sure the NotificationPublisher logger is invoked as per the standard processing flow.
125 new NotificationPublisher().publishDeployFailure(client, new NotificationDataImpl(), artifactInfo);
127 responseMessage += "\nSDC publishing is enabled but has been bypassed";
129 response = ResponseEntity.internalServerError().body(responseMessage);