aboutsummaryrefslogtreecommitdiffstats
path: root/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/main/java/org/openecomp/sdc/validation/tos/ContrailResourcesMappingTo.java
blob: f351af0992ea7ac5cf42cd39b83d2dadbc10ac2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.sdc.validation.tos;

import org.apache.commons.collections4.MapUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class ContrailResourcesMappingTo {
  private Map<String, List<String>> contrailV1Resources;
  private Map<String, List<String>> contrailV2Resources;

  /**
   * Add ContrailV1Resource.
   *
   * @param fileName       the file name
   * @param resourceName   the resource name
   */
  public void addContrailV1Resource(String fileName, String resourceName) {
    if (MapUtils.isEmpty(contrailV1Resources)) {
      contrailV1Resources = new HashMap<>();
    }
    contrailV1Resources.putIfAbsent(fileName, new ArrayList<>());
    contrailV1Resources.get(fileName).add(resourceName);
  }

  /**
   * Add ContrailV1Resource.
   *
   * @param fileName       the file name
   * @param resourceName   the resource name
   */
  public void addContrailV2Resource(String fileName, String resourceName) {
    if (MapUtils.isEmpty(contrailV2Resources)) {
      contrailV2Resources = new HashMap<>();
    }
    contrailV2Resources.putIfAbsent(fileName, new ArrayList<>());
    contrailV2Resources.get(fileName).add(resourceName);
  }

  public void addAll(ContrailResourcesMappingTo contrailResourcesMappingTo) {
    addContrailV1Resources(contrailResourcesMappingTo.getContrailV1Resources());
    addContrailV2Resources(contrailResourcesMappingTo.getContrailV2Resources());
  }

  public String fetchContrailV1Resources() {
    return fetchContrailResources(contrailV1Resources);
  }

  public String fetchContrailV2Resources() {
    return fetchContrailResources(contrailV2Resources);
  }

  private void addContrailV1Resources(Map<String, List<String>> contrailV1Resources) {
    if (!MapUtils.isEmpty(contrailV1Resources)) {
      for (Map.Entry<String, List<String>> fileResourcesEntry : contrailV1Resources.entrySet()) {
        for (String resourceName : fileResourcesEntry.getValue()) {
          this.addContrailV1Resource(fileResourcesEntry.getKey(), resourceName);
        }
      }
    }
  }

  private void addContrailV2Resources(Map<String, List<String>> contrailV2Resources) {
    if (!MapUtils.isEmpty(contrailV2Resources)) {
      for (Map.Entry<String, List<String>> fileResourcesEntry : contrailV2Resources.entrySet()) {
        for (String resourceName : fileResourcesEntry.getValue()) {
          this.addContrailV2Resource(fileResourcesEntry.getKey(), resourceName);
        }
      }
    }
  }

  private String fetchContrailResources(Map<String, List<String>> contrailResources) {
    StringBuilder buffer = new StringBuilder();
    if (MapUtils.isEmpty(contrailResources)) {
      return "";
    }
    for (Map.Entry<String, List<String>> fileResourcesEntry : contrailResources.entrySet()) {
      buffer.append(" file '").append(fileResourcesEntry.getKey()).append("' , resources :");
      for (String resourceName : fileResourcesEntry.getValue()) {
        buffer.append("'").append(resourceName).append("', ");
      }
    }
    buffer.deleteCharAt(buffer.lastIndexOf(","));
    return buffer.toString();
  }

  public Map<String, List<String>> getContrailV1Resources() {
    return contrailV1Resources;
  }

  public Map<String, List<String>> getContrailV2Resources() {
    return contrailV2Resources;
  }
}