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