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