RDK Documentation (Open Sourced RDK Components)
XrdkCentralComRFCVar.cpp
1 /*
2  * If not stated otherwise in this file or this component's Licenses.txt file the
3  * following copyright and licenses apply:
4  *
5  * Copyright 2018 RDK Management
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18 */
19 
20 #include <algorithm>
21 #include <fstream>
22 #include <string>
23 
24 #include "XrdkCentralComRFCVar.h"
25 #include "hostIf_utils.h"
26 
27 #define RFC_VAR_KEY "RFC_VAR_FILENAME"
28 #define RFC_PROPERTIES_FILE "/etc/rfc.properties"
29 
30 XRFCVarStore* XRFCVarStore::xrfcVarStoreInstance = NULL;
31 
32 void XRFCVarStore::reloadCache()
33 {
34  RDK_LOG (RDK_LOG_TRACE1, LOG_TR69HOSTIF, "Entering %s \n", __FUNCTION__);
35 
36  initDone = loadRFCVarIntoCache();
37 
38  RDK_LOG (RDK_LOG_TRACE1, LOG_TR69HOSTIF, "Leaving %s \n", __FUNCTION__);
39 }
40 
41 string XRFCVarStore::getValue(const string &key)
42 {
43  RDK_LOG (RDK_LOG_TRACE1, LOG_TR69HOSTIF, "Entering %s \n", __FUNCTION__);
44  if(!initDone)
45  {
46  RDK_LOG(RDK_LOG_ERROR, LOG_TR69HOSTIF, "Init Failed, can't handle the request\n");
47  return "";
48  }
49 
50  unordered_map<string,string>::const_iterator it = m_dict.find(key);
51  if (it == m_dict.end()) {
52  return "";
53  }
54  RDK_LOG (RDK_LOG_TRACE1, LOG_TR69HOSTIF, "Leaving %s : Value = %s \n", __FUNCTION__, it->second.c_str());
55 
56  return it->second;
57 }
58 
59 void XRFCVarStore::initRFCVarFileName()
60 {
61  RDK_LOG (RDK_LOG_TRACE1, LOG_TR69HOSTIF, "Entering %s \n", __FUNCTION__);
62  ifstream ifs_rfc(RFC_PROPERTIES_FILE);
63  if(!ifs_rfc.is_open())
64  {
65  RDK_LOG (RDK_LOG_ERROR, LOG_TR69HOSTIF, "%s: Trying to open a non-existent file [%s] \n", __FUNCTION__, RFC_PROPERTIES_FILE);
66  }
67  else
68  {
69  string line;
70  while (getline(ifs_rfc, line)) {
71  size_t splitterPos = line.find('=');
72  if (splitterPos < line.length()) {
73  string key = line.substr(0, splitterPos);
74  string value = line.substr(splitterPos+1, line.length());
75  if(!strcmp(key.c_str(), RFC_VAR_KEY))
76  {
77  m_filename = value;
78  RDK_LOG(RDK_LOG_DEBUG, LOG_TR69HOSTIF, "RFC Variables FileName = %s\n", m_filename.c_str());
79  }
80  }
81  }
82  ifs_rfc.close();
83 
84  if(m_filename.empty())
85  {
86  RDK_LOG(RDK_LOG_ERROR, LOG_TR69HOSTIF, "Didn't find %s in %s\n", RFC_VAR_KEY, RFC_PROPERTIES_FILE);
87  }
88  }
89  RDK_LOG (RDK_LOG_TRACE1, LOG_TR69HOSTIF, "Leaving %s \n", __FUNCTION__);
90 }
91 
92 bool XRFCVarStore::loadRFCVarIntoCache()
93 {
94  RDK_LOG (RDK_LOG_TRACE1, LOG_TR69HOSTIF, "Entering %s \n", __FUNCTION__);
95  if(m_filename.empty())
96  {
97  RDK_LOG(RDK_LOG_ERROR, LOG_TR69HOSTIF, "Invalid RFC Variables filename, Unable to load properties\n");
98  return false;
99  }
100  // get rid of quotes, it is quite common with properties files
101  m_filename.erase(remove(m_filename.begin(), m_filename.end(), '\"'), m_filename.end());
102  m_dict.clear();
103 
104  RDK_LOG (RDK_LOG_DEBUG, LOG_TR69HOSTIF, "RFC Variables File : %s \n", m_filename.c_str());
105  ifstream ifs_rfcVar(m_filename);
106  if (!ifs_rfcVar.is_open()) {
107  RDK_LOG (RDK_LOG_ERROR, LOG_TR69HOSTIF, "%s: Trying to open a non-existent file [%s] \n", __FUNCTION__, m_filename.c_str());
108  return false;
109  }
110  else
111  {
112  string line;
113  while (getline(ifs_rfcVar, line)) {
114  line=line.substr(line.find_first_of(" \t")+1);//Remove the export word that is before the key
115  size_t splitterPos = line.find('=');
116  if (splitterPos < line.length()) {
117  string key = line.substr(0, splitterPos);
118  string value = line.substr(splitterPos+1, line.length());
119  m_dict[key] = value;
120  RDK_LOG(RDK_LOG_DEBUG, LOG_TR69HOSTIF, "Key = %s : Value = %s\n", key.c_str(), value.c_str());
121  }
122  }
123  ifs_rfcVar.close();
124  }
125  RDK_LOG (RDK_LOG_TRACE1, LOG_TR69HOSTIF, "Leaving %s \n", __FUNCTION__);
126  return true;
127 }
128 
129 XRFCVarStore::XRFCVarStore()
130 {
131  RDK_LOG (RDK_LOG_TRACE1, LOG_TR69HOSTIF, "Entering %s \n", __FUNCTION__);
132 
133  initRFCVarFileName();
134 
135  initDone = loadRFCVarIntoCache();
136 
137  RDK_LOG (RDK_LOG_TRACE1, LOG_TR69HOSTIF, "Leaving %s \n", __FUNCTION__);
138 }
139 
140 XRFCVarStore* XRFCVarStore::getInstance()
141 {
142  RDK_LOG (RDK_LOG_TRACE1, LOG_TR69HOSTIF, "Entering %s \n", __FUNCTION__);
143 
144  if(!xrfcVarStoreInstance)
145  xrfcVarStoreInstance = new XRFCVarStore;
146 
147  RDK_LOG (RDK_LOG_TRACE1, LOG_TR69HOSTIF, "Leaving %s \n", __FUNCTION__);
148  return xrfcVarStoreInstance;
149 }
RDK_LOG
#define RDK_LOG
Definition: rdk_debug.h:258
XRFCVarStore
Definition: XrdkCentralComRFCVar.h:30