aboutsummaryrefslogtreecommitdiffstats
path: root/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/helper/ResourceTranslationNeutronPortHelper.java
blob: a99c95c148d84002a22800ba60bbc320ff8dace9 (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package org.openecomp.sdc.translator.services.heattotosca.helper;

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

public class ResourceTranslationNeutronPortHelper {

  public static final String IP_COUNT_REQUIRED = "ip_count_required";
  public static final String FLOATING_IP_COUNT_REQUIRED = "floating_ip_count_required";
  public static final String NETWORK = "network";
  public static final String NETWORK_ROLE_TAG = "network_role_tag";
  public static final String FIXED_IPS = "fixed_ips";
  public static final String IP_VERSION = "ip_version";
  public static final String IP_ADDRESS = "ip_address";
  public static final String GET_INPUT = "get_input";
  public static final String ALLOWED_ADDRESS_PAIRS = "allowed_address_pairs";
  public static final String FLOATING_IP = "_floating_ip";
  public static final String FLOATING_V6_IP = "_floating_v6_ip";
  public static final String IPS = "_ips";
  public static final String V6_IPS = "_v6_ips";
  public static final String NET_NAME = "_net_name";
  public static final String NET_ID = "_net_id";
  public static final String NET_FQDN = "_net_fqdn";
  public static final String IPV4_REGEX = "\\w*_ip_\\d+";
  public static final String IPV6_REGEX = "\\w*_v6_ip_\\d+";
  public static final String MAC_COUNT_REQUIRED = "mac_count_required";
  public static final String MAC_ADDRESS = "mac_address";
  public static final String IS_REQUIRED = "is_required";
  public static final String IP_REQUIREMENTS = "ip_requirements";
  public static final String MAC_REQUIREMENTS = "mac_requirements";

  public void setAdditionalProperties(Map<String, Object> properties) {
    setNetworkRoleTag(properties);
    Map<String, Object> ipRequirements = new HashMap();
    Map<String, Object> macRequirements = new HashMap();
    Map<String, Object> isRequired = new HashMap();
    Map<String, Object> floatingIsRequired = new HashMap();
    Map<String, Object> macIsRequired = new HashMap();

    isRequired.put(IS_REQUIRED, Boolean.FALSE);
    floatingIsRequired.put(IS_REQUIRED, Boolean.FALSE);
    macIsRequired.put(IS_REQUIRED, Boolean.FALSE);

    ipRequirements.put(IP_COUNT_REQUIRED, isRequired);
    ipRequirements.put(FLOATING_IP_COUNT_REQUIRED, floatingIsRequired);
    ipRequirements.put(IP_VERSION, 4);
    macRequirements.put(MAC_COUNT_REQUIRED, macIsRequired);

    List<Map<String, Object>> ipRequirementsList = new ArrayList<>();
    ipRequirementsList.add(ipRequirements);
    properties.put(IP_REQUIREMENTS , ipRequirementsList);

    properties.put(MAC_REQUIREMENTS , macRequirements);

    setIpVersion(properties);
    setFloatingIpVersion(properties);

    setMacCount(properties);
  }

  private void setMacCount(Map<String, Object> properties) {
    if(properties.containsKey(MAC_ADDRESS)) {
      Map<String, Object> macRequirements = (Map<String, Object>) properties.get(MAC_REQUIREMENTS);
      Map<String, Object> macIsRequired = new HashMap();
      macIsRequired.put(IS_REQUIRED, Boolean.TRUE);
      macRequirements.put(MAC_COUNT_REQUIRED, macIsRequired);
      properties.put(MAC_REQUIREMENTS, macRequirements);
    }
  }

  private void setFloatingIpVersion(Map<String, Object> properties) {
    List<Map<String, Object>> ipRequirementsList =
        (List<Map<String, Object>>) properties.get(IP_REQUIREMENTS);
    Map<String, Object> ipRequirements = ipRequirementsList.get(0);
    Object propertyValue;
    Map<String, Object> isRequired = new HashMap();
    isRequired.put(IS_REQUIRED, Boolean.TRUE);

    propertyValue = properties.get(ALLOWED_ADDRESS_PAIRS);
    if (propertyValue instanceof Map && !((Map) propertyValue).isEmpty()) {
      Map.Entry<String, Object> mapEntry =
          (Map.Entry<String, Object>) ((Map) propertyValue).entrySet().iterator().next();
      if (getFloatingIpVersion(mapEntry.getValue()) != null) {
        ipRequirements.put(IP_VERSION, getFloatingIpVersion(mapEntry.getValue()));
        ipRequirements.put(FLOATING_IP_COUNT_REQUIRED, isRequired);
      }
    }
    else if (propertyValue instanceof List && !((List) propertyValue).isEmpty()) {
      for (int i = 0; i < ((List) propertyValue).size(); i++) {
        Object ipMap = ((List) propertyValue).get(i);
        if(ipMap instanceof Map && !((Map) ipMap).isEmpty()) {
          Object ipAddressMap = ((Map) ipMap).get(IP_ADDRESS);
          if (ipAddressMap instanceof Map && !((Map) ipAddressMap).isEmpty()) {
            Object ipList = ((Map) ipAddressMap).get(GET_INPUT);
            if (ipList instanceof String && !((String) ipList).isEmpty()) {
              if (getFloatingIpVersion(ipList) != null) {
                ipRequirements.put(IP_VERSION, getFloatingIpVersion(ipList));
                ipRequirements.put(FLOATING_IP_COUNT_REQUIRED, isRequired);
              }
            }
          }
        }
      }
    }
  }

  private void setIpVersion(Map<String, Object> properties) {
    List<Map<String, Object>> ipRequirementsList =
        (List<Map<String, Object>>) properties.get(IP_REQUIREMENTS);
    Map<String, Object> ipRequirements = ipRequirementsList.get(0);
    Object propertyValue;
    Map<String, Object> isRequired = new HashMap();
    isRequired.put(IS_REQUIRED, Boolean.TRUE);

    propertyValue = properties.get(FIXED_IPS);
    if (propertyValue instanceof Map && !((Map) propertyValue).isEmpty()) {
      Map.Entry<String, Object> mapEntry =
          (Map.Entry<String, Object>) ((Map) propertyValue).entrySet().iterator().next();
      if (getIpVersion(mapEntry.getValue()) != null) {
        ipRequirements.put(IP_VERSION, getIpVersion(mapEntry.getValue()));
        ipRequirements.put(IP_COUNT_REQUIRED, isRequired);
      }
    }
    else if (propertyValue instanceof List && !((List) propertyValue).isEmpty()) {
      for (int i = 0; i < ((List) propertyValue).size(); i++) {
        Object ipMap = ((List) propertyValue).get(i);
        if(ipMap instanceof Map && !((Map) ipMap).isEmpty()) {
          Object ipAddressMap = ((Map) ipMap).get(IP_ADDRESS);
          if (ipAddressMap instanceof Map && !((Map) ipAddressMap).isEmpty()) {
            Object ipList = ((Map) ipAddressMap).get(GET_INPUT);
            if (ipList instanceof List && !((List) ipList).isEmpty()) {
              if (getIpVersion(((List) ipList).get(0)) != null) {
                ipRequirements.put(IP_VERSION, getIpVersion(((List) ipList).get(0)));
                ipRequirements.put(IP_COUNT_REQUIRED, isRequired);
              }
            }
            else if (ipList instanceof String && !((String) ipList).isEmpty()) {
              if (getIpVersion(ipList) != null) {
                ipRequirements.put(IP_VERSION, getIpVersion(ipList));
                ipRequirements.put(IP_COUNT_REQUIRED, isRequired);
              }
            }
          }
        }
      }
    }
  }

  private void setNetworkRoleTag(Map<String, Object> properties) {
    Object propertyValue = properties.get(NETWORK);
    if (propertyValue instanceof Map && !((Map) propertyValue).isEmpty()) {
      Map.Entry<String, String> mapEntry =
          (Map.Entry<String, String>) ((Map) propertyValue).entrySet().iterator().next();
      if (mapEntry.getValue() instanceof String && getNetworkRole(mapEntry.getValue())!=null) {
        properties.put(NETWORK_ROLE_TAG, getNetworkRole(mapEntry.getValue()));
      }
    }
  }

  private Object getFloatingIpVersion(Object value) {
    Object ipVersion = null;
    if(value instanceof String) {
      if (((String) value).endsWith(FLOATING_V6_IP)) {
        ipVersion = 6;
      }
      else {
        ipVersion = 4;
      }
    }
    return ipVersion;
  }

  private Object getIpVersion(Object value) {
    Object ipVersion = null;
    if(value instanceof String) {
      if (((String) value).endsWith(V6_IPS) || ((String) value).matches(IPV6_REGEX)) {
        ipVersion = 6;
      }
      else {
        ipVersion = 4;
      }
    }
    return ipVersion;
  }

  private Object getNetworkRole(String value) {
    Object networkRole = null;
    if(value.endsWith(NET_NAME)) {
      networkRole = (Object) value.substring(0, value.length() - NET_NAME.length());
    }
    else if(value.endsWith(NET_ID)) {
      networkRole = (Object) value.substring(0, value.length() - NET_ID.length());
    }
    else if(value.endsWith(NET_FQDN)) {
      networkRole = (Object) value.substring(0, value.length() - NET_FQDN.length());
    }
    return networkRole;
  }
}