Allow handling of legact model artifacts
[aai/model-loader.git] / src / main / java / org / openecomp / modelloader / entity / model / ModelArtifactHandler.java
1 /**
2  * ============LICENSE_START=======================================================
3  * Model Loader
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * http://www.apache.org/licenses/LICENSE-2.0
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  *
20  * ECOMP and OpenECOMP are trademarks
21  * and service marks of AT&T Intellectual Property.
22  */
23 package org.openecomp.modelloader.entity.model;
24
25 import org.openecomp.cl.api.Logger;
26 import org.openecomp.cl.eelf.LoggerFactory;
27 import org.openecomp.modelloader.config.ModelLoaderConfig;
28 import org.openecomp.modelloader.entity.Artifact;
29 import org.openecomp.modelloader.entity.ArtifactHandler;
30 import org.openecomp.modelloader.restclient.AaiRestClient;
31 import org.openecomp.modelloader.service.ModelLoaderMsgs;
32
33 import java.util.ArrayList;
34 import java.util.List;
35
36
37 public class ModelArtifactHandler extends ArtifactHandler {
38
39   private static Logger logger = LoggerFactory.getInstance().getLogger(ModelArtifactHandler.class.getName());
40   
41   public ModelArtifactHandler(ModelLoaderConfig config) {
42     super(config);
43   }
44
45   @Override
46   public boolean pushArtifacts(List<Artifact> artifacts, String distributionID) {
47     ModelSorter modelSorter = new ModelSorter();
48     List<Artifact> sortedModelArtifacts; 
49     try {
50       sortedModelArtifacts = modelSorter.sort(artifacts);
51     }
52     catch (RuntimeException ex) {
53       logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR, "Unable to resolve models: " + ex.getMessage());
54       return false;
55     }
56     
57     // Push the ordered list of model artifacts to A&AI.  If one fails, we need to roll back
58     // the changes.
59     List<AbstractModelArtifact> completedModels = new ArrayList<AbstractModelArtifact>();
60     AaiRestClient aaiClient = new AaiRestClient(config);
61
62     for (Artifact art : sortedModelArtifacts) {
63       AbstractModelArtifact model = (AbstractModelArtifact)art;
64       if (model.push(aaiClient, config, distributionID, completedModels) != true) {
65         for (AbstractModelArtifact modelToDelete : completedModels) {
66           modelToDelete.rollbackModel(aaiClient, config, distributionID);
67         }
68
69         return false;
70       }
71     }
72
73     return true;
74   }
75
76   // This method is used for the test REST interface to load models without an ASDC
77   public void loadModelTest(byte[] payload) {
78     List<Artifact> modelArtifacts = new ArrayList<Artifact>();
79     ModelArtifactParser parser = new ModelArtifactParser();
80     modelArtifacts.addAll(parser.parse(payload, "Test-Artifact"));
81     ModelSorter modelSorter = new ModelSorter();
82     List<Artifact> sortedModelArtifacts = modelSorter.sort(modelArtifacts);
83     pushArtifacts(sortedModelArtifacts, "Test-Distribution");
84   }
85 }