aboutsummaryrefslogtreecommitdiffstats
path: root/appc-outbound/appc-aai-client/provider/src/main/java/org/openecomp/appc/aai/client/node/AAIResourceNode.java
blob: 2505083582d0f180caf9a4087cd387e4f6c4f18c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/*-
 * ============LICENSE_START=======================================================
 * ONAP : APPC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Copyright (C) 2017 Amdocs
 * =============================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 * ============LICENSE_END=========================================================
 */

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

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import org.onap.appc.aai.client.AppcAaiClientConstant;
import org.onap.appc.aai.client.aai.AaiService;


import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;

import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
import org.onap.ccsdk.sli.core.sli.SvcLogicException;
import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;

//import com.fasterxml.jackson.databind.ObjectMapper;


public class AAIResourceNode implements SvcLogicJavaPlugin {

	private static final EELFLogger log = EELFManager.getInstance().getLogger(AAIResourceNode.class);

	
	public AaiService getAaiService() {
		return new AaiService();
	}
	/* Gets VNF Info and All VServers associated with Vnf */
	public void getVnfInfo(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {

		log.info("Received getVnfInfo call with params : " + inParams);

		String responsePrefix = inParams.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
		
		try {

			
			responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix+".") : "";
			AaiService aai = getAaiService();
			
			
					
			aai.getGenericVnfInfo(inParams,ctx);
			
						
						
			
			ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_STATUS,
					AppcAaiClientConstant.OUTPUT_STATUS_SUCCESS);
			log.info("getVnfInfo Successful ");
		} catch (Exception e) {
			ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_STATUS,
					AppcAaiClientConstant.OUTPUT_STATUS_FAILURE);
			ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
			log.error("Failed in getVnfInfo " + e.getMessage());

			throw new SvcLogicException(e.getMessage());
		}
	}
	
	
	
	public void getAllVServersVnfcsInfo(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
		
		log.info("Received getAllVServersVnfcsInfo call with params : " + inParams);

		String responsePrefix = inParams.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
		
		try {
			responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix+".") : "";
			AaiService aai = getAaiService();
			
			
			
			ArrayList<Map<String, String>> vservers = new ArrayList<Map<String, String>>();
			
			int vmWithNoVnfcsCount = 0;
			String  vmCountStr = ctx.getAttribute(responsePrefix + "vm-count");
			
			if ( vmCountStr == null )
				throw new Exception("Unable to get VServers for the VNF");
			
			int vmCount = Integer.parseInt(vmCountStr);
			for ( int i = 0; i < vmCount; i++ ) {
				
				SvcLogicContext vmServerCtx = new SvcLogicContext();
				
				Map<String, String> paramsVm = new HashMap<String, String>();
	            paramsVm.put("vserverId", ctx.getAttribute(responsePrefix + "vm[" + i + "].vserver-id"));
	            paramsVm.put("tenantId", ctx.getAttribute(responsePrefix +"vm[" + i + "].tenant-id"));
	            paramsVm.put("cloudOwner", ctx.getAttribute(responsePrefix +"vm[" + i + "].cloud-owner"));
	            paramsVm.put("cloudRegionId", ctx.getAttribute(responsePrefix +"vm[" + i + "].cloud-region-id"));
	        	paramsVm.put(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX, inParams.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX));
	            
	            
	            
				aai.getVMInfo(paramsVm, vmServerCtx);
				
				HashMap<String, String> vserverMap = new HashMap<String, String>();
				vserverMap.put("vserver-id", ctx.getAttribute(responsePrefix + "vm[" + i + "].vserver-id"));
				vserverMap.put("tenant-id", ctx.getAttribute(responsePrefix +"vm[" + i + "].tenant-id"));
				vserverMap.put("cloud-owner", ctx.getAttribute(responsePrefix +"vm[" + i + "].cloud-owner"));
				vserverMap.put("cloud-region-id", ctx.getAttribute(responsePrefix +"vm[" + i + "].cloud-region-id"));
				
				// Parameters returned by getVMInfo
				vserverMap.put("vserver-name", vmServerCtx.getAttribute(responsePrefix + "vm.vserver-name"));
				vserverMap.put("vf-module-id", vmServerCtx.getAttribute(responsePrefix + "vm.vf-module-id"));
				
				
				// as Per 17.07 requirements we are supporting only one VNFC per VM.
			      
				String vnfcName = vmServerCtx.getAttribute(responsePrefix + "vm.vnfc[0].vnfc-name");
				vserverMap.put("vnfc-name", vnfcName);
				
				
				String vnfcCount = vmServerCtx.getAttribute(responsePrefix + "vm.vnfc-count");
				if ( vnfcCount == null )
					vnfcCount = "0";
				
				vserverMap.put("vnfc-count", vnfcCount);
				
				if ( vnfcName != null  ) {
					Map<String, String> paramsVnfc = new HashMap<String, String>();
                    paramsVnfc.put("vnfcName", vnfcName);
                   
        			paramsVnfc.put(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX, inParams.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX));
        			
        			SvcLogicContext vnfcCtx = new SvcLogicContext();
	                    
        			aai.getVnfcInfo(paramsVnfc, vnfcCtx);
        			
        			vserverMap.put("vnfc-type", vnfcCtx.getAttribute(responsePrefix + "vnfc.vnfc-type"));
        			vserverMap.put("vnfc-function-code", vnfcCtx.getAttribute(responsePrefix + "vnfc.vnfc-function-code"));
        			vserverMap.put("group-notation", vnfcCtx.getAttribute(responsePrefix + "vnfc.group-notation"));
        			
	    				
				}
				else
					vmWithNoVnfcsCount++;
				
				
				
				vservers.add(vserverMap);
				
			} // vmCount
			
			
			
			
    		Collections.sort(vservers, new Comparator<Map<String, String>>() {
			    @Override
			    public int compare(Map<String, String> o1, Map<String, String> o2) {
			        return o1.get("vserver-name").compareTo(o2.get("vserver-name"));
			    }
			});
    		
    		log.info("SORTED VSERVERS " + vservers.toString());
    		
    		populateContext(vservers, ctx, responsePrefix);
    		
    		log.info("VMCOUNT IN GETALLVSERVERS " + vmCount);
    		log.info("VMSWITHNOVNFCSCOUNT IN GETALLVSERVERS " + vmWithNoVnfcsCount);
    		ctx.setAttribute(responsePrefix+"vnf.vm-count", String.valueOf(vmCount));
    		ctx.setAttribute(responsePrefix+"vnf.vm-with-no-vnfcs-count", String.valueOf(vmWithNoVnfcsCount));
    		
			
		} catch (Exception e) {
			ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_STATUS,
					AppcAaiClientConstant.OUTPUT_STATUS_FAILURE);
			ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
			log.error("Failed in getAllVServersVnfcsInfo " + e.getMessage());

			throw new SvcLogicException(e.getMessage());
		}
	}
	
	
	
	
	public void populateContext(ArrayList<Map<String, String>> vservers, SvcLogicContext ctx, String prefix) {
		
		
		log.info("Populating Final Context");
		int ctr = 0;
		
		for (Map<String, String> entry : vservers) {
		    for (String key : entry.keySet()) {
		        String value = entry.get(key);
		        
		       	ctx.setAttribute(prefix+ "vm[" + ctr + "]."+ key, value);
		    	log.info("Populating Context Key = " + prefix+ "vm[" + ctr + "]."+ key + " Value = " + value);
		   	
		    }
		    
		   
		    ctr++;
		}
		
		String firstVServerName = null;
		for  ( int i =0; i < ctr; i++ ) {
			String vnfcName = ctx.getAttribute(prefix + "vm[" + i + "].vnfc-name");
		    log.info("VNFCNAME " + i + vnfcName);
		    if (  vnfcName == null && firstVServerName == null ) {
	       		firstVServerName = ctx.getAttribute(prefix + "vm[" + i + "].vserver-name");
	       		ctx.setAttribute("vm-name" , firstVServerName);
	       		log.info("Populating Context Key = " +  "vm-name" + " Value = " + firstVServerName);
		    }
		}
	}



	public void addVnfcs(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {

		log.info("Received addVnfcs call with params : " + inParams);

		String responsePrefix = inParams.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
		
		int vnfcRefLen =0 ;
		int vmCount = 0;
		int vmWithNoVnfcCount = 0;
		
		try {

			responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix+".") : "";
			AaiService aai = getAaiService();
			
			
			
			String vnfcRefLenStr = ctx.getAttribute("vnfcReference_length");
			
			if ( vnfcRefLenStr == null) {
				log.info("Vnfc Reference data is missing");
				throw new Exception("Vnfc Reference data is missing");
				
			}
			else		
				vnfcRefLen = Integer.parseInt(vnfcRefLenStr);
			
			String vmWithNoVnfcCountStr = ctx.getAttribute(responsePrefix+"vnf.vm-with-no-vnfcs-count");
			
      //Commented  for backward compatibility
			
			/*if ( vmWithNoVnfcCountStr == null) {
			throw new Exception("VNFCs to be added data from A&AI is missing");
			//log.info("VNFCs to be added data from A&AI is missing");
		     }
			else
				vmWithNoVnfcCount = Integer.parseInt(vmWithNoVnfcCountStr);

			if ( vmWithNoVnfcCount!= vnfcRefLen ) 
				throw new Exception("Unable to Add Vnfcs to A&AI. Reference data mismatch.");

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

			if ( vmCountStr == null)
				throw new Exception("VM data from A&AI is missing");
			else
				vmCount = Integer.parseInt(vmCountStr);


			log.info("VMCOUNT " + vmCount);
			log.info("VNFCREFLEN " + vnfcRefLen);
			aai.insertVnfcs(inParams,ctx, vnfcRefLen, vmCount);
						
		 */
		
		
		// Modified for 1710
		
			if ( vmWithNoVnfcCountStr == null) {
				log.info("Parameter VM without VNFCs(vmWithNoVnfcCountStr) from A&AI is Null");
			}
			else
				vmWithNoVnfcCount = Integer.parseInt(vmWithNoVnfcCountStr);
			
			log.info("No of VM without VNFCs(vmWithNoVnfcCount) from A&AI is " +vmWithNoVnfcCount);

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

			if ( vmCountStr == null)
				throw new Exception("VM data from A&AI is missing");
			else
				vmCount = Integer.parseInt(vmCountStr);

			log.info("VMCOUNT " + vmCount);
			log.info("VNFCREFLEN " + vnfcRefLen);

			if ( vmWithNoVnfcCount!= vnfcRefLen ) {
				//throw new Exception("Unable to Add Vnfcs to A&AI. Reference data mismatch.");
				log.info("vmWithNoVnfcCount and vnfcRefLen data from table are not same ");
				aai.checkAndUpdateVnfc(inParams,ctx, vnfcRefLen, vmCount);
			}	

			else {

				aai.insertVnfcs(inParams,ctx, vnfcRefLen, vmCount);
			}

				   //// Modified 1710
			
			ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_STATUS,
					AppcAaiClientConstant.OUTPUT_STATUS_SUCCESS);
			
			log.info("addVnfcs Successful ");
		} catch (Exception e) {
			ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_STATUS,
					AppcAaiClientConstant.OUTPUT_STATUS_FAILURE);
			ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
			log.error("Failed in addVnfcs " + e.getMessage());

			throw new SvcLogicException(e.getMessage());
		}
	}
	
	
	public void updateVnfAndVServerStatus(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {

		log.info("Received updateVnfAndVServerStatus call with params : " + inParams);

		String responsePrefix = inParams.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
		
		
		int vmCount = 0;
		
		
		try {

			responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix+".") : "";
			AaiService aai = getAaiService();
			
						
			
			String vmCountStr = ctx.getAttribute(responsePrefix+"vnf.vm-count");
			
			if ( vmCountStr == null)
				throw new Exception("VM data from A&AI is missing");
			else
				vmCount = Integer.parseInt(vmCountStr);
			
			
			log.info("VMCOUNT " + vmCount);
			
			
			aai.updateVnfStatus(inParams, ctx);
			aai.updateVServerStatus(inParams,ctx, vmCount);
			
			ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_STATUS,
					AppcAaiClientConstant.OUTPUT_STATUS_SUCCESS);
			
			log.info("updateVnfAndVServerStatus Successful ");
		} catch (Exception e) {
			ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_STATUS,
					AppcAaiClientConstant.OUTPUT_STATUS_FAILURE);
			ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
			log.error("Failed in updateVnfAndVServerStatus " + e.getMessage());

			throw new SvcLogicException(e.getMessage());
		}
	}
	
	/*public void getDummyValues(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
		
		log.info("Received getDummyValues call with params : " + inParams);

		String responsePrefix = inParams.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
		
		try {
			
				responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix+".") : "";
				String instarKeys = inParams.get("instarKeys");
				ObjectMapper mapper = new ObjectMapper();
				if ( instarKeys != null ) {
					
					List<String> keyList = mapper.readValue(instarKeys, ArrayList.class);
					
					Map<String, String> instarParams  =new HashMap<String, String>();
					if(keyList != null){
						//System.out.println(keyList.toString());
						
						
						for(int i=0;i<keyList.size();i++)
						{
							log.info(" -->"+keyList.get(i));
						    
						    //ctx.setAttribute(keyList.get(i), "test" + i);
						    
						    instarParams.put( keyList.get(i), "test" + i);
						}
						
					}
					log.info("INSTARPARAMMAP " + instarParams);
					String jsonString = mapper.writeValueAsString(instarParams);
					log.info(jsonString);
					ctx.setAttribute(responsePrefix + "configuration-parameters", jsonString);
					
				}

				log.info("getDummyValues Successful ");
			} catch (Exception e) {
				ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_STATUS,
						AppcAaiClientConstant.OUTPUT_STATUS_FAILURE);
				ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
				log.error("Failed in getDummyValues " + e.getMessage());
		
				throw new SvcLogicException(e.getMessage());
			}
	
	}
	
	*/
	/*public void getRequestKeys(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
		
		log.info("Received getRequestKeys call with params : " + inParams);

		String responsePrefix = inParams.get(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX);
		
		try {
			
				responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix+".") : "";
				String instarKeys = inParams.get("instarKeys");
				
				ObjectMapper mapper = new ObjectMapper();
				if ( instarKeys != null ) {
					
					List<String> keyList = mapper.readValue(instarKeys, ArrayList.class);
					
					//Map<String, String> instarParams  =new HashMap<String, String>();
					if(keyList != null){
											
						
						for(int i=0;i<keyList.size();i++)
						{
							log.info("INSTARKEY -->"+keyList.get(i));
							
						   				   
							String instarParameter = ctx.getAttribute("INSTAR." + keyList.get(i));
							log.info("INSTARPARAMETER " + instarParameter);
							Parameter param = parseParameterContent(instarParameter);
							
							log.info("PARAMETER KEY SIZE " + param.getRequestKeys().size());
							log.info("RULE TYPE " + param.getClassType());
							
							for ( int j =0 ; j < param.getRequestKeys().size() ; j++ ) {
								
								log.info(" PARAM KEY NAME " + param.getRequestKeys().get(j).getKeyName());
								log.info(" PARAM KEY VALUE " + param.getRequestKeys().get(j).getKeyValue());
							}
						    
						   // instarParams.put( keyList.get(i), "test" + i);
						}
						
					}
					//log.info("INSTARPARAMMAP " + instarParams);
					//String jsonString = mapper.writeValueAsString(instarParams);
					//log.info(jsonString);
					//ctx.setAttribute(responsePrefix + "configuration-parameters", jsonString);
					
				}

				log.info("getRequestKeys Successful ");
			} catch (Exception e) {
				ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_STATUS,
						AppcAaiClientConstant.OUTPUT_STATUS_FAILURE);
				ctx.setAttribute(responsePrefix + AppcAaiClientConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
				log.error("Failed in getRequestKeys " + e.getMessage());
		
				throw new SvcLogicException(e.getMessage());
			}
	
	}


	public Parameter parseParameterContent(String parameter) throws JsonParseException, JsonMappingException, IOException{
		Parameter parameterDefinition = null;
		if(StringUtils.isNotBlank(parameter)){
			ObjectMapper mapper = new ObjectMapper();
			parameterDefinition = mapper.readValue(parameter, Parameter.class);
		}
		return parameterDefinition;
	}*/
}