6d75306c5fa705d2571d26c457273886bf0f61d2
[aai/model-loader.git] / src / main / java / org / onap / aai / modelloader / notification / ArtifactDeploymentManager.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 Amdocs
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.notification;
22
23 import java.util.ArrayList;
24 import java.util.List;
25 import org.onap.aai.modelloader.config.ModelLoaderConfig;
26 import org.onap.aai.modelloader.entity.Artifact;
27 import org.onap.aai.modelloader.entity.catalog.VnfCatalogArtifactHandler;
28 import org.onap.aai.modelloader.entity.model.ModelArtifactHandler;
29 import org.onap.aai.modelloader.restclient.AaiRestClient;
30 import org.openecomp.sdc.api.IDistributionClient;
31 import org.openecomp.sdc.api.notification.IArtifactInfo;
32 import org.openecomp.sdc.api.notification.INotificationData;
33
34 /**
35  * This class is responsible for deploying model and catalog artifacts.
36  */
37 public class ArtifactDeploymentManager {
38
39     private IDistributionClient client;
40     private ModelLoaderConfig config;
41     private ModelArtifactHandler modelArtifactHandler;
42     private VnfCatalogArtifactHandler vnfCatalogArtifactHandler;
43     private NotificationPublisher notificationPublisher;
44
45     public ArtifactDeploymentManager(IDistributionClient client, ModelLoaderConfig config) {
46         this.client = client;
47         this.config = config;
48     }
49
50     /**
51      * Deploys model and catalog artifacts to A&AI
52      *
53      * @param data data about the notification that is being processed
54      * @param artifacts the specific artifacts found in the data.
55      * @param modelArtifacts collection of artifacts that represent yml files found in a TOSCA_CSAR file that have been
56      *        converted to XML and also those for model query specs
57      * @param catalogArtifacts collection of artifacts that represent vnf catalog files
58      * @return boolean <code>true</code> if all deployments were successful otherwise <code>false</code>
59      */
60     public boolean deploy(final INotificationData data, final List<IArtifactInfo> artifacts,
61             final List<Artifact> modelArtifacts, final List<Artifact> catalogArtifacts) {
62
63         AaiRestClient aaiClient = new AaiRestClient(config);
64         String distributionId = data.getDistributionID();
65
66         List<Artifact> completedArtifacts = new ArrayList<>();
67         boolean deploySuccess =
68                 getModelArtifactHandler().pushArtifacts(modelArtifacts, distributionId, completedArtifacts, aaiClient);
69
70         if (!deploySuccess) {
71             getModelArtifactHandler().rollback(completedArtifacts, distributionId, aaiClient);
72         } else {
73             List<Artifact> completedImageData = new ArrayList<>();
74             deploySuccess = getVnfCatalogArtifactHandler().pushArtifacts(catalogArtifacts, distributionId,
75                     completedImageData, aaiClient);
76             if (!deploySuccess) {
77                 getModelArtifactHandler().rollback(completedArtifacts, distributionId, aaiClient);
78                 getVnfCatalogArtifactHandler().rollback(completedImageData, distributionId, aaiClient);
79             }
80         }
81
82         publishNotifications(data, "TOSCA_CSAR", artifacts, deploySuccess);
83
84         return deploySuccess;
85     }
86
87     private void publishNotifications(INotificationData data, String filterType, List<IArtifactInfo> artifacts,
88             boolean deploymentSuccess) {
89         if (deploymentSuccess) {
90             artifacts.stream().filter(a -> filterType.equalsIgnoreCase(a.getArtifactType()))
91                     .forEach(a -> getNotificationPublisher().publishDeploySuccess(client, data, a));
92             getNotificationPublisher().publishComponentSuccess(client, data);
93         } else {
94             artifacts.stream().filter(a -> filterType.equalsIgnoreCase(a.getArtifactType()))
95                     .forEach(a -> getNotificationPublisher().publishDeployFailure(client, data, a));
96             getNotificationPublisher().publishComponentFailure(client, data, "deploy failure");
97         }
98     }
99
100     private ModelArtifactHandler getModelArtifactHandler() {
101         if (modelArtifactHandler == null) {
102             modelArtifactHandler = new ModelArtifactHandler(config);
103         }
104
105         return modelArtifactHandler;
106     }
107
108     private NotificationPublisher getNotificationPublisher() {
109         if (notificationPublisher == null) {
110             notificationPublisher = new NotificationPublisher();
111         }
112
113         return notificationPublisher;
114     }
115
116     private VnfCatalogArtifactHandler getVnfCatalogArtifactHandler() {
117         if (vnfCatalogArtifactHandler == null) {
118             this.vnfCatalogArtifactHandler = new VnfCatalogArtifactHandler(config);
119         }
120
121         return vnfCatalogArtifactHandler;
122     }
123 }