Commit 7014ac3f authored by Mosaical's avatar Mosaical

20201112

parent 066804bb
...@@ -20,6 +20,10 @@ ...@@ -20,6 +20,10 @@
#include <stdbool.h> #include <stdbool.h>
#include <regex> #include <regex>
#include "inforMatch.h" #include "inforMatch.h"
#include "get2rdNodeMessage.h"
#include <fstream>
#include <iostream>
#include <nlohmann/json.hpp>
namespace org { namespace org {
namespace openapitools { namespace openapitools {
namespace server { namespace server {
...@@ -74,6 +78,7 @@ void NFInstanceIDDocumentApi::deregister_nf_instance_handler(const Pistache::Res ...@@ -74,6 +78,7 @@ void NFInstanceIDDocumentApi::deregister_nf_instance_handler(const Pistache::Res
} }
void NFInstanceIDDocumentApi::get_nf_instance_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) { void NFInstanceIDDocumentApi::get_nf_instance_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) {
// Getting the path params // Getting the path params
try{
auto nfInstanceID = request.param(":nfInstanceID").as<std::string>(); auto nfInstanceID = request.param(":nfInstanceID").as<std::string>();
std::cout << "get_nf_instance_handler启动了" << std::endl; std::cout << "get_nf_instance_handler启动了" << std::endl;
std::cout << "nfInstanceID是: " << nfInstanceID << std::endl; std::cout << "nfInstanceID是: " << nfInstanceID << std::endl;
...@@ -144,13 +149,64 @@ void NFInstanceIDDocumentApi::get_nf_instance_handler(const Pistache::Rest::Requ ...@@ -144,13 +149,64 @@ void NFInstanceIDDocumentApi::get_nf_instance_handler(const Pistache::Rest::Requ
std::cout << testJ << std::endl; std::cout << testJ << std::endl;
/* /*
* 取出某二级节点下的显性信息 *取出type节点下的信息,包含此type的全部instanceId,用分号间隔
*/ */
std::string instancePath = "/AMF/duklAMF";//可以更改
//先取出2级节点下的全部instanceId信息字符串
get2rdNodeMessage Get2rdNodeMessage;
std::string path_NfType = "/" + targetNfTypeString;//要取出的2级节点路径,即NF类型
std::string NFInstanceIdsString = Get2rdNodeMessage.getNodeMessage(path_NfType);//取出信息格式为: duklAMF;mxcAMF;
//string转char数组,检验分号";",转化为单个string
int NodeMessageSize = NFInstanceIdsString.size();
//std::cout << "NodeMessageSize is : " << NodeMessageSize << std::endl;
char NFInstanceIdsChar[NodeMessageSize];
strcpy(NFInstanceIdsChar,NFInstanceIdsString.c_str());
int ArraySubscript = 0;
std::string OneOfInstanceId;
std::string responseBodyProfile;
int numberFlag = 0;
for( ; ArraySubscript < NodeMessageSize ; ArraySubscript++){
if(NFInstanceIdsChar[ArraySubscript] != 59/*分号的ASCLL码为59*/){
std::string s(NFInstanceIdsString, ArraySubscript, 1);
OneOfInstanceId = OneOfInstanceId + s;
}
else{//遇到分号了,此时OneOfInstanceId中存的信息是上一个instanceId
/*
* 处理刚刚取出的一个三级节点路径,判断是否匹配
*/
std::string instancePath = path_NfType;
instancePath = instancePath + "/" + OneOfInstanceId;
inforMatch InforMatch; inforMatch InforMatch;
bool restBool = InforMatch.ifIsMatching(instancePath, jsonInformation);//判断实例duklAMF是否是要寻找的 bool restBool = InforMatch.ifIsMatching(instancePath, jsonInformation);
if(restBool) printf("匹配成功!"); if(restBool){
else printf("匹配失败!"); printf("匹配成功!");
numberFlag = numberFlag + 1;
//使用三节节点名称取出对应配置文件
std::string fileName = OneOfInstanceId + ".json";
std::ifstream readProfile(fileName);
nlohmann::json thisProfileJson;
readProfile>> thisProfileJson;
std::string thisProfileString = thisProfileJson.dump();
//std::cout << "*****************************************thisProfileJson:::::" << thisProfileJson << std::endl;
//response.send(Pistache::Http::Code::Ok, "Do some magic1\n");
//response.send(Pistache::Http::Code::Ok, thisProfileString);
responseBodyProfile = responseBodyProfile + thisProfileString;
//goto endPoint;
OneOfInstanceId = "";
}
else {
OneOfInstanceId = "";
}
}
}
//endPoint: std::cout << "匹配循环已结束!" << std::endl;
std::string responseBody = "{\"validityPeriod\":\"3600\",\"nfInstances\":";//{"validityPeriod":"3600","nfInstances":
if(numberFlag > 1) responseBodyProfile = "{" + responseBodyProfile + "}";
responseBody = responseBody + responseBodyProfile + "}";// {"validityPeriod":"3600","nfInstances":{responseBodyProfile}}
//response的M部分添加完毕
//searchId,numNfInstComplete,nrfSupportedFeatures三个属性未加
response.send(Pistache::Http::Code::Ok, responseBody);
...@@ -158,11 +214,22 @@ void NFInstanceIDDocumentApi::get_nf_instance_handler(const Pistache::Rest::Requ ...@@ -158,11 +214,22 @@ void NFInstanceIDDocumentApi::get_nf_instance_handler(const Pistache::Rest::Requ
std::cout << aS << std::endl; std::cout << aS << std::endl;
std::cout << nFType.nfType << std::endl; std::cout << nFType.nfType << std::endl;
} catch (nlohmann::detail::exception &e) {
//send a 400 error
response.send(Pistache::Http::Code::Bad_Request, e.what());
return;
} catch (Pistache::Http::HttpError &e) {
response.send(static_cast<Pistache::Http::Code>(e.code()), e.what());
return;
} catch (std::exception &e) {
//send a 500 error
response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
return;
}
/* try {
try {
this->get_nf_instance(nfInstanceID, response); this->get_nf_instance(nfInstanceID, response);
} catch (nlohmann::detail::exception &e) { } catch (nlohmann::detail::exception &e) {
//send a 400 error //send a 400 error
...@@ -176,7 +243,7 @@ void NFInstanceIDDocumentApi::get_nf_instance_handler(const Pistache::Rest::Requ ...@@ -176,7 +243,7 @@ void NFInstanceIDDocumentApi::get_nf_instance_handler(const Pistache::Rest::Requ
response.send(Pistache::Http::Code::Internal_Server_Error, e.what()); response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
return; return;
} }
*/
} }
void NFInstanceIDDocumentApi::register_nf_instance_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) { void NFInstanceIDDocumentApi::register_nf_instance_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) {
// Getting the path params // Getting the path params
......
...@@ -57,7 +57,7 @@ void NFInstanceIDDocumentApiImpl::register_nf_instance(const std::string &nfInst ...@@ -57,7 +57,7 @@ void NFInstanceIDDocumentApiImpl::register_nf_instance(const std::string &nfInst
//以上为测试内容 //以上为测试内容
//j.erase("");删除无用标志信息剩余有用信息的json //j.erase("");删除无用标志信息剩余有用信息的json
j.erase("nfStatus"); //j.erase("nfStatus");
//将剩余的有用信息化为string //将剩余的有用信息化为string
//std::string jS = j.dump(); //std::string jS = j.dump();
...@@ -67,10 +67,12 @@ void NFInstanceIDDocumentApiImpl::register_nf_instance(const std::string &nfInst ...@@ -67,10 +67,12 @@ void NFInstanceIDDocumentApiImpl::register_nf_instance(const std::string &nfInst
std::string nfInstanceId; std::string nfInstanceId;
j.at("nfInstanceId").get_to(nfInstanceId); j.at("nfInstanceId").get_to(nfInstanceId);
std::string pointJson = ".json"; std::string pointJson = ".json";
std::string pointTXT = ".txt";
std::string value; std::string value;
value = nfInstanceId + pointJson;//Value 文件名 value = nfInstanceId + pointJson;//Value 文件名
std::ofstream myfile(value); std::ofstream myfile(value);
std::string jString = j.dump();
myfile << std::setw(75) << j << std::endl; myfile << std::setw(75) << j << std::endl;
zookeeperClient zooK; zookeeperClient zooK;
...@@ -110,15 +112,28 @@ void NFInstanceIDDocumentApiImpl::register_nf_instance(const std::string &nfInst ...@@ -110,15 +112,28 @@ void NFInstanceIDDocumentApiImpl::register_nf_instance(const std::string &nfInst
//std::string nfType; //std::string nfType
//j.at("nfType").get_to(nfType); //j.at("nfType").get_to(nfType);
std::string path; std::string path;
std::string fullPath; std::string fullPath;
path = source + nfType; path = source + nfType;//初始化2级节点
fullPath = path + "/" + nfInstanceId ;//fullPath是每一个实例的全部路径,包含关键信息 fullPath = path + "/" + nfInstanceId ;//fullPath是每一个实例的全部路径,包含关键信息
cout << " path: " << path << endl; cout << " path: " << path << endl;
bool b; bool b;
b = Register.registerZK("this_is_A_nfType", path, zk); /*
* 二级节点应该已经初始化完毕
* b = Register.registerZK("/", path, zk);
*
*/
/*初始化二级节点用 b = Register.registerZK("", path, zk); */
/**/
std::string getValue;
utility::zoo_rc getRet = zk->get_node(path.c_str(), getValue, nullptr, true);//取出type节点下的信息放在getValue中
getValue = getValue + nfInstanceId + ";";
utility::zoo_rc setRet = zk->set_node(path.c_str(), getValue, -1);
/**/
b = Register.registerZK(InforValue, fullPath, zk); b = Register.registerZK(InforValue, fullPath, zk);
response.send(Pistache::Http::Code::Ok, "Do some magic\n"); response.send(Pistache::Http::Code::Ok, "Do some magic\n");
} }
......
#include "get2rdNodeMessage.h"
#include "zookeeperClient.h"
#include <stdbool.h>
std::string get2rdNodeMessage::getNodeMessage(std::string path){
std::string value;
zookeeperClient zooK;
bool atest = zooK.connect2ZKServer("192.168.110.129:2181");
utility::zk_cpp* zk;
zk = zooK.getZk();
utility::zoo_rc ret = zk->get_node(path.c_str(), value, nullptr, true);
return value;
}
#include <stdio.h>
#include <stdbool.h>
#include <string>
#include <iostream>
#include <nlohmann/json.hpp>
class get2rdNodeMessage{
public:
std::string getNodeMessage(std::string path);
};
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment