[SDC-29] Amdocs OnBoard 1707 initial commit.
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-software-product-manager / src / main / java / org / openecomp / sdc / vendorsoftwareproduct / types / OrchestrationTemplateActionResponse.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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
21 package org.openecomp.sdc.vendorsoftwareproduct.types;
22
23
24 import org.apache.commons.collections4.CollectionUtils;
25 import org.openecomp.sdc.datatypes.error.ErrorLevel;
26 import org.openecomp.sdc.datatypes.error.ErrorMessage;
27
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.List;
31 import java.util.Map;
32
33 public class OrchestrationTemplateActionResponse {
34   private List<String> fileNames;
35   private Map<String, List<ErrorMessage>> errors = new HashMap<>();
36   private UploadFileStatus status = UploadFileStatus.Success;
37
38   public UploadFileStatus getStatus() {
39     return status;
40   }
41
42   public void setStatus(UploadFileStatus status) {
43     this.status = status;
44   }
45
46   public List<String> getFileNames() {
47     return fileNames;
48   }
49
50   public void setFileNames(List<String> fileNames) {
51     this.fileNames = fileNames;
52   }
53
54   public void addNewFileToList(String filename) {
55     this.fileNames.add(filename);
56   }
57
58   public void removeFileFromList(String toRemove) {
59     this.fileNames.remove(toRemove);
60   }
61
62   /**
63    * Add structure errors.
64    *
65    * @param errorsByFileName the errors by file name
66    */
67   public void addStructureErrors(Map<String, List<ErrorMessage>> errorsByFileName) {
68     if (errorsByFileName == null) {
69       return;
70     }
71
72     errors.putAll(errorsByFileName);
73
74     if (status == UploadFileStatus.Failure) {
75       return;
76     }
77     for (Map.Entry<String, List<ErrorMessage>> entry : errorsByFileName.entrySet()) {
78       for (ErrorMessage errorMessage : entry.getValue()) {
79         if (errorMessage.getLevel() == ErrorLevel.ERROR) {
80           status = UploadFileStatus.Failure;
81           return;
82         }
83       }
84     }
85   }
86
87   /**
88    * Add error message to map.
89    *
90    * @param key   the key
91    * @param error the error
92    * @param level the level
93    */
94   public void addErrorMessageToMap(String key, String error, ErrorLevel level) {
95     ErrorMessage errorMessage = new ErrorMessage(level, error);
96     List<ErrorMessage> errorMessages = getErrorList(key);
97
98     errorMessages.add(errorMessage);
99     this.errors.put(key, errorMessages);
100
101     if (level.equals(ErrorLevel.ERROR)) {
102       status = UploadFileStatus.Failure;
103     }
104   }
105
106   private List<ErrorMessage> getErrorList(String key) {
107     List<ErrorMessage> errorMessages = this.errors.get(key);
108     if (CollectionUtils.isEmpty(errorMessages)) {
109       errorMessages = new ArrayList<>();
110     }
111
112     return errorMessages;
113   }
114
115   public Map<String, List<ErrorMessage>> getErrors() {
116     return errors;
117   }
118 }