Skip to content

Commit

Permalink
Merge pull request #28 from guruchandru/webcfg_meta
Browse files Browse the repository at this point in the history
webconfig metadata parsing and processing
  • Loading branch information
shilpa24balaji authored Aug 27, 2020
2 parents 6702047 + a061eaa commit 1d7b4dc
Show file tree
Hide file tree
Showing 11 changed files with 571 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
# limitations under the License.

set(PROJ_WEBCFG webcfg)
set(HEADERS webcfg.h webcfg_param.h webcfg_pack.h webcfg_multipart.h webcfg_auth.h webcfg_notify.h webcfg_generic.h webcfg_db.h webcfg_log.h webcfg_blob.h webcfg_event.h webcfg_aker.h)
set(SOURCES webcfg_helpers.c webcfg.c webcfg_param.c webcfg_pack.c webcfg_multipart.c webcfg_auth.c webcfg_notify.c webcfg_db.c webcfg_generic.c webcfg_blob.c webcfg_event.c webcfg_client.c webcfg_aker.c)
set(HEADERS webcfg.h webcfg_param.h webcfg_pack.h webcfg_multipart.h webcfg_auth.h webcfg_notify.h webcfg_generic.h webcfg_db.h webcfg_log.h webcfg_blob.h webcfg_event.h webcfg_aker.h webcfg_metadata.h)
set(SOURCES webcfg_helpers.c webcfg.c webcfg_param.c webcfg_pack.c webcfg_multipart.c webcfg_auth.c webcfg_notify.c webcfg_db.c webcfg_generic.c webcfg_blob.c webcfg_event.c webcfg_client.c webcfg_aker.c webcfg_metadata.c)

add_library(${PROJ_WEBCFG} STATIC ${HEADERS} ${SOURCES})
add_library(${PROJ_WEBCFG}.shared SHARED ${HEADERS} ${SOURCES})
Expand Down
6 changes: 5 additions & 1 deletion src/webcfg.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <base64.h>
#include "webcfg_db.h"
#include "webcfg_aker.h"
#include "webcfg_metadata.h"
/*----------------------------------------------------------------------------*/
/* Macros */
/*----------------------------------------------------------------------------*/
Expand Down Expand Up @@ -94,6 +95,8 @@ void *WebConfigMultipartTask(void *status)
Status = (unsigned long)status;

//start webconfig notification thread.
initWebcfgProperties(WEBCFG_PROPERTIES_FILE);

initWebConfigNotifyTask();
initWebConfigClient();
WebcfgInfo("initDB %s\n", WEBCFG_DB_FILE);
Expand Down Expand Up @@ -417,7 +420,7 @@ int testUtility()
{
ptr1_count = memchr(ptr_count+1, '\r', test_dataSize - (ptr_count - data_body));
temp = strndup(ptr_count, (ptr1_count-ptr_count));
strcpy(ct,temp);
strncpy(ct,temp,(sizeof(ct)-1));
break;
}

Expand All @@ -443,6 +446,7 @@ int testUtility()
}
else
{
WEBCFG_FREE(transaction_uuid);
WebcfgError("webConfigData is empty, need to retry\n");
}
sprintf(command,"rm -rf %s",TEST_FILE_LOCATION);
Expand Down
1 change: 1 addition & 0 deletions src/webcfg_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ static void get_parodus_url(char **parodus_url, char **client_url)
if (NULL != fp)
{
char str[255] = {'\0'};
//TODO:: Use Fgets to fix coverity issue "fscanf" assumes an arbitrarily long string.
while(fscanf(fp,"%s", str) != EOF)
{
char *value = NULL;
Expand Down
5 changes: 3 additions & 2 deletions src/webcfg_event.c
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ void* processSubdocEvents()
WebcfgInfo("ACK for mesh/cujo received: %s,%lu,%lu,%s,%lu\n", eventParam->subdoc_name,(long)eventParam->trans_id, (long)eventParam->version, eventParam->status, (long)eventParam->timeout);
handleConnectedClientNotify(eventParam->status);
}

WebcfgInfo("ACK EVENT: %s,%lu,%lu,ACK,%lu %s\n", eventParam->subdoc_name,(long)eventParam->trans_id, (long)eventParam->version, (long)eventParam->timeout, "(doc apply success)");
WebcfgInfo("doc apply success, proceed to add to DB\n");
if( validateEvent(subdoc_node, eventParam->subdoc_name, eventParam->trans_id) == WEBCFG_SUCCESS)
Expand Down Expand Up @@ -980,7 +981,7 @@ uint32_t getDocVersionFromTmpList(webconfig_tmp_data_t *temp, char *docname)
//validate each event based on exact doc txid in tmp list.
WEBCFG_STATUS validateEvent(webconfig_tmp_data_t *temp, char *docname, uint16_t txid)
{
if (NULL != temp)
if ((NULL != temp) && (docname != NULL))
{
WebcfgDebug("validateEvent: temp->name %s, temp->trans_id %hu\n",temp->name, temp->trans_id);
if( strcmp(docname, temp->name) == 0)
Expand Down Expand Up @@ -1034,7 +1035,7 @@ void handleConnectedClientNotify(char *status)

if(status != NULL)
{
strcpy(tmpStr , status);
strncpy(tmpStr , status,(sizeof(tmpStr)-1));
token = strtok(tmpStr, s);
if( token != NULL )
{
Expand Down
253 changes: 253 additions & 0 deletions src/webcfg_metadata.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,253 @@
/*
* Copyright 2020 Comcast Cable Communications Management, LLC
*
* 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.
*/
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include "webcfg_log.h"
#include "webcfg_metadata.h"
#include "webcfg_multipart.h"

/*----------------------------------------------------------------------------*/
/* Macros */
/*----------------------------------------------------------------------------*/
#define MAXCHAR 1024
/*----------------------------------------------------------------------------*/
/* Data Structures */
/*----------------------------------------------------------------------------*/
typedef struct SubDocSupportMap
{
char name[256];//portforwarding or wlan
char support[8];//true or false;
struct SubDocSupportMap *next;
}SubDocSupportMap_t;

/*----------------------------------------------------------------------------*/
/* File Scoped Variables */
/*----------------------------------------------------------------------------*/
static char * supported_bits = NULL;
static char * supported_version = NULL;
SubDocSupportMap_t *g_sdInfoHead = NULL;
SubDocSupportMap_t *g_sdInfoTail = NULL;
/*----------------------------------------------------------------------------*/
/* Function Prototypes */
/*----------------------------------------------------------------------------*/
void displaystruct();
SubDocSupportMap_t * get_global_sdInfoHead(void);
SubDocSupportMap_t * get_global_sdInfoTail(void);
/*----------------------------------------------------------------------------*/
/* External Functions */
/*----------------------------------------------------------------------------*/


void initWebcfgProperties(char * filename)
{
FILE *fp = NULL;
char str[MAXCHAR] = {'\0'};
//For WEBCONFIG_SUBDOC_MAP parsing
char *p;
char *token;

WebcfgDebug("webcfg properties file path is %s\n", filename);
fp = fopen(filename,"r");

if (fp == NULL)
{
WebcfgError("Failed to open file %s\n", filename);
return;
}

while (fgets(str, MAXCHAR, fp) != NULL)
{
char * value = NULL;

if(NULL != (value = strstr(str,"WEBCONFIG_SUPPORTED_DOCS_BIT=")))
{
WebcfgDebug("The value stored is %s\n", str);
value = value + strlen("WEBCONFIG_SUPPORTED_DOCS_BIT=");
value[strlen(value)-1] = '\0';
setsupportedDocs(value);
value = NULL;
}

if(NULL != (value =strstr(str,"WEBCONFIG_DOC_SCHEMA_VERSION")))
{
WebcfgDebug("The value stored is %s\n", str);
value = value + strlen("WEBCONFIG_DOC_SCHEMA_VERSION=");
value[strlen(value)-1] = '\0';
setsupportedVersion(value);
value = NULL;
}

if(strncmp(str,"WEBCONFIG_SUBDOC_MAP",strlen("WEBCONFIG_SUBDOC_MAP")) ==0)
{

p = str;
char *tok = NULL;
WebcfgDebug("The value of tok is %s\n", tok);
tok = strtok_r(p, " =",&p);
token = strtok_r(p,",",&p);
while(token!= NULL)
{

char subdoc[100];
char *subtoken;
SubDocSupportMap_t *sdInfo = NULL;
strncpy(subdoc,token,(sizeof(subdoc)-1));
puts(subdoc);
sdInfo = (SubDocSupportMap_t *)malloc(sizeof(SubDocSupportMap_t));
if( sdInfo==NULL )
{
fclose(fp);
WebcfgError("Unable to allocate memory");
return;
}
memset(sdInfo, 0, sizeof(SubDocSupportMap_t));

subtoken = strtok(subdoc,":");//portforwarding or lan

if(subtoken == NULL)
{
fclose(fp);
WEBCFG_FREE(sdInfo);
return;
}

strncpy(sdInfo->name,subtoken,(sizeof(sdInfo->name)-1));
subtoken = strtok(NULL,":");//skip 1st value
subtoken = strtok(NULL,":");//true or false
strncpy(sdInfo->support,subtoken,(sizeof(sdInfo->support)-1));
token =strtok_r(p,",",&p);
sdInfo->next = NULL;

if(g_sdInfoTail == NULL)
{
g_sdInfoHead = sdInfo;
g_sdInfoTail = sdInfo;
}
else
{
SubDocSupportMap_t *temp =NULL;
temp = get_global_sdInfoTail();
temp->next = sdInfo;
g_sdInfoTail = sdInfo;
}

}

}
}
fclose(fp);

if(g_sdInfoHead != NULL)
{
displaystruct();
}
}

void setsupportedDocs( char * value)
{
if(value != NULL)
{
supported_bits = strdup(value);
}
else
{
supported_bits = NULL;
}
}

void setsupportedVersion( char * value)
{
if(value != NULL)
{
supported_version = strdup(value);
}
else
{
supported_version = NULL;
}
}

char * getsupportedDocs()
{
WebcfgDebug("The value in supportedbits get is %s\n",supported_bits);
return supported_bits;
}

char * getsupportedVersion()
{
WebcfgDebug("The value in supportedversion get is %s\n",supported_version);
return supported_version;
}

WEBCFG_STATUS isSubDocSupported(char *subDoc)
{

SubDocSupportMap_t *sd = NULL;

sd = get_global_sdInfoHead();

while(sd != NULL)
{
if(strncmp(sd->name, subDoc, strlen(subDoc)) == 0)
{
WebcfgDebug("The subdoc %s is present\n",sd->name);
if(strncmp(sd->support, "true", strlen("true")) == 0)
{
WebcfgInfo("%s is supported\n",subDoc);
return WEBCFG_SUCCESS;

}
else
{
WebcfgInfo("%s is not supported\n",subDoc);
return WEBCFG_FAILURE;
}
}
sd = sd->next;

}
WebcfgError("Supported doc bit not found for %s\n",subDoc);
return WEBCFG_FAILURE;
}

/*----------------------------------------------------------------------------*/
/* Internal functions */
/*----------------------------------------------------------------------------*/
SubDocSupportMap_t * get_global_sdInfoHead(void)
{
SubDocSupportMap_t *tmp = NULL;
tmp = g_sdInfoHead;
return tmp;
}

SubDocSupportMap_t * get_global_sdInfoTail(void)
{
SubDocSupportMap_t *tmp = NULL;
tmp = g_sdInfoTail;
return tmp;
}

void displaystruct()
{
SubDocSupportMap_t *temp =NULL;
temp = get_global_sdInfoHead();
while(temp != NULL)
{
WebcfgDebug(" %s %s\n",temp->name,temp->support);
temp=temp->next;
}
}
44 changes: 44 additions & 0 deletions src/webcfg_metadata.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2020 Comcast Cable Communications Management, LLC
*
* 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.
*/
#ifndef __WEBCFG__METADATA_H__
#define __WEBCFG__METADATA_H__

#include <stdint.h>
#include <string.h>
#include "webcfg.h"
/*----------------------------------------------------------------------------*/
/* Macros */
/*----------------------------------------------------------------------------*/
#ifdef BUILD_YOCTO
#define WEBCFG_PROPERTIES_FILE "/etc/webconfig.properties"
#else
#define WEBCFG_PROPERTIES_FILE "/tmp/webconfig.properties"
#endif

/*----------------------------------------------------------------------------*/
/* Data Structures */
/*----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------*/
/* External Functions */
/*----------------------------------------------------------------------------*/
WEBCFG_STATUS isSubDocSupported(char *subDoc);
void initWebcfgProperties(char * filename);
void setsupportedDocs( char * value);
void setsupportedVersion( char * value);
char * getsupportedDocs();
char * getsupportedVersion();
#endif
Loading

0 comments on commit 1d7b4dc

Please sign in to comment.