Remove jersey and aai-rest-client dependencies from 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.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;
45
46 /**
47  * Service class in charge of managing the negotiating model loading capabilities between AAI and an ASDC.
48  */
49 @RestController
50 @RequestMapping("/services/model-loader/v1/model-service")
51 public class ModelController {
52
53     private static final Logger logger = LoggerFactory.getInstance().getLogger(ModelController.class);
54
55     private final IDistributionClient client;
56     private final ModelLoaderConfig config;
57     private final ArtifactDeploymentManager artifactDeploymentManager;
58     private final ArtifactDownloadManager artifactDownloadManager;
59
60     public ModelController(IDistributionClient client, ModelLoaderConfig config, ArtifactDeploymentManager artifactDeploymentManager, ArtifactDownloadManager artifactDownloadManager) {
61         this.client = client;
62         this.config = config;
63         this.artifactDeploymentManager = artifactDeploymentManager;
64         this.artifactDownloadManager = artifactDownloadManager;
65     }
66
67     @GetMapping(value = "/loadModel/{modelid}", produces = "application/json")
68     public ResponseEntity<String> loadModel(@PathVariable String modelid) {
69         return ResponseEntity.ok("{\"model_loaded\":\"" + modelid + "\"}");
70     }
71
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 + "\"}");
75     }
76
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;
81
82         if (config.getIngestSimulatorEnabled()) {
83             response = processTestArtifact(modelName, modelVersion, payload);
84         } else {
85             logger.debug("Simulation interface disabled");
86             response = ResponseEntity.internalServerError().build();
87         }
88
89         return response;
90     }
91
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);
96
97         ResponseEntity<String> response;
98         try {
99             logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Received test artifact " + modelName + " " + modelVersion);
100
101             byte[] csarFile = Base64.getDecoder().decode(payload);
102
103             logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Generating XML models from test artifact");
104
105             List<Artifact> modelArtifacts = new ArrayList<>();
106             List<Artifact> catalogArtifacts = new ArrayList<>();
107
108             artifactDownloadManager.processToscaArtifacts(modelArtifacts,
109                     catalogArtifacts, csarFile, artifactInfo, "test-transaction-id", modelVersion);
110
111             logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Loading xml models from test artifacts: "
112                     + modelArtifacts.size() + " model(s) and " + catalogArtifacts.size() + " catalog(s)");
113
114             NotificationDataImpl notificationData = new NotificationDataImpl();
115             notificationData.setDistributionID("TestDistributionID");
116             boolean success =
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);
126             } else {
127                 responseMessage += "\nSDC publishing is enabled but has been bypassed";
128             }
129             response = ResponseEntity.internalServerError().body(responseMessage);
130         }
131         return response;
132     }
133 }