570d8fe4d9a2b14fb47126db9f0e6ca2404baa07
[aai/model-loader.git] / src / main / java / org / onap / aai / modelloader / entity / model / ModelArtifactHandler.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.entity.model;
22
23 import org.onap.aai.modelloader.config.ModelLoaderConfig;
24 import org.onap.aai.modelloader.entity.Artifact;
25 import org.onap.aai.modelloader.entity.ArtifactHandler;
26 import org.onap.aai.modelloader.restclient.AaiRestClient;
27 import org.onap.aai.modelloader.service.ModelLoaderMsgs;
28 import org.onap.aai.cl.api.Logger;
29 import org.onap.aai.cl.eelf.LoggerFactory;
30
31 import java.util.ArrayList;
32 import java.util.List;
33
34
35 public class ModelArtifactHandler extends ArtifactHandler {
36
37   private static Logger logger = LoggerFactory.getInstance().getLogger(ModelArtifactHandler.class.getName());
38   
39   public ModelArtifactHandler(ModelLoaderConfig config) {
40     super(config);
41   }
42
43   @Override
44   public boolean pushArtifacts(List<Artifact> artifacts, String distributionID) {
45     ModelSorter modelSorter = new ModelSorter();
46     List<Artifact> sortedModelArtifacts; 
47     try {
48       sortedModelArtifacts = modelSorter.sort(artifacts);
49     }
50     catch (RuntimeException ex) {
51       logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR, "Unable to resolve models: " + ex.getMessage());
52       return false;
53     }
54     
55     // Push the ordered list of model artifacts to A&AI.  If one fails, we need to roll back
56     // the changes.
57     List<AbstractModelArtifact> completedModels = new ArrayList<>();
58     AaiRestClient aaiClient = new AaiRestClient(config);
59
60     for (Artifact art : sortedModelArtifacts) {
61       AbstractModelArtifact model = (AbstractModelArtifact)art;
62       if (model.push(aaiClient, config, distributionID, completedModels) != true) {
63         for (AbstractModelArtifact modelToDelete : completedModels) {
64           modelToDelete.rollbackModel(aaiClient, config, distributionID);
65         }
66
67         return false;
68       }
69     }
70
71     return true;
72   }
73
74   // This method is used for the test REST interface to load models without an ASDC
75   public void loadModelTest(byte[] payload) {
76     List<Artifact> modelArtifacts = new ArrayList<Artifact>();
77     ModelArtifactParser parser = new ModelArtifactParser();
78     modelArtifacts.addAll(parser.parse(payload, "Test-Artifact"));
79     ModelSorter modelSorter = new ModelSorter();
80     List<Artifact> sortedModelArtifacts = modelSorter.sort(modelArtifacts);
81     pushArtifacts(sortedModelArtifacts, "Test-Distribution");
82   }
83 }