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