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