Refactor babel-related code to not update parameter values
[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 javax.ws.rs.core.MediaType;
29 import javax.ws.rs.core.Response;
30
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;
44
45 /**
46  * Service class in charge of managing the negotiating model loading capabilities between AAI and an ASDC.
47  */
48 @RestController
49 @RequestMapping("/services/model-loader/v1/model-service")
50 public class ModelController implements ModelLoaderInterface {
51
52     private static final Logger logger = LoggerFactory.getInstance().getLogger(ModelController.class);
53
54     private final IDistributionClient client;
55     private final ModelLoaderConfig config;
56     private final ArtifactDeploymentManager artifactDeploymentManager;
57     private final ArtifactDownloadManager artifactDownloadManager;
58
59     public ModelController(IDistributionClient client, ModelLoaderConfig config, ArtifactDeploymentManager artifactDeploymentManager, ArtifactDownloadManager artifactDownloadManager) {
60         this.client = client;
61         this.config = config;
62         this.artifactDeploymentManager = artifactDeploymentManager;
63         this.artifactDownloadManager = artifactDownloadManager;
64     }
65
66     /**
67      * (non-Javadoc)
68      *
69      * @see org.onap.aai.modelloader.service.ModelLoaderInterface#loadModel(java.lang.String)
70      */
71     @Override
72     public Response loadModel(@PathVariable String modelid) {
73         return Response.ok("{\"model_loaded\":\"" + modelid + "\"}").build();
74     }
75
76     /**
77      * (non-Javadoc)
78      *
79      * @see org.onap.aai.modelloader.service.ModelLoaderInterface#saveModel(java.lang.String, java.lang.String)
80      */
81     @Override
82     public Response saveModel(@PathVariable String modelid, @PathVariable String modelname) {
83         return Response.ok("{\"model_saved\":\"" + modelid + "-" + modelname + "\"}").build();
84     }
85
86     @Override
87     public Response ingestModel(@PathVariable String modelName, @PathVariable String modelVersion,
88             @RequestBody String payload) throws IOException {
89         Response response;
90
91         if (config.getIngestSimulatorEnabled()) {
92             response = processTestArtifact(modelName, modelVersion, payload);
93         } else {
94             logger.debug("Simulation interface disabled");
95             response = Response.serverError().build();
96         }
97
98         return response;
99     }
100
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);
105
106         Response response;
107         try {
108             logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Received test artifact " + modelName + " " + modelVersion);
109
110             byte[] csarFile = Base64.getDecoder().decode(payload);
111
112             logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Generating XML models from test artifact");
113
114             List<Artifact> modelArtifacts = new ArrayList<>();
115             List<Artifact> catalogArtifacts = new ArrayList<>();
116
117             artifactDownloadManager.processToscaArtifacts(modelArtifacts,
118                     catalogArtifacts, csarFile, artifactInfo, "test-transaction-id", modelVersion);
119
120             logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Loading xml models from test artifacts: "
121                     + modelArtifacts.size() + " model(s) and " + catalogArtifacts.size() + " catalog(s)");
122
123             NotificationDataImpl notificationData = new NotificationDataImpl();
124             notificationData.setDistributionID("TestDistributionID");
125             boolean success =
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);
135             } else {
136                 responseMessage += "\nSDC publishing is enabled but has been bypassed";
137             }
138             response = Response.serverError().entity(responseMessage).type(MediaType.APPLICATION_XML).build();
139         }
140         return response;
141     }
142 }