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