This is an automated email from the ASF dual-hosted git repository.

phrocker pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nifi-minifi-cpp.git


The following commit(s) were added to refs/heads/master by this push:
     new dfbd3bd  MINIFICPP-852: Add details to restful response. CoAP will not 
include details for the time being
dfbd3bd is described below

commit dfbd3bd2417d5c6e1ffd2cfe444a909ec63d19b4
Author: Marc Parisi <[email protected]>
AuthorDate: Fri May 17 15:17:36 2019 -0400

    MINIFICPP-852: Add details to restful response. CoAP will not include 
details for the time being
    
    This closes #560.
    
    Approved on GH by kevdoran.
    
    Signed-off-by: Marc Parisi <[email protected]>
---
 extensions/http-curl/tests/C2FailedUpdateTest.cpp | 30 +++++++++++--
 libminifi/include/core/logging/Logger.h           |  3 +-
 libminifi/include/core/yaml/YamlConfiguration.h   | 52 ++++++++++-------------
 libminifi/src/c2/C2Agent.cpp                      |  1 +
 libminifi/src/c2/protocols/RESTProtocol.cpp       | 25 ++++++++++-
 thirdparty/google-styleguide/run_linter.sh        |  2 +-
 6 files changed, 78 insertions(+), 35 deletions(-)

diff --git a/extensions/http-curl/tests/C2FailedUpdateTest.cpp 
b/extensions/http-curl/tests/C2FailedUpdateTest.cpp
index 1df1219..ec98cf7 100644
--- a/extensions/http-curl/tests/C2FailedUpdateTest.cpp
+++ b/extensions/http-curl/tests/C2FailedUpdateTest.cpp
@@ -57,8 +57,31 @@ class ConfigHandler : public CivetHandler {
   ConfigHandler() {
     calls_ = 0;
   }
-  bool handlePost(CivetServer *server, struct mg_connection *conn) {
+  virtual bool handlePost(CivetServer *server, struct mg_connection *conn) 
override {
     calls_++;
+    const struct mg_request_info *req_info = mg_get_request_info(conn);
+    long long remainlen;
+    long long readlen = 0;
+    long long contentlen = req_info->content_length;
+    char buf[1024];
+
+    std::string data;
+    while (readlen < contentlen) {
+      remainlen = contentlen - readlen;
+      if (remainlen > sizeof(buf)) {
+        remainlen = sizeof(buf);
+      }
+      remainlen = mg_read(conn, buf, (size_t) remainlen);
+      if (remainlen <= 0) {
+        break;
+      }
+      readlen += remainlen;
+      data += std::string(buf, remainlen);
+    }
+    if (data.find("operationState") != std::string::npos) {
+      assert(data.find("state\": \"NOT_APPLIED") != std::string::npos);
+    }
+
     if (responses.size() > 0) {
       std::string top_str = responses.back();
       responses.pop_back();
@@ -73,7 +96,7 @@ class ConfigHandler : public CivetHandler {
     return true;
   }
 
-  bool handleGet(CivetServer *server, struct mg_connection *conn) {
+  virtual bool handleGet(CivetServer *server, struct mg_connection *conn) 
override {
     std::ifstream myfile(test_file_location_.c_str());
 
     if (myfile.is_open()) {
@@ -146,6 +169,7 @@ int main(int argc, char **argv) {
   configuration->set("nifi.c2.enable", "true");
   configuration->set("nifi.c2.agent.class", "test");
   configuration->set("nifi.c2.rest.url", "http://localhost:7071/update";);
+  configuration->set("nifi.c2.rest.url.ack", "http://localhost:7071/update";);
   configuration->set("nifi.c2.agent.heartbeat.period", "1000");
   mkdir("content_repository", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
 
@@ -161,7 +185,7 @@ int main(int argc, char **argv) {
   std::shared_ptr<TestRepository> repo = 
std::static_pointer_cast<TestRepository>(test_repo);
 
   std::shared_ptr<minifi::FlowController> controller = 
std::make_shared<minifi::FlowController>(test_repo, test_flow_repo, 
configuration, std::move(yaml_ptr), content_repo, DEFAULT_ROOT_GROUP_NAME,
-  true);
+                                                                               
                 true);
 
   core::YamlConfiguration yaml_config(test_repo, test_repo, content_repo, 
stream_factory, configuration, test_file_location);
 
diff --git a/libminifi/include/core/logging/Logger.h 
b/libminifi/include/core/logging/Logger.h
index 37ef390..4b8be46 100644
--- a/libminifi/include/core/logging/Logger.h
+++ b/libminifi/include/core/logging/Logger.h
@@ -271,7 +271,8 @@ class Logger : public BaseLogger {
     if (!delegate_->should_log(level)) {
       return;
     }
-    delegate_->log(level, format_string(format, 
conditional_conversion(args)...));
+    const auto str = format_string(format, conditional_conversion(args)...);
+    delegate_->log(level, str);
   }
 
   Logger(Logger const&);
diff --git a/libminifi/include/core/yaml/YamlConfiguration.h 
b/libminifi/include/core/yaml/YamlConfiguration.h
index cf8209e..318cb27 100644
--- a/libminifi/include/core/yaml/YamlConfiguration.h
+++ b/libminifi/include/core/yaml/YamlConfiguration.h
@@ -55,12 +55,8 @@ namespace core {
 class YamlConfiguration : public FlowConfiguration {
 
  public:
-  explicit YamlConfiguration(std::shared_ptr<core::Repository> repo,
-                             std::shared_ptr<core::Repository> flow_file_repo,
-                             std::shared_ptr<core::ContentRepository> 
content_repo,
-                             std::shared_ptr<io::StreamFactory> stream_factory,
-                             std::shared_ptr<Configure> configuration,
-                             const std::string path = 
DEFAULT_FLOW_YAML_FILE_NAME)
+  explicit YamlConfiguration(std::shared_ptr<core::Repository> repo, 
std::shared_ptr<core::Repository> flow_file_repo, 
std::shared_ptr<core::ContentRepository> content_repo,
+                             std::shared_ptr<io::StreamFactory> 
stream_factory, std::shared_ptr<Configure> configuration, const std::string 
path = DEFAULT_FLOW_YAML_FILE_NAME)
       : FlowConfiguration(repo, flow_file_repo, content_repo, stream_factory, 
configuration, path),
         logger_(logging::LoggerFactory<YamlConfiguration>::getLogger()) {
     stream_factory_ = stream_factory;
@@ -100,8 +96,14 @@ class YamlConfiguration : public FlowConfiguration {
    *                           configuration tree
    */
   std::unique_ptr<core::ProcessGroup> getYamlRoot(std::istream 
&yamlConfigStream) {
-    YAML::Node rootYamlNode = YAML::Load(yamlConfigStream);
-    return getYamlRoot(&rootYamlNode);
+    try {
+      YAML::Node rootYamlNode = YAML::Load(yamlConfigStream);
+      return getYamlRoot(&rootYamlNode);
+    } catch (const YAML::ParserException &pe) {
+      logger_->log_error(pe.what());
+      std::rethrow_exception(std::current_exception());
+    }
+    return nullptr;
   }
 
   /**
@@ -116,8 +118,14 @@ class YamlConfiguration : public FlowConfiguration {
    *                           configuration tree
    */
   std::unique_ptr<core::ProcessGroup> getRootFromPayload(const std::string 
&yamlConfigPayload) {
-    YAML::Node rootYamlNode = YAML::Load(yamlConfigPayload);
-    return getYamlRoot(&rootYamlNode);
+    try {
+      YAML::Node rootYamlNode = YAML::Load(yamlConfigPayload);
+      return getYamlRoot(&rootYamlNode);
+    } catch (const YAML::ParserException &pe) {
+      logger_->log_error(pe.what());
+      std::rethrow_exception(std::current_exception());
+    }
+    return nullptr;
   }
 
   /**
@@ -128,9 +136,7 @@ class YamlConfiguration : public FlowConfiguration {
    * @param component_name
    * @param yaml_section
    */
-  void validateComponentProperties(const 
std::shared_ptr<ConfigurableComponent> &component,
-                                   const std::string &component_name,
-                                   const std::string &yaml_section) const;
+  void validateComponentProperties(const 
std::shared_ptr<ConfigurableComponent> &component, const std::string 
&component_name, const std::string &yaml_section) const;
 
  protected:
 
@@ -262,10 +268,7 @@ class YamlConfiguration : public FlowConfiguration {
    * @param propertiesNode the YAML::Node containing the properties
    * @param processor      the Processor to which to add the resulting 
properties
    */
-  void parsePropertiesNodeYaml(YAML::Node *propertiesNode,
-                               std::shared_ptr<core::ConfigurableComponent> 
processor,
-                               const std::string &component_name,
-                               const std::string &yaml_section);
+  void parsePropertiesNodeYaml(YAML::Node *propertiesNode, 
std::shared_ptr<core::ConfigurableComponent> processor, const std::string 
&component_name, const std::string &yaml_section);
 
   /**
    * A helper function for parsing or generating optional id fields.
@@ -303,10 +306,7 @@ class YamlConfiguration : public FlowConfiguration {
    * @throws std::invalid_argument if the required field 'fieldName' is
    *                               not present in 'yamlNode'
    */
-  void checkRequiredField(YAML::Node *yamlNode,
-                          const std::string &fieldName,
-                          const std::string &yamlSection = "",
-                          const std::string &errorMessage = "");
+  void checkRequiredField(YAML::Node *yamlNode, const std::string &fieldName, 
const std::string &yamlSection = "", const std::string &errorMessage = "");
 
   /**
    * This is a helper function for getting an optional value, if it exists.
@@ -322,11 +322,7 @@ class YamlConfiguration : public FlowConfiguration {
    *                       the optional field is missing. If not provided,
    *                       a default info message will be generated.
    */
-  YAML::Node getOptionalField(YAML::Node *yamlNode,
-                              const std::string &fieldName,
-                              const YAML::Node &defaultValue,
-                              const std::string &yamlSection = "",
-                              const std::string &infoMessage = "");
+  YAML::Node getOptionalField(YAML::Node *yamlNode, const std::string 
&fieldName, const YAML::Node &defaultValue, const std::string &yamlSection = 
"", const std::string &infoMessage = "");
 
  protected:
   std::shared_ptr<io::StreamFactory> stream_factory_;
@@ -341,9 +337,7 @@ class YamlConfiguration : public FlowConfiguration {
    * @param yaml_section
    * @param reason
    */
-  void raiseComponentError(const std::string &component_name,
-                           const std::string &yaml_section,
-                           const std::string &reason) const;
+  void raiseComponentError(const std::string &component_name, const 
std::string &yaml_section, const std::string &reason) const;
 };
 
 } /* namespace core */
diff --git a/libminifi/src/c2/C2Agent.cpp b/libminifi/src/c2/C2Agent.cpp
index e3dc90a..5c3a0d0 100644
--- a/libminifi/src/c2/C2Agent.cpp
+++ b/libminifi/src/c2/C2Agent.cpp
@@ -694,6 +694,7 @@ void C2Agent::handle_update(const C2ContentResponse &resp) {
       } else {
         logger_->log_debug("update failed.");
         C2Payload response(Operation::ACKNOWLEDGE, 
state::UpdateState::SET_ERROR, resp.ident, false, true);
+        response.setRawData("Error while applying flow. Likely missing 
processors");
         enqueue_c2_response(std::move(response));
       }
       // send
diff --git a/libminifi/src/c2/protocols/RESTProtocol.cpp 
b/libminifi/src/c2/protocols/RESTProtocol.cpp
index 6eb91cb..fad4d67 100644
--- a/libminifi/src/c2/protocols/RESTProtocol.cpp
+++ b/libminifi/src/c2/protocols/RESTProtocol.cpp
@@ -256,8 +256,31 @@ std::string RESTProtocol::serializeJsonRootPayload(const 
C2Payload& payload) {
 
   std::string operationid = payload.getIdentifier();
   if (operationid.length() > 0) {
-    json_payload.AddMember("operationid", getStringValue(operationid, alloc), 
alloc);
     json_payload.AddMember("operationId", getStringValue(operationid, alloc), 
alloc);
+    std::string operationStateStr = "FULLY_APPLIED";
+    switch (payload.getStatus().getState()) {
+      case state::UpdateState::FULLY_APPLIED:
+        operationStateStr = "FULLY_APPLIED";
+        break;
+      case state::UpdateState::PARTIALLY_APPLIED:
+        operationStateStr = "PARTIALLY_APPLIED";
+        break;
+      case state::UpdateState::READ_ERROR:
+        operationStateStr = "OPERATION_NOT_UNDERSTOOD";
+        break;
+      case state::UpdateState::SET_ERROR:
+      default:
+        operationStateStr = "NOT_APPLIED";
+    }
+
+    rapidjson::Value opstate(rapidjson::kObjectType);
+
+    opstate.AddMember("state", getStringValue(operationStateStr, alloc), 
alloc);
+    const auto details = payload.getRawData();
+
+    opstate.AddMember("details", getStringValue(std::string(details.data(), 
details.size()), alloc), alloc);
+
+    json_payload.AddMember("operationState", opstate, alloc);
     json_payload.AddMember("identifier", getStringValue(operationid, alloc), 
alloc);
   }
 
diff --git a/thirdparty/google-styleguide/run_linter.sh 
b/thirdparty/google-styleguide/run_linter.sh
index a76ff61..de4de2a 100755
--- a/thirdparty/google-styleguide/run_linter.sh
+++ b/thirdparty/google-styleguide/run_linter.sh
@@ -40,4 +40,4 @@ done
 
 HEADERS=`find $INCLUDE_DIRS -name '*.h' | sort | uniq | tr '\n' ','`
 SOURCES=`find $SOURCE_DIRS -name  '*.cpp' | sort | uniq | tr '\n' ' '`
-python2 ${SCRIPT_DIR}/cpplint.py --linelength=200 --headers=${HEADERS} 
${SOURCES}
+python ${SCRIPT_DIR}/cpplint.py --linelength=200 --headers=${HEADERS} 
${SOURCES}

Reply via email to