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