@@ -379,7 +379,7 @@ void CV2::initializeNetworks()
379379
380380 try {
381381 char qbuf[2048 ];
382- sprintf (qbuf, " SELECT id, name, description, configuration , created_at , last_modified, revision FROM networks WHERE controller_id = '%s'" , _myAddressStr.c_str ());
382+ sprintf (qbuf, " SELECT id, name, configuration , creation_time , last_modified, revision FROM networks_ctl WHERE controller_id = '%s'" , _myAddressStr.c_str ());
383383
384384 auto c = _pool->borrow ();
385385 pqxx::work w (*c->c );
@@ -389,7 +389,6 @@ void CV2::initializeNetworks()
389389 std::tuple<
390390 std::string // network ID
391391 , std::optional<std::string> // name
392- , std::optional<std::string> // description
393392 , std::string // configuration
394393 , std::optional<uint64_t > // created_at
395394 , std::optional<uint64_t > // last_modified
@@ -407,15 +406,13 @@ void CV2::initializeNetworks()
407406
408407 std::string nwid = std::get<0 >(row);
409408 std::string name = std::get<1 >(row).value_or (" " );
410- std::string description = std::get<2 >(row).value_or (" " );
411- json cfgtmp = json::parse (std::get<3 >(row));
412- std::optional<uint64_t > created_at = std::get<4 >(row);
413- std::optional<uint64_t > last_modified = std::get<5 >(row);
414- std::optional<uint64_t > revision = std::get<6 >(row);
409+ json cfgtmp = json::parse (std::get<2 >(row));
410+ std::optional<uint64_t > created_at = std::get<3 >(row);
411+ std::optional<uint64_t > last_modified = std::get<4 >(row);
412+ std::optional<uint64_t > revision = std::get<5 >(row);
415413
416414 config[" id" ] = nwid;
417415 config[" name" ] = name;
418- config[" description" ] = description;
419416 config[" creationTime" ] = created_at.value_or (0 );
420417 config[" lastModified" ] = last_modified.value_or (0 );
421418 config[" revision" ] = revision.value_or (0 );
@@ -496,15 +493,15 @@ void CV2::initializeMembers()
496493 try {
497494 char qbuf[2048 ];
498495 sprintf (qbuf,
499- " SELECT dn .device_id, dn .network_id, dn .authorized, dn .active_bridge, dn .ip_assignments, dn .no_auto_assign_ips, "
500- " dn .sso_exempt, authentication_expiry_time, dn .creation_time, dn .identity, dn .last_authorized_credential, "
501- " dn .last_authorized_time, dn .last_deauthorized_time, dn .remote_trace_level, dn .remote_trace_target, "
502- " dn .revision, dn .capabilities, dn .tags "
503- " FROM device_networks dn "
496+ " SELECT nm .device_id, nm .network_id, nm .authorized, nm .active_bridge, nm .ip_assignments, nm .no_auto_assign_ips, "
497+ " nm .sso_exempt, authentication_expiry_time, nm .creation_time, nm .identity, nm .last_authorized_credential, "
498+ " nm .last_authorized_time, nm .last_deauthorized_time, nm .remote_trace_level, nm .remote_trace_target, "
499+ " nm .revision, nm .capabilities, nm .tags "
500+ " FROM network_memberships_ctl nm "
504501 " INNER JOIN devices d "
505- " ON dn .device_id = d.id "
502+ " ON nm .device_id = d.id "
506503 " INNER JOIN networks n "
507- " ON dn .network_id = n.id "
504+ " ON nm .network_id = n.id "
508505 " WHERE n.controller_id = '%s'" , _myAddressStr.c_str ());
509506
510507 auto c = _pool->borrow ();
@@ -656,7 +653,7 @@ void CV2::heartbeat()
656653
657654 try {
658655 pqxx::work w{*c->c };
659- w.exec_params0 (" INSERT INTO controller (id, hostname, last_heartbeat, public_identity, version) VALUES "
656+ w.exec_params0 (" INSERT INTO controllers_ctl (id, hostname, last_heartbeat, public_identity, version) VALUES "
660657 " ($1, $2, TO_TIMESTAMP($3::double precision/1000), $4, $5) "
661658 " ON CONFLICT (id) DO UPDATE SET hostname = EXCLUDED.hostname, last_heartbeat = EXCLUDED.last_heartbeat, "
662659 " public_identity = EXCLUDED.public_identity, version = EXCLUDED.version" ,
@@ -772,24 +769,14 @@ void CV2::commitThread()
772769 // bool isNewMember = (membercount == 0);
773770
774771 pqxx::result res = w.exec_params0 (
775- " INSERT INTO devices (id, version_major, version_minor, version_revision, version_protocol) "
776- " VALUES ($1, $2, $3, $4, $5) ON CONFLICT (id) DO UPDATE SET "
777- " version_major = EXCLUDED.version_major, version_minor = EXCLUDED.version_minor, "
778- " version_revision = EXCLUDED.version_revision, version_protocol = EXCLUDED.version_protocol " ,
779- memberId,
780- (int )config[" vMajor" ],
781- (int )config[" vMinor" ],
782- (int )config[" vRev" ],
783- (int )config[" vProto" ]);
784-
785- res = w.exec_params0 (
786- " INSERT INTO device_networks (device_id, network_id, authorized, active_bridge, ip_assignments, "
772+ " INSERT INTO network_memberships_ctl (device_id, network_id, authorized, active_bridge, ip_assignments, "
787773 " no_auto_assign_ips, sso_exempt, authentication_expiry_time, capabilities, creation_time, "
788774 " identity, last_authorized_credential, last_authorized_time, last_deauthorized_time, "
789- " remote_trace_level, remote_trace_target, revision, tags) "
775+ " remote_trace_level, remote_trace_target, revision, tags, version_major, version_minor, "
776+ " version_revision, version_protocol) "
790777 " VALUES ($1, $2, $3, $4, $5, $6, $7, TO_TIMESTAMP($8::double precision/1000), $9, "
791778 " TO_TIMESTAMP($10::double precision/1000), $11, 12, TO_TIMESTAMP($13::double precision/1000), "
792- " TO_TIMESTAMP($14::double precision/1000), $15, $16, $17, $18) "
779+ " TO_TIMESTAMP($14::double precision/1000), $15, $16, $17, $18, $19, $20, $21, $22 ) "
793780 " ON CONFLICT (device_id, network_id) DO UPDATE SET "
794781 " authorized = EXCLUDED.authorized, active_bridge = EXCLUDED.active_bridge, "
795782 " ip_assignments = EXCLUDED.ip_assignments, no_auto_assign_ips = EXCLUDED.no_auto_assign_ips, "
@@ -798,7 +785,9 @@ void CV2::commitThread()
798785 " identity = EXCLUDED.identity, last_authorized_credential = EXCLUDED.last_authorized_credential, "
799786 " last_authorized_time = EXCLUDED.last_authorized_time, last_deauthorized_time = EXCLUDED.last_deauthorized_time, "
800787 " remote_trace_level = EXCLUDED.remote_trace_level, remote_trace_target = EXCLUDED.remote_trace_target, "
801- " revision = EXCLUDED.revision, tags = EXCLUDED.tags" ,
788+ " revision = EXCLUDED.revision, tags = EXCLUDED.tags, version_major = EXCLUDED.version_major, "
789+ " version_minor = EXCLUDED.version_minor, version_revision = EXCLUDED.version_revision, "
790+ " version_protocol = EXCLUDED.version_protocol" ,
802791 memberId,
803792 networkId,
804793 (bool )config[" authorized" ],
@@ -816,7 +805,11 @@ void CV2::commitThread()
816805 (int )config[" remoteTraceLevel" ],
817806 target,
818807 (uint64_t )config[" revision" ],
819- OSUtils::jsonDump (config[" tags" ], -1 ));
808+ OSUtils::jsonDump (config[" tags" ], -1 ),
809+ (int )config[" vMajor" ],
810+ (int )config[" vMinor" ],
811+ (int )config[" vRev" ],
812+ (int )config[" vProto" ]);
820813
821814 w.commit ();
822815
@@ -862,13 +855,18 @@ void CV2::commitThread()
862855 pqxx::work w (*c->c );
863856
864857 std::string id = config[" id" ];
865-
858+
866859 // network must already exist
867860 pqxx::result res = w.exec_params0 (
868- " UPDATE networks SET configuration = $1, revision = $2 WHERE id = $3" ,
861+ " INSERT INTO networks_ctl (id, name, configuration, controller_id, revision) "
862+ " VALUES ($1, $2, $3, $4, $5) "
863+ " ON CONFLICT (id) DO UPDATE SET "
864+ " name = EXCLUDED.name, configuration = EXCLUDED.configuration, revision = EXCLUDED.revision+1" ,
865+ id,
866+ OSUtils::jsonString (config[" name" ], " " ),
869867 OSUtils::jsonDump (config, -1 ),
870- ( uint64_t )config[ " revision " ] ,
871- id
868+ _myAddressStr ,
869+ (( uint64_t )config[ " revision " ])
872870 );
873871
874872 w.commit ();
@@ -891,33 +889,30 @@ void CV2::commitThread()
891889 // fprintf(stderr, "%s: commitThread: delete network\n", _myAddressStr.c_str());
892890 try {
893891 // don't think we need this. Deletion handled by CV2 API
892+
893+ pqxx::work w (*c->c );
894+ std::string networkId = config[" id" ];
895+
896+ w.exec_params0 (" DELETE FROM network_memberships_ctl WHERE network_id = $1" , networkId);
897+ w.exec_params0 (" DELETE FROM networks_ctl WHERE id = $1" , networkId);
894898
895- // pqxx::work w(*c->c);
896-
897- // std::string networkId = config["nwid"];
898-
899- // pqxx::result res = w.exec_params0("UPDATE ztc_network SET deleted = true WHERE id = $1",
900- // networkId);
901-
902- // w.commit();
899+ w.commit ();
903900 } catch (std::exception &e) {
904901 fprintf (stderr, " %s ERROR: Error deleting network: %s\n " , _myAddressStr.c_str (), e.what ());
905902 }
906903 } else if (objtype == " _delete_member" ) {
907904 // fprintf(stderr, "%s commitThread: delete member\n", _myAddressStr.c_str());
908905 try {
909- // don't think we need this. Deletion handled by CV2 API
910-
911- // pqxx::work w(*c->c);
906+ pqxx::work w (*c->c );
912907
913- // std::string memberId = config["id"];
914- // std::string networkId = config["nwid"];
908+ std::string memberId = config[" id" ];
909+ std::string networkId = config[" nwid" ];
915910
916- // pqxx::result res = w.exec_params0(
917- // "UPDATE ztc_member SET hidden = true, deleted = true WHERE id = $1 AND network_id = $2",
918- // memberId, networkId);
911+ pqxx::result res = w.exec_params0 (
912+ " DELETE FROM network_memberships_ctl WHERE device_id = $1 AND network_id = $2" ,
913+ memberId, networkId);
919914
920- // w.commit();
915+ w.commit ();
921916 } catch (std::exception &e) {
922917 fprintf (stderr, " %s ERROR: Error deleting member: %s\n " , _myAddressStr.c_str (), e.what ());
923918 }
0 commit comments