LSDB Building
diff --git a/nlsr.c b/nlsr.c
index 088df30..bcf897c 100644
--- a/nlsr.c
+++ b/nlsr.c
@@ -314,10 +314,29 @@
 		hashtb_next(e);		
 	}
 	hashtb_end(e);
-
-	
-
 	hashtb_destroy(&nlsr->adl);
+
+	/* Destroying every element in Name LSDB Hash Table */
+	hashtb_start(nlsr->adl, e);
+	element=hashtb_n(nlsr->lsdb->name_lsdb);
+
+	struct nlsa *name_lsa;
+
+	for(i=0;i<element;i++)
+	{
+		name_lsa=e->data;	
+		ccn_charbuf_destroy(&name_lsa->name_prefix);
+		ccn_charbuf_destroy(&name_lsa->header->orig_router);
+		free(name_lsa->header);
+		free(name_lsa);
+		hashtb_next(e);		
+	}
+	hashtb_end(e);
+
+	hashtb_destroy(&nlsr->lsdb->name_lsdb);
+
+	hashtb_destroy(&nlsr->lsdb->adj_lsdb);
+
 	hashtb_destroy(&nlsr->npl);
 	ccn_schedule_destroy(&nlsr->sched);
 	ccn_destroy(&nlsr->ccn);
@@ -342,12 +361,10 @@
 	nlsr->in_content.p = &incoming_content;
 
 	nlsr->lsdb=(struct linkStateDatabase *)malloc(sizeof(struct linkStateDatabase *));
-	//nlsr->lsdb->version=(char *)malloc(16);
-	//nlsr->lsdb->version="0000000000000000";
 	nlsr->lsdb->version=0;
 
-	nlsr->lsdb->adj_lsdb = hashtb_create(sizeof(struct adj_lsa), &param_adj_lsdb);
-	nlsr->lsdb->name_lsdb = hashtb_create(sizeof(struct name_lsa), &param_name_lsdb);
+	nlsr->lsdb->adj_lsdb = hashtb_create(sizeof(struct alsa), &param_adj_lsdb);
+	nlsr->lsdb->name_lsdb = hashtb_create(sizeof(struct nlsa), &param_name_lsdb);
 
 	nlsr->is_synch_init=1;
 	nlsr->nlsa_id=0;
@@ -408,6 +425,7 @@
 	nlsr->sched = ccn_schedule_create(nlsr, &ndn_rtr_ticker);
 
 	nlsr->event_send_lsdb_interest = ccn_schedule_event(nlsr->sched, 1000000, &send_lsdb_interest, NULL, 0);
+	nlsr->event_send_lsdb_interest = ccn_schedule_event(nlsr->sched, 500000, &initial_build_name_lsa, NULL, 0);
 
 	while(1)
 	{
diff --git a/nlsr.h b/nlsr.h
index 803e6b8..d6779b8 100644
--- a/nlsr.h
+++ b/nlsr.h
@@ -12,7 +12,6 @@
 
 struct ndn_neighbor
 {
-	//struct name_prefix *neighbor;
 	struct ccn_charbuf *neighbor;
 	int face;
 	int status;
@@ -45,7 +44,7 @@
 	struct linkStateDatabase *lsdb;
 
 	int is_synch_init;
-	int nlsa_id;
+	long int nlsa_id;
 };
 
 struct nlsr *nlsr;
diff --git a/nlsr_lsdb.c b/nlsr_lsdb.c
index 65b812d..ccdd513 100644
--- a/nlsr_lsdb.c
+++ b/nlsr_lsdb.c
@@ -19,5 +19,178 @@
 
 #include "nlsr.h"
 #include "nlsr_lsdb.h"
+#include "utility.h"
 
+void
+make_name_lsa_key(struct ccn_charbuf *key, struct ccn_charbuf *orig_router, unsigned int ls_type, long int nlsa_id, long int orig_time)
+{
+	printf("make_name_lsa_key called \n");	
+	ccn_charbuf_append_string(key,ccn_charbuf_as_string(orig_router));
 
+	struct ccn_charbuf *c=ccn_charbuf_create();
+	ccn_charbuf_reset(c);
+	ccn_charbuf_putf(c, "%d", (unsigned)ls_type);
+	ccn_charbuf_append_string(key,ccn_charbuf_as_string(c));
+	
+	ccn_charbuf_reset(c);
+	ccn_charbuf_putf(c, "%d", (unsigned)nlsa_id);
+	ccn_charbuf_append_string(key,ccn_charbuf_as_string(c));
+	
+	ccn_charbuf_reset(c);
+	ccn_charbuf_putf(c, "%ld", (unsigned)orig_time);
+	ccn_charbuf_append_string(key,ccn_charbuf_as_string(c));
+
+	ccn_charbuf_destroy(&c);
+
+	printf("Key: %s length: %d\n",ccn_charbuf_as_string(key),(int)key->length);
+
+}
+
+void 
+install_name_lsa(struct nlsa *new_name_lsa)
+{
+	printf("install_name_lsa called \n");
+	struct ccn_charbuf *key=ccn_charbuf_create();
+	make_name_lsa_key(key,new_name_lsa->header->orig_router,new_name_lsa->header->ls_type,new_name_lsa->header->ls_id,new_name_lsa->header->orig_time);
+
+	struct nlsa *name_lsa=(struct nlsa*)malloc(sizeof(struct nlsa *));
+	
+	struct hashtb_enumerator ee;
+    	struct hashtb_enumerator *e = &ee; 	
+    	int res;
+
+   	hashtb_start(nlsr->lsdb->name_lsdb, e);
+    	res = hashtb_seek(e, key->buf , key->length, 0);
+
+	if(res == HT_NEW_ENTRY )
+	{
+   		printf("New Name LSA. Added...\n");
+		name_lsa = e->data;
+
+		name_lsa->header=(struct nlsa_header *)malloc(sizeof(struct nlsa_header *));
+		name_lsa->header->orig_router=ccn_charbuf_create();
+	
+		name_lsa->header->ls_type=new_name_lsa->header->ls_type;
+		name_lsa->header->orig_time=new_name_lsa->header->orig_time;
+		name_lsa->header->ls_id=new_name_lsa->header->ls_id;
+		ccn_charbuf_append_string(name_lsa->header->orig_router,ccn_charbuf_as_string(new_name_lsa->header->orig_router));	
+		name_lsa->header->isValid=new_name_lsa->header->isValid;
+
+		name_lsa->name_prefix=ccn_charbuf_create();	
+		//ccn_charbuf_append_string(name_lsa->name_prefix,ccn_charbuf_as_string(new_name_lsa->name_prefix));
+		ccn_charbuf_append_charbuf(name_lsa->name_prefix,new_name_lsa->name_prefix);
+	}
+	else if(res == HT_OLD_ENTRY)
+	{
+		printf("Duplicate Name LSA. Discarded...\n");
+	
+	}
+	
+    	hashtb_end(e);
+
+	ccn_charbuf_destroy(&key);
+	
+}
+
+struct nlsa *
+build_name_lsa(struct ccn_charbuf *name_prefix)
+{
+	printf("build_name_lsa called \n");
+	struct nlsa *name_lsa=(struct nlsa*)malloc(sizeof(struct nlsa *));
+
+	name_lsa->header=(struct nlsa_header *)malloc(sizeof(struct nlsa_header *));
+	name_lsa->header->orig_router=ccn_charbuf_create();
+	
+	name_lsa->header->ls_type=LS_TYPE_NAME;
+	name_lsa->header->orig_time=get_current_time_sec();
+	name_lsa->header->ls_id=++nlsr->nlsa_id;
+	ccn_charbuf_append_string(name_lsa->header->orig_router,nlsr->router_name);	
+	name_lsa->header->isValid=1;
+
+	name_lsa->name_prefix=ccn_charbuf_create();	
+	ccn_charbuf_append_string(name_lsa->name_prefix,ccn_charbuf_as_string(name_prefix));
+	//ccn_charbuf_append_charbuf(name_lsa->name_prefix,name_prefix);
+
+	return name_lsa;
+}
+
+int 
+initial_build_name_lsa(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
+{
+	printf("initial_build_name_lsa called \n");	
+	int i, npl_element;
+	struct name_prefix *np;
+
+	struct hashtb_enumerator ee;
+    	struct hashtb_enumerator *e = &ee;
+    	
+    	hashtb_start(nlsr->npl, e);
+	npl_element=hashtb_n(nlsr->npl);
+
+	for(i=0;i<npl_element;i++)
+	{
+		np=e->data;
+
+		struct nlsa *name_lsa;
+		struct ccn_charbuf *name;
+		name=ccn_charbuf_create();
+		ccn_charbuf_append_string(name,np->name);
+		name_lsa=build_name_lsa(name);
+		install_name_lsa(name_lsa);
+
+		free(name_lsa);
+		ccn_charbuf_destroy(&name);
+	
+		hashtb_next(e);		
+	}
+
+	hashtb_end(e);
+
+	print_name_lsdb();
+
+	return 0;
+}
+
+void 
+print_name_lsa(struct nlsa *name_lsa)
+{
+	printf("print_name_lsa called \n");
+	printf("-----------Name LSA---------------\n");
+	printf("	Origination Router       :	%s\n",ccn_charbuf_as_string(name_lsa->header->orig_router));
+	printf("	Origination Router Length:	%d\n",(int)name_lsa->header->orig_router->length);
+	printf("	LS Type			 :	%d\n",name_lsa->header->ls_type);
+	printf("	LS Id			 :	%ld\n",name_lsa->header->ls_id);
+	printf("	Origination Time	 :	%ld\n",name_lsa->header->orig_time);
+	printf("	Is Valid 		 :	%d\n",name_lsa->header->isValid);
+
+	printf("	LSA Data			\n");
+	printf("		Name Prefix:	 	:	%s\n",ccn_charbuf_as_string(name_lsa->name_prefix));
+	printf("		Name Prefix Length	:	%d\n",(int)name_lsa->name_prefix->length);
+
+	printf("\n");
+
+}
+
+void
+print_name_lsdb(void)
+{
+	printf("print_name_lsdb called \n");	
+	int i, lsdb_element;
+	struct nlsa *name_lsa;
+
+	struct hashtb_enumerator ee;
+    	struct hashtb_enumerator *e = &ee;
+    	
+    	hashtb_start(nlsr->lsdb->name_lsdb, e);
+	lsdb_element=hashtb_n(nlsr->lsdb->name_lsdb);
+
+	for(i=0;i<lsdb_element;i++)
+	{
+		name_lsa=e->data;
+		print_name_lsa(name_lsa);		
+		hashtb_next(e);		
+	}
+
+	hashtb_end(e);
+
+}
diff --git a/nlsr_lsdb.h b/nlsr_lsdb.h
index 21826e9..3983305 100644
--- a/nlsr_lsdb.h
+++ b/nlsr_lsdb.h
@@ -1,33 +1,54 @@
 #ifndef _NLSR_LSDB_H_
 #define _NLSR_LSDB_H_
 
+
+#define LS_TYPE_ADJ 1
+#define LS_TYPE_NAME 2
+
 struct link
 {
-	struct charbuf *nbr;
+	struct ccn_charbuf *nbr;
 	int face;
 	int metric;
 };
 
-struct adj_lsa
+
+struct alsa_header 
 {
+	unsigned int ls_type;
+	long int orig_time;
+	struct ccn_charbuf *orig_router;
+};
 
-	unsigned char ls_type;
-	char orig_time[15];
-	struct charbuf *orig_router;
+struct alsa
+{
+	struct alsa_header *header;	
 	int no_link;
-
 	struct link *links;
 };
 
-struct name_lsa
+struct nlsa_header
 {
-	unsigned char ls_type;
-	char orig_time[15];
-	long ls_id;
-	struct charbuf *orig_router;
+	unsigned int ls_type;
+	long int orig_time;
+	long int ls_id;
+	struct ccn_charbuf *orig_router;
 	unsigned char isValid;
 
-	struct charbuf *name_prefix;
 };
 
+struct nlsa
+{
+	struct nlsa_header *header;
+	struct ccn_charbuf *name_prefix;
+};
+
+
+int initial_build_name_lsa(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags);
+struct nlsa * build_name_lsa(struct ccn_charbuf *name_prefix);
+void install_name_lsa(struct nlsa *new_name_lsa);
+void make_name_lsa_key(struct ccn_charbuf *key, struct ccn_charbuf *orig_router, unsigned int ls_type, long int nlsa_id, long int orig_time);
+void print_name_lsdb(void);
+void print_name_lsa(struct nlsa *name_lsa);
+
 #endif
diff --git a/utility.c b/utility.c
index 420a911..ed22483 100644
--- a/utility.c
+++ b/utility.c
@@ -61,6 +61,12 @@
 
 	return component;
 
+}
 
-
+long int 
+get_current_time_sec(void)
+{
+	struct timeval now;
+	gettimeofday(&now,NULL);
+	return now.tv_sec;
 }
diff --git a/utility.h b/utility.h
index 55863b3..96cd3cf 100644
--- a/utility.h
+++ b/utility.h
@@ -4,5 +4,6 @@
 char * getLocalTimeStamp(void);
 char * getGmTimeStamp(void);
 char * nth_named_component(const char *name_prefix, int n);
+long int get_current_time_sec(void);
 
 #endif