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