# Path: netl2api/lib/config.py
# class RedisClient(object):
#     # TODO: move-me to a more appropriate library file
#     def __init__(self, db=7, timeout=3):
#         cfg       = get_netl2server_cfg()
#         self.host = cfg.get("redis", "host")
#         self.port = cfg.getint("redis", "port")
#         self.timeout = timeout
#         self.db      = db
#         self._redis  = redis.Redis(host=self.host, port=self.port, db=self.db,
#                                    socket_timeout=self.timeout)
# 
#     def get_connection(self):
#         if self._redis is None or not self._redis.ping():
#             self._redis = redis.Redis(host=self.host, port=self.port, db=self.db,
#                                       socket_timeout=self.timeout)
#         return self._redis
# 
# def get_netl2server_cfg():
#     return get_cfg("netl2server", check_permission=None)
# 
# def setup_netl2server_logger(cfg):
#     return setup_logger(cfg, "netl2server")
# 
# def setup_persistence_ctrl_logger(cfg):
#     return setup_logger(cfg, "netl2server.job.switch_cfg_persistence")


from netl2api.lib.config import RedisClient, get_netl2server_cfg, setup_netl2server_logger, setup_persistence_ctrl_logger
#    distributed under the License is distributed on an "AS IS" BASIS,
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#    See the License for the specific language governing permissions and
#    limitations under the License.
#
# @author: Eduardo S. Scarpellini
# @author: Luiz Ozaki


__copyright__ = "Copyright 2012, Locaweb IDC"




__all__ = ["defer_save_switch_cfg", "acquire_persistence_lock", "list_pending_persistence_jobs",
           "finish_persistence_job"]


cfg                 = get_netl2server_cfg()
logger_netl2server  = setup_netl2server_logger(cfg)
logger_persist_ctrl = setup_persistence_ctrl_logger(cfg)
redis_cli           = RedisClient()


def defer_save_switch_cfg(device=None):
    try:
        sw_persist_ctrl_db = redis_cli.get_connection()
    except Exception, e:
        logger_netl2server.exception("Error in Redis connection (persistence control database)")
        return

# Path: netl2api/lib/config.py
# class RedisClient(object):
#     # TODO: move-me to a more appropriate library file
#     def __init__(self, db=7, timeout=3):
#         cfg       = get_netl2server_cfg()
#         self.host = cfg.get("redis", "host")
#         self.port = cfg.getint("redis", "port")
#         self.timeout = timeout
#         self.db      = db
#         self._redis  = redis.Redis(host=self.host, port=self.port, db=self.db,
#                                    socket_timeout=self.timeout)
# 
#     def get_connection(self):
#         if self._redis is None or not self._redis.ping():
#             self._redis = redis.Redis(host=self.host, port=self.port, db=self.db,
#                                       socket_timeout=self.timeout)
#         return self._redis
# 
# def get_netl2server_cfg():
#     return get_cfg("netl2server", check_permission=None)
# 
# def setup_netl2server_logger(cfg):
#     return setup_logger(cfg, "netl2server")
# 
# def setup_persistence_ctrl_logger(cfg):
#     return setup_logger(cfg, "netl2server.job.switch_cfg_persistence")


# Path: netl2api/server/workers/switch_cfg_persistence_utils.py
from netl2api.lib.config import RedisClient, get_netl2server_cfg, setup_netl2server_logger, setup_persistence_ctrl_logger

#    distributed under the License is distributed on an "AS IS" BASIS,
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#    See the License for the specific language governing permissions and
#    limitations under the License.
#
# @author: Eduardo S. Scarpellini
# @author: Luiz Ozaki


__copyright__ = "Copyright 2012, Locaweb IDC"




__all__ = ["defer_save_switch_cfg", "acquire_persistence_lock", "list_pending_persistence_jobs",
           "finish_persistence_job"]


cfg                 = get_netl2server_cfg()
logger_netl2server  = setup_netl2server_logger(cfg)
logger_persist_ctrl = setup_persistence_ctrl_logger(cfg)
redis_cli           = RedisClient()


def defer_save_switch_cfg(device=None):
    try:
        sw_persist_ctrl_db = redis_cli.get_connection()
    except Exception, e:
        logger_netl2server.exception("Error in Redis connection (persistence control database)")
        return

    sw_persist_ctrl_db.incr("rwopts:%s:counter" % device, amount=1)