Patch for 'own_prefix' support, logging enabling, init timeout 23/57023/1
authorArek Antoniak <a.antoniak2@samsung.com>
Thu, 14 Jan 2016 10:57:11 +0000 (11:57 +0100)
committerArek Antoniak <a.antoniak2@samsung.com>
Thu, 14 Jan 2016 11:04:56 +0000 (12:04 +0100)
Change-Id: I63edf55513a3471c90967d415ab654647b1fb822

src/internal/internal.cpp
src/internal/timer.hpp
src/internal/tslog.hpp
src/internal/xml_parser.hpp
src/internal/xml_policy.hpp

index e5e5a18..274d321 100644 (file)
@@ -46,7 +46,7 @@ static const char* get_message_type(int type) {
 int __internal_init(unsigned int bus_type, const char* const config_name)
 {
     _ldp_xml_parser::XmlAsyncParser p;
-    auto err = p.parse_policy(get_bus(bus_type), get_str(config_name), std::chrono::milliseconds(10000));
+    auto err = p.parse_policy(get_bus(bus_type), get_str(config_name));
     return err.get();
 }
 
index fe46cc6..2d856b3 100644 (file)
@@ -16,6 +16,7 @@
 
 #ifndef _TIMER_HPP
 #define _TIMER_HPP
+#include <chrono>
 
 namespace _ldp_timer
 {
index f1fdeaa..3452dbc 100644 (file)
 #include <iostream>
 #include <thread>
 #include <mutex>
+#include <stdlib.h>
 
 namespace _ldp_tslog
 {
     typedef std::ostream& (*t_ManFun)(std::ostream&);
 
     namespace {
-        static constexpr bool LOG_ENABLE = false;
-        static constexpr bool LOG_VERBOSE = false;
+        static constexpr bool LOG_DEFAULT_ENABLE = false;
+        static constexpr bool LOG_DEFAULT_VERBOSE = false;
+        static const std::string LDP_ENV_VERBOSE_NAME = "LDP_VERBOSE";
+        static const std::string LDP_ENV_LOG_NAME = "LDP_LOG";
+
+        const bool get_log_env(const std::string& name) {
+            bool bret;
+            char* ldp_log_mode = getenv(name.c_str());
+            if(ldp_log_mode) {
+                const std::string slog(ldp_log_mode);
+                bret = (slog == "0") ? false : true;
+            } else {
+                bret = (name == LDP_ENV_LOG_NAME) ? LOG_DEFAULT_ENABLE : LOG_DEFAULT_VERBOSE;
+            }
+            return bret;
+        }
     }
 
     const bool get_verbose() {
-        return LOG_VERBOSE;
+        return get_log_env(LDP_ENV_VERBOSE_NAME);
     }
 
     const bool get_enable() {
-        return LOG_ENABLE;
+        return get_log_env(LDP_ENV_LOG_NAME);
     }
 
     class TsLog
index 8d3eb43..05df566 100644 (file)
@@ -40,7 +40,9 @@ namespace _ldp_xml_parser
             virtual ~XmlAsyncParser() {
             }
 
-            ErrCode parse_policy(const std::string bus, const std::string fname, const std::chrono::milliseconds timeout) {
+            ErrCode parse_policy(const std::string bus,
+                    const std::string fname,
+                    const std::chrono::milliseconds timeout = std::chrono::milliseconds(std::numeric_limits<int32_t>::max())) {
                 set_policy_bus_filename(bus, fname);
                 m_xml_policy.init();
                 ErrCode err = parse(timeout);
index f00a879..80d3330 100644 (file)
@@ -290,6 +290,10 @@ namespace _ldp_xml_parser
                             key.m_bown = true;
                             key.m_path_content[Key::IDX_SERVICE] = data_str;
                         }
+                        if(v.first == "own_prefix") {
+                            key.m_bown = true;
+                            key.m_path_content[Key::IDX_SERVICE] = data_str + "*";
+                        }
                         if(field_has(v, "_destination")) {
                             key.m_path_content[Key::IDX_DEST] = data_str;
                         }
@@ -427,6 +431,28 @@ namespace _ldp_xml_parser
             }
         }
 
+        ErrCode service_leaf_found(const Leaf& leaf, const std::string& label, const std::vector<std::string>& idx_v) {
+            ErrCode err;
+            if(leaf.get_check()) {
+                verbose::tout << __func__
+                    << ": cynara check needed for privilege " << leaf.get_privilege()
+                    << ", weight " << leaf.get_weight()
+                    << std::endl;
+
+                //cynara check
+                try {
+                    bool br = _ldp_cynara::Cynara::check(label, leaf.get_privilege(), idx_v[Key::IDX_USER]);
+                    err = ErrCode::ok(br);
+                } catch(const std::runtime_error& ex) {
+                    err = ErrCode::error(ex.what());
+                }
+            } else {
+                err = ErrCode::ok(leaf.get_decision());
+            }
+
+            return err;
+        }
+
         ErrCode index_decision_tree(const boost::property_tree::ptree& pt,
             const std::vector<std::string>& idx_v,
             const std::string& label,
@@ -465,23 +491,7 @@ namespace _ldp_xml_parser
                 }
 
                 if(found) {
-                    if(leaf_found.get_check()) {
-                        verbose::tout << __func__
-                            << ": cynara check needed for privilege " << leaf_found.get_privilege()
-                            << ", weight " << leaf_found.get_weight()
-                            << std::endl;
-
-                        //cynara check
-                        try {
-                            bool br = _ldp_cynara::Cynara::check(label, leaf_found.get_privilege(), idx_v[Key::IDX_USER]);
-                            err = ErrCode::ok(br);
-                        } catch(const std::runtime_error& ex) {
-                            err = ErrCode::error(ex.what());
-                        }
-                    } else {
-                        err = ErrCode::ok(leaf_found.get_decision());
-                    }
-
+                    err = service_leaf_found(leaf_found, label, idx_v);
                     verbose::tout << __func__ << ": returning decision #" << err.get() << " " << err.get_str() << ", weight " << leaf_found.get_weight() << std::endl;
                     break;
                 }
@@ -571,7 +581,29 @@ namespace _ldp_xml_parser
         }
 
         ErrCode can_own_what(const std::string bus, const std::vector<std::string>& idx_v) {
-            return can_do_action(bus, "OWN", idx_v);
+            ErrCode err;
+
+            //Evaluate own_prefix
+            std::vector<std::string> iv = idx_v;
+            const std::string srv = iv[iv.size() - 1];
+            const size_t srv_size = srv.size();
+            for(size_t n = 1; n <= srv_size; ++n) {
+                const std::string sub = srv.substr(0, n) + "*";
+                verbose::tout << "own_prefix: " << sub << std::endl;
+                iv.pop_back();
+                iv.push_back(sub);
+                err = can_do_action(bus, "OWN", iv);
+                if(err.is_ok()) {
+                    break;
+                }
+            }
+
+            //Evaluate own
+            if(err.is_error()) {
+                err = can_do_action(bus, "OWN", idx_v);
+            }
+
+            return err;
         }
 
         void print_decision_trees() {