2505083582d0f180caf9a4087cd387e4f6c4f18c
[appc.git] / appc-outbound / appc-aai-client / provider / src / main / java / org / onap / appc / aai / client / node / AAIResourceNode.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.node;
26
27 import java.io.IOException;
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.Collections;
31 import java.util.Comparator;
32 import java.util.HashMap;
33 import java.util.List;
34 import java.util.Map;
35
36 import org.apache.commons.lang3.StringUtils;
37
38 import org.onap.appc.aai.client.AppcAaiClientConstant;
39 import org.onap.appc.aai.client.aai.AaiService;
40
41
42 import com.att.eelf.configuration.EELFLogger;
43 import com.att.eelf.configuration.EELFManager;
44
45 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
46 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
47 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
48
49 //import com.fasterxml.jackson.databind.ObjectMapper;
50
51
52 public class AAIResourceNode implements SvcLogicJavaPlugin {
53
54         private static final EELFLogger log = EELFManager.getInstance().getLogger(AAIResourceNode.class);
55
56         
57         public AaiService getAaiService() {
58                 return new AaiService();
59         }
60         /* Gets VNF Info and All VServers associated with Vnf */
61         public void getVnfInfo(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
62
63                 log.info("Received getVnfInfo call with params : " + inParams);
64
65                 String responsePrefix = inParams.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
66                 
67                 try {
68
69                         
70                         responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix+".") : "";
71                         AaiService aai = getAaiService();
72                         
73                         
74                                         
75                         aai.getGenericVnfInfo(inParams,ctx);
76                         
77                                                 
78                                                 
79                         
80                         ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_STATUS,
81                                         AppcAaiClientConstant.OUTPUT_STATUS_SUCCESS);
82                         log.info("getVnfInfo Successful ");
83                 } catch (Exception e) {
84                         ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_STATUS,
85                                         AppcAaiClientConstant.OUTPUT_STATUS_FAILURE);
86                         ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
87                         log.error("Failed in getVnfInfo " + e.getMessage());
88
89                         throw new SvcLogicException(e.getMessage());
90                 }
91         }
92         
93         
94         
95         public void getAllVServersVnfcsInfo(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
96                 
97                 log.info("Received getAllVServersVnfcsInfo call with params : " + inParams);
98
99                 String responsePrefix = inParams.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
100                 
101                 try {
102                         responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix+".") : "";
103                         AaiService aai = getAaiService();
104                         
105                         
106                         
107                         ArrayList<Map<String, String>> vservers = new ArrayList<Map<String, String>>();
108                         
109                         int vmWithNoVnfcsCount = 0;
110                         String  vmCountStr = ctx.getAttribute(responsePrefix + "vm-count");
111                         
112                         if ( vmCountStr == null )
113                                 throw new Exception("Unable to get VServers for the VNF");
114                         
115                         int vmCount = Integer.parseInt(vmCountStr);
116                         for ( int i = 0; i < vmCount; i++ ) {
117                                 
118                                 SvcLogicContext vmServerCtx = new SvcLogicContext();
119                                 
120                                 Map<String, String> paramsVm = new HashMap<String, String>();
121                     paramsVm.put("vserverId", ctx.getAttribute(responsePrefix + "vm[" + i + "].vserver-id"));
122                     paramsVm.put("tenantId", ctx.getAttribute(responsePrefix +"vm[" + i + "].tenant-id"));
123                     paramsVm.put("cloudOwner", ctx.getAttribute(responsePrefix +"vm[" + i + "].cloud-owner"));
124                     paramsVm.put("cloudRegionId", ctx.getAttribute(responsePrefix +"vm[" + i + "].cloud-region-id"));
125                         paramsVm.put(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX, inParams.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX));
126                     
127                     
128                     
129                                 aai.getVMInfo(paramsVm, vmServerCtx);
130                                 
131                                 HashMap<String, String> vserverMap = new HashMap<String, String>();
132                                 vserverMap.put("vserver-id", ctx.getAttribute(responsePrefix + "vm[" + i + "].vserver-id"));
133                                 vserverMap.put("tenant-id", ctx.getAttribute(responsePrefix +"vm[" + i + "].tenant-id"));
134                                 vserverMap.put("cloud-owner", ctx.getAttribute(responsePrefix +"vm[" + i + "].cloud-owner"));
135                                 vserverMap.put("cloud-region-id", ctx.getAttribute(responsePrefix +"vm[" + i + "].cloud-region-id"));
136                                 
137                                 // Parameters returned by getVMInfo
138                                 vserverMap.put("vserver-name", vmServerCtx.getAttribute(responsePrefix + "vm.vserver-name"));
139                                 vserverMap.put("vf-module-id", vmServerCtx.getAttribute(responsePrefix + "vm.vf-module-id"));
140                                 
141                                 
142                                 // as Per 17.07 requirements we are supporting only one VNFC per VM.
143                               
144                                 String vnfcName = vmServerCtx.getAttribute(responsePrefix + "vm.vnfc[0].vnfc-name");
145                                 vserverMap.put("vnfc-name", vnfcName);
146                                 
147                                 
148                                 String vnfcCount = vmServerCtx.getAttribute(responsePrefix + "vm.vnfc-count");
149                                 if ( vnfcCount == null )
150                                         vnfcCount = "0";
151                                 
152                                 vserverMap.put("vnfc-count", vnfcCount);
153                                 
154                                 if ( vnfcName != null  ) {
155                                         Map<String, String> paramsVnfc = new HashMap<String, String>();
156                     paramsVnfc.put("vnfcName", vnfcName);
157                    
158                                 paramsVnfc.put(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX, inParams.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX));
159                                 
160                                 SvcLogicContext vnfcCtx = new SvcLogicContext();
161                             
162                                 aai.getVnfcInfo(paramsVnfc, vnfcCtx);
163                                 
164                                 vserverMap.put("vnfc-type", vnfcCtx.getAttribute(responsePrefix + "vnfc.vnfc-type"));
165                                 vserverMap.put("vnfc-function-code", vnfcCtx.getAttribute(responsePrefix + "vnfc.vnfc-function-code"));
166                                 vserverMap.put("group-notation", vnfcCtx.getAttribute(responsePrefix + "vnfc.group-notation"));
167                                 
168                                         
169                                 }
170                                 else
171                                         vmWithNoVnfcsCount++;
172                                 
173                                 
174                                 
175                                 vservers.add(vserverMap);
176                                 
177                         } // vmCount
178                         
179                         
180                         
181                         
182                 Collections.sort(vservers, new Comparator<Map<String, String>>() {
183                             @Override
184                             public int compare(Map<String, String> o1, Map<String, String> o2) {
185                                 return o1.get("vserver-name").compareTo(o2.get("vserver-name"));
186                             }
187                         });
188                 
189                 log.info("SORTED VSERVERS " + vservers.toString());
190                 
191                 populateContext(vservers, ctx, responsePrefix);
192                 
193                 log.info("VMCOUNT IN GETALLVSERVERS " + vmCount);
194                 log.info("VMSWITHNOVNFCSCOUNT IN GETALLVSERVERS " + vmWithNoVnfcsCount);
195                 ctx.setAttribute(responsePrefix+"vnf.vm-count", String.valueOf(vmCount));
196                 ctx.setAttribute(responsePrefix+"vnf.vm-with-no-vnfcs-count", String.valueOf(vmWithNoVnfcsCount));
197                 
198                         
199                 } catch (Exception e) {
200                         ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_STATUS,
201                                         AppcAaiClientConstant.OUTPUT_STATUS_FAILURE);
202                         ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
203                         log.error("Failed in getAllVServersVnfcsInfo " + e.getMessage());
204
205                         throw new SvcLogicException(e.getMessage());
206                 }
207         }
208         
209         
210         
211         
212         public void populateContext(ArrayList<Map<String, String>> vservers, SvcLogicContext ctx, String prefix) {
213                 
214                 
215                 log.info("Populating Final Context");
216                 int ctr = 0;
217                 
218                 for (Map<String, String> entry : vservers) {
219                     for (String key : entry.keySet()) {
220                         String value = entry.get(key);
221                         
222                         ctx.setAttribute(prefix+ "vm[" + ctr + "]."+ key, value);
223                         log.info("Populating Context Key = " + prefix+ "vm[" + ctr + "]."+ key + " Value = " + value);
224                         
225                     }
226                     
227                    
228                     ctr++;
229                 }
230                 
231                 String firstVServerName = null;
232                 for  ( int i =0; i < ctr; i++ ) {
233                         String vnfcName = ctx.getAttribute(prefix + "vm[" + i + "].vnfc-name");
234                     log.info("VNFCNAME " + i + vnfcName);
235                     if (  vnfcName == null && firstVServerName == null ) {
236                         firstVServerName = ctx.getAttribute(prefix + "vm[" + i + "].vserver-name");
237                         ctx.setAttribute("vm-name" , firstVServerName);
238                         log.info("Populating Context Key = " +  "vm-name" + " Value = " + firstVServerName);
239                     }
240                 }
241         }
242
243
244
245         public void addVnfcs(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
246
247                 log.info("Received addVnfcs call with params : " + inParams);
248
249                 String responsePrefix = inParams.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
250                 
251                 int vnfcRefLen =0 ;
252                 int vmCount = 0;
253                 int vmWithNoVnfcCount = 0;
254                 
255                 try {
256
257                         responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix+".") : "";
258                         AaiService aai = getAaiService();
259                         
260                         
261                         
262                         String vnfcRefLenStr = ctx.getAttribute("vnfcReference_length");
263                         
264                         if ( vnfcRefLenStr == null) {
265                                 log.info("Vnfc Reference data is missing");
266                                 throw new Exception("Vnfc Reference data is missing");
267                                 
268                         }
269                         else            
270                                 vnfcRefLen = Integer.parseInt(vnfcRefLenStr);
271                         
272                         String vmWithNoVnfcCountStr = ctx.getAttribute(responsePrefix+"vnf.vm-with-no-vnfcs-count");
273                         
274       //Commented  for backward compatibility
275                         
276                         /*if ( vmWithNoVnfcCountStr == null) {
277                         throw new Exception("VNFCs to be added data from A&AI is missing");
278                         //log.info("VNFCs to be added data from A&AI is missing");
279                      }
280                         else
281                                 vmWithNoVnfcCount = Integer.parseInt(vmWithNoVnfcCountStr);
282
283                         if ( vmWithNoVnfcCount!= vnfcRefLen ) 
284                                 throw new Exception("Unable to Add Vnfcs to A&AI. Reference data mismatch.");
285
286                         String vmCountStr = ctx.getAttribute(responsePrefix+"vnf.vm-count");
287
288                         if ( vmCountStr == null)
289                                 throw new Exception("VM data from A&AI is missing");
290                         else
291                                 vmCount = Integer.parseInt(vmCountStr);
292
293
294                         log.info("VMCOUNT " + vmCount);
295                         log.info("VNFCREFLEN " + vnfcRefLen);
296                         aai.insertVnfcs(inParams,ctx, vnfcRefLen, vmCount);
297                                                 
298                  */
299                 
300                 
301                 // Modified for 1710
302                 
303                         if ( vmWithNoVnfcCountStr == null) {
304                                 log.info("Parameter VM without VNFCs(vmWithNoVnfcCountStr) from A&AI is Null");
305                         }
306                         else
307                                 vmWithNoVnfcCount = Integer.parseInt(vmWithNoVnfcCountStr);
308                         
309                         log.info("No of VM without VNFCs(vmWithNoVnfcCount) from A&AI is " +vmWithNoVnfcCount);
310
311                         String vmCountStr = ctx.getAttribute(responsePrefix+"vnf.vm-count");
312
313                         if ( vmCountStr == null)
314                                 throw new Exception("VM data from A&AI is missing");
315                         else
316                                 vmCount = Integer.parseInt(vmCountStr);
317
318                         log.info("VMCOUNT " + vmCount);
319                         log.info("VNFCREFLEN " + vnfcRefLen);
320
321                         if ( vmWithNoVnfcCount!= vnfcRefLen ) {
322                                 //throw new Exception("Unable to Add Vnfcs to A&AI. Reference data mismatch.");
323                                 log.info("vmWithNoVnfcCount and vnfcRefLen data from table are not same ");
324                                 aai.checkAndUpdateVnfc(inParams,ctx, vnfcRefLen, vmCount);
325                         }       
326
327                         else {
328
329                                 aai.insertVnfcs(inParams,ctx, vnfcRefLen, vmCount);
330                         }
331
332                                    //// Modified 1710
333                         
334                         ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_STATUS,
335                                         AppcAaiClientConstant.OUTPUT_STATUS_SUCCESS);
336                         
337                         log.info("addVnfcs Successful ");
338                 } catch (Exception e) {
339                         ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_STATUS,
340                                         AppcAaiClientConstant.OUTPUT_STATUS_FAILURE);
341                         ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
342                         log.error("Failed in addVnfcs " + e.getMessage());
343
344                         throw new SvcLogicException(e.getMessage());
345                 }
346         }
347         
348         
349         public void updateVnfAndVServerStatus(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
350
351                 log.info("Received updateVnfAndVServerStatus call with params : " + inParams);
352
353                 String responsePrefix = inParams.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
354                 
355                 
356                 int vmCount = 0;
357                 
358                 
359                 try {
360
361                         responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix+".") : "";
362                         AaiService aai = getAaiService();
363                         
364                                                 
365                         
366                         String vmCountStr = ctx.getAttribute(responsePrefix+"vnf.vm-count");
367                         
368                         if ( vmCountStr == null)
369                                 throw new Exception("VM data from A&AI is missing");
370                         else
371                                 vmCount = Integer.parseInt(vmCountStr);
372                         
373                         
374                         log.info("VMCOUNT " + vmCount);
375                         
376                         
377                         aai.updateVnfStatus(inParams, ctx);
378                         aai.updateVServerStatus(inParams,ctx, vmCount);
379                         
380                         ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_STATUS,
381                                         AppcAaiClientConstant.OUTPUT_STATUS_SUCCESS);
382                         
383                         log.info("updateVnfAndVServerStatus Successful ");
384                 } catch (Exception e) {
385                         ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_STATUS,
386                                         AppcAaiClientConstant.OUTPUT_STATUS_FAILURE);
387                         ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
388                         log.error("Failed in updateVnfAndVServerStatus " + e.getMessage());
389
390                         throw new SvcLogicException(e.getMessage());
391                 }
392         }
393         
394         /*public void getDummyValues(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
395                 
396                 log.info("Received getDummyValues call with params : " + inParams);
397
398                 String responsePrefix = inParams.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
399                 
400                 try {
401                         
402                                 responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix+".") : "";
403                                 String instarKeys = inParams.get("instarKeys");
404                                 ObjectMapper mapper = new ObjectMapper();
405                                 if ( instarKeys != null ) {
406                                         
407                                         List<String> keyList = mapper.readValue(instarKeys, ArrayList.class);
408                                         
409                                         Map<String, String> instarParams  =new HashMap<String, String>();
410                                         if(keyList != null){
411                                                 //System.out.println(keyList.toString());
412                                                 
413                                                 
414                                                 for(int i=0;i<keyList.size();i++)
415                                                 {
416                                                         log.info(" -->"+keyList.get(i));
417                                                     
418                                                     //ctx.setAttribute(keyList.get(i), "test" + i);
419                                                     
420                                                     instarParams.put( keyList.get(i), "test" + i);
421                                                 }
422                                                 
423                                         }
424                                         log.info("INSTARPARAMMAP " + instarParams);
425                                         String jsonString = mapper.writeValueAsString(instarParams);
426                                         log.info(jsonString);
427                                         ctx.setAttribute(responsePrefix + "configuration-parameters", jsonString);
428                                         
429                                 }
430
431                                 log.info("getDummyValues Successful ");
432                         } catch (Exception e) {
433                                 ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_STATUS,
434                                                 AppcAaiClientConstant.OUTPUT_STATUS_FAILURE);
435                                 ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
436                                 log.error("Failed in getDummyValues " + e.getMessage());
437                 
438                                 throw new SvcLogicException(e.getMessage());
439                         }
440         
441         }
442         
443         */
444         /*public void getRequestKeys(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
445                 
446                 log.info("Received getRequestKeys call with params : " + inParams);
447
448                 String responsePrefix = inParams.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
449                 
450                 try {
451                         
452                                 responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix+".") : "";
453                                 String instarKeys = inParams.get("instarKeys");
454                                 
455                                 ObjectMapper mapper = new ObjectMapper();
456                                 if ( instarKeys != null ) {
457                                         
458                                         List<String> keyList = mapper.readValue(instarKeys, ArrayList.class);
459                                         
460                                         //Map<String, String> instarParams  =new HashMap<String, String>();
461                                         if(keyList != null){
462                                                                                         
463                                                 
464                                                 for(int i=0;i<keyList.size();i++)
465                                                 {
466                                                         log.info("INSTARKEY -->"+keyList.get(i));
467                                                         
468                                                                                    
469                                                         String instarParameter = ctx.getAttribute("INSTAR." + keyList.get(i));
470                                                         log.info("INSTARPARAMETER " + instarParameter);
471                                                         Parameter param = parseParameterContent(instarParameter);
472                                                         
473                                                         log.info("PARAMETER KEY SIZE " + param.getRequestKeys().size());
474                                                         log.info("RULE TYPE " + param.getClassType());
475                                                         
476                                                         for ( int j =0 ; j < param.getRequestKeys().size() ; j++ ) {
477                                                                 
478                                                                 log.info(" PARAM KEY NAME " + param.getRequestKeys().get(j).getKeyName());
479                                                                 log.info(" PARAM KEY VALUE " + param.getRequestKeys().get(j).getKeyValue());
480                                                         }
481                                                     
482                                                    // instarParams.put( keyList.get(i), "test" + i);
483                                                 }
484                                                 
485                                         }
486                                         //log.info("INSTARPARAMMAP " + instarParams);
487                                         //String jsonString = mapper.writeValueAsString(instarParams);
488                                         //log.info(jsonString);
489                                         //ctx.setAttribute(responsePrefix + "configuration-parameters", jsonString);
490                                         
491                                 }
492
493                                 log.info("getRequestKeys Successful ");
494                         } catch (Exception e) {
495                                 ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_STATUS,
496                                                 AppcAaiClientConstant.OUTPUT_STATUS_FAILURE);
497                                 ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
498                                 log.error("Failed in getRequestKeys " + e.getMessage());
499                 
500                                 throw new SvcLogicException(e.getMessage());
501                         }
502         
503         }
504
505
506         public Parameter parseParameterContent(String parameter) throws JsonParseException, JsonMappingException, IOException{
507                 Parameter parameterDefinition = null;
508                 if(StringUtils.isNotBlank(parameter)){
509                         ObjectMapper mapper = new ObjectMapper();
510                         parameterDefinition = mapper.readValue(parameter, Parameter.class);
511                 }
512                 return parameterDefinition;
513         }*/
514 }