NLSR Api server finished
diff --git a/nlsr.c b/nlsr.c
index f983557..06cc75f 100644
--- a/nlsr.c
+++ b/nlsr.c
@@ -603,9 +603,109 @@
 	char *msg;
 	msg=(char *)malloc(100);	
 	memset(msg,100,0);
-	strcpy(msg,"Action Carried Out for NLSR Api Client");
+	//strcpy(msg,"Action Carried Out for NLSR Api Client");
 
+	const char *sep=" \t\n";
+	char *rem=NULL;
+	char *cmd_type=NULL;
+	char *op_type=NULL;
+	char *name=NULL;
+	char *face=NULL;
+	int face_id;
+	int res;
+
+	op_type=strtok_r(command,sep,&rem);
+	cmd_type=strtok_r(NULL,sep,&rem);
+	name=strtok_r(NULL,sep,&rem);
+	if ( name[strlen(name)-1] == '/' )
+		name[strlen(name)-1]='\0';
+
+	struct name_prefix *np=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
+	np->name=(char *)malloc(strlen(name)+1);
+	memset(np->name,0,strlen(name)+1);
+	memcpy(np->name,name,strlen(name)+1);
+	np->length=strlen(name)+1;
+
+	if ( strcmp(cmd_type,"name")!= 0 )
+	{
+		face=strtok_r(NULL,sep,&rem);
+		sscanf(face,"face%d",&face_id);
+	}
 	
+	if ( strcmp(cmd_type,"name")== 0 )
+	{
+		if ( strcmp(op_type,"del") == 0 ) 
+		{
+			res=does_name_exist_in_npl(np);
+			if ( res == 0)
+			{
+				sprintf(msg,"Name %s does not exist !!",name);
+			}
+			else
+			{
+				long int ls_id=get_lsa_id_from_npl(np);
+				if ( ls_id != 0 )
+				{
+					make_name_lsa_invalid(np,LS_TYPE_NAME,ls_id);
+					sprintf(msg,"Name %s has been deleted and Advertised.",name);
+				}
+				else 
+				{
+					sprintf(msg,"Name %s does not have an Name LSA yet !!",name);
+				}
+			}			
+		}
+		else if ( strcmp(op_type,"add") == 0 )
+		{
+			res=does_name_exist_in_npl(np);
+			if ( res == 0)
+			{
+				add_name_to_npl(np);
+				build_and_install_single_name_lsa(np);
+				sprintf(msg,"Name %s has been added to advertise.",name);
+			}
+			else
+			{
+				sprintf(msg,"Name %s has already been advertised from this router !!",name);
+			}
+		} 
+	}
+	else if ( strcmp(cmd_type,"neighbor") == 0 )
+	{
+		if ( strcmp(op_type,"del") == 0 ) 
+		{
+			res=is_neighbor(np->name);
+			if ( res == 0)
+			{
+				sprintf(msg,"Neighbor %s does not exist !!",name);
+			}
+			else
+			{
+				update_adjacent_status_to_adl(np,NBR_DOWN);
+				delete_nbr_from_adl(np);
+				if(!nlsr->is_build_adj_lsa_sheduled)
+				{
+					nlsr->event_build_adj_lsa = ccn_schedule_event(nlsr->sched, 1000, &build_and_install_adj_lsa, NULL, 0);
+					nlsr->is_build_adj_lsa_sheduled=1;		
+				}
+				sprintf(msg,"Neighbor %s has been deleted from adjacency list.",name);	
+			}
+		}
+		else if ( strcmp(op_type,"add") == 0 )
+		{
+			res=is_neighbor(np->name);
+			if ( res == 0 )
+			{
+				add_nbr_to_adl(np,face_id);
+				sprintf(msg,"Neighbor %s has been added to adjacency list.",name);
+			}
+			else
+			{
+				sprintf(msg,"Neighbor %s already exists in adjacency list.",name);
+			}
+		}
+	}
+		
 
 	return msg;
 }
@@ -786,7 +886,7 @@
 	struct hashtb_param param_adl = {0};
 	nlsr->adl=hashtb_create(sizeof(struct ndn_neighbor), &param_adl);
 	struct hashtb_param param_npl = {0};
-	nlsr->npl = hashtb_create(sizeof(struct name_prefix), &param_npl);
+	nlsr->npl = hashtb_create(sizeof(struct name_prefix_list_entry), &param_npl);
 	struct hashtb_param param_pit_alsa = {0};	
 	nlsr->pit_alsa = hashtb_create(sizeof(struct pending_interest), &param_pit_alsa);
 	struct hashtb_param param_npt = {0};	
diff --git a/nlsr_adl.c b/nlsr_adl.c
index 1c30be8..54d8954 100644
--- a/nlsr_adl.c
+++ b/nlsr_adl.c
@@ -378,6 +378,35 @@
 	hashtb_end(e);
 }
 
+
+void 
+delete_nbr_from_adl(struct name_prefix *nbr)
+{
+	if ( nlsr->debugging )
+		printf("delete_nbr_from_adl called\n");
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"delete_nbr_from_adl called \n");
+
+	int res;
+	struct hashtb_enumerator ee;
+    	struct hashtb_enumerator *e = ⅇ
+
+	hashtb_start(nlsr->adl, e);
+   	res = hashtb_seek(e, nbr->name, nbr->length, 0);
+
+
+	if (res == HT_OLD_ENTRY)
+	{
+		hashtb_delete(e);	
+	}
+	else if(res == HT_NEW_ENTRY)
+	{
+		hashtb_delete(e);
+	}
+
+	hashtb_end(e);
+}
+
 void 
 update_lsdb_synch_interval_to_adl(struct name_prefix *nbr, long int interval)
 {
diff --git a/nlsr_adl.h b/nlsr_adl.h
index a1f48b2..5e9fa5a 100644
--- a/nlsr_adl.h
+++ b/nlsr_adl.h
@@ -21,6 +21,7 @@
 };
 
 void add_nbr_to_adl(struct name_prefix *new_nbr,int face);
+void delete_nbr_from_adl(struct name_prefix *nbr);
 void print_adjacent(struct ndn_neighbor *nbr);
 void print_adjacent_from_adl(void);
 int get_adjacent_status(struct name_prefix *nbr);
diff --git a/nlsr_lsdb.c b/nlsr_lsdb.c
index 5b30e70..bec9869 100644
--- a/nlsr_lsdb.c
+++ b/nlsr_lsdb.c
@@ -64,7 +64,7 @@
 	memcpy(key+strlen(key),"/",1);
 	memcpy(key+strlen(key),lsid,strlen(lsid));
 
-	printf("Key: %s\n",key);
+	//printf("Key: %s\n",key);
 	
 }
 
@@ -77,7 +77,8 @@
 		writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_and_install_name_lsas called\n");
 
 	int i, npl_element;
-	struct name_prefix *np;
+	//struct name_prefix *np;
+	struct name_prefix_list_entry *npe;
 
 	struct hashtb_enumerator ee;
     	struct hashtb_enumerator *e = ⅇ
@@ -87,11 +88,12 @@
 
 	for(i=0;i<npl_element;i++)
 	{
-		np=e->data;
+		npe=e->data;
 		struct nlsa *name_lsa=(struct nlsa *)malloc(sizeof( struct nlsa ));
-		build_name_lsa(name_lsa,np);
+		build_name_lsa(name_lsa,npe->np);
 		
 		install_name_lsa(name_lsa);
+		update_nlsa_id_for_name_in_npl(npe->np,name_lsa->header->ls_id);
 		free(name_lsa->header->orig_router->name);
 		free(name_lsa->header->orig_router);
 		free(name_lsa->header);
@@ -102,6 +104,33 @@
 	}
 
 	hashtb_end(e);	
+	
+	print_name_prefix_from_npl();
+
+}
+
+void 
+build_and_install_single_name_lsa(struct name_prefix *np)
+{
+	if ( nlsr->debugging )
+		printf("build_and_install_single_name_lsa called \n");
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"build_and_install_single_name_lsa called\n");
+	
+	struct nlsa *name_lsa=(struct nlsa *)malloc(sizeof( struct nlsa ));
+	build_name_lsa(name_lsa,np);
+		
+	install_name_lsa(name_lsa);
+	update_nlsa_id_for_name_in_npl(np,name_lsa->header->ls_id);
+
+	free(name_lsa->header->orig_router->name);
+	free(name_lsa->header->orig_router);
+	free(name_lsa->header);
+	free(name_lsa->name_prefix->name);
+	free(name_lsa->name_prefix);
+	free(name_lsa);
+	
+	print_name_prefix_from_npl();
 
 }
 
@@ -162,8 +191,12 @@
 		memset(key,0,strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1);
 
 
-		make_name_lsa_key(key, name_lsa->header->orig_router->name,name_lsa->header->ls_type,name_lsa->header->ls_id);	
-		printf("Key:%s Length:%d\n",key,(int)strlen(key));
+		make_name_lsa_key(key, name_lsa->header->orig_router->name,name_lsa->header->ls_type,name_lsa->header->ls_id);
+		if ( nlsr->debugging )
+			printf("Key:%s Length:%d\n",key,(int)strlen(key));
+		if ( nlsr->detailed_logging )
+			writeLogg(__FILE__,__FUNCTION__,__LINE__,"Key:%s Length:%d\n",key,(int)strlen(key));	
+		
 
 		struct nlsa *new_name_lsa=(struct nlsa*)malloc(sizeof(struct nlsa )); //free
 
@@ -504,7 +537,10 @@
 
 	hashtb_end(e);
 
-	printf("\n");
+	if ( nlsr->debugging )
+		printf("\n");
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
 }
 
 
@@ -732,7 +768,10 @@
 		{
 			if ( adj_lsa->no_link > 0)
 			{
-				printf("New ADJ LSA... Adding to LSDB\n");
+				if ( nlsr->debugging )
+					printf("New ADJ LSA... Adding to LSDB\n");
+				if ( nlsr->detailed_logging )
+					writeLogg(__FILE__,__FUNCTION__,__LINE__,"New ADJ LSA... Adding to LSDB\n");
 				new_adj_lsa = e->data;
 
 				new_adj_lsa->header=(struct alsa_header *)malloc(sizeof(struct alsa_header ));
@@ -1008,7 +1047,10 @@
 
 	hashtb_end(e);
 
-	printf("\n");
+	if ( nlsr->debugging )
+		printf("\n");
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
 }
 
 void 
@@ -1456,6 +1498,75 @@
 	hashtb_end(e);
 }
 
+void
+make_name_lsa_invalid(struct name_prefix *np,int ls_type, long int ls_id)
+{
+
+	if ( nlsr->debugging )
+		printf("make_name_lsa_invalid called  \n");	
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"make_name_lsa_invalid called  \n");
+
+
+	char lst[2];
+	memset(lst,0,2);
+	sprintf(lst,"%d",ls_type);	
+
+	char lsid[10];
+	memset(lsid,0,10);
+	sprintf(lsid,"%ld",ls_id);
+	
+	
+	char *key=(char *)malloc(strlen(np->name)+1+strlen(lst)+1+strlen(lsid)+1);
+	memset(key,0,strlen(np->name)+1+strlen(lst)+1+strlen(lsid)+1);
+
+
+	make_name_lsa_key(key, np->name,ls_type,ls_id);	
+	printf("Key:%s Length:%d\n",key,(int)strlen(key));
+
+	struct nlsa *nlsa;
+
+	struct hashtb_enumerator ee;
+    	struct hashtb_enumerator *e = &ee;
+	 	
+    	int res;
+
+   	hashtb_start(nlsr->lsdb->name_lsdb, e);
+    	res = hashtb_seek(e, key,strlen(key) , 0);
+
+	if( res == HT_OLD_ENTRY )
+	{
+		nlsa=e->data;
+	
+		nlsa->header->isValid=0;
+
+		writeLogg(__FILE__,__FUNCTION__,__LINE__," Name-LSA\n");
+		writeLogg(__FILE__,__FUNCTION__,__LINE__," Deleting name lsa\n");
+		write_log_for_name_lsa(nlsa);
+		writeLogg(__FILE__,__FUNCTION__,__LINE__," name_lsa_end\n");
+
+		hashtb_delete(e);
+	}
+	else if( res == HT_NEW_ENTRY )
+	{
+		hashtb_delete(e);
+	}
+	hashtb_end(e);
+	
+	if ( nlsr->debugging )
+		printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
+
+	set_new_lsdb_version();	
+
+	if ( nlsr->debugging )
+		printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version);
+
+}
+
 int 
 delete_name_lsa(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
 {
diff --git a/nlsr_lsdb.h b/nlsr_lsdb.h
index 9ba33b1..0383954 100644
--- a/nlsr_lsdb.h
+++ b/nlsr_lsdb.h
@@ -47,6 +47,7 @@
 
 void set_new_lsdb_version(void);
 void build_and_install_name_lsas(void);
+void build_and_install_single_name_lsa(struct name_prefix *np);
 void build_name_lsa(struct nlsa *name_lsa, struct name_prefix *np);
 void install_name_lsa(struct nlsa *name_lsa);
 void print_name_lsa(struct nlsa *name_lsa);
@@ -81,4 +82,6 @@
 void get_name_lsa_data(struct ccn_charbuf *lsa_data,struct name_prefix *lsaId);
 void get_adj_lsa_data(struct ccn_charbuf *lsa_data,struct name_prefix *lsaId);
 
+void make_name_lsa_invalid(struct name_prefix *np,int ls_type, long int ls_id);
+
 #endif
diff --git a/nlsr_ndn.c b/nlsr_ndn.c
index 82e8df8..9af0a05 100644
--- a/nlsr_ndn.c
+++ b/nlsr_ndn.c
@@ -839,38 +839,69 @@
 			orig_router=strtok_r(NULL,sep,&rem);
 			lst=strtok_r(NULL,sep,&rem);
 			ls_type=atoi(lst);
-			printf("Orig Router: %s ls Type: %d",orig_router,ls_type);			
+
+			if ( nlsr->debugging )
+				printf("Orig Router: %s ls Type: %d",orig_router,ls_type);
+			if ( nlsr->detailed_logging )
+				writeLogg(__FILE__,__FUNCTION__,__LINE__,"Orig Router: %s ls Type: %d",orig_router,ls_type);
+		
 
 			if(ls_type == LS_TYPE_NAME)
 			{
 				lsid=strtok_r(NULL,sep,&rem);
 				ls_id=atoi(lsid);
 				orig_time=strtok_r(NULL,sep,&rem);
-				printf(" LS Id: %ld  Orig Time: %s\n",ls_id ,orig_time);
+
+				if ( nlsr->debugging )
+					printf(" LS Id: %ld  Orig Time: %s\n",ls_id ,orig_time);
+				if ( nlsr->detailed_logging )
+					writeLogg(__FILE__,__FUNCTION__,__LINE__," LS Id: %ld  Orig Time: %s\n",ls_id ,orig_time);
+
+				
 				int is_new_name_lsa=check_is_new_name_lsa(orig_router,lst,lsid,orig_time);
 				if ( is_new_name_lsa == 1 )
 				{
-					printf("New NAME LSA.....\n");
+					if ( nlsr->debugging )
+						printf("New NAME LSA.....\n");
+					if ( nlsr->detailed_logging )
+						writeLogg(__FILE__,__FUNCTION__,__LINE__,"New NAME LSA.....\n");
+					
 					send_interest_for_name_lsa(nbr,orig_router,lst,lsid);	
 				}
 				else 
 				{
-					printf("Name LSA already exists in LSDB\n");
+					if ( nlsr->debugging )
+						printf("Name LSA already exists in LSDB\n");
+					if ( nlsr->detailed_logging )
+						writeLogg(__FILE__,__FUNCTION__,__LINE__,"Name LSA already exists in LSDB\n");
+					
 				}
 			}
 			else
 			{
 				orig_time=strtok_r(NULL,sep,&rem);
-				printf(" Orig Time: %s\n",orig_time);
+
+				if ( nlsr->debugging )
+					printf(" Orig Time: %s\n",orig_time);
+				if ( nlsr->detailed_logging )
+					writeLogg(__FILE__,__FUNCTION__,__LINE__," Orig Time: %s\n",orig_time);
+				
+
 				int is_new_adj_lsa=check_is_new_adj_lsa(orig_router,lst,orig_time);
 				if ( is_new_adj_lsa == 1 )
 				{
-					printf("New ADJ LSA.....\n");
+					if ( nlsr->debugging )
+						printf("New Adj LSA.....\n");
+					if ( nlsr->detailed_logging )
+						writeLogg(__FILE__,__FUNCTION__,__LINE__,"New Adj LSA.....\n");
 					send_interest_for_adj_lsa(nbr,orig_router,lst);
 				}
 				else
 				{
-					printf("ADJ LSA already exists in LSDB\n");
+					if ( nlsr->debugging )
+						printf("Adj LSA already exists in LSDB\n");
+					if ( nlsr->detailed_logging )
+						writeLogg(__FILE__,__FUNCTION__,__LINE__,"Adj LSA already exists in LSDB\n");
 				}
 			}
 
@@ -880,9 +911,17 @@
 		memset(lsdb_version,0,20);
 		get_lsdb_version(lsdb_version,selfp,info);
 
-		printf("Old LSDB Version of Neighbor: %s is :%s\n",nbr->name,get_nbr_lsdb_version(nbr->name));		
+		if ( nlsr->debugging )
+			printf("Old LSDB Version of Neighbor: %s is :%s\n",nbr->name,get_nbr_lsdb_version(nbr->name));
+		if ( nlsr->detailed_logging )
+			writeLogg(__FILE__,__FUNCTION__,__LINE__,"Old LSDB Version of Neighbor: %s is :%s\n",nbr->name,get_nbr_lsdb_version(nbr->name));
+
 		update_adjacent_lsdb_version_to_adl(nbr,lsdb_version);
-		printf("New LSDB Version of Neighbor: %s is :%s\n",nbr->name,get_nbr_lsdb_version(nbr->name));
+		
+		if ( nlsr->debugging )
+			printf("New LSDB Version of Neighbor: %s is :%s\n",nbr->name,get_nbr_lsdb_version(nbr->name));
+		if ( nlsr->detailed_logging )
+			writeLogg(__FILE__,__FUNCTION__,__LINE__,"New LSDB Version of Neighbor: %s is :%s\n",nbr->name,get_nbr_lsdb_version(nbr->name));
 
 		update_lsdb_interest_timed_out_zero_to_adl(nbr);
 
@@ -901,7 +940,11 @@
 	}
 	else 
 	{
-		printf("NACK Content Received\n");
+		
+		if ( nlsr->debugging )
+			printf("NACK Content Received\n");
+		if ( nlsr->detailed_logging )
+			writeLogg(__FILE__,__FUNCTION__,__LINE__,"NACK Content Received\n");
 		struct name_prefix *nbr=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
 		get_nbr(nbr,selfp,info);
 		update_lsdb_interest_timed_out_zero_to_adl(nbr);
@@ -913,7 +956,12 @@
 void 
 process_incoming_content_lsa(struct ccn_closure *selfp, struct ccn_upcall_info* info)
 {
-	printf("process_incoming_content_lsa called \n");
+	
+
+	if ( nlsr->debugging )
+		printf("process_incoming_content_lsa called \n");
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_content_lsa called \n");	
 
 	char *sep="|";
 	char *rem;
@@ -942,9 +990,10 @@
 	
 
 	
-	
-
-	printf("LSA Data\n");
+	if ( nlsr->debugging )
+		printf("LSA Data \n");
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"LSA Data\n");	
 
 	if( strlen((char *) ptr ) > 0 )
 	{
@@ -953,13 +1002,17 @@
 		orl=strtok_r(NULL,sep,&rem);
 		orig_router_length=atoi(orl);
 
-		printf("	Orig Router Name  : %s\n",orig_router);
-		printf("	Orig Router Length: %d\n",orig_router_length);
+		if ( nlsr->debugging )
+		{
+			printf("	Orig Router Name  : %s\n",orig_router);
+			printf("	Orig Router Length: %d\n",orig_router_length);
+		}
 
 		lst=strtok_r(NULL,sep,&rem);		
 		ls_type=atoi(lst);
 
-		printf("	LS Type  : %d\n",ls_type);
+		if ( nlsr->debugging )
+			printf("	LS Type  : %d\n",ls_type);
 
 		if ( ls_type == LS_TYPE_NAME )
 		{
@@ -971,11 +1024,14 @@
 			np=strtok_r(NULL,sep,&rem);
 			np_length=strtok_r(NULL,sep,&rem);
 			name_length=atoi(np_length);
-			printf("	LS ID  : %ld\n",ls_id);
-			printf("	isValid  : %d\n",isValid);
-			printf("	Name Prefix : %s\n",np);
-			printf("	Orig Time   : %s\n",orig_time);
-			printf("	Name Prefix length: %d\n",name_length);
+			if ( nlsr->debugging )
+			{
+				printf("	LS ID  : %ld\n",ls_id);
+				printf("	isValid  : %d\n",isValid);
+				printf("	Name Prefix : %s\n",np);
+				printf("	Orig Time   : %s\n",orig_time);
+				printf("	Name Prefix length: %d\n",name_length);
+			}
 
 			build_and_install_others_name_lsa(orig_router,ls_type,ls_id,orig_time,isValid,np);
 
@@ -987,9 +1043,11 @@
 			no_link=atoi(num_link);
 			data=rem;
 
-			printf("	No Link  : %d\n",no_link);
-			printf("	Data  : %s\n",data);
-
+			if ( nlsr->debugging )
+			{
+				printf("	No Link  : %d\n",no_link);
+				printf("	Data  : %s\n",data);
+			}
 			build_and_install_others_adj_lsa(orig_router,ls_type,orig_time,no_link,data);
 		}
 	}
@@ -999,7 +1057,13 @@
 void
 process_incoming_timed_out_interest(struct ccn_closure* selfp, struct ccn_upcall_info* info)
 {
-	printf("process_incoming_timed_out_interest called \n");
+	
+
+	if ( nlsr->debugging )
+		printf("process_incoming_timed_out_interest called \n");
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_timed_out_interest called \n");
+
 	int res,i;
 	int nlsr_position=0;
 	int name_comps=(int)info->interest_comps->n;
@@ -1031,27 +1095,38 @@
 void
 process_incoming_timed_out_interest_info(struct ccn_closure* selfp, struct ccn_upcall_info* info)
 {
-	printf("process_incoming_timed_out_interest_info called \n");
+	
+	if ( nlsr->debugging )
+		printf("process_incoming_timed_out_interest_info called \n");
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_timed_out_interest_info called \n");
 
 	struct name_prefix *nbr=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
 	get_nbr(nbr,selfp,info);
 
-	printf("Info Interest Timed Out for for Neighbor: %s Length:%d\n",nbr->name,nbr->length);
+	if ( nlsr->debugging )
+		printf("Info Interest Timed Out for for Neighbor: %s Length:%d\n",nbr->name,nbr->length);
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"Info Interest Timed Out for for Neighbor: %s Length:%d\n",nbr->name,nbr->length);
+	
 
 
 	update_adjacent_timed_out_to_adl(nbr,1);
 	print_adjacent_from_adl();	
 	int timed_out=get_timed_out_number(nbr);
 
+	if ( nlsr->debugging )
+		printf("Neighbor: %s Info Interest Timed Out: %d times\n",nbr->name,timed_out);
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"Neighbor: %s Info Interest Timed Out: %d times\n",nbr->name,timed_out);
+
 
 	if(timed_out<nlsr->interest_retry && timed_out>0) // use configured variables 
 	{
-		printf("Neighbor: %s Info Interest Timed Out: %d times\n",nbr->name,timed_out);
 		send_info_interest_to_neighbor(nbr);
 	}
 	else
-	{
-		printf("Neighbor: %s Info Interest Timed Out: %d times\n",nbr->name,timed_out);		
+	{		
 		update_adjacent_status_to_adl(nbr,NBR_DOWN);
 		if(!nlsr->is_build_adj_lsa_sheduled)
 		{
@@ -1068,18 +1143,30 @@
 void
 process_incoming_timed_out_interest_lsdb(struct ccn_closure* selfp, struct ccn_upcall_info* info)
 {
-	printf("process_incoming_timed_out_interest_lsdb called \n");
+	if ( nlsr->debugging )
+		printf("process_incoming_timed_out_interest_lsdb called \n");
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_timed_out_interest_lsdb called \n");
 
 	struct name_prefix *nbr=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
 	get_nbr(nbr,selfp,info);
 
-	printf("LSDB Interest Timed Out for for Neighbor: %s Length:%d\n",nbr->name,nbr->length);
+	if ( nlsr->debugging )
+		printf("LSDB Interest Timed Out for for Neighbor: %s Length:%d\n",nbr->name,nbr->length);
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"LSDB Interest Timed Out for for Neighbor: %s Length:%d\n",nbr->name,nbr->length);
+	
 
 	update_lsdb_interest_timed_out_to_adl(nbr,1);
 
 	int interst_timed_out_num=get_lsdb_interest_timed_out_number(nbr);
 
-	printf("Interest Timed out number : %d Interest Retry: %d \n",interst_timed_out_num,nlsr->interest_retry);
+	if ( nlsr->debugging )
+		printf("Interest Timed out number : %d Interest Retry: %d \n",interst_timed_out_num,nlsr->interest_retry);
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"Interest Timed out number : %d Interest Retry: %d \n",interst_timed_out_num,nlsr->interest_retry);
+
+	
 
 	if( interst_timed_out_num >= nlsr->interest_retry )
 	{
@@ -1097,7 +1184,10 @@
 void
 process_incoming_timed_out_interest_lsa(struct ccn_closure* selfp, struct ccn_upcall_info* info)
 {
-	printf("process_incoming_timed_out_interest_lsa called \n");
+	if ( nlsr->debugging )
+		printf("process_incoming_timed_out_interest_lsa called \n");
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_timed_out_interest_lsa called \n");
 	
 }
 
@@ -1111,8 +1201,15 @@
 
          nlsr_lock();
 
-	printf("send_info_interest called \n");
-	printf("\n");
+	if ( nlsr->debugging )
+		printf("send_info_interest called \n");
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"send_info_interest called \n");
+
+	if ( nlsr->debugging )
+		printf("\n");
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
 
 	int adl_element,i;
 	struct ndn_neighbor *nbr;
@@ -1141,7 +1238,12 @@
 void 
 send_info_interest_to_neighbor(struct name_prefix *nbr)
 {
-	printf("send_info_interest_to_neighbor called \n");
+
+	if ( nlsr->debugging )
+		printf("send_info_interest_to_neighbor called \n");
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"send_info_interest_to_neighbor called \n");
+
 
 	int res;
 	char info_str[5];
@@ -1186,12 +1288,20 @@
 		ccn_charbuf_append_closer(templ); /* </Interest> */
 		/* Adding InterestLifeTime and InterestScope filter done */
 	
-		printf("Sending info interest on name prefix : %s \n",int_name);
+		if ( nlsr->debugging )
+			printf("Sending info interest on name prefix : %s \n",int_name);
+		if ( nlsr->detailed_logging )
+			writeLogg(__FILE__,__FUNCTION__,__LINE__,"Sending info interest on name prefix : %s \n",int_name);
 
 		res=ccn_express_interest(nlsr->ccn,name,&(nlsr->in_content),templ);
 
 		if ( res >= 0 )
-			printf("Info interest sending Successfull .... \n");	
+		{
+			if ( nlsr->debugging )
+				printf("Info interest sending Successfull .... \n");
+			if ( nlsr->detailed_logging )
+				writeLogg(__FILE__,__FUNCTION__,__LINE__,"Info interest sending Successfull .... \n");
+		}	
 		ccn_charbuf_destroy(&templ);
 	}
 	ccn_charbuf_destroy(&name);
@@ -1203,7 +1313,10 @@
 int 
 send_lsdb_interest(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
 {
-	printf("send_lsdb_interest called \n");	
+	if ( nlsr->debugging )
+		printf("send_lsdb_interest called \n");
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"send_lsdb_interest called \n");	
 
 	if(flags == CCN_SCHEDULE_CANCEL)
 	{
@@ -1229,8 +1342,12 @@
 		{	
 			if(nbr->is_lsdb_send_interest_scheduled == 0)
 			{
-				long int time_diff=get_nbr_time_diff_lsdb_req(nbr->neighbor->name);	
-				printf("Time since last time LSDB requested : %ld Seconds for Neighbor: %s \n",time_diff,nbr->neighbor->name);		
+				long int time_diff=get_nbr_time_diff_lsdb_req(nbr->neighbor->name);
+				if ( nlsr->debugging )
+					printf("Time since last time LSDB requested : %ld Seconds for Neighbor: %s \n",time_diff,nbr->neighbor->name);
+				if ( nlsr->detailed_logging )
+					writeLogg(__FILE__,__FUNCTION__,__LINE__,"Time since last time LSDB requested : %ld Seconds for Neighbor: %s \n",time_diff,nbr->neighbor->name);	
+						
 
 				if( time_diff >= ( get_lsdb_synch_interval(nbr->neighbor->name) + get_nbr_random_time_component(nbr->neighbor->name) ) )
 				{
@@ -1253,14 +1370,21 @@
 void 
 send_lsdb_interest_to_nbr(struct name_prefix *nbr)
 {
-	printf("send_lsdb_interest_to_nbr called \n");	
+	if ( nlsr->debugging )
+		printf("send_lsdb_interest_to_nbr called \n");
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"send_lsdb_interest_to_nbr called \n");
 
 	char *last_lsdb_version=get_nbr_lsdb_version(nbr->name);
 
 	if(last_lsdb_version !=NULL)
 	{
-		printf("Last LSDB Version: %s \n",last_lsdb_version);
+		
 
+		if ( nlsr->debugging )
+			printf("Last LSDB Version: %s \n",last_lsdb_version);
+		if ( nlsr->detailed_logging )
+			writeLogg(__FILE__,__FUNCTION__,__LINE__,"Last LSDB Version: %s \n",last_lsdb_version);
 
 		struct ccn_charbuf *name;
 		int res;
@@ -1272,7 +1396,11 @@
 		memset(&lsdb_str,0,5);
 		sprintf(lsdb_str,"lsdb");		
 		//make and send interest with exclusion filter as last_lsdb_version
-		printf("Sending interest for name prefix:%s/%s/%s\n",nbr->name,nlsr_str,lsdb_str);	
+		if ( nlsr->debugging )
+			printf("Sending interest for name prefix:%s/%s/%s\n",nbr->name,nlsr_str,lsdb_str);
+		if ( nlsr->detailed_logging )
+			writeLogg(__FILE__,__FUNCTION__,__LINE__,"Sending interest for name prefix:%s/%s/%s\n",nbr->name,nlsr_str,lsdb_str);
+			
 		name=ccn_charbuf_create();
 		res=ccn_name_from_uri(name,nbr->name);
 
@@ -1315,7 +1443,10 @@
 
 			if ( res >= 0 )
 			{
-				printf("Interest sending Successfull .... \n");	
+				if ( nlsr->debugging )
+					printf("Interest sending Successfull .... \n");
+				if ( nlsr->detailed_logging )
+					writeLogg(__FILE__,__FUNCTION__,__LINE__,"Interest sending Successfull .... \n");	
 				update_adjacent_last_lsdb_requested_to_adl(nbr->name,get_current_time_sec());
 
 			}
@@ -1330,7 +1461,10 @@
 void 
 send_interest_for_name_lsa(struct name_prefix *nbr, char *orig_router, char *ls_type, char *ls_id)
 {
-	printf("send_interest_for_name_lsa called\n");
+	if ( nlsr->debugging )
+		printf("send_interest_for_name_lsa called\n");
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"send_interest_for_name_lsa called\n");
 
 	int res;
 	char lsa_str[5];
@@ -1381,13 +1515,22 @@
 	ccn_charbuf_append_closer(templ); /* </Interest> */
 	/* Adding InterestLifeTime and InterestScope filter done */
 
-	printf("Sending NAME LSA interest on name prefix : %s/%s/%s\n",int_name,ls_type,ls_id);
+	if ( nlsr->debugging )
+		printf("Sending NAME LSA interest on name prefix : %s/%s/%s\n",int_name,ls_type,ls_id);
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"Sending NAME LSA interest on name prefix : %s/%s/%s\n",int_name,ls_type,ls_id);
+
 
 	res=ccn_express_interest(nlsr->ccn,name,&(nlsr->in_content),templ);
 
 	if ( res >= 0 )
-		printf("NAME LSA interest sending Successfull .... \n");	
+	{
+		if ( nlsr->debugging )
+			printf("NAME LSA interest sending Successfull .... \n");
+		if ( nlsr->detailed_logging )
+			writeLogg(__FILE__,__FUNCTION__,__LINE__,"NAME LSA interest sending Successfull .... \n");
 	
+	}	
 	ccn_charbuf_destroy(&templ);
 	ccn_charbuf_destroy(&name);
 	free(int_name);
@@ -1398,7 +1541,10 @@
 void 
 send_interest_for_adj_lsa(struct name_prefix *nbr, char *orig_router, char *ls_type)
 {
-	printf("send_interest_for_adj_lsa called\n");
+	if ( nlsr->debugging )
+		printf("send_interest_for_name_lsa called\n");
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"send_interest_for_name_lsa called\n");
 
 	int res;
 	char lsa_str[5];
@@ -1446,13 +1592,19 @@
 	ccn_charbuf_append_closer(templ); /* </Interest> */
 	/* Adding InterestLifeTime and InterestScope filter done */
 
-	printf("Sending ADJ LSA interest on name prefix : %s\n",int_name);
+	if ( nlsr->debugging )
+		printf("Sending ADJ LSA interest on name prefix : %s\n",int_name);
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"Sending ADJ LSA interest on name prefix : %s\n",int_name);
 
 	res=ccn_express_interest(nlsr->ccn,name,&(nlsr->in_content),templ);
 
 	if ( res >= 0 )
 	{
-		printf("ADJ LSA interest sending Successfull .... \n");	
+		if ( nlsr->debugging )
+			printf("ADJ LSA interest sending Successfull .... \n");	
+		if ( nlsr->detailed_logging )
+			writeLogg(__FILE__,__FUNCTION__,__LINE__,"ADJ LSA interest sending Successfull .... \n");	
 	}
 	
 	ccn_charbuf_destroy(&templ);
diff --git a/nlsr_npl.c b/nlsr_npl.c
index c384133..a2e2271 100644
--- a/nlsr_npl.c
+++ b/nlsr_npl.c
@@ -25,7 +25,8 @@
 void 
 add_name_to_npl(struct name_prefix *np)
 {
-	struct name_prefix *hnp=(struct name_prefix *)malloc(sizeof(struct name_prefix )); //free
+	struct name_prefix_list_entry *npe=(struct name_prefix_list_entry *)malloc(sizeof(struct name_prefix_list_entry));
+	//struct name_prefix *hnp=(struct name_prefix *)malloc(sizeof(struct name_prefix )); //free
 
 	struct hashtb_enumerator ee;
     	struct hashtb_enumerator *e = &ee; 	
@@ -36,17 +37,81 @@
 
 	if(res == HT_NEW_ENTRY)
 	{   
-
-		hnp = e->data;
-		hnp->length=np->length;
-		hnp->name=(char *)malloc(np->length); //free
-		memcpy(hnp->name,np->name,np->length);
+		npe=e->data;
+		npe->np=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
+		npe->np->length=np->length;
+		npe->np->name=(char *)malloc(np->length);
+		memcpy(npe->np->name,np->name,np->length);
+		npe->name_lsa_id=0;
+		//hnp = e->data;
+		//hnp->length=np->length;
+		//hnp->name=(char *)malloc(np->length); //free
+		//memcpy(hnp->name,np->name,np->length);
 	}
     	
 	hashtb_end(e);
 
 }
 
+int  
+does_name_exist_in_npl(struct name_prefix *np)
+{
+	int ret=0;
+
+	//struct name_prefix_entry *npe;
+
+	struct hashtb_enumerator ee;
+    	struct hashtb_enumerator *e = &ee; 	
+    	int res;
+
+   	hashtb_start(nlsr->npl, e);
+    	res = hashtb_seek(e, np->name, np->length, 0);
+
+	if(res == HT_NEW_ENTRY)
+	{   
+		hashtb_delete(e);
+		ret=0;
+	}
+	else
+	{
+		ret=1;
+    	}
+	hashtb_end(e);
+
+	return ret;
+
+}
+
+
+long int  
+get_lsa_id_from_npl(struct name_prefix *np)
+{
+	int ret=0;
+
+	struct name_prefix_list_entry *npe;
+
+	struct hashtb_enumerator ee;
+    	struct hashtb_enumerator *e = &ee; 	
+    	int res;
+
+   	hashtb_start(nlsr->npl, e);
+    	res = hashtb_seek(e, np->name, np->length, 0);
+
+	if(res == HT_NEW_ENTRY)
+	{   
+		hashtb_delete(e);
+		ret=0;
+	}
+	else
+	{
+		npe=e->data;
+		ret=npe->name_lsa_id;
+    	}
+	hashtb_end(e);
+
+	return ret;
+
+}
 
 void
 print_name_prefix_from_npl(void)
@@ -56,7 +121,8 @@
 	if ( nlsr->detailed_logging )
 		writeLogg(__FILE__,__FUNCTION__,__LINE__,"print_name_prefix_from_npl called\n");
 	int i, npl_element;
-	struct name_prefix *np;
+	//struct name_prefix *np;
+	struct name_prefix_list_entry *npe;
 
 	struct hashtb_enumerator ee;
     	struct hashtb_enumerator *e = &ee;
@@ -66,16 +132,43 @@
 
 	for(i=0;i<npl_element;i++)
 	{
-		np=e->data;
+		npe=e->data;
 		if ( nlsr->debugging )
-			printf("Name Prefix: %s and Length: %d \n",np->name,np->length);
+			printf("Name Prefix: %s and Length: %d and LSA Id: %ld\n",npe->np->name,npe->np->length,npe->name_lsa_id);
 		if ( nlsr->detailed_logging )
-			writeLogg(__FILE__,__FUNCTION__,__LINE__,"Name Prefix: %s and Length: %d \n",np->name,np->length);	
+			writeLogg(__FILE__,__FUNCTION__,__LINE__,"Name Prefix: %s and Length: %d \n",npe->np->name,npe->np->length);	
 		hashtb_next(e);		
 	}
 
 	hashtb_end(e);
 
-	printf("\n");
+	if ( nlsr->debugging )
+		printf("\n");
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
+}
+
+void 
+update_nlsa_id_for_name_in_npl(struct name_prefix *np, long int nlsa_id)
+{
+	struct name_prefix_list_entry *npe;
+	struct hashtb_enumerator ee;
+    	struct hashtb_enumerator *e = &ee; 	
+    	int res;
+
+   	hashtb_start(nlsr->npl, e);
+    	res = hashtb_seek(e, np->name, np->length, 0);
+
+	if(res == HT_OLD_ENTRY)
+	{   
+		npe=e->data;
+		npe->name_lsa_id=nlsa_id;
+	}
+	else
+	{
+		hashtb_delete(e);	
+	}
+    	
+	hashtb_end(e);
 }
 
diff --git a/nlsr_npl.h b/nlsr_npl.h
index 3c22f8e..4fce119 100644
--- a/nlsr_npl.h
+++ b/nlsr_npl.h
@@ -1,7 +1,15 @@
 #ifndef _NLSR_NPL_H_
 #define _NLSR_NPL_H_
 
+struct name_prefix_list_entry
+{
+	struct name_prefix *np;
+	long int name_lsa_id;
+};
+
 void add_name_to_npl(struct name_prefix *np);
 void print_name_prefix_from_npl(void);
-
+int does_name_exist_in_npl(struct name_prefix *np);
+void update_nlsa_id_for_name_in_npl(struct name_prefix *np, long int nlsa_id);
+long int get_lsa_id_from_npl(struct name_prefix *np);
 #endif
diff --git a/nlsr_npt.c b/nlsr_npt.c
index 9f690ff..81879b9 100644
--- a/nlsr_npt.c
+++ b/nlsr_npt.c
@@ -494,7 +494,10 @@
 
 	hashtb_end(e);
 
-	printf("\n");
+	if ( nlsr->debugging )
+		printf("\n");
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
 }
 
 void
@@ -1091,5 +1094,8 @@
 
 	hashtb_end(e);
 
-	printf("\n");
+	if ( nlsr->debugging )
+		printf("\n");
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
 }
diff --git a/nlsr_route.c b/nlsr_route.c
index 0fbe8fd..8537b2e 100644
--- a/nlsr_route.c
+++ b/nlsr_route.c
@@ -71,7 +71,8 @@
 			adj_matrix[i] = malloc(map_element * sizeof(int));
 		}
 		make_adj_matrix(adj_matrix,map_element);
-		print_adj_matrix(adj_matrix,map_element);
+		if ( nlsr->debugging )
+			print_adj_matrix(adj_matrix,map_element);
 
 		long int source=get_mapping_no(nlsr->router_name);
 		long int *parent=(long int *)malloc(map_element * sizeof(long int));
@@ -202,8 +203,18 @@
 			me=e->data;
 			if(me->mapping != source)
 			{
-				print_path(parent,(long int)me->mapping);
-				printf("\n");
+				
+				if ( nlsr->debugging )
+				{
+					print_path(parent,(long int)me->mapping);
+					printf("\n");
+				}
+				if ( nlsr->detailed_logging )
+				{
+					print_path(parent,(long int)me->mapping);
+					writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
+				}
+				
 			}
 			hashtb_next(e);		
 		}
@@ -266,7 +277,6 @@
 {
 	if (parent[dest] != EMPTY_PARENT )
 		print_path(parent,parent[dest]);
-
 	printf(" %ld",dest);
 }
 
@@ -987,7 +997,10 @@
 
 	hashtb_end(e);
 
-	printf("\n");
+	if ( nlsr->debugging )
+		printf("\n");
+	if ( nlsr->detailed_logging )
+		writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
 }