Changed to unmaintained
[appc.git] / appc-outbound / appc-aai-client / provider / src / main / java / org / onap / appc / aai / client / aai / AaiService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * ================================================================================
9  * Modifications (C) 2019 Ericsson
10  * Modifications (C) 2019 IBM
11  * =============================================================================
12  * Licensed under the Apache License, Version 2.0 (the "License");
13  * you may not use this file except in compliance with the License.
14  * You may obtain a copy of the License at
15  *
16  *      http://www.apache.org/licenses/LICENSE-2.0
17  *
18  * Unless required by applicable law or agreed to in writing, software
19  * distributed under the License is distributed on an "AS IS" BASIS,
20  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  * See the License for the specific language governing permissions and
22  * limitations under the License.
23  *
24  * ============LICENSE_END=========================================================
25  */
26
27 package org.onap.appc.aai.client.aai;
28
29 import com.att.eelf.configuration.EELFLogger;
30 import com.att.eelf.configuration.EELFManager;
31 import org.apache.commons.lang3.StringUtils;
32 import org.apache.commons.lang3.math.NumberUtils;
33 import org.onap.appc.aai.client.AppcAaiClientConstant;
34 import org.onap.ccsdk.sli.adaptors.aai.AAIClient;
35 import org.onap.ccsdk.sli.adaptors.aai.AAIService;
36 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
37 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
38 import org.onap.ccsdk.sli.core.sli.SvcLogicResource;
39 import org.osgi.framework.BundleContext;
40 import org.osgi.framework.FrameworkUtil;
41 import org.osgi.framework.ServiceReference;
42
43 import java.util.ArrayList;
44 import java.util.HashMap;
45 import java.util.List;
46 import java.util.Map;
47
48 public class AaiService {
49
50     private static final EELFLogger log = EELFManager.getInstance().getLogger(AaiService.class);
51     private static final String STR_VNF_ID = "generic-vnf.vnf-id = '";
52     private static final String STR_VNFC_REF = "vnfcReference[";
53     private static final String STR_VNFC_REF_KEY = "VNFCREFKEY ";
54     private static final String STR_AAI_REF_KEY = "AAIREFKEY ";
55     private static final String STR_RELATIONSHIP_LIST = ".relationship-list.relationship[";
56     private static final String STR_VNFC_NAME = "vnfc.vnfc-name = '";
57     private static final String QUERY_STR_VNFC_NAME = "VNFCNAME IN INSERTVNFCS ";
58
59     private static final String PARAM_GENERIC_VNF = "generic-vnf";
60     private static final String PARAM_VNF_INFO = "vnfInfo";
61     private static final String PARAM_VSERVER = "vserver";
62     private static final String PARAM_VM_INFO = "vmInfo";
63     private static final String PARAM_PROV_STATUS = "prov-status";
64     private static final String PARAM_VAL_NVTPROV = "NVTPROV";
65
66     private static final String ATTR_VSERVER_ID = "vserver-id";
67     private static final String ATTR_TENANT_ID = "tenant-id";
68     private static final String ATTR_CLOUD_OWNER = "cloud-owner";
69     private static final String ATTR_CLOUD_REGION_ID = "cloud-region-id";
70     private static final String ATTR_VNFC_COUNT = "vm.vnfc-count";
71     private static final String ATTR_VNFC_NAME = "vnfc-name";
72     private static final String ATTR_VNFC_FUNC_CODE = "VNFC-FUNCTION-CODE";
73     private static final String ATTR_VSERVER_NAME = "vserver-name";
74     private static final String ATTR_VNF_ID = "vnf-id";
75     private static final String ATTR_VF_MODULE_ID = "vf-module-id";
76
77     private AAIClient aaiClient;
78
79     public AaiService(AAIClient aaiClient) {
80         this.aaiClient = aaiClient;
81     }
82
83     public AaiService() {
84         BundleContext bctx = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
85         ServiceReference sref = bctx.getServiceReference(AAIService.class);
86         aaiClient = (AAIClient) bctx.getService(sref);
87     }
88
89     public void getGenericVnfInfo(Map<String, String> params, SvcLogicContext ctx)
90         throws AaiServiceInternalException, SvcLogicException {
91
92         String vnfId = params.get("vnfId");
93         if (StringUtils.isBlank(vnfId)) {
94             throw new AaiServiceInternalException("VnfId is missing");
95         }
96         String prefix = params.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
97         prefix = StringUtils.isNotBlank(prefix) ? (prefix + ".") : "";
98         String resourceKey = STR_VNF_ID + vnfId + "'";
99         SvcLogicContext vnfCtx = readResource(resourceKey, PARAM_VNF_INFO, PARAM_GENERIC_VNF);
100
101         ctx.setAttribute(prefix + "vnf.vnf-name", vnfCtx.getAttribute("vnfInfo.vnf-name"));
102         ctx.setAttribute(prefix + "vnf.vnf-type", vnfCtx.getAttribute("vnfInfo.vnf-type"));
103         ctx.setAttribute(prefix + "vnf.prov-status", vnfCtx.getAttribute("vnfInfo.prov-status"));
104         ctx.setAttribute(prefix + "vnf.orchestration-status", vnfCtx.getAttribute("vnfInfo.orchestration-status"));
105         ctx.setAttribute(prefix + "vnf.ipv4-oam-address", vnfCtx.getAttribute("vnfInfo.ipv4-oam-address"));
106
107         int vmCount = 0;
108         String relLen = vnfCtx.getAttribute("vnfInfo.relationship-list.relationship_length");
109         int relationshipLength = 0;
110         if (relLen != null) {
111             relationshipLength = Integer.parseInt(relLen);
112         }
113         log.info("RELLEN " + relationshipLength);
114         for (int i = 0; i < relationshipLength; i++) {
115             String vserverId = getRelationshipValue(i, vnfCtx, PARAM_VSERVER, "vserver.vserver-id", PARAM_VNF_INFO);
116             String tenantId = getRelationshipValue(i, vnfCtx, PARAM_VSERVER, "tenant.tenant-id", PARAM_VNF_INFO);
117             String cloudOwner = getRelationshipValue(i, vnfCtx, PARAM_VSERVER, "cloud-region.cloud-owner",
118                 PARAM_VNF_INFO);
119             String cloudRegionId =
120                 getRelationshipValue(i, vnfCtx, PARAM_VSERVER, "cloud-region.cloud-region-id", PARAM_VNF_INFO);
121             if (vserverId != null) {
122                 log.info("VSERVER KEYS " + vserverId + " " + tenantId + " " + cloudOwner + " " + cloudRegionId);
123                 String vnfPrefix = prefix + "vm[" + vmCount + "].";
124                 ctx.setAttribute(vnfPrefix + ATTR_VSERVER_ID, vserverId);
125                 ctx.setAttribute(vnfPrefix + ATTR_TENANT_ID, tenantId);
126                 ctx.setAttribute(vnfPrefix + ATTR_CLOUD_OWNER, cloudOwner);
127                 ctx.setAttribute(vnfPrefix + ATTR_CLOUD_REGION_ID, cloudRegionId);
128                 vmCount++;
129             }
130         }
131         ctx.setAttribute(prefix + "vm-count", String.valueOf(vmCount));
132         log.info("VMCOUNT FROM VNF INFO " + ctx.getAttribute(prefix + "vm-count"));
133     }
134
135     public void getVMInfo(Map<String, String> params, SvcLogicContext ctx)
136         throws SvcLogicException {
137
138         try {
139             log.info("Received getVmInfo call with params : " + params);
140             String prefix = params.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
141             prefix = StringUtils.isNotBlank(prefix) ? (prefix + ".") : "";
142             int vnfcCount = 0;
143             ctx.setAttribute(prefix + ATTR_VNFC_COUNT, String.valueOf(vnfcCount)); // In case no vnfcs are found
144
145             VServerInfo vServerInfo = new VServerInfo(params);
146
147             String resourceKey =
148                 "vserver.vserver-id = '" + vServerInfo.getVserverId() + "' AND tenant.tenant-id = '" + vServerInfo
149                     .getTenantId()
150                     + "' AND cloud-region.cloud-owner = '" + vServerInfo.getCloudOwner()
151                     + "' AND cloud-region.cloud-region-id = '"
152                     + vServerInfo.getCloudRegionId() + "'";
153
154             SvcLogicContext vmCtx = readResource(resourceKey, PARAM_VM_INFO, PARAM_VSERVER);
155             ctx.setAttribute(prefix + "vm.prov-status", vmCtx.getAttribute("vmInfo.prov-status"));
156             ctx.setAttribute(prefix + "vm.vserver-name", vmCtx.getAttribute("vmInfo.vserver-name"));
157             ctx.setAttribute(prefix + "vm.vserver-selflink", vmCtx.getAttribute("vmInfo.vserver-selflink"));
158
159             String relLen = vmCtx.getAttribute("vmInfo.relationship-list.relationship_length");
160             int relationshipLength = 0;
161             if (relLen != null) {
162                 relationshipLength = Integer.parseInt(relLen);
163             }
164             log.info("RELLEN" + relationshipLength);
165             for (int i = 0; i < relationshipLength; i++) {
166                 String vfModuleId = getRelationshipValue(i, vmCtx, "vf-module", "vf-module.vf-module-id",
167                     PARAM_VM_INFO);
168                 if (vfModuleId != null) {
169                     ctx.setAttribute(prefix + "vm.vf-module-id", vfModuleId);
170                 }
171
172                 String vnfcName = getRelationshipValue(i, vmCtx, "vnfc", "vnfc.vnfc-name", PARAM_VM_INFO);
173                 if (vnfcName != null) {
174                     ctx.setAttribute(prefix + "vm.vnfc[" + vnfcCount + "].vnfc-name", vnfcName);
175                     vnfcCount++;
176                 }
177
178             } // relationshipLength
179             ctx.setAttribute(prefix + ATTR_VNFC_COUNT, String.valueOf(vnfcCount));
180             log.info("VSERVERNAME " + ctx.getAttribute(prefix + "vm.vserver-name") + " HAS NUM VNFCS = "
181                 + ctx.getAttribute(prefix + ATTR_VNFC_COUNT));
182         } catch (Exception e) {
183             log.error("An error occurred when fetching Vm info", e);
184             throw new SvcLogicException("Failed to fetch VM info", e);
185         }
186     }
187
188     private String getRelationshipValue(int i, SvcLogicContext ctx, String relatedTo, String relationshipKey,
189         String prefix) {
190
191         if (relatedTo.equals(ctx.getAttribute(prefix + STR_RELATIONSHIP_LIST + i + "].related-to"))) {
192             log.info("RELATEDTO " + relatedTo);
193             int relationshipDataLength = 0;
194             String relDataLen =
195                 ctx.getAttribute(prefix + STR_RELATIONSHIP_LIST + i + "].relationship-data_length");
196             if (relDataLen != null) {
197                 relationshipDataLength = Integer.parseInt(relDataLen);
198             }
199
200             for (int j = 0; j < relationshipDataLength; j++) {
201
202                 String key = ctx.getAttribute(prefix + STR_RELATIONSHIP_LIST + i + "].relationship-data["
203                     + j + "].relationship-key");
204
205                 String value = ctx.getAttribute(prefix + STR_RELATIONSHIP_LIST + i + "].relationship-data["
206                     + j + "].relationship-value");
207
208                 log.info("GENERIC KEY " + key);
209                 log.info("GENERIC VALUE " + value);
210
211                 if (relationshipKey.equals(key)) {
212                     return value;
213                 }
214
215             } // relationshipDataLength
216         } // if related-To
217
218         return null;
219     }
220
221     public void getVnfcInfo(Map<String, String> params, SvcLogicContext ctx)
222         throws AaiServiceInternalException, SvcLogicException {
223         log.info("Received getVnfc call with params : " + params);
224
225         String prefix = params.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
226         prefix = StringUtils.isNotBlank(prefix) ? (prefix + ".") : "";
227
228         String vnfcName = params.get("vnfcName");
229         if (StringUtils.isBlank(vnfcName)) {
230             throw new AaiServiceInternalException("Vnfc Name is missing");
231         }
232
233         String resourceKey = STR_VNFC_NAME + vnfcName + "'";
234         SvcLogicContext vnfcCtx = readResource(resourceKey, "vnfcInfo", "vnfc");
235
236         // Changes for US 315820 for 1710 vnfc-type renamed to nfc-function,vnfc-function-code renamed to
237         // nfc-naming-code
238
239         ctx.setAttribute(prefix + "vnfc.vnfc-type", vnfcCtx.getAttribute("vnfcInfo.nfc-function"));
240         ctx.setAttribute(prefix + "vnfc.vnfc-function-code", vnfcCtx.getAttribute("vnfcInfo.nfc-naming-code"));
241         ctx.setAttribute(prefix + "vnfc.group-notation", vnfcCtx.getAttribute("vnfcInfo.group-notation"));
242         ctx.setAttribute(prefix + "vnfc.ipaddress-v4-oam-vip", vnfcCtx.getAttribute("vnfcInfo.ipaddress-v4-oam-vip"));
243
244     }
245
246     public void insertVnfcs(Map<String, String> params, SvcLogicContext ctx, int vmCount,
247                             String vfModuleIdFromRequest)
248         throws AaiServiceInternalException, SvcLogicException {
249         log.info("Received insertVnfcs call with params : " + params);
250
251         String prefix = params.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
252
253         prefix = StringUtils.isNotBlank(prefix) ? (prefix + ".") : "";
254         int vnfcRefIndx = 0;
255         log.debug("vnfcRefIndx" + vnfcRefIndx);
256         for (int i = 0; i < vmCount; i++) {
257             String aaiRefKey = prefix + "vm[" + i + "].";
258
259             //ConfigScaleOut - Do not process vms that are not associated with vfmodule id if vfmodule id is present
260             if (StringUtils.isNotBlank(vfModuleIdFromRequest)) {
261                 String vmVfModuleId = ctx.getAttribute(aaiRefKey + ATTR_VF_MODULE_ID);
262                 log.info("insertVnfcs():::vfModule for vm is=" + vmVfModuleId);
263                 if (StringUtils.isBlank(vmVfModuleId) || !StringUtils
264                     .equalsIgnoreCase(vmVfModuleId, vfModuleIdFromRequest)) {
265                     continue;
266                 }
267             }
268
269             log.info(QUERY_STR_VNFC_NAME + ctx.getAttribute(aaiRefKey + ATTR_VNFC_NAME));
270             String vnfcNameAAI = ctx.getAttribute(aaiRefKey + ATTR_VNFC_NAME);
271             // Get Vnfc_reference data from the table
272             String vnfcRefKey = STR_VNFC_REF + vnfcRefIndx + "].";
273
274             log.info(STR_VNFC_REF_KEY + vnfcRefKey);
275             log.info(STR_AAI_REF_KEY + aaiRefKey);
276
277             String groupNotationType = ctx.getAttribute(vnfcRefKey + "GROUP-NOTATION-TYPE");
278             String groupNotationValue = ctx.getAttribute(vnfcRefKey + "GROUP-NOTATION-VALUE");
279             String vnfcType = ctx.getAttribute(vnfcRefKey + "VNFC-TYPE");
280             String vnfcFuncCode = ctx.getAttribute(vnfcRefKey + ATTR_VNFC_FUNC_CODE);
281             String populateIpAddressV4OamVip = ctx.getAttribute(vnfcRefKey + "IPADDRESS-V4-OAM-VIP");
282
283             // Get vnfc Data to be added
284             String vserverName = ctx.getAttribute(aaiRefKey + ATTR_VSERVER_NAME);
285             String vnfcName = vserverName + vnfcFuncCode + "001";
286             String groupNotation = getGroupNotation(groupNotationType, groupNotationValue, vnfcName, vserverName,
287                 prefix, ctx, vnfcType, vnfcFuncCode, vmCount);
288
289             String ipAddressV4OamVip = null;
290             if ("Y".equals(populateIpAddressV4OamVip)) {
291                 ipAddressV4OamVip = ctx.getAttribute("vnf-host-ip-address"); // from input
292             }
293             Map<String, String> vnfcParams =
294                 populateVnfcParams(ctx, aaiRefKey, ipAddressV4OamVip, groupNotation, vnfcType, vnfcFuncCode);
295
296             log.info("Vnfc name from AAI: " + vnfcNameAAI);
297             log.info("Vnfc name generated: " + vnfcName);
298
299             if (StringUtils.isNotBlank(vnfcNameAAI)) {
300                 if (vnfcName.equalsIgnoreCase(vnfcNameAAI)) {
301                     updateVnfcStatus(vnfcNameAAI, params, prefix);
302                     vnfcRefIndx++;
303                 }
304                 continue;
305             }
306             vnfcRefIndx++;
307             addVnfc(vnfcName, vnfcParams, prefix);
308
309             // Add VNFC Info to context for current added VNFC
310             ctx.setAttribute(aaiRefKey + ATTR_VNFC_NAME, vnfcName);
311             ctx.setAttribute(aaiRefKey + "vnfc-type", vnfcType);
312             ctx.setAttribute(aaiRefKey + "vnfc-function-code", vnfcFuncCode);
313             ctx.setAttribute(aaiRefKey + "group-notation", groupNotation);
314         }
315     }
316
317     public List<String> getVnfcData(Map<String, String> params, SvcLogicContext ctx, int vmCount) {
318
319         String prefix = params.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
320         prefix = StringUtils.isNotBlank(prefix) ? (prefix + ".") : "";
321         List<String> vnfcNames = new ArrayList<>();
322         int vnfcRefIndx = -1;
323         for (int i = 0; i < vmCount; i++) {
324             String aaiRefKey = prefix + "vm[" + i + "].";
325             log.info(QUERY_STR_VNFC_NAME + ctx.getAttribute(aaiRefKey + ATTR_VNFC_NAME));
326             if (ctx.getAttribute(aaiRefKey + ATTR_VNFC_NAME) != null) {
327                 continue;
328             } else {
329                 vnfcRefIndx++;
330             }
331             String vnfcRefKey = STR_VNFC_REF + vnfcRefIndx + "].";
332             log.info(STR_VNFC_REF_KEY + vnfcRefKey);
333             log.info(STR_AAI_REF_KEY + aaiRefKey);
334             String vnfcFuncCode = ctx.getAttribute(vnfcRefKey + ATTR_VNFC_FUNC_CODE);
335
336             // Get vnfc Data to be added
337             String vserverName = ctx.getAttribute(aaiRefKey + ATTR_VSERVER_NAME);
338             String vnfcName = vserverName + vnfcFuncCode + "001";
339
340             vnfcNames.add(vnfcName);
341         }
342
343         return vnfcNames;
344     }
345
346     private Map<String, String> populateVnfcParams(SvcLogicContext ctx, String aaiRefKey, String ipAddressV4OamVip,
347         String groupNotation, String vnfcType, String vnfcFuncCode) {
348
349         Map<String, String> vnfcParams = new HashMap<>();
350
351         // Changes for vnfc-type renamed to nfc-function,vnfc-function-code renamed to
352         // nfc-naming-code
353         vnfcParams.put("nfc-naming-code", vnfcFuncCode);
354         vnfcParams.put("nfc-function", vnfcType);
355         vnfcParams.put("ipaddress-v4-oam-vip", ipAddressV4OamVip);
356         vnfcParams.put(PARAM_PROV_STATUS, PARAM_VAL_NVTPROV);
357         vnfcParams.put("orchestration-status", "CONFIGURED");
358         vnfcParams.put("in-maint", "false");
359         vnfcParams.put("is-closed-loop", "false");
360         vnfcParams.put("group-notation", groupNotation);
361         vnfcParams.put("relationship-list.relationship[0].related-to", PARAM_VSERVER);
362         vnfcParams.put("relationship-list.relationship[0].relationship-data[0].relationship-key", "vserver.vserver-id");
363         vnfcParams.put("relationship-list.relationship[0].relationship-data[0].relationship-value",
364             ctx.getAttribute(aaiRefKey + ATTR_VSERVER_ID));
365         vnfcParams.put("relationship-list.relationship[0].relationship-data[1].relationship-key", "tenant.tenant-id");
366         vnfcParams.put("relationship-list.relationship[0].relationship-data[1].relationship-value",
367             ctx.getAttribute(aaiRefKey + ATTR_TENANT_ID));
368         vnfcParams.put("relationship-list.relationship[0].relationship-data[2].relationship-key",
369             "cloud-region.cloud-owner");
370         vnfcParams.put("relationship-list.relationship[0].relationship-data[2].relationship-value",
371             ctx.getAttribute(aaiRefKey + ATTR_CLOUD_OWNER));
372         vnfcParams.put("relationship-list.relationship[0].relationship-data[3].relationship-key",
373             "cloud-region.cloud-region-id");
374         vnfcParams.put("relationship-list.relationship[0].relationship-data[3].relationship-value",
375             ctx.getAttribute(aaiRefKey + ATTR_CLOUD_REGION_ID));
376         vnfcParams.put("relationship-list.relationship[1].related-to", PARAM_GENERIC_VNF);
377         vnfcParams.put("relationship-list.relationship[1].relationship-data[0].relationship-key", "generic-vnf.vnf-id");
378         vnfcParams.put("relationship-list.relationship[1].relationship-data[0].relationship-value",
379             ctx.getAttribute(ATTR_VNF_ID));
380         vnfcParams.put("relationship-list.relationship[2].related-to", "vf-module");
381         vnfcParams.put("relationship-list.relationship[2].relationship-data[0].relationship-key", "generic-vnf.vnf-id");
382         vnfcParams.put("relationship-list.relationship[2].relationship-data[0].relationship-value",
383             ctx.getAttribute(ATTR_VNF_ID));
384         vnfcParams.put("relationship-list.relationship[2].relationship-data[1].relationship-key",
385             "vf-module.vf-module-id");
386         vnfcParams.put("relationship-list.relationship[2].relationship-data[1].relationship-value",
387             ctx.getAttribute(aaiRefKey + ATTR_VF_MODULE_ID));
388
389         return vnfcParams;
390     }
391
392     public void addVnfc(String vnfcName, Map<String, String> params, String prefix)
393         throws AaiServiceInternalException, SvcLogicException {
394
395         log.info("Received addVnfc call with vnfcName : " + vnfcName);
396         log.info("Received addVnfc call with params : " + params);
397         String resourceKey = STR_VNFC_NAME + vnfcName + "'";
398         log.info("Received addVnfc call with resourceKey : " + resourceKey);
399
400         SvcLogicContext vnfcCtx = new SvcLogicContext();
401         SvcLogicResource.QueryStatus response =
402             aaiClient.save("vnfc", true, false, resourceKey, params, prefix, vnfcCtx);
403
404         if (SvcLogicResource.QueryStatus.SUCCESS.equals(response)) {
405             log.info("Added VNFC SUCCESSFULLY " + vnfcName);
406         } else if (SvcLogicResource.QueryStatus.FAILURE.equals(response)) {
407             throw new AaiServiceInternalException("VNFC Add failed for vnfc_name " + vnfcName);
408         }
409     }
410
411     public String getGroupNotation(String groupNotationType, String groupNotationValue, String vnfcName,
412         String vserverName, String prefix, SvcLogicContext ctx, String vnfcRefVnfcType, String vnfcFuncCode,
413         int vmCount) {
414
415         String groupNotation = null;
416
417         if ("fixed-value".equals(groupNotationType)) {
418             groupNotation = groupNotationValue;
419         } else if ("first-vnfc-name".equals(groupNotationType)) {
420
421             /*
422              * If the group-notation-type value = ?first-vnfc-name?,
423              * then populate the group-notation value with the concatenation of
424              * [vnfc name associated with the first vnfc for the vnfc-type (e.g., *******)]
425              * and [the value in group-notation-value (e.g., pair)].
426              * There may be several vnfc-types associated with the VM?s.
427              */
428             /* Vnfc-type should be from refrence data */
429
430             /* vDBE has 2 VNFCs with same VNFC type . The pair name should be same for both . */
431             /*
432              * When first VNFC is added details should be added to context so FirstVnfcName doesnt return null second
433              * time.
434              */
435             String tmpVnfcName = getFirstVnfcNameForVnfcType(ctx, prefix, vnfcRefVnfcType);
436
437             log.info("RETURNED FIRSTVNFCNAME" + tmpVnfcName);
438             log.info("CURRENTVNFCNAME" + vnfcName);
439             groupNotation = resolveGroupNotation(groupNotationValue, vnfcName, tmpVnfcName);
440         } else if ("relative-value".equals(groupNotationType)) {
441
442             /*
443              * If the group-notation-type = ?relative-value?, then find the group-notation value
444              * from the prior vnfc (where prior means the vnfc with where the last three digits of the
445              * vm-name is one lower than the current one; note that this vnfc may have been previously configured.)
446              * 1. If the group-notation-value = next, then add 1 to the group-notation value from the prior vnfc and use
447              * this value
448              * 2. If the group-notation-value = same, then use the group-notation-value from the prior vnfc record
449              */
450
451             // next and same cant be defined for first VM. if next will not generate grpNotation if Prior is not a
452             // number
453             String tmpVserverName;
454             if (vserverName != null) {
455
456                 String vmNamePrefix = vserverName.substring(0, vserverName.length() - 3);
457                 String lastThreeChars = vserverName.substring(vserverName.length() - 3);
458
459                 if (NumberUtils.isDigits(lastThreeChars)) {
460                     int vmNum = Integer.parseInt(lastThreeChars) - 1;
461                     String formatted = String.format("%03d", vmNum);
462
463                     log.info("FORMATTED " + formatted);
464
465                     tmpVserverName = vmNamePrefix + formatted;
466
467                     String priorGroupNotation = getGroupNotationForVServer(ctx, prefix, tmpVserverName);
468                     groupNotation = resolveGroupNotation(groupNotationValue, priorGroupNotation);
469                 }
470             }
471         } else if ("existing-value".equals(groupNotationType)) {
472          /* This is a new value being added.  Find the existing vnfc records in A&AI inventory with the same vnfc-function code as the value in vnfc_reference table.
473           * Verify that the group-notation value is the same for all such records found in inventory.
474           * if all records do not have the same group-notation value, write the new vnfc record to A&AI inventory without a group-notation value and continue to the next VM in the vnfc_reference table.  A 501 intermediate error message should be sent after all new VNFC records have been added to A&AI.
475           * If all records match, use the same group-notation value for the new vnfc record as found in the existing vnfc records.
476           */
477             groupNotation = getGroupNotationForExistigValue(ctx, vnfcFuncCode, vmCount);
478         }
479
480         log.info("RETURNED GROUPNOTATION " + groupNotation);
481         return groupNotation;
482     }
483
484     private String resolveGroupNotation(String groupNotationValue, String vnfcName, String tmpVnfcName) {
485         if (tmpVnfcName == null) {
486             log.info("CURRENTVNFCNAME" + vnfcName);
487             // No Vnfcs currently exist. Use Current vnfcName
488             return vnfcName + groupNotationValue;
489         } else {
490             return tmpVnfcName + groupNotationValue;
491         }
492     }
493
494     private String resolveGroupNotation(String groupNotationValue, String priorGroupNotation) {
495         if ("same".equals(groupNotationValue)) {
496             return priorGroupNotation;
497         } else if ("next".equals(groupNotationValue) && priorGroupNotation != null
498             && NumberUtils.isDigits(priorGroupNotation)) {
499
500             int nextGrpNotation = Integer.parseInt(priorGroupNotation) + 1;
501             return String.valueOf(nextGrpNotation);
502         }
503         return null;
504     }
505
506     public String getGroupNotationForExistigValue(SvcLogicContext ctx, String vnfcFuncCode,
507                                                   int vmCount) {
508         String vfModuleId = ctx.getAttribute("req-vf-module-id"); //Coming from request-params
509         boolean first = true;
510         String aaiGroupNotationValue = null;
511         for (int i = 0; i < vmCount; i++) {
512             String ind = "tmp.vnfInfo.vm[" + i + "].";
513             String aaiFuncCode = ctx.getAttribute(ind + "vnfc-function-code");
514             String aaiGroupNotation = ctx.getAttribute(ind + "group-notation");
515             String aaiVfModuleId = ctx.getAttribute(ind + ATTR_VF_MODULE_ID);
516
517             log.info("getGroupNotationForExistigValue()::: vfModuleId=" + vfModuleId + ", aaiFuncCode=" + aaiFuncCode
518                 + ", aaiGroupNotation=" + aaiGroupNotation + ",aaiVfMOduleId=" + aaiVfModuleId);
519
520             if (StringUtils.isNotBlank(aaiFuncCode) && aaiFuncCode.equals(vnfcFuncCode) &&
521                 (StringUtils.isNotBlank(vfModuleId) && StringUtils.isNotBlank(aaiVfModuleId) && aaiVfModuleId
522                     .equals(vfModuleId))) {
523                 if (null == aaiGroupNotationValue && first) {
524                     if (null == aaiGroupNotation) {//Return if null
525                         return null;
526                     }
527                     aaiGroupNotationValue = ctx.getAttribute(ind + "group-notation");
528                     first = false;
529                 } else {
530                     if (!StringUtils.equals(aaiGroupNotationValue, ctx.getAttribute(ind + "group-notation"))) {
531                         log.info("Values are different, returning null");
532                         return null;
533                     }
534                 }
535             }
536         }
537
538         return aaiGroupNotationValue;
539     }
540
541     public String getGroupNotationForVServer(SvcLogicContext ctx, String prefix, String vserverName) {
542
543         String vmCountStr = ctx.getAttribute(prefix + "vnf.vm-count");
544
545         if (vmCountStr == null) {
546             return null;
547         }
548
549         int vmCount = Integer.parseInt(vmCountStr);
550         for (int i = 0; i < vmCount; i++) {
551
552             String tmpVserver = ctx.getAttribute(prefix + "vm[" + i + "].vserver-name");
553
554             if (vserverName.equals(tmpVserver)) {
555                 return ctx.getAttribute(prefix + "vm[" + i + "].group-notation");
556             }
557         } // vmCount
558         return null;
559     }
560
561     public String getFirstVnfcNameForVnfcType(SvcLogicContext ctx, String prefix, String vnfcRefVnfcType) {
562
563         String vmCountStr = ctx.getAttribute(prefix + "vnf.vm-count");
564         if (vmCountStr == null) {
565             return null;
566         }
567         int vmCount = Integer.parseInt(vmCountStr);
568         for (int i = 0; i < vmCount; i++) {
569
570             String tmpvnfcType = ctx.getAttribute(prefix + "vm[" + i + "].vnfc-type");
571
572             if (vnfcRefVnfcType.equals(tmpvnfcType)) {
573                 return ctx.getAttribute(prefix + "vm[" + i + "].vnfc-name");
574             }
575         } // vmCount
576         return null;
577     }
578
579     public void updateVServerStatus(Map<String, String> params, SvcLogicContext ctx, int vmCount)
580         throws AaiServiceInternalException, SvcLogicException {
581         log.info("Received updateVServerStatus call with params : " + params);
582
583         String prefix = params.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
584
585         prefix = StringUtils.isNotBlank(prefix) ? (prefix + ".") : "";
586         Map<String, String> vServerParams = new HashMap<>();
587
588         // TODO - Should this just update prov-status or both? What about generic-vnf status? Will that be updated by
589         // Dispatcher?
590
591         vServerParams.put(PARAM_PROV_STATUS, PARAM_VAL_NVTPROV);
592
593         for (int i = 0; i < vmCount; i++) {
594             String aaiRefKey = prefix + "vm[" + i + "].";
595
596             log.info("VNFCNAME IN UpdateVServer " + ctx.getAttribute(aaiRefKey + ATTR_VNFC_NAME));
597
598             if (ctx.getAttribute(aaiRefKey + ATTR_VNFC_NAME) != null) {
599                 continue;
600             }
601
602             String resourceKey = "vserver.vserver-id = '" + ctx.getAttribute(aaiRefKey + ATTR_VSERVER_ID) + "'"
603                 + " AND tenant.tenant-id = '" + ctx.getAttribute(aaiRefKey + ATTR_TENANT_ID) + "'"
604                 + " AND cloud-region.cloud-owner = '" + ctx.getAttribute(aaiRefKey + ATTR_CLOUD_OWNER) + "'"
605                 + " AND cloud-region.cloud-region-id = '" + ctx.getAttribute(aaiRefKey + ATTR_CLOUD_REGION_ID) + "'";
606
607             updateResource(PARAM_VSERVER, resourceKey, vServerParams);
608         }
609     }
610
611     public void updateVnfStatus(Map<String, String> params, SvcLogicContext ctx)
612         throws AaiServiceInternalException, SvcLogicException {
613         log.info("Received updateVnfStatus call with params : " + params);
614
615         Map<String, String> vnfParams = new HashMap<>();
616
617         // TODO - Should this just update prov-status or both? What about generic-vnf status? Will that be updated by
618         // Dispatcher?
619
620         vnfParams.put(PARAM_PROV_STATUS, PARAM_VAL_NVTPROV);
621
622         String resourceKey = STR_VNF_ID + ctx.getAttribute(ATTR_VNF_ID) + "'";
623
624         updateResource(PARAM_GENERIC_VNF, resourceKey, vnfParams);
625     }
626
627     public void updateResource(String resource, String resourceKey, Map<String, String> params)
628         throws AaiServiceInternalException, SvcLogicException {
629
630         log.info("Received updateResource call with Key : " + resourceKey);
631
632         SvcLogicContext ctx = new SvcLogicContext();
633         SvcLogicResource.QueryStatus response = aaiClient.update(resource, resourceKey, params, "tmp.update", ctx);
634
635         if (SvcLogicResource.QueryStatus.SUCCESS.equals(response)) {
636             log.info("Updated " + resource + " SUCCESSFULLY for " + resourceKey);
637
638         } else if (SvcLogicResource.QueryStatus.FAILURE.equals(response)) {
639             throw new AaiServiceInternalException(resource + " Update failed for " + resourceKey);
640         }
641     }
642
643     public SvcLogicContext readResource(String query, String prefix, String resourceType)
644         throws AaiServiceInternalException, SvcLogicException {
645         SvcLogicContext resourceContext = new SvcLogicContext();
646
647         SvcLogicResource.QueryStatus response =
648             aaiClient.query(resourceType, false, null, query, prefix, null, resourceContext);
649         log.info("AAIResponse: " + response.toString());
650         if (resourceType==null || !resourceType.equals("cloud-region")) {
651             if (!SvcLogicResource.QueryStatus.SUCCESS.equals(response)) {
652                 throw new AaiServiceInternalException("Error Retrieving " + resourceType + " from A&AI");
653             }
654         }
655         return resourceContext;
656     }
657
658     // Added for Backward Compatibility
659     public void checkAndUpdateVnfc(Map<String, String> params, SvcLogicContext ctx, int vnfcRefLen, int vmCount)
660         throws AaiServiceInternalException, SvcLogicException {
661         log.info("Received checkAndUpdateVnfcStatus call with params : " + params);
662
663         String prefix = params.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
664
665         prefix = StringUtils.isNotBlank(prefix) ? (prefix + ".") : "";
666
667         for (int i = 0; i < vmCount; i++) {
668             String aaiRefKey = prefix + "vm[" + i + "].";
669
670             log.info(QUERY_STR_VNFC_NAME + aaiRefKey + "vnfc-name:" + ctx.getAttribute(aaiRefKey + ATTR_VNFC_NAME));
671
672             String vnfcNameAai = ctx.getAttribute(aaiRefKey + ATTR_VNFC_NAME);
673
674             if (StringUtils.isNotBlank(vnfcNameAai)) {
675                 // Get Vnfc_reference data
676                 for (int vnfcRefIndx = 0; vnfcRefIndx < vnfcRefLen; vnfcRefIndx++) {
677
678                     String vnfcRefKey = STR_VNFC_REF + vnfcRefIndx + "].";
679
680                     log.info(STR_VNFC_REF_KEY + vnfcRefKey);
681                     log.info(STR_AAI_REF_KEY + aaiRefKey);
682
683                     String vnfcFuncCode = ctx.getAttribute(vnfcRefKey + ATTR_VNFC_FUNC_CODE);
684                     String vserverName = ctx.getAttribute(aaiRefKey + ATTR_VSERVER_NAME);
685                     String vnfcNameReference = vserverName + vnfcFuncCode + "001";
686                     tryUpdateVnfcStatus(params, prefix, vnfcNameAai, vnfcNameReference);
687                 }
688             }
689         }
690     }
691
692     private void tryUpdateVnfcStatus(Map<String, String> params, String prefix, String vnfcNameAai,
693         String vnfcNameReference) throws AaiServiceInternalException, SvcLogicException {
694         if (vnfcNameAai.equals(vnfcNameReference)) {
695             updateVnfcStatus(vnfcNameAai, params, prefix);
696         }
697     }
698
699     public void updateVnfcStatus(String vnfcName, Map<String, String> params, String prefix)
700         throws AaiServiceInternalException, SvcLogicException {
701
702         log.info("Received updateVnfcStatus call with vnfcName : " + vnfcName);
703         log.info("Received updateVnfcStatus call with params : " + params);
704
705         String resourceKey = STR_VNFC_NAME + vnfcName + "'";
706         log.info("Received updateVnfcStatus call with resourceKey : " + resourceKey);
707
708         Map<String, String> vnfcParams = new HashMap<>();
709         vnfcParams.put(PARAM_PROV_STATUS, PARAM_VAL_NVTPROV);
710         vnfcParams.put("orchestration-status", "CONFIGURED");
711
712         log.info("In updateVnfcStatus call with vnfcParams : " + vnfcParams);
713
714         updateResource("vnfc", resourceKey, vnfcParams);
715
716         log.info("End of updateVnfcStatus");
717     }
718
719     public void updateVnfStatusWithOAMAddress(Map<String, String> params, SvcLogicContext ctx)
720         throws AaiServiceInternalException, SvcLogicException {
721         log.info("Received updateVnfStatusWithOAMAddress call with params : " + params);
722
723         String ipAddress = ctx.getAttribute("vnf-host-ip-address");
724         log.debug("Vnf-host-ip-address" + ipAddress);
725
726         Map<String, String> vnfParams = new HashMap<>();
727         vnfParams.put("ipv4-oam-address", ipAddress);
728         String resourceKey = STR_VNF_ID + ctx.getAttribute(ATTR_VNF_ID) + "'";
729         updateResource(PARAM_GENERIC_VNF, resourceKey, vnfParams);
730     }
731
732     public void getVfModuleInfo(Map<String, String> params, SvcLogicContext vfModuleCtx) throws Exception {
733         log.info("Received getVfModuleInfo call with params : " + params);
734         String prefix = params.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
735         prefix = StringUtils.isNotBlank(prefix) ? (prefix + ".") : "";
736
737         String vnfId = params.get("vnfId");
738         String vfModuleId = params.get("vfModuleId");
739         String resourceKey = STR_VNF_ID + vnfId +
740             "' AND vf-module.vf-module-id = '" + vfModuleId + "'";
741         String queryPrefix = "vfModuleInfo";
742         String resourceType = "vf-module";
743         SvcLogicContext vfmCtx = readResource(resourceKey, queryPrefix, resourceType);
744         String modelInvariantId = vfmCtx.getAttribute("vfModuleInfo.model-invariant-id");
745         log.info("getVfModuleInfo():::modelInvariant=" + modelInvariantId);
746         vfModuleCtx.setAttribute(prefix + "vfModule.model-invariant-id",
747             vfmCtx.getAttribute("vfModuleInfo.model-invariant-id"));
748         vfModuleCtx
749             .setAttribute(prefix + "vfModule.model-version-id", vfmCtx.getAttribute("vfModuleInfo.model-version-id"));
750         log.info("End - getVfModuleInfo");
751     }
752
753     public void getModelVersionInfo(Map<String, String> modelParams, SvcLogicContext modelCtx) throws Exception {
754         log.info("Received getModelVersionInfo call with params : " + modelParams);
755         String prefix = modelParams.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
756         prefix = StringUtils.isNotBlank(prefix) ? (prefix + ".") : "";
757
758         String modelInvariantId = modelParams.get("model-invariant-id");
759         String modelVersionId = modelParams.get("model-version-id");
760         String resourceKey = "model.model-invariant-id = '" + modelInvariantId +
761             "' AND model-ver.model-version-id = '" + modelVersionId + "'";
762         String queryPrefix = "modelInfo";
763         String resourceType = "model-ver";
764         SvcLogicContext vfmCtx = readResource(resourceKey, queryPrefix, resourceType);
765         log.info("getModelVersionInfo():::modelname=" + vfmCtx.getAttribute("modelInfo.model-name"));
766         modelCtx.setAttribute(prefix + "vfModule.model-name", vfmCtx.getAttribute("modelInfo.model-name"));
767         log.info("End - getModelVersionInfo");
768
769     }
770
771     public void getIdentityUrl(Map<String, String> params, SvcLogicContext ctx) throws Exception{
772         log.info("Recieved getIdentityUrl call with params : "+params);
773         String prefix = params.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
774         prefix = StringUtils.isNotBlank(prefix) ? (prefix + ".") : "";
775
776         String cloudOwner = params.get("cloudOwner");
777         String cloudRegionId = params.get("cloudRegionId");
778         // per comment from git review, we need to sanitize the two parameters 
779         // to avoid security issues
780         cloudOwner = cloudOwner.replaceAll("'", "");
781         cloudRegionId = cloudRegionId.replaceAll("'", "");
782         log.debug("cloudOwner" +cloudOwner +"," +"cloudRegionId"+ cloudRegionId);
783         String resourceKey = "depth = '" + 0 + "' AND cloud-region.cloud-owner = '" + cloudOwner +
784                 "' AND cloud-region.cloud-region-id = '" + cloudRegionId + "'";
785         String queryPrefix ="urlInfo";
786         String resourceType = "cloud-region";
787         SvcLogicContext urlCtx = readResource(resourceKey, queryPrefix, resourceType);
788         log.info("IdentityUrl: "+urlCtx.getAttribute("urlInfo.identity-url"));
789         log.info("Prefix for getIdentityUrl: "+prefix+"cloud-region.identity-url");
790         ctx.setAttribute(prefix+"cloud-region.identity-url", urlCtx.getAttribute("urlInfo.identity-url"));
791  
792     }
793 }