aboutsummaryrefslogtreecommitdiffstats
path: root/appc-outbound/appc-aai-client/provider/src/main/java/org/onap/appc/aai/client/aai/AaiService.java
blob: cb439ee1619e4f419d1408edf4e406c87de35c94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
/*-
 * ============LICENSE_START=======================================================
 * ONAP : APPC
 * ================================================================================
 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Copyright (C) 2017 Amdocs
 * =============================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 * ============LICENSE_END=========================================================
 */

package org.onap.appc.aai.client.aai;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.onap.appc.aai.client.AppcAaiClientConstant;
import org.onap.ccsdk.sli.adaptors.aai.AAIClient;
import org.onap.ccsdk.sli.adaptors.aai.AAIService;
import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
import org.onap.ccsdk.sli.core.sli.SvcLogicResource;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.ServiceReference;
import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;

public class AaiService {

    private static final EELFLogger log = EELFManager.getInstance().getLogger(AaiService.class);
    private AAIClient aaiClient;

    public AaiService(AAIClient aaiClient) {
        this.aaiClient = aaiClient;
    }

    public AaiService() {
        BundleContext bctx = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
        ServiceReference sref = bctx.getServiceReference(AAIService.class);
        aaiClient = (AAIClient) bctx.getService(sref);
    }

    public void getGenericVnfInfo(Map<String, String> params, SvcLogicContext ctx) throws Exception {

        String vnfId = params.get("vnfId");
        if (StringUtils.isBlank(vnfId)) {
            throw new Exception("VnfId is missing");
        }
        String prefix = params.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
        prefix = StringUtils.isNotBlank(prefix) ? (prefix + ".") : "";
        String resourceKey = "generic-vnf.vnf-id = '" + vnfId + "'";
        String resourceType = "generic-vnf";
        String queryPrefix = "vnfInfo";
        SvcLogicContext vnfCtx = readResource(resourceKey, queryPrefix, resourceType);

        ctx.setAttribute(prefix + "vnf.vnf-name", vnfCtx.getAttribute("vnfInfo.vnf-name"));
        ctx.setAttribute(prefix + "vnf.vnf-type", vnfCtx.getAttribute("vnfInfo.vnf-type"));
        ctx.setAttribute(prefix + "vnf.prov-status", vnfCtx.getAttribute("vnfInfo.prov-status"));
        ctx.setAttribute(prefix + "vnf.orchestration-status", vnfCtx.getAttribute("vnfInfo.orchestration-status"));
        ctx.setAttribute(prefix + "vnf.ipv4-oam-address", vnfCtx.getAttribute("vnfInfo.ipv4-oam-address"));

        int vmCount = 0;
        String relLen = vnfCtx.getAttribute("vnfInfo.relationship-list.relationship_length");
        int relationshipLength = 0;
        if (relLen != null) {
            relationshipLength = Integer.parseInt(relLen);
        }
        log.info("RELLEN " + relationshipLength);
        for (int i = 0; i < relationshipLength; i++) {
            String vserverId = getRelationshipValue(i, vnfCtx, "vserver", "vserver.vserver-id", "vnfInfo");
            String tenantId = getRelationshipValue(i, vnfCtx, "vserver", "tenant.tenant-id", "vnfInfo");
            String cloudOwner = getRelationshipValue(i, vnfCtx, "vserver", "cloud-region.cloud-owner", "vnfInfo");
            String cloudRegionId =
                    getRelationshipValue(i, vnfCtx, "vserver", "cloud-region.cloud-region-id", "vnfInfo");
            if (vserverId != null) {
                log.info("VSERVER KEYS " + vserverId + " " + tenantId + " " + cloudOwner + " " + cloudRegionId);
                String vnfPrefix = prefix + "vm[" + vmCount + "].";
                ctx.setAttribute(vnfPrefix + "vserver-id", vserverId);
                ctx.setAttribute(vnfPrefix + "tenant-id", tenantId);
                ctx.setAttribute(vnfPrefix + "cloud-owner", cloudOwner);
                ctx.setAttribute(vnfPrefix + "cloud-region-id", cloudRegionId);
                vmCount++;
            }
        }
        ctx.setAttribute(prefix + "vm-count", String.valueOf(vmCount));
        log.info("VMCOUNT FROM VNF INFO " + ctx.getAttribute(prefix + "vm-count"));
    }

    public void getVMInfo(Map<String, String> params, SvcLogicContext ctx) throws Exception {
        log.info("Received getVmInfo call with params : " + params);
        String prefix = params.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
        prefix = StringUtils.isNotBlank(prefix) ? (prefix + ".") : "";
        int vnfcCount = 0;
        ctx.setAttribute(prefix + "vm.vnfc-count", String.valueOf(vnfcCount)); // Incase no vnfcs are found
        String vserverId = params.get("vserverId");
        if (StringUtils.isBlank(vserverId)) {
            throw new Exception("VServerId is missing");
        }
        String tenantId = params.get("tenantId");
        if (StringUtils.isBlank(tenantId)) {
            throw new Exception("TenantId is missing");
        }
        String cloudOwner = params.get("cloudOwner");
        if (StringUtils.isBlank(cloudOwner)) {
            throw new Exception("Cloud Owner is missing");
        }
        String cloudRegionId = params.get("cloudRegionId");
        if (StringUtils.isBlank(cloudRegionId)) {
            throw new Exception("Cloud region Id is missing");
        }
        String resourceKey = "vserver.vserver-id = '" + vserverId + "' AND tenant.tenant-id = '" + tenantId
                + "' AND cloud-region.cloud-owner = '" + cloudOwner + "' AND cloud-region.cloud-region-id = '"
                + cloudRegionId + "'";
        String queryPrefix = "vmInfo";
        String resourceType = "vserver";
        SvcLogicContext vmCtx = readResource(resourceKey, queryPrefix, resourceType);

        ctx.setAttribute(prefix + "vm.prov-status", vmCtx.getAttribute("vmInfo.prov-status"));
        ctx.setAttribute(prefix + "vm.vserver-name", vmCtx.getAttribute("vmInfo.vserver-name"));

        String relLen = vmCtx.getAttribute("vmInfo.relationship-list.relationship_length");
        int relationshipLength = 0;
        if (relLen != null) {
            relationshipLength = Integer.parseInt(relLen);
        }
        log.info("RELLEN" + relationshipLength);
        for (int i = 0; i < relationshipLength; i++) {
            String vfModuleId = getRelationshipValue(i, vmCtx, "vf-module", "vf-module.vf-module-id", "vmInfo");
            if (vfModuleId != null)
                ctx.setAttribute(prefix + "vm.vf-module-id", vfModuleId);

            String vnfcName = getRelationshipValue(i, vmCtx, "vnfc", "vnfc.vnfc-name", "vmInfo");
            if (vnfcName != null) {
                ctx.setAttribute(prefix + "vm.vnfc[" + vnfcCount + "].vnfc-name", vnfcName);
                vnfcCount++;
            }

        } // relationshipLength
        ctx.setAttribute(prefix + "vm.vnfc-count", String.valueOf(vnfcCount));
        log.info("VSERVERNAME " + ctx.getAttribute(prefix + "vm.vserver-name") + " HAS NUM VNFCS = "
                + ctx.getAttribute(prefix + "vm.vnfc-count"));
    }

    private String getRelationshipValue(int i, SvcLogicContext ctx, String relatedTo, String relationshipKey,
            String prefix) throws Exception {

        if (relatedTo.equals(ctx.getAttribute(prefix + ".relationship-list.relationship[" + i + "].related-to"))) {
            log.info("RELATEDTO " + relatedTo);
            int relationshipDataLength = 0;
            String relDataLen =
                    ctx.getAttribute(prefix + ".relationship-list.relationship[" + i + "].relationship-data_length");
            if (relDataLen != null) {
                relationshipDataLength = Integer.parseInt(relDataLen);
            }

            for (int j = 0; j < relationshipDataLength; j++) {

                String key = ctx.getAttribute(prefix + ".relationship-list.relationship[" + i + "].relationship-data["
                        + j + "].relationship-key");

                String value = ctx.getAttribute(prefix + ".relationship-list.relationship[" + i + "].relationship-data["
                        + j + "].relationship-value");

                log.info("GENERIC KEY " + key);
                log.info("GENERIC VALUE " + value);

                if (relationshipKey.equals(key)) {
                    return value;
                }

            } // relationshipDataLength
        } // if related-To

        return null;
    }

    public void getVnfcInfo(Map<String, String> params, SvcLogicContext ctx) throws Exception {
        log.info("Received getVnfc call with params : " + params);

        String prefix = params.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
        prefix = StringUtils.isNotBlank(prefix) ? (prefix + ".") : "";

        String vnfcName = params.get("vnfcName");
        if (StringUtils.isBlank(vnfcName)) {
            throw new Exception("Vnfc Name is missing");
        }

        String resourceKey = "vnfc.vnfc-name = '" + vnfcName + "'";
        String queryPrefix = "vnfcInfo";
        String resourceType = "vnfc";
        SvcLogicContext vnfcCtx = readResource(resourceKey, queryPrefix, resourceType);

        // Changes for US 315820 for 1710 vnfc-type renamed to nfc-function,vnfc-function-code renamed to
        // nfc-naming-code

        ctx.setAttribute(prefix + "vnfc.vnfc-type", vnfcCtx.getAttribute("vnfcInfo.nfc-function"));
        ctx.setAttribute(prefix + "vnfc.vnfc-function-code", vnfcCtx.getAttribute("vnfcInfo.nfc-naming-code"));
        ctx.setAttribute(prefix + "vnfc.group-notation", vnfcCtx.getAttribute("vnfcInfo.group-notation"));
        ctx.setAttribute(prefix + "vnfc.ipaddress-v4-oam-vip", vnfcCtx.getAttribute("vnfcInfo.ipaddress-v4-oam-vip"));

    }

    public void insertVnfcs(Map<String, String> params, SvcLogicContext ctx, int vnfcRefLen, int vmCount)
            throws Exception {
        log.info("Received insertVnfcs call with params : " + params);

        String prefix = params.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);

        prefix = StringUtils.isNotBlank(prefix) ? (prefix + ".") : "";
        int vnfcRefIndx = 0;
        log.debug("vnfcRefIndx" + vnfcRefIndx);
        for (int i = 0; i < vmCount; i++) {
            String aaiRefKey = prefix + "vm[" + i + "].";

            log.info("VNFCNAME IN INSERTVNFCS " + ctx.getAttribute(aaiRefKey + "vnfc-name"));
            String vnfcNameAAI = ctx.getAttribute(aaiRefKey + "vnfc-name");

            // Get Vnfc_reference data from the table
            String vnfcRefKey = "vnfcReference[" + vnfcRefIndx + "].";

            log.info("VNFCREFKEY " + vnfcRefKey);
            log.info("AAIREFKEY " + aaiRefKey);

            String groupNotationType = ctx.getAttribute(vnfcRefKey + "GROUP-NOTATION-TYPE");
            String groupNotationValue = ctx.getAttribute(vnfcRefKey + "GROUP-NOTATION-VALUE");
            String vnfcType = ctx.getAttribute(vnfcRefKey + "VNFC-TYPE");
            String vnfcFuncCode = ctx.getAttribute(vnfcRefKey + "VNFC-FUNCTION-CODE");
            String populateIpAddressV4OamVip = ctx.getAttribute(vnfcRefKey + "IPADDRESS-V4-OAM-VIP");

            // Get vnfc Data to be added
            String vserverName = ctx.getAttribute(aaiRefKey + "vserver-name");
            String vnfcName = vserverName + vnfcFuncCode + "001";
            String groupNotation = getGroupNotation(groupNotationType, groupNotationValue, vnfcName, vserverName,
                    prefix, ctx, vnfcType);

            String ipAddressV4OamVip = null;
            if ("Y".equals(populateIpAddressV4OamVip)) {
                ipAddressV4OamVip = ctx.getAttribute("vnf-host-ip-address"); // from input
            }
            Map<String, String> vnfcParams =
                    populateVnfcParams(ctx, aaiRefKey, ipAddressV4OamVip, groupNotation, vnfcType, vnfcFuncCode);

            log.info("Vnfc name from AAI: " + vnfcNameAAI);
            log.info("Vnfc name generated: " + vnfcName);

            if (StringUtils.isNotBlank(vnfcNameAAI)) {
                if (vnfcName.equalsIgnoreCase(vnfcNameAAI)) {
                    updateVnfcStatus(vnfcNameAAI, params, prefix);
                    vnfcRefIndx++;
                }
                continue;
            }
            vnfcRefIndx++;
            addVnfc(vnfcName, vnfcParams, prefix);

            // Add VNFC Info to context for current added VNFC
            ctx.setAttribute(aaiRefKey + "vnfc-name", vnfcName);
            ctx.setAttribute(aaiRefKey + "vnfc-type", vnfcType);
            ctx.setAttribute(aaiRefKey + "vnfc-function-code", vnfcFuncCode);
            ctx.setAttribute(aaiRefKey + "group-notation", groupNotation);
        }
    }

    public List<String> getVnfcData(Map<String, String> params, SvcLogicContext ctx, int vnfcRefLen, int vmCount)
            throws Exception {

        String prefix = params.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
        prefix = StringUtils.isNotBlank(prefix) ? (prefix + ".") : "";
        List<String> vnfcNames = new ArrayList<String>();
        int vnfcRefIndx = -1;
        for (int i = 0; i < vmCount; i++) {
            String aaiRefKey = prefix + "vm[" + i + "].";
            log.info("VNFCNAME IN INSERTVNFCS " + ctx.getAttribute(aaiRefKey + "vnfc-name"));
            if (ctx.getAttribute(aaiRefKey + "vnfc-name") != null) {
                continue;
            } else {
                vnfcRefIndx++;
            }
            String vnfcRefKey = "vnfcReference[" + vnfcRefIndx + "].";
            log.info("VNFCREFKEY " + vnfcRefKey);
            log.info("AAIREFKEY " + aaiRefKey);
            String vnfcFuncCode = ctx.getAttribute(vnfcRefKey + "VNFC-FUNCTION-CODE");

            // Get vnfc Data to be added
            String vserverName = ctx.getAttribute(aaiRefKey + "vserver-name");
            String vnfcName = vserverName + vnfcFuncCode + "001";


            vnfcNames.add(vnfcName);
        }

        return vnfcNames;
    }

    private Map<String, String> populateVnfcParams(SvcLogicContext ctx, String aaiRefKey, String ipAddressV4OamVip,
            String groupNotation, String vnfcType, String vnfcFuncCode) throws Exception {

        Map<String, String> vnfcParams = new HashMap<String, String>();

        // Changes for vnfc-type renamed to nfc-function,vnfc-function-code renamed to
        // nfc-naming-code
        vnfcParams.put("nfc-naming-code", vnfcFuncCode);
        vnfcParams.put("nfc-function", vnfcType);
        vnfcParams.put("ipaddress-v4-oam-vip", ipAddressV4OamVip);
        vnfcParams.put("prov-status", "NVTPROV");
        vnfcParams.put("orchestration-status", "CONFIGURED");
        vnfcParams.put("in-maint", "false");
        vnfcParams.put("is-closed-loop", "false");
        vnfcParams.put("group-notation", groupNotation);
        vnfcParams.put("relationship-list.relationship[0].related-to", "vserver");
        vnfcParams.put("relationship-list.relationship[0].relationship-data[0].relationship-key", "vserver.vserver-id");
        vnfcParams.put("relationship-list.relationship[0].relationship-data[0].relationship-value",
                ctx.getAttribute(aaiRefKey + "vserver-id"));
        vnfcParams.put("relationship-list.relationship[0].relationship-data[1].relationship-key", "tenant.tenant-id");
        vnfcParams.put("relationship-list.relationship[0].relationship-data[1].relationship-value",
                ctx.getAttribute(aaiRefKey + "tenant-id"));
        vnfcParams.put("relationship-list.relationship[0].relationship-data[2].relationship-key",
                "cloud-region.cloud-owner");
        vnfcParams.put("relationship-list.relationship[0].relationship-data[2].relationship-value",
                ctx.getAttribute(aaiRefKey + "cloud-owner"));
        vnfcParams.put("relationship-list.relationship[0].relationship-data[3].relationship-key",
                "cloud-region.cloud-region-id");
        vnfcParams.put("relationship-list.relationship[0].relationship-data[3].relationship-value",
                ctx.getAttribute(aaiRefKey + "cloud-region-id"));
        vnfcParams.put("relationship-list.relationship[1].related-to", "generic-vnf");
        vnfcParams.put("relationship-list.relationship[1].relationship-data[0].relationship-key", "generic-vnf.vnf-id");
        vnfcParams.put("relationship-list.relationship[1].relationship-data[0].relationship-value",
                ctx.getAttribute("vnf-id"));
        vnfcParams.put("relationship-list.relationship[2].related-to", "vf-module");
        vnfcParams.put("relationship-list.relationship[2].relationship-data[0].relationship-key", "generic-vnf.vnf-id");
        vnfcParams.put("relationship-list.relationship[2].relationship-data[0].relationship-value",
                ctx.getAttribute("vnf-id"));
        vnfcParams.put("relationship-list.relationship[2].relationship-data[1].relationship-key",
                "vf-module.vf-module-id");
        vnfcParams.put("relationship-list.relationship[2].relationship-data[1].relationship-value",
                ctx.getAttribute(aaiRefKey + "vf-module-id"));

        return vnfcParams;
    }

    public void addVnfc(String vnfcName, Map<String, String> params, String prefix) throws Exception {
        log.info("Received addVnfc call with vnfcName : " + vnfcName);
        log.info("Received addVnfc call with params : " + params);
        String resourceKey = "vnfc.vnfc-name = '" + vnfcName + "'";
        log.info("Received addVnfc call with resourceKey : " + resourceKey);

        SvcLogicContext vnfcCtx = new SvcLogicContext();
        SvcLogicResource.QueryStatus response =
                aaiClient.save("vnfc", true, false, resourceKey, params, prefix, vnfcCtx);

        if (SvcLogicResource.QueryStatus.SUCCESS.equals(response)) {
            log.info("Added VNFC SUCCESSFULLY " + vnfcName);
        } else if (SvcLogicResource.QueryStatus.FAILURE.equals(response)) {
            throw new Exception("VNFC Add failed for for vnfc_name " + vnfcName);
        }
    }

    public String getGroupNotation(String groupNotationType, String groupNotationValue, String vnfcName,
            String vserverName, String prefix, SvcLogicContext ctx, String vnfcRefVnfcType) throws Exception {

        String grpNotation = null;

        if ("fixed-value".equals(groupNotationType)) {
            grpNotation = groupNotationValue;
        } else if ("first-vnfc-name".equals(groupNotationType)) {

            /*
             * If the group-notation-type value = ?first-vnfc-name?,
             * then populate the group-notation value with the concatenation of
             * [vnfc name associated with the first vnfc for the vnfc-type (e.g., *******)]
             * and [the value in group-notation-value (e.g., pair)].
             * There may be several vnfc-types associated with the VM?s.
             */
            /* Vnfc-type should be from refrence data */

            /* vDBE has 2 VNFCs with same VNFC type . The pair name should be same for both . */
            /*
             * When first VNFC is added details should be added to context so FirstVnfcName doesnt return null second
             * time.
             */
            String tmpVnfcName = getFirstVnfcNameForVnfcType(ctx, prefix, vnfcRefVnfcType);

            log.info("RETURNED FIRSTVNFCNAME" + tmpVnfcName);
            log.info("CURRENTVNFCNAME" + vnfcName);
            if (tmpVnfcName == null) {
                log.info("CURRENTVNFCNAME" + vnfcName);
                // No Vnfcs currently exist. Use Current vnfcName
                grpNotation = vnfcName + groupNotationValue;
            } else {
                grpNotation = tmpVnfcName + groupNotationValue;
            }
        } else if ("relative-value".equals(groupNotationType)) {

            /*
             * If the group-notation-type = ?relative-value?, then find the group-notation value
             * from the prior vnfc (where prior means the vnfc with where the last three digits of the
             * vm-name is one lower than the current one; note that this vnfc may have been previously configured.)
             * 1. If the group-notation-value = next, then add 1 to the group-notation value from the prior vnfc and use
             * this value
             * 2. If the group-notation-value = same, then use the group-notation-value from the prior vnfc record
             */

            // next and same cant be defined for first VM. if next will not generate grpNotation if Prior is not a
            // number
            String tmpVserverName = null;
            if (vserverName != null) {

                String vmNamePrefix = vserverName.substring(0, vserverName.length() - 3);

                String lastThreeChars = vserverName.substring(vserverName.length() - 3);

                if (NumberUtils.isDigits(lastThreeChars)) {
                    int vmNum = Integer.parseInt(lastThreeChars) - 1;
                    String formatted = String.format("%03d", vmNum);

                    log.info("FORMATTED " + formatted);

                    tmpVserverName = vmNamePrefix + formatted;

                    String priorGroupNotation = getGroupNotationForVServer(ctx, prefix, tmpVserverName);

                    if ("same".equals(groupNotationValue)) {
                        grpNotation = priorGroupNotation;
                    } else if ("next".equals(groupNotationValue)) {
                        if (priorGroupNotation != null && NumberUtils.isDigits(priorGroupNotation)) {
                            int nextGrpNotation = Integer.parseInt(priorGroupNotation) + 1;
                            grpNotation = String.valueOf(nextGrpNotation);
                        }
                    }
                }
            }
        }

        log.info("RETURNED GROUPNOTATION " + grpNotation);
        return grpNotation;
    }

    public String getGroupNotationForVServer(SvcLogicContext ctx, String prefix, String vserverName) {

        String vmCountStr = ctx.getAttribute(prefix + "vnf.vm-count");

        if (vmCountStr == null) {
            return null;
        }

        int vmCount = Integer.valueOf(vmCountStr);
        for (int i = 0; i < vmCount; i++) {

            String tmpVserver = ctx.getAttribute(prefix + "vm[" + i + "].vserver-name");

            if (vserverName.equals(tmpVserver)) {
                return ctx.getAttribute(prefix + "vm[" + i + "].group-notation");
            }
        } // vmCount

        return null;
    }

    public String getFirstVnfcNameForVnfcType(SvcLogicContext ctx, String prefix, String vnfcRefVnfcType) {

        String vmCountStr = ctx.getAttribute(prefix + "vnf.vm-count");

        if (vmCountStr == null) {
            return null;
        }

        int vmCount = Integer.valueOf(vmCountStr);
        for (int i = 0; i < vmCount; i++) {

            String tmpvnfcType = ctx.getAttribute(prefix + "vm[" + i + "].vnfc-type");

            if (vnfcRefVnfcType.equals(tmpvnfcType)) {
                return ctx.getAttribute(prefix + "vm[" + i + "].vnfc-name");
            }
        } // vmCount
        return null;
    }

    public void updateVServerStatus(Map<String, String> params, SvcLogicContext ctx, int vmCount) throws Exception {
        log.info("Received updateVServerStatus call with params : " + params);

        String prefix = params.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);

        prefix = StringUtils.isNotBlank(prefix) ? (prefix + ".") : "";
        Map<String, String> vServerParams = new HashMap<String, String>();

        // TODO - Should this just update prov-status or both? What about generic-vnf status? Will that be updated by
        // Dispatcher?

        vServerParams.put("prov-status", "NVTPROV");

        for (int i = 0; i < vmCount; i++) {
            String aaiRefKey = prefix + "vm[" + i + "].";

            log.info("VNFCNAME IN UpdateVServer " + ctx.getAttribute(aaiRefKey + "vnfc-name"));

            if (ctx.getAttribute(aaiRefKey + "vnfc-name") != null) {
                continue;
            }

            String resourceKey = "vserver.vserver-id = '" + ctx.getAttribute(aaiRefKey + "vserver-id") + "'"
                    + " AND tenant.tenant-id = '" + ctx.getAttribute(aaiRefKey + "tenant-id") + "'"
                    + " AND cloud-region.cloud-owner = '" + ctx.getAttribute(aaiRefKey + "cloud-owner") + "'"
                    + " AND cloud-region.cloud-region-id = '" + ctx.getAttribute(aaiRefKey + "cloud-region-id") + "'";

            updateResource("vserver", resourceKey, vServerParams);
        }
    }

    public void updateVnfStatus(Map<String, String> params, SvcLogicContext ctx) throws Exception {
        log.info("Received updateVnfStatus call with params : " + params);

        String prefix = params.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);

        prefix = StringUtils.isNotBlank(prefix) ? (prefix + ".") : "";

        Map<String, String> vnfParams = new HashMap<String, String>();

        // TODO - Should this just update prov-status or both? What about generic-vnf status? Will that be updated by
        // Dispatcher?

        vnfParams.put("prov-status", "NVTPROV");

        String resourceKey = "generic-vnf.vnf-id = '" + ctx.getAttribute("vnf-id") + "'";

        updateResource("generic-vnf", resourceKey, vnfParams);
    }

    public void updateResource(String resource, String resourceKey, Map<String, String> params) throws Exception {

        log.info("Received updateResource call with Key : " + resourceKey);

        SvcLogicContext ctx = new SvcLogicContext();
        SvcLogicResource.QueryStatus response = aaiClient.update(resource, resourceKey, params, "tmp.update", ctx);

        if (SvcLogicResource.QueryStatus.SUCCESS.equals(response)) {
            log.info("Updated " + resource + " SUCCESSFULLY for " + resourceKey);

        } else if (SvcLogicResource.QueryStatus.FAILURE.equals(response)) {
            throw new Exception(resource + " Update failed for " + resourceKey);
        }
    }

    public SvcLogicContext readResource(String query, String prefix, String resourceType) throws Exception {
        SvcLogicContext resourceContext = new SvcLogicContext();

        SvcLogicResource.QueryStatus response =
                aaiClient.query(resourceType, false, null, query, prefix, null, resourceContext);
        log.info("AAIResponse: " + response.toString());
        if (!SvcLogicResource.QueryStatus.SUCCESS.equals(response)) {
            throw new Exception("Error Retrieving " + resourceType + " from A&AI");
        }

        return resourceContext;
    }

    // Added for Backward Compatibility

    public void checkAndUpdateVnfc(Map<String, String> params, SvcLogicContext ctx, int vnfcRefLen, int vmCount)
            throws Exception {
        log.info("Received checkAndUpdateVnfcStatus call with params : " + params);

        String prefix = params.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);

        prefix = StringUtils.isNotBlank(prefix) ? (prefix + ".") : "";

        for (int i = 0; i < vmCount; i++) {
            String aaiRefKey = prefix + "vm[" + i + "].";

            log.info("VNFCNAME IN INSERTVNFCS " + aaiRefKey + "vnfc-name:" + ctx.getAttribute(aaiRefKey + "vnfc-name"));

            String vnfcNameAai = ctx.getAttribute(aaiRefKey + "vnfc-name");

            if (StringUtils.isNotBlank(vnfcNameAai)) {
                // Get Vnfc_reference data
                for (int vnfcRefIndx = 0; vnfcRefIndx < vnfcRefLen; vnfcRefIndx++) {

                    String vnfcRefKey = "vnfcReference[" + vnfcRefIndx + "].";

                    log.info("VNFCREFKEY " + vnfcRefKey);
                    log.info("AAIREFKEY " + aaiRefKey);

                    String vnfcFuncCode = ctx.getAttribute(vnfcRefKey + "VNFC-FUNCTION-CODE");
                    String vserverName = ctx.getAttribute(aaiRefKey + "vserver-name");
                    String vnfcNameReference = vserverName + vnfcFuncCode + "001";

                    if (vnfcNameAai.equals(vnfcNameReference)) {
                        updateVnfcStatus(vnfcNameAai, params, prefix);
                    }
                }
            }
        }
    }

    public void updateVnfcStatus(String vnfcName, Map<String, String> params, String prefix) throws Exception {

        log.info("Received updateVnfcStatus call with vnfcName : " + vnfcName);
        log.info("Received updateVnfcStatus call with params : " + params);

        String resourceKey = "vnfc.vnfc-name = '" + vnfcName + "'";
        log.info("Received updateVnfcStatus call with resourceKey : " + resourceKey);

        Map<String, String> vnfcParams = new HashMap<String, String>();
        vnfcParams.put("prov-status", "NVTPROV");
        vnfcParams.put("orchestration-status", "CONFIGURED");

        log.info("In updateVnfcStatus call with vnfcParams : " + vnfcParams);

        updateResource("vnfc", resourceKey, vnfcParams);

        log.info("End of updateVnfcStatus");
    }

    public void updateVnfStatusWithOAMAddress(Map<String, String> params, SvcLogicContext ctx) throws Exception {
        log.info("Received updateVnfStatusWithOAMAddress call with params : " + params);

        String prefix = params.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
        String ipAddress = ctx.getAttribute("vnf-host-ip-address");
        log.debug("Vnf-host-ip-address" + ipAddress);

        prefix = StringUtils.isNotBlank(prefix) ? (prefix + ".") : "";

        Map<String, String> vnfParams = new HashMap<String, String>();

        vnfParams.put("ipv4-oam-address", ipAddress);

        String resourceKey = "generic-vnf.vnf-id = '" + ctx.getAttribute("vnf-id") + "'";

        updateResource("generic-vnf", resourceKey, vnfParams);
    }

    public void getVfModuleInfo(Map<String, String> params, SvcLogicContext vfModuleCtx) throws Exception {
        log.info("Received getVfModuleInfo call with params : " + params);
        String prefix = params.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
        prefix = StringUtils.isNotBlank(prefix) ? (prefix + ".") : "";

        String vnfId = params.get("vnfId");
        String vfModuleId = params.get("vfModuleId");
        String resourceKey = "generic-vnf.vnf-id = '" + vnfId +
                "' AND vf-module.vf-module-id = '" + vfModuleId + "'";
        String queryPrefix = "vfModuleInfo";
        String resourceType = "vf-module";
        SvcLogicContext vfmCtx = readResource(resourceKey, queryPrefix, resourceType);
        String modelInvariantId=vfmCtx.getAttribute("vfModuleInfo.model-invariant-id");
        log.info("getVfModuleInfo():::modelInvariant="+modelInvariantId);
        vfModuleCtx.setAttribute(prefix+"vfModule.model-invariant-id", vfmCtx.getAttribute("vfModuleInfo.model-invariant-id"));
        vfModuleCtx.setAttribute(prefix+"vfModule.model-version-id", vfmCtx.getAttribute("vfModuleInfo.model-version-id"));
        log.info("End - getVfModuleInfo");
    }

    public void getModelVersionInfo(Map<String, String> modelParams, SvcLogicContext modelCtx) throws Exception {
        log.info("Received getModelVersionInfo call with params : " + modelParams);
        String prefix = modelParams.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
        prefix = StringUtils.isNotBlank(prefix) ? (prefix + ".") : "";

        String modelInvariantId = modelParams.get("model-invariant-id");
        String modelVersionId = modelParams.get("model-version-id");
        String resourceKey = "model.model-invariant-id = '" + modelInvariantId +
                "' AND model-ver.model-version-id = '" + modelVersionId + "'";
        String queryPrefix = "modelInfo";
        String resourceType = "model-ver";
        SvcLogicContext vfmCtx = readResource(resourceKey, queryPrefix, resourceType);
        log.info("getModelVersionInfo():::modelname="+vfmCtx.getAttribute("modelInfo.model-name"));
        modelCtx.setAttribute(prefix+"vfModule.model-name", vfmCtx.getAttribute("modelInfo.model-name"));
        log.info("End - getModelVersionInfo");

    }
}