|
| 1 | +/* |
| 2 | + * Copyright 2019-2020 Broadcom Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include "gearboxparser.h" |
| 18 | +#include "phyparser.h" |
| 19 | +#include <vector> |
| 20 | + |
| 21 | +void GearboxParser::notifyGearboxConfigDone(bool success) |
| 22 | +{ |
| 23 | + swss::ProducerStateTable *p = getProducerStateTable().get(); |
| 24 | + |
| 25 | + swss::FieldValueTuple finish_notice("success", std::to_string(success)); |
| 26 | + std::vector<swss::FieldValueTuple> attrs = { finish_notice }; |
| 27 | + |
| 28 | + p->set("GearboxConfigDone", attrs); |
| 29 | +} |
| 30 | + |
| 31 | +bool GearboxParser::parse() |
| 32 | +{ |
| 33 | + json root; |
| 34 | + |
| 35 | + try |
| 36 | + { |
| 37 | + root = getJSONRoot(); |
| 38 | + } |
| 39 | + catch (const std::exception& e) |
| 40 | + { |
| 41 | + SWSS_LOG_ERROR("JSON root not parseable"); |
| 42 | + return false; |
| 43 | + } |
| 44 | + |
| 45 | + json phys, phy, interfaces, interface, val, lanes; |
| 46 | + |
| 47 | + std::vector<swss::FieldValueTuple> attrs; |
| 48 | + |
| 49 | + try |
| 50 | + { |
| 51 | + phys = root["phys"]; |
| 52 | + if (phys.size() == 0) |
| 53 | + { |
| 54 | + SWSS_LOG_ERROR("zero-sized 'phys' field in gearbox configuration"); |
| 55 | + return false; |
| 56 | + } |
| 57 | + } |
| 58 | + catch (const std::exception& e) |
| 59 | + { |
| 60 | + SWSS_LOG_ERROR("unable to read gearbox configuration (invalid format)"); |
| 61 | + return false; |
| 62 | + } |
| 63 | + |
| 64 | + for (uint32_t iter = 0; iter < phys.size(); iter++) |
| 65 | + { |
| 66 | + phy = phys[iter]; |
| 67 | + try |
| 68 | + { |
| 69 | + attrs.clear(); |
| 70 | + swss::FieldValueTuple attr; |
| 71 | + if (phy.find("phy_id") == phy.end()) |
| 72 | + { |
| 73 | + SWSS_LOG_ERROR("missing 'phy_id' field in 'phys' item %d in gearbox configuration", iter); |
| 74 | + return false; |
| 75 | + } |
| 76 | + val = phy["phy_id"]; |
| 77 | + int phyId = val.get<int>(); |
| 78 | + attr = std::make_pair("phy_id", std::to_string(phyId)); |
| 79 | + attrs.push_back(attr); |
| 80 | + if (phy.find("name") == phy.end()) |
| 81 | + { |
| 82 | + SWSS_LOG_ERROR("missing 'name' field in 'phys' item %d in gearbox configuration", iter); |
| 83 | + return false; |
| 84 | + } |
| 85 | + val = phy["name"]; |
| 86 | + std::string name(val.get<std::string>()); |
| 87 | + attr = std::make_pair("name", std::string(val.get<std::string>())); |
| 88 | + attrs.push_back(attr); |
| 89 | + if (phy.find("address") == phy.end()) { |
| 90 | + SWSS_LOG_ERROR("missing 'address' field in 'phys' item %d in gearbox configuration", iter); |
| 91 | + return false; |
| 92 | + } |
| 93 | + val = phy["address"]; |
| 94 | + attr = std::make_pair("address", std::string(val.get<std::string>())); |
| 95 | + attrs.push_back(attr); |
| 96 | + if (phy.find("lib_name") == phy.end()) |
| 97 | + { |
| 98 | + SWSS_LOG_ERROR("missing 'lib_name' field in 'phys' item %d in gearbox configuration", iter); |
| 99 | + return false; |
| 100 | + } |
| 101 | + val = phy["lib_name"]; |
| 102 | + attr = std::make_pair("lib_name", std::string(val.get<std::string>())); |
| 103 | + attrs.push_back(attr); |
| 104 | + if (phy.find("firmware_path") == phy.end()) |
| 105 | + { |
| 106 | + SWSS_LOG_ERROR("missing 'firmware_path' field in 'phys' item %d in gearbox configuration", iter); |
| 107 | + return false; |
| 108 | + } |
| 109 | + val = phy["firmware_path"]; |
| 110 | + attr = std::make_pair("firmware_path", std::string(val.get<std::string>())); |
| 111 | + attrs.push_back(attr); |
| 112 | + if (phy.find("config_file") == phy.end()) |
| 113 | + { |
| 114 | + SWSS_LOG_ERROR("missing 'config_file' field in 'phys' item %d in gearbox configuration", iter); |
| 115 | + return false; |
| 116 | + } |
| 117 | + val = phy["config_file"]; |
| 118 | + std::string cfgFile(val.get<std::string>()); |
| 119 | + attr = std::make_pair("config_file", cfgFile); |
| 120 | + attrs.push_back(attr); |
| 121 | + if (phy.find("sai_init_config_file") == phy.end()) |
| 122 | + { |
| 123 | + SWSS_LOG_ERROR("missing 'sai_init_config_file' field in 'phys' item %d in gearbox configuration", iter); |
| 124 | + return false; |
| 125 | + } |
| 126 | + val = phy["sai_init_config_file"]; |
| 127 | + std::string bcmCfgFile(std::string(val.get<std::string>())); |
| 128 | + attr = std::make_pair("sai_init_config_file", bcmCfgFile); |
| 129 | + attrs.push_back(attr); |
| 130 | + if (phy.find("phy_access") == phy.end()) |
| 131 | + { |
| 132 | + SWSS_LOG_ERROR("missing 'phy_access' field in 'phys' item %d in gearbox configuration", iter); |
| 133 | + return false; |
| 134 | + } |
| 135 | + val = phy["phy_access"]; |
| 136 | + attr = std::make_pair("phy_access", std::string(val.get<std::string>())); |
| 137 | + attrs.push_back(attr); |
| 138 | + if (phy.find("bus_id") == phy.end()) |
| 139 | + { |
| 140 | + SWSS_LOG_ERROR("missing 'bus_id' field in 'phys' item %d in gearbox configuration", iter); |
| 141 | + return false; |
| 142 | + } |
| 143 | + val = phy["bus_id"]; |
| 144 | + attr = std::make_pair("bus_id", std::to_string(val.get<int>())); |
| 145 | + attrs.push_back(attr); |
| 146 | + std::string key; |
| 147 | + key = "phy:" + std::to_string(phyId); |
| 148 | + if (getWriteToDb() == true) |
| 149 | + { |
| 150 | + writeToDb(key, attrs); |
| 151 | + } |
| 152 | + PhyParser p; |
| 153 | + p.setPhyId(phyId); |
| 154 | + p.setWriteToDb(getWriteToDb()); |
| 155 | + p.setConfigPath(cfgFile); |
| 156 | + if (p.parse() == false) |
| 157 | + { |
| 158 | + SWSS_LOG_ERROR("phy parser failed to parse item %d in gearbox configuration", iter); |
| 159 | + return false; |
| 160 | + } |
| 161 | + } |
| 162 | + catch (const std::exception& e) |
| 163 | + { |
| 164 | + SWSS_LOG_ERROR("unable to read 'phys' item %d in gearbox configuration (invalid format)", iter); |
| 165 | + return false; |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + if (root.find("interfaces") != root.end()) |
| 170 | + { |
| 171 | + interfaces = root["interfaces"]; // vec |
| 172 | + if (interfaces.size() == 0) |
| 173 | + { |
| 174 | + SWSS_LOG_ERROR("zero-sized 'interfaces' field in gearbox configuration"); |
| 175 | + return false; |
| 176 | + } |
| 177 | + for (uint32_t iter = 0; iter < interfaces.size(); iter++) |
| 178 | + { |
| 179 | + attrs.clear(); |
| 180 | + interface = interfaces[iter]; |
| 181 | + try |
| 182 | + { |
| 183 | + swss::FieldValueTuple attr; |
| 184 | + |
| 185 | + if (interface.find("name") == interface.end()) |
| 186 | + { |
| 187 | + SWSS_LOG_ERROR("missing 'name' field in 'interfaces' item %d in gearbox configuration", iter); |
| 188 | + return false; |
| 189 | + } |
| 190 | + val = interface["name"]; |
| 191 | + attr = std::make_pair("name", std::string(val.get<std::string>())); |
| 192 | + attrs.push_back(attr); |
| 193 | + |
| 194 | + if (interface.find("index") == interface.end()) |
| 195 | + { |
| 196 | + SWSS_LOG_ERROR("missing 'index' field in 'interfaces' item %d in gearbox configuration", iter); |
| 197 | + return false; |
| 198 | + } |
| 199 | + val = interface["index"]; |
| 200 | + int index = val.get<int>(); |
| 201 | + attr = std::make_pair("index", std::to_string(index)); |
| 202 | + attrs.push_back(attr); |
| 203 | + |
| 204 | + if (interface.find("phy_id") == interface.end()) |
| 205 | + { |
| 206 | + SWSS_LOG_ERROR("missing 'phy_id' field in 'interfaces' item %d in gearbox configuration", iter); |
| 207 | + return false; |
| 208 | + } |
| 209 | + val = interface["phy_id"]; |
| 210 | + attr = std::make_pair("phy_id", std::to_string(val.get<int>())); |
| 211 | + attrs.push_back(attr); |
| 212 | + |
| 213 | + if (interface.find("system_lanes") != interface.end()) |
| 214 | + { |
| 215 | + lanes = interface["system_lanes"]; // vec |
| 216 | + std::string laneStr(""); |
| 217 | + if (lanes.size() == 0) |
| 218 | + { |
| 219 | + SWSS_LOG_ERROR("zero-sized 'system_lanes' field in 'interfaces' item %d in gearbox configuration", iter); |
| 220 | + return false; |
| 221 | + } |
| 222 | + for (uint32_t iter2 = 0; iter2 < lanes.size(); iter2++) |
| 223 | + { |
| 224 | + val = lanes[iter2]; |
| 225 | + if (laneStr.length() > 0) |
| 226 | + { |
| 227 | + laneStr += ","; |
| 228 | + } |
| 229 | + laneStr += std::to_string(val.get<int>()); |
| 230 | + } |
| 231 | + attr = std::make_pair("system_lanes", laneStr); |
| 232 | + attrs.push_back(attr); |
| 233 | + } |
| 234 | + else |
| 235 | + { |
| 236 | + SWSS_LOG_ERROR("missing 'system_lanes' field in 'interfaces' item %d in gearbox configuration", iter); |
| 237 | + return false; |
| 238 | + } |
| 239 | + |
| 240 | + if (interface.find("line_lanes") != interface.end()) |
| 241 | + { |
| 242 | + lanes = interface["line_lanes"]; // vec |
| 243 | + std::string laneStr(""); |
| 244 | + if (lanes.size() == 0) |
| 245 | + { |
| 246 | + SWSS_LOG_ERROR("zero-sized 'line_lanes' field in 'interfaces' item %d in gearbox configuration", iter); |
| 247 | + return false; |
| 248 | + } |
| 249 | + for (uint32_t iter2 = 0; iter2 < lanes.size(); iter2++) |
| 250 | + { |
| 251 | + val = lanes[iter2]; |
| 252 | + if (laneStr.length() > 0) |
| 253 | + { |
| 254 | + laneStr += ","; |
| 255 | + } |
| 256 | + laneStr += std::to_string(val.get<int>()); |
| 257 | + } |
| 258 | + attr = std::make_pair("line_lanes", laneStr); |
| 259 | + attrs.push_back(attr); |
| 260 | + } |
| 261 | + else |
| 262 | + { |
| 263 | + SWSS_LOG_ERROR("missing 'line_lanes' field in 'interfaces' item %d in gearbox configuration", iter); |
| 264 | + return false; |
| 265 | + } |
| 266 | + std::string key; |
| 267 | + key = "interface:" + std::to_string(index); |
| 268 | + if (getWriteToDb() == true) |
| 269 | + { |
| 270 | + writeToDb(key, attrs); |
| 271 | + } |
| 272 | + } |
| 273 | + catch (const std::exception& e) |
| 274 | + { |
| 275 | + SWSS_LOG_ERROR("unable to read 'interfaces' item %d in gearbox configuration (invalid format)", iter); |
| 276 | + return false; |
| 277 | + } |
| 278 | + } |
| 279 | + } |
| 280 | + else |
| 281 | + { |
| 282 | + SWSS_LOG_ERROR("unable to read 'interfaces' item in gearbox configuration"); |
| 283 | + return false; |
| 284 | + } |
| 285 | + return true; |
| 286 | +} |
0 commit comments