nc_trans.cc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. // Copyright (C) 2013 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <d2/d2_log.h>
  15. #include <d2/nc_trans.h>
  16. namespace isc {
  17. namespace d2 {
  18. // Common transaction states
  19. const int NameChangeTransaction::READY_ST;
  20. const int NameChangeTransaction::SELECTING_FWD_SERVER_ST;
  21. const int NameChangeTransaction::SELECTING_REV_SERVER_ST;
  22. const int NameChangeTransaction::PROCESS_TRANS_OK_ST;
  23. const int NameChangeTransaction::PROCESS_TRANS_FAILED_ST;
  24. const int NameChangeTransaction::NCT_DERIVED_STATE_MIN;
  25. // Common transaction events
  26. const int NameChangeTransaction::SELECT_SERVER_EVT;
  27. const int NameChangeTransaction::SERVER_SELECTED_EVT;
  28. const int NameChangeTransaction::SERVER_IO_ERROR_EVT;
  29. const int NameChangeTransaction::NO_MORE_SERVERS_EVT;
  30. const int NameChangeTransaction::IO_COMPLETED_EVT;
  31. const int NameChangeTransaction::UPDATE_OK_EVT;
  32. const int NameChangeTransaction::UPDATE_FAILED_EVT;
  33. const int NameChangeTransaction::NCT_DERIVED_EVENT_MIN;
  34. NameChangeTransaction::
  35. NameChangeTransaction(isc::asiolink::IOService& io_service,
  36. dhcp_ddns::NameChangeRequestPtr& ncr,
  37. DdnsDomainPtr& forward_domain,
  38. DdnsDomainPtr& reverse_domain)
  39. : io_service_(io_service), ncr_(ncr), forward_domain_(forward_domain),
  40. reverse_domain_(reverse_domain), dns_client_(),
  41. dns_update_status_(DNSClient::OTHER), dns_update_response_(),
  42. forward_change_completed_(false), reverse_change_completed_(false),
  43. current_server_list_(), current_server_(), next_server_pos_(0) {
  44. if (!ncr_) {
  45. isc_throw(NameChangeTransactionError, "NameChangeRequest cannot null");
  46. }
  47. if (ncr_->isForwardChange() && !(forward_domain_)) {
  48. isc_throw(NameChangeTransactionError,
  49. "Forward change must have a forward domain");
  50. }
  51. if (ncr_->isReverseChange() && !(reverse_domain_)) {
  52. isc_throw(NameChangeTransactionError,
  53. "Reverse change must have a reverse domain");
  54. }
  55. }
  56. NameChangeTransaction::~NameChangeTransaction(){
  57. }
  58. void
  59. NameChangeTransaction::startTransaction() {
  60. startModel(READY_ST);
  61. }
  62. void
  63. NameChangeTransaction::operator()(DNSClient::Status status) {
  64. // Stow the completion status and re-enter the run loop with the event
  65. // set to indicate IO completed.
  66. // runModel is exception safe so we are good to call it here.
  67. // It won't exit until we hit the next IO wait or the state model ends.
  68. setDnsUpdateStatus(status);
  69. runModel(IO_COMPLETED_EVT);
  70. }
  71. void
  72. NameChangeTransaction::defineEvents() {
  73. // Call superclass impl first.
  74. StateModel::defineEvents();
  75. // Define NCT events.
  76. defineEvent(SELECT_SERVER_EVT, "SELECT_SERVER_EVT");
  77. defineEvent(SERVER_SELECTED_EVT, "SERVER_SELECTED_EVT");
  78. defineEvent(SERVER_IO_ERROR_EVT, "SERVER_IO_ERROR_EVT");
  79. defineEvent(NO_MORE_SERVERS_EVT, "NO_MORE_SERVERS_EVT");
  80. defineEvent(IO_COMPLETED_EVT, "IO_COMPLETED_EVT");
  81. defineEvent(UPDATE_OK_EVT, "UPDATE_OK_EVT");
  82. defineEvent(UPDATE_FAILED_EVT, "UPDATE_FAILED_EVT");
  83. }
  84. void
  85. NameChangeTransaction::verifyEvents() {
  86. // Call superclass impl first.
  87. StateModel::verifyEvents();
  88. // Verify NCT events.
  89. getEvent(SELECT_SERVER_EVT);
  90. getEvent(SERVER_SELECTED_EVT);
  91. getEvent(SERVER_IO_ERROR_EVT);
  92. getEvent(NO_MORE_SERVERS_EVT);
  93. getEvent(IO_COMPLETED_EVT);
  94. getEvent(UPDATE_OK_EVT);
  95. getEvent(UPDATE_FAILED_EVT);
  96. }
  97. void
  98. NameChangeTransaction::defineStates() {
  99. // Call superclass impl first.
  100. StateModel::defineStates();
  101. // This class is "abstract" in that it does not supply handlers for its
  102. // states, derivations must do that therefore they must define them.
  103. }
  104. void
  105. NameChangeTransaction::verifyStates() {
  106. // Call superclass impl first.
  107. StateModel::verifyStates();
  108. // Verify NCT states. This ensures that derivations provide the handlers.
  109. getState(READY_ST);
  110. getState(SELECTING_FWD_SERVER_ST);
  111. getState(SELECTING_REV_SERVER_ST);
  112. getState(PROCESS_TRANS_OK_ST);
  113. getState(PROCESS_TRANS_FAILED_ST);
  114. }
  115. void
  116. NameChangeTransaction::onModelFailure(const std::string& explanation) {
  117. setNcrStatus(dhcp_ddns::ST_FAILED);
  118. LOG_ERROR(dctl_logger, DHCP_DDNS_STATE_MODEL_UNEXPECTED_ERROR)
  119. .arg(explanation);
  120. }
  121. void
  122. NameChangeTransaction::setDnsUpdateStatus(const DNSClient::Status& status) {
  123. dns_update_status_ = status;
  124. }
  125. void
  126. NameChangeTransaction::setForwardChangeCompleted(const bool value) {
  127. forward_change_completed_ = value;
  128. }
  129. void
  130. NameChangeTransaction::setReverseChangeCompleted(const bool value) {
  131. reverse_change_completed_ = value;
  132. }
  133. const dhcp_ddns::NameChangeRequestPtr&
  134. NameChangeTransaction::getNcr() const {
  135. return (ncr_);
  136. }
  137. const TransactionKey&
  138. NameChangeTransaction::getTransactionKey() const {
  139. return (ncr_->getDhcid());
  140. }
  141. dhcp_ddns::NameChangeStatus
  142. NameChangeTransaction::getNcrStatus() const {
  143. return (ncr_->getStatus());
  144. }
  145. DdnsDomainPtr&
  146. NameChangeTransaction::getForwardDomain() {
  147. return (forward_domain_);
  148. }
  149. DdnsDomainPtr&
  150. NameChangeTransaction::getReverseDomain() {
  151. return (reverse_domain_);
  152. }
  153. void
  154. NameChangeTransaction::initServerSelection(const DdnsDomainPtr& domain) {
  155. if (!domain) {
  156. isc_throw(NameChangeTransactionError,
  157. "initServerSelection called with an empty domain");
  158. }
  159. current_server_list_ = domain->getServers();
  160. next_server_pos_ = 0;
  161. current_server_.reset();
  162. }
  163. bool
  164. NameChangeTransaction::selectNextServer() {
  165. if ((current_server_list_) &&
  166. (next_server_pos_ < current_server_list_->size())) {
  167. current_server_ = (*current_server_list_)[next_server_pos_];
  168. dns_update_response_.reset(new
  169. D2UpdateMessage(D2UpdateMessage::INBOUND));
  170. // @todo Protocol is set on DNSClient constructor. We need
  171. // to propagate a configuration value downward, probably starting
  172. // at global, then domain, then server
  173. // Once that is supported we need to add it here.
  174. dns_client_.reset(new DNSClient(dns_update_response_ , this,
  175. DNSClient::UDP));
  176. ++next_server_pos_;
  177. return (true);
  178. }
  179. return (false);
  180. }
  181. const DNSClientPtr&
  182. NameChangeTransaction::getDNSClient() const {
  183. return (dns_client_);
  184. }
  185. const DnsServerInfoPtr&
  186. NameChangeTransaction::getCurrentServer() const {
  187. return (current_server_);
  188. }
  189. void
  190. NameChangeTransaction::setNcrStatus(const dhcp_ddns::NameChangeStatus& status) {
  191. return (ncr_->setStatus(status));
  192. }
  193. DNSClient::Status
  194. NameChangeTransaction::getDnsUpdateStatus() const {
  195. return (dns_update_status_);
  196. }
  197. const D2UpdateMessagePtr&
  198. NameChangeTransaction::getDnsUpdateResponse() const {
  199. return (dns_update_response_);
  200. }
  201. bool
  202. NameChangeTransaction::getForwardChangeCompleted() const {
  203. return (forward_change_completed_);
  204. }
  205. bool
  206. NameChangeTransaction::getReverseChangeCompleted() const {
  207. return (reverse_change_completed_);
  208. }
  209. } // namespace isc::d2
  210. } // namespace isc