forked from stratum/stratum
-
Notifications
You must be signed in to change notification settings - Fork 7
/
onlp_cli.cc
135 lines (117 loc) · 4.03 KB
/
onlp_cli.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright 2019 Dell EMC
// Copyright 2024 Intel Corporation
// Copyright 2023 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include "absl/strings/str_split.h"
#include "absl/time/clock.h"
#include "absl/time/time.h"
#include "gflags/gflags.h"
#include "stratum/glue/init_google.h"
#include "stratum/glue/status/status.h"
#include "stratum/glue/status/status_macros.h"
#include "stratum/glue/status/statusor.h"
#include "stratum/hal/lib/phal/onlp/onlp_wrapper.h"
#include "stratum/lib/macros.h"
namespace stratum {
namespace hal {
namespace phal {
namespace onlp {
// Handles various CLI interactions with an attribute database.
class OnlpCli {
public:
// All CLI queries are run on the given attribute database.
OnlpCli() : onlp_interface_(nullptr) {}
// Print OID List
::util::Status PrintOidList(std::string use_wrapper, std::string stype,
onlp_oid_type_flag_t type) {
// call onlp directly
if (use_wrapper == "n") {
biglist_t* oid_hdr_list;
onlp_oid_hdr_get_all(ONLP_OID_CHASSIS, type, 0, &oid_hdr_list);
biglist_t* curr_node = oid_hdr_list;
// std::cout << stype << " OID List:" << std::endl;
while (curr_node != nullptr) {
onlp_oid_hdr_t* oid_hdr =
reinterpret_cast<onlp_oid_hdr_t*>(curr_node->data);
std::cout << " oid: " << oid_hdr->id << std::endl;
curr_node = curr_node->next;
}
onlp_oid_get_all_free(oid_hdr_list);
// using onlp_wrapper
} else {
ASSIGN_OR_RETURN(std::vector<OnlpOid> OnlpOids,
onlp_interface_->GetOidList(type));
if (OnlpOids.size() == 0) {
std::cout << "no " << stype << " OIDs" << std::endl;
return ::util::OkStatus();
}
std::cout << stype << " OID List:" << std::endl;
for (unsigned int i = 0; i < OnlpOids.size(); i++) {
std::cout << " " << i << ": oid: " << OnlpOids[i] << std::endl;
}
}
return ::util::OkStatus();
}
// Runs the main CLI loop.
::util::Status RunCli() {
// Create the OnlpInterface object
onlp_interface_ = OnlpWrapper::CreateSingleton();
while (true) { // Grap input from std::cin and pass it to a OnlpCli.
std::string use_wrapper;
std::cout << "Use Wrapper (Y/n): ";
std::string query;
std::getline(std::cin, query);
std::cout << "Enter an OID type: ";
std::getline(std::cin, query);
if (std::cin.eof()) break;
if (query == "") {
std::cout << "Use ^D to quit." << std::endl;
} else if (query == "chassis") {
PrintOidList(use_wrapper, query, ONLP_OID_TYPE_FLAG_CHASSIS);
} else if (query == "module") {
PrintOidList(use_wrapper, query, ONLP_OID_TYPE_FLAG_MODULE);
} else if (query == "thermal") {
PrintOidList(use_wrapper, query, ONLP_OID_TYPE_FLAG_THERMAL);
} else if (query == "fan") {
PrintOidList(use_wrapper, query, ONLP_OID_TYPE_FLAG_FAN);
} else if (query == "psu") {
PrintOidList(use_wrapper, query, ONLP_OID_TYPE_FLAG_PSU);
} else if (query == "led") {
PrintOidList(use_wrapper, query, ONLP_OID_TYPE_FLAG_LED);
} else if (query == "sfp") {
PrintOidList(use_wrapper, query, ONLP_OID_TYPE_FLAG_SFP);
} else if (query == "generic") {
PrintOidList(use_wrapper, query, ONLP_OID_TYPE_FLAG_GENERIC);
} else {
std::cout << "unknown oid type" << std::endl;
}
}
std::cout << "Exiting." << std::endl;
return ::util::OkStatus();
}
private:
OnlpInterface* onlp_interface_;
};
static ::util::Status Main(int argc, char** argv) {
InitGoogle("onlp_cli", &argc, &argv, true);
OnlpCli cli;
cli.RunCli();
return ::util::OkStatus();
}
} // namespace onlp
} // namespace phal
} // namespace hal
} // namespace stratum
int main(int argc, char** argv) {
::util::Status status = stratum::hal::phal::onlp::Main(argc, argv);
if (status.ok()) {
return 0;
} else {
LOG(ERROR) << status;
return 1;
}
}