af006f56f49ef20b53adff26783717346811f738
[aai/model-loader.git] / src / main / java / org / onap / aai / modelloader / service / 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 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.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.onap.sdc.api.notification.INotificationData;
31
32 /**
33  * This class is responsible for deploying model and catalog artifacts.
34  */
35 public class ArtifactDeploymentManager {
36
37     private ModelLoaderConfig config;
38     private ModelArtifactHandler modelArtifactHandler;
39     private VnfCatalogArtifactHandler vnfCatalogArtifactHandler;
40
41     public ArtifactDeploymentManager(ModelLoaderConfig config) {
42         this.config = config;
43     }
44
45     /**
46      * Deploys model and catalog artifacts to A&AI.
47      *
48      * @param data data about the notification that is being processed
49      * @param modelArtifacts collection of artifacts that represent yml files found in a TOSCA_CSAR file that have been
50      *        converted to XML and also those for model query specs
51      * @param catalogArtifacts collection of artifacts that represent vnf catalog files
52      * @return boolean <code>true</code> if all deployments were successful otherwise <code>false</code>
53      */
54     public boolean deploy(final INotificationData data, final List<Artifact> modelArtifacts,
55             final List<Artifact> catalogArtifacts) {
56
57         AaiRestClient aaiClient = new AaiRestClient(config);
58         String distributionId = data.getDistributionID();
59
60         List<Artifact> completedArtifacts = new ArrayList<>();
61         boolean deploySuccess =
62                 getModelArtifactHandler().pushArtifacts(modelArtifacts, distributionId, completedArtifacts, aaiClient);
63
64         if (!deploySuccess) {
65             getModelArtifactHandler().rollback(completedArtifacts, distributionId, aaiClient);
66         } else {
67             List<Artifact> completedImageData = new ArrayList<>();
68             deploySuccess = getVnfCatalogArtifactHandler().pushArtifacts(catalogArtifacts, distributionId,
69                     completedImageData, aaiClient);
70             if (!deploySuccess) {
71                 getModelArtifactHandler().rollback(completedArtifacts, distributionId, aaiClient);
72                 getVnfCatalogArtifactHandler().rollback(completedImageData, distributionId, aaiClient);
73             }
74         }
75
76         return deploySuccess;
77     }
78
79     private ModelArtifactHandler getModelArtifactHandler() {
80         if (modelArtifactHandler == null) {
81             modelArtifactHandler = new ModelArtifactHandler(config);
82         }
83
84         return modelArtifactHandler;
85     }
86
87     private VnfCatalogArtifactHandler getVnfCatalogArtifactHandler() {
88         if (vnfCatalogArtifactHandler == null) {
89             this.vnfCatalogArtifactHandler = new VnfCatalogArtifactHandler(config);
90         }
91
92         return vnfCatalogArtifactHandler;
93     }
94 }