lfc_controller.h 6.83 KB
Newer Older
1
// Copyright (C) 2015,2017 Internet Systems Consortium, Inc. ("ISC")
2
//
3 4 5
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6

7 8
#ifndef LFC_CONTROLLER_H
#define LFC_CONTROLLER_H
9 10

#include <exceptions/exceptions.h>
11
#include <string>
12 13 14 15 16 17 18 19 20 21 22

namespace isc {
namespace lfc {

/// @brief Exception thrown when the command line is invalid.
class InvalidUsage : public isc::Exception {
public:
    InvalidUsage(const char* file, size_t line, const char* what) :
        isc::Exception(file, line, what) { };
};

23 24
/// @brief Exceptions thrown when a method is unable to manipulate
/// (remove or rename) a file.
25 26 27 28 29 30
class RunTimeFail : public isc::Exception {
public:
    RunTimeFail(const char* file, size_t line, const char* what) :
        isc::Exception(file, line, what) { };
};

31
/// @brief Process controller for LFC process
32 33
///
/// This class provides the LFC process functions. These are used to:
34 35 36 37
/// manage the command line, check for already running instances,
/// invoke the code to process the lease files and finally to rename
/// the lease files as necessary.
class LFCController {
38 39 40 41 42 43 44 45 46 47
public:
    /// @brief Defines the application name, it may be used to locate
    /// configuration data and appears in log statements.
    static const char* lfc_app_name_;

    /// @brief Defines the executable name, by convention this should match
    /// the executable name.
    static const char* lfc_bin_name_;

    /// @brief Constructor
48
    LFCController();
49 50

    /// @brief Destructor
51
    ~LFCController();
52 53

    /// @brief Acts as the primary entry point to start execution
54 55
    /// of the process.
    ///
56
    /// Provides the control logic to combine two lease files and
57
    /// weed out duplicate and expired leases. A description of
58
    /// the design can be found at http://oldkea.isc.org/wiki/LFCDesign
59
    ///
60
    /// -# parse command line arguments
61 62
    /// -# verify that it is the only instance
    /// -# create pid file
63 64 65 66
    /// -# read leases files
    /// -# write lease file
    /// -# move leases files
    /// -# cleanup artifacts
67
    /// -# remove pid file
68
    /// -# exit to the caller
69 70 71
    ///
    /// @param argc Number of strings in the @c argv array.
    /// @param argv Array of arguments passed in via the program's main function.
72 73 74 75
    /// @param test_mode is a bool value which indicates if @c launch
    /// should be run in the test mode (if true).  This parameter doesn't
    /// have a default value to force test implementers to enable test
    /// mode explicitly.
76 77
    ///
    /// @throw InvalidUsage if the command line parameters are invalid.
78
    void launch(int argc, char* argv[], const bool test_mode);
79

80 81 82
    /// @brief Process the command line arguments.
    ///
    /// It is the first step taken after the process has been launched.
83 84 85 86 87
    ///
    /// @param argc Number of strings in the @c argv array.
    /// @param argv Array of arguments passed in via the program's main function.
    ///
    /// @throw InvalidUsage if the command line parameters are invalid.
88 89
    void parseArgs(int argc, char* argv[]);

90 91 92 93 94
    /// @brief Rotate files.
    ///
    /// After we have a finish file, either from doing the cleanup or because
    /// a previous instance was interrupted, delete the work files (previous
    /// & copy) and move the finish file to be the new previous file.
95
    ///
96 97
    /// @throw RunTimeFail if we can't manipulate the files.
    void fileRotate() const;
98

99 100
    /// @name Accessor methods mainly used for testing purposes
    //@{
101

Josh Soref's avatar
Josh Soref committed
102
    /// @brief Gets the protocol version of the leases files
103
    ///
104 105 106
    /// @return Returns the value of the DHCP protocol version.
    /// This can be 4 or 6 while in use and 0 before parsing
    /// any arguments.
107
    int getProtocolVersion() const {
108 109 110 111 112
      return (protocol_version_);
    }

    /// @brief Gets the config file name
    ///
113
    /// @return Returns the path to the config file
114 115 116 117
    std::string getConfigFile() const {
        return (config_file_);
    }

118
    /// @brief Gets the previous file name
119
    ///
120
    /// @return Returns the path to the previous file
121 122 123 124 125 126
    std::string getPreviousFile() const {
        return (previous_file_);
    }

    /// @brief Gets the copy file name
    ///
127
    /// @return Returns the path to the copy file
128 129 130 131 132 133
    std::string getCopyFile() const {
        return (copy_file_);
    }

    /// @brief Gets the output file name
    ///
134
    /// @return Returns the path to the output file
135 136 137 138 139 140
    std::string getOutputFile() const {
        return (output_file_);
    }

    /// @brief Gets the finish file name
    ///
141
    /// @return Returns the path to the finish file
142 143 144 145 146 147
    std::string getFinishFile() const {
        return (finish_file_);
    }

    /// @brief Gets the pid file name
    ///
148
    /// @return Returns the path to the pid file
149 150 151
    std::string getPidFile() const {
        return (pid_file_);
    }
152
    //@}
153

154
private:
155
    /// Version of the DHCP protocol used, i.e. 4 or 6.
156
    int protocol_version_;
157
    /// When true output the result of parsing the command line
158
    bool verbose_;
159 160 161 162 163 164
    std::string config_file_;   ///< The path to the config file
    std::string previous_file_; ///< The path to the previous LFC file (if any)
    std::string copy_file_;     ///< The path to the copy of the lease file
    std::string output_file_;   ///< The path to the output file
    std::string finish_file_;   ///< The path to the finished output file
    std::string pid_file_;      ///< The path to the pid file
165 166 167 168 169 170 171 172 173 174 175 176 177

    /// @brief Prints the program usage text to std error.
    ///
    /// @param text is a string message which will preceded the usage text.
    /// This is intended to be used for specific usage violation messages.
    void usage(const std::string& text);

    /// @brief Gets the Kea version number for printing
    ///
    /// @param extended is a boolean indicating if the version string
    /// should be short (false) or extended (true)
    std::string getVersion(const bool extended) const;

178 179 180 181 182
    /// @brief Process files.
    ///
    /// Read in the leases from any previous & copy files we have and
    /// write the results out to the output file.  Upon completion of
    /// the write move the file to the finish file.
183 184 185 186 187 188 189 190
    ///
    /// @tparam LeaseObjectType A @c Lease4 or @c Lease6.
    /// @tparam LeaseFileType A @c CSVLeaseFile4 or @c CSVLeaseFile6.
    /// @tparam StorageType A @c Lease4Storage or @c Lease6Storage.
    ///
    /// @throw RunTimeFail if we can't move the file.
    template<typename LeaseObjectType, typename LeaseFileType, typename StorageType>
    void processLeases() const;
Shawn Routhier's avatar
Shawn Routhier committed
191 192 193 194 195 196

    ///@brief Start up the logging system
    ///
    /// @param test_mode indicates if we have have been started from the test
    /// system (true) or are running normally (false)
    void startLogger(const bool test_mode) const;
197 198
};

199
}; // namespace isc::lfc
200 201
}; // namespace isc

202
#endif // LFC_CONTROLLER_H