Redfish NetworkProtocol
Node version of the NetworkProtocol implementation.
Change-Id: I6f52dca4e530917a5b815a32f1222b481059aa23
Signed-off-by: Borawski.Lukasz <lukasz.borawski@intel.com>
diff --git a/include/redfish_v1.hpp b/include/redfish_v1.hpp
index 0c070ed..aa6da06 100644
--- a/include/redfish_v1.hpp
+++ b/include/redfish_v1.hpp
@@ -214,63 +214,6 @@
res.end();
});
- CROW_ROUTE(app, "/redfish/v1/Managers/NetworkProtocol/")
- .methods(
- "GET"_method)([&](const crow::request& req, crow::response& res) {
- std::array<char, HOST_NAME_MAX> hostname;
- if (gethostname(hostname.data(), hostname.size()) != 0) {
- res.code = 500;
- res.end();
- return;
- }
- res.json_value = {
- {"@odata.context",
- "/redfish/v1/"
- "$metadata#ManagerNetworkProtocol.ManagerNetworkProtocol"},
- {"@odata.id", "/redfish/v1/Managers/BMC/NetworkProtocol"},
- {"@odata.type",
- "#ManagerNetworkProtocol.v1_1_0.ManagerNetworkProtocol"},
- {"Id", "NetworkProtocol"},
- {"Name", "Manager Network Protocol"},
- {"Description", "Manager Network Service"},
- {"Status",
- {{"State", "Enabled"}, {"Health", "OK"}, {"HealthRollup", "OK"}}},
- {"HostName", hostname.data()}}; // TODO(ed) get hostname
- std::string netstat_out = execute_process("netstat -tuln");
-
- std::map<int, const char*> service_types{{22, "SSH"},
- {443, "HTTPS"},
- {1900, "SSDP"},
- {623, "IPMI"},
- {427, "SLP"}};
-
- std::vector<std::string> lines;
- boost::split(lines, netstat_out, boost::is_any_of("\n"));
- auto lines_it = lines.begin();
- lines_it++; // skip the netstat header
- lines_it++;
- while (lines_it != lines.end()) {
- std::vector<std::string> columns;
- boost::split(columns, *lines_it, boost::is_any_of("\t "),
- boost::token_compress_on);
- if (columns.size() >= 5) {
- std::size_t found = columns[3].find_last_of(":");
- if (found != std::string::npos) {
- std::string port_str = columns[3].substr(found + 1);
- int port = std::stoi(port_str.c_str());
- auto type_it = service_types.find(port);
- if (type_it != service_types.end()) {
- res.json_value[type_it->second] = {{"ProtocolEnabled", true},
- {"Port", port}};
- }
- }
- }
- lines_it++;
- }
-
- get_redfish_sub_routes(app, "/redfish/v1/", res.json_value);
- res.end();
- });
}
} // namespace redfish
} // namespace crow
diff --git a/redfish-core/include/redfish.hpp b/redfish-core/include/redfish.hpp
index fc8c080..e14845f 100644
--- a/redfish-core/include/redfish.hpp
+++ b/redfish-core/include/redfish.hpp
@@ -16,6 +16,7 @@
#pragma once
#include "../lib/account_service.hpp"
+#include "../lib/network_protocol.hpp"
#include "../lib/redfish_sessions.hpp"
#include "../lib/roles.hpp"
#include "../lib/service_root.hpp"
@@ -40,6 +41,7 @@
nodes.emplace_back(std::make_unique<Roles>(app));
nodes.emplace_back(std::make_unique<RoleCollection>(app));
nodes.emplace_back(std::make_unique<ServiceRoot>(app));
+ nodes.emplace_back(std::make_unique<NetworkProtocol>(app));
for (auto& node : nodes) {
node->getSubRoutes(nodes);
diff --git a/redfish-core/lib/network_protocol.hpp b/redfish-core/lib/network_protocol.hpp
new file mode 100644
index 0000000..2b45b40
--- /dev/null
+++ b/redfish-core/lib/network_protocol.hpp
@@ -0,0 +1,105 @@
+/*
+// Copyright (c) 2018 Intel Corporation
+//
+// 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.
+*/
+#pragma once
+
+#include "node.hpp"
+
+namespace redfish {
+
+static OperationMap managerNetworkProtocolOpMap = {
+ {crow::HTTPMethod::GET, {{"Login"}}},
+ {crow::HTTPMethod::HEAD, {{"Login"}}},
+ {crow::HTTPMethod::PATCH, {{"ConfigureManager"}}},
+ {crow::HTTPMethod::PUT, {{"ConfigureManager"}}},
+ {crow::HTTPMethod::DELETE, {{"ConfigureManager"}}},
+ {crow::HTTPMethod::POST, {{"ConfigureManager"}}}};
+
+class NetworkProtocol : public Node {
+ public:
+ NetworkProtocol(CrowApp& app)
+ : Node(app, EntityPrivileges(std::move(managerNetworkProtocolOpMap)),
+ "/redfish/v1/Managers/openbmc/NetworkProtocol") {
+ Node::json["@odata.type"] =
+ "#ManagerNetworkProtocol.v1_1_0.ManagerNetworkProtocol";
+ Node::json["@odata.id"] = "/redfish/v1/Managers/openbmc/NetworkProtocol";
+ Node::json["@odata.context"] =
+ "/redfish/v1/$metadata#ManagerNetworkProtocol.ManagerNetworkProtocol";
+ Node::json["Id"] = "NetworkProtocol";
+ Node::json["Name"] = "Manager Network Protocol";
+ Node::json["Description"] = "Manager Network Service";
+ Node::json["Status"]["Health"] = "OK";
+ Node::json["Status"]["HealthRollup"] = "OK";
+ Node::json["Status"]["State"] = "Enabled";
+ }
+
+ private:
+ void doGet(crow::response& res, const crow::request& req,
+ const std::vector<std::string>& params) override {
+ refreshProtocolsState();
+ Node::json["HostName"] = getHostName();
+ res.json_value = Node::json;
+ res.end();
+ }
+
+ std::string getHostName() const {
+ std::string hostName;
+
+ std::array<char, HOST_NAME_MAX> hostNameCStr;
+ if (gethostname(hostNameCStr.data(), hostNameCStr.size()) == 0) {
+ hostName = hostNameCStr.data();
+ }
+ return hostName;
+ }
+
+ void refreshProtocolsState() {
+ refreshListeningPorts();
+ for (auto& kv : portToProtocolMap) {
+ Node::json[kv.second]["Port"] = kv.first;
+ if (listeningPorts.find(kv.first) != listeningPorts.end()) {
+ Node::json[kv.second]["ProtocolEnabled"] = true;
+ } else {
+ Node::json[kv.second]["ProtocolEnabled"] = false;
+ }
+ }
+ }
+
+ void refreshListeningPorts() {
+ listeningPorts.clear();
+ std::array<char, 128> netstatLine;
+ FILE* p = popen("netstat -tuln | awk '{ print $4 }'", "r");
+ if (p != nullptr) {
+ while (fgets(netstatLine.data(), netstatLine.size(), p) != nullptr) {
+ auto s = std::string(netstatLine.data());
+
+ // get port num from strings such as: ".*:.*:.*:port"
+ s.erase(0, s.find_last_of(":") + strlen(":"));
+
+ auto port = atoi(s.c_str());
+ if (port != 0 &&
+ portToProtocolMap.find(port) != portToProtocolMap.end()) {
+ listeningPorts.insert(port);
+ }
+ }
+ }
+ }
+
+ std::map<int, std::string> portToProtocolMap{
+ {22, "SSH"}, {80, "HTTP"}, {443, "HTTPS"}, {623, "IPMI"}, {1900, "SSDP"}};
+
+ std::set<int> listeningPorts;
+};
+
+} // namespace redfish
diff --git a/redfish-core/lib/service_root.hpp b/redfish-core/lib/service_root.hpp
index 129d58d..0afc0a2 100644
--- a/redfish-core/lib/service_root.hpp
+++ b/redfish-core/lib/service_root.hpp
@@ -49,7 +49,6 @@
private:
void doGet(crow::response& res, const crow::request& req,
const std::vector<std::string>& params) override {
- res.add_header("Content-Type", "application/json");
res.json_value = Node::json;
res.end();
}