[SDC-29] Amdocs OnBoard 1707 initial commit.
[sdc.git] / openecomp-be / lib / openecomp-sdc-validation-lib / openecomp-sdc-validation-impl / src / main / java / org / openecomp / sdc / validation / tos / ContrailResourcesMappingTo.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.validation.tos;
22
23 import org.apache.commons.collections4.MapUtils;
24
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29
30
31 public class ContrailResourcesMappingTo {
32   private Map<String, List<String>> contrailV1Resources;
33   private Map<String, List<String>> contrailV2Resources;
34
35   /**
36    * Add ContrailV1Resource.
37    *
38    * @param fileName       the file name
39    * @param resourceName   the resource name
40    */
41   public void addContrailV1Resource(String fileName, String resourceName) {
42     if (MapUtils.isEmpty(contrailV1Resources)) {
43       contrailV1Resources = new HashMap<>();
44     }
45     contrailV1Resources.putIfAbsent(fileName, new ArrayList<>());
46     contrailV1Resources.get(fileName).add(resourceName);
47   }
48
49   /**
50    * Add ContrailV1Resource.
51    *
52    * @param fileName       the file name
53    * @param resourceName   the resource name
54    */
55   public void addContrailV2Resource(String fileName, String resourceName) {
56     if (MapUtils.isEmpty(contrailV2Resources)) {
57       contrailV2Resources = new HashMap<>();
58     }
59     contrailV2Resources.putIfAbsent(fileName, new ArrayList<>());
60     contrailV2Resources.get(fileName).add(resourceName);
61   }
62
63   public void addAll(ContrailResourcesMappingTo contrailResourcesMappingTo) {
64     addContrailV1Resources(contrailResourcesMappingTo.getContrailV1Resources());
65     addContrailV2Resources(contrailResourcesMappingTo.getContrailV2Resources());
66   }
67
68   public String fetchContrailV1Resources() {
69     return fetchContrailResources(contrailV1Resources);
70   }
71
72   public String fetchContrailV2Resources() {
73     return fetchContrailResources(contrailV2Resources);
74   }
75
76   private void addContrailV1Resources(Map<String, List<String>> contrailV1Resources) {
77     if (!MapUtils.isEmpty(contrailV1Resources)) {
78       for (Map.Entry<String, List<String>> fileResourcesEntry : contrailV1Resources.entrySet()) {
79         for (String resourceName : fileResourcesEntry.getValue()) {
80           this.addContrailV1Resource(fileResourcesEntry.getKey(), resourceName);
81         }
82       }
83     }
84   }
85
86   private void addContrailV2Resources(Map<String, List<String>> contrailV2Resources) {
87     if (!MapUtils.isEmpty(contrailV2Resources)) {
88       for (Map.Entry<String, List<String>> fileResourcesEntry : contrailV2Resources.entrySet()) {
89         for (String resourceName : fileResourcesEntry.getValue()) {
90           this.addContrailV2Resource(fileResourcesEntry.getKey(), resourceName);
91         }
92       }
93     }
94   }
95
96   private String fetchContrailResources(Map<String, List<String>> contrailResources) {
97     StringBuilder buffer = new StringBuilder();
98     if (MapUtils.isEmpty(contrailResources)) {
99       return "";
100     }
101     for (Map.Entry<String, List<String>> fileResourcesEntry : contrailResources.entrySet()) {
102       buffer.append(" file '").append(fileResourcesEntry.getKey()).append("' , resources :");
103       for (String resourceName : fileResourcesEntry.getValue()) {
104         buffer.append("'").append(resourceName).append("', ");
105       }
106     }
107     buffer.deleteCharAt(buffer.lastIndexOf(","));
108     return buffer.toString();
109   }
110
111   public Map<String, List<String>> getContrailV1Resources() {
112     return contrailV1Resources;
113   }
114
115   public Map<String, List<String>> getContrailV2Resources() {
116     return contrailV2Resources;
117   }
118 }