Upgrade spring-boot to 2.7.X in model-loader
[aai/model-loader.git] / src / main / java / org / onap / aai / modelloader / service / ModelController.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21 package org.onap.aai.modelloader.service;
22
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.Base64;
26 import java.util.List;
27
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.HttpStatus;
38 import org.springframework.http.ResponseEntity;
39 import org.springframework.web.bind.annotation.GetMapping;
40 import org.springframework.web.bind.annotation.PathVariable;
41 import org.springframework.web.bind.annotation.PostMapping;
42 import org.springframework.web.bind.annotation.PutMapping;
43 import org.springframework.web.bind.annotation.RequestBody;
44 import org.springframework.web.bind.annotation.RequestMapping;
45 import org.springframework.web.bind.annotation.RestController;
46
47 /**
48  * Service class in charge of managing the negotiating model loading capabilities between AAI and an ASDC.
49  */
50 @RestController
51 @RequestMapping("/services/model-loader/v1/model-service")
52 public class ModelController {
53
54     private static final Logger logger = LoggerFactory.getInstance().getLogger(ModelController.class);
55
56     private final IDistributionClient client;
57     private final ModelLoaderConfig config;
58     private final ArtifactDeploymentManager artifactDeploymentManager;
59     private final ArtifactDownloadManager artifactDownloadManager;
60
61     public ModelController(IDistributionClient client, ModelLoaderConfig config, ArtifactDeploymentManager artifactDeploymentManager, ArtifactDownloadManager artifactDownloadManager) {
62         this.client = client;
63         this.config = config;
64         this.artifactDeploymentManager = artifactDeploymentManager;
65         this.artifactDownloadManager = artifactDownloadManager;
66     }
67
68     @GetMapping(value = "/loadModel/{modelid}", produces = "application/json")
69     public ResponseEntity<String> loadModel(@PathVariable String modelid) {
70         return ResponseEntity.ok("{\"model_loaded\":\"" + modelid + "\"}");
71     }
72
73     @PutMapping(value = "/saveModel/{modelid}/{modelname}", produces = "application/json")
74     public ResponseEntity<String> saveModel(@PathVariable String modelid, @PathVariable String modelname) {
75         return ResponseEntity.ok("{\"model_saved\":\"" + modelid + "-" + modelname + "\"}");
76     }
77
78     @PostMapping(value = "/ingestModel/{modelName}/{modelVersion}", produces = "application/json")
79     public ResponseEntity<String> ingestModel(@PathVariable String modelName, @PathVariable String modelVersion,
80             @RequestBody String payload) throws IOException {
81         ResponseEntity<String> response;
82
83         if (config.getIngestSimulatorEnabled()) {
84             response = processTestArtifact(modelName, modelVersion, payload);
85         } else {
86             logger.debug("Simulation interface disabled");
87             response = ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
88         }
89
90         return response;
91     }
92
93     private ResponseEntity<String> processTestArtifact(String modelName, String modelVersion, String payload) {
94         IArtifactInfo artifactInfo = new ArtifactInfoImpl();
95         ((ArtifactInfoImpl) artifactInfo).setArtifactName(modelName);
96         ((ArtifactInfoImpl) artifactInfo).setArtifactVersion(modelVersion);
97
98         ResponseEntity<String> response;
99         try {
100             logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Received test artifact " + modelName + " " + modelVersion);
101
102             byte[] csarFile = Base64.getDecoder().decode(payload);
103
104             logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Generating XML models from test artifact");
105
106             List<Artifact> modelArtifacts = new ArrayList<>();
107             List<Artifact> catalogArtifacts = new ArrayList<>();
108
109             artifactDownloadManager.processToscaArtifacts(modelArtifacts,
110                     catalogArtifacts, csarFile, artifactInfo, "test-transaction-id", modelVersion);
111
112             logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Loading xml models from test artifacts: "
113                     + modelArtifacts.size() + " model(s) and " + catalogArtifacts.size() + " catalog(s)");
114
115             NotificationDataImpl notificationData = new NotificationDataImpl();
116             notificationData.setDistributionID("TestDistributionID");
117             boolean success =
118                     artifactDeploymentManager.deploy(notificationData, modelArtifacts, catalogArtifacts);
119             logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Deployment success was " + success);
120             response = success ? ResponseEntity.ok().build() : ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
121         } catch (Exception e) {
122             String responseMessage = e.getMessage();
123             logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Exception handled: " + responseMessage);
124             if (config.getASDCConnectionDisabled()) {
125                 // Make sure the NotificationPublisher logger is invoked as per the standard processing flow.
126                 new NotificationPublisher().publishDeployFailure(client, new NotificationDataImpl(), artifactInfo);
127             } else {
128                 responseMessage += "\nSDC publishing is enabled but has been bypassed";
129             }
130             response = ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(responseMessage);
131         }
132         return response;
133     }
134 }