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