brbzull0 commented on a change in pull request #7023:
URL: https://github.com/apache/trafficserver/pull/7023#discussion_r518134060



##########
File path: 
plugins/experimental/nexthop_strategy_consistenthash/consistenthash_config.cc
##########
@@ -0,0 +1,207 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 "strategy.h"
+#include "consistenthash.h"
+#include "util.h"
+
+#include <cinttypes>
+#include <string>
+#include <algorithm>
+#include <mutex>
+#include <unordered_map>
+#include <unordered_set>
+#include <fstream>
+#include <cstring>
+
+#include <sys/stat.h>
+#include <dirent.h>
+
+#include <yaml-cpp/yaml.h>
+
+#include "tscore/HashSip.h"
+#include "tscore/ConsistentHash.h"
+#include "tscore/ink_assert.h"
+#include "ts/ts.h"
+#include "ts/remap.h"
+#include "tscpp/api/nexthop.h"
+#include "tscpp/api/parentresult.h"
+
+void loadConfigFile(const std::string fileName, std::stringstream &doc, 
std::unordered_set<std::string> &include_once);
+
+// createStrategy creates and initializes a Consistent Hash strategy from the 
given YAML node.
+// Caller takes ownership of the returned pointer, and must call delete on it.
+TSNextHopSelectionStrategy *
+createStrategy(const std::string &name, const YAML::Node &node)
+{
+  NextHopConsistentHash *st = new NextHopConsistentHash(name);
+  if (!st->Init(node)) {
+    return nullptr;
+  }
+  return st;
+}
+
+// createStrategyFromFile creates a Consistent Hash strategy from the given 
config file.
+// Caller takes ownership of the returned pointer, and must call delete on it.
+TSNextHopSelectionStrategy *
+createStrategyFromFile(const char *file, const char *strategyName)
+{
+  NH_Debug(NH_DEBUG_TAG, "plugin createStrategyFromFile file '%s' strategy 
'%s'", file, strategyName);
+
+  YAML::Node config;
+  YAML::Node strategies;
+  std::stringstream doc;
+  std::unordered_set<std::string> include_once;
+  std::string_view fn = file;
+
+  // strategy policy
+  constexpr std::string_view consistent_hash = "consistent_hash";
+
+  const char *basename = fn.substr(fn.find_last_of('/') + 1).data();
+
+  try {
+    NH_Note("%s loading ...", basename);
+    loadConfigFile(fn.data(), doc, include_once);
+
+    config = YAML::Load(doc);
+    if (config.IsNull()) {
+      NH_Note("No NextHop strategy configs were loaded.");
+      return nullptr;
+    }
+
+    strategies = config["strategies"];
+    if (strategies.Type() != YAML::NodeType::Sequence) {
+      NH_Error("malformed %s file, expected a 'strategies' sequence", 
basename);
+      return nullptr;
+    }
+
+    for (unsigned int i = 0; i < strategies.size(); ++i) {
+      YAML::Node strategy = strategies[i];

Review comment:
       Have you considered following 
[this](https://github.com/jbeder/yaml-cpp/wiki/Tutorial#converting-tofrom-native-data-types)
 custom Data types like `TSNextHopSelectionStrategy`?




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to