<commit_before>/* -*- C++ -*-; c-basic-offset: 4; indent-tabs-mode: nil */
/*
 * Implementation for TunnelEpManager class.
 *
 * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */

#include <config.h>
#ifdef HAVE_IFADDRS_H
#include <arpa/inet.h>
#include <ifaddrs.h>
#endif

#include <opflex/modb/Mutator.h>
#include <boost/foreach.hpp>
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>

#include "Agent.h"
#include "TunnelEpManager.h"
#include "logging.h"

namespace ovsagent {

using std::string;
using opflex::modb::URI;
using opflex::modb::Mutator;
using boost::optional;
using boost::shared_ptr;
using boost::unique_lock;
using boost::mutex;
using boost::uuids::to_string;
using boost::uuids::random_generator;
using boost::asio::deadline_timer;
using boost::asio::placeholders::error;
using boost::posix_time::milliseconds;
using boost::bind;
using boost::system::error_code;

TunnelEpManager::TunnelEpManager(Agent* agent_, long timer_interval_)
    : agent(agent_), 
      agent_io(agent_->getAgentIOService()), 
      timer_interval(timer_interval_), stopping(false),
      tunnelEpUUID(to_string(random_generator()())) {

}

TunnelEpManager::~TunnelEpManager() {

}

void TunnelEpManager::start() {
    LOG(DEBUG) << "Starting tunnel endpoint manager";
    stopping = false;

    Mutator mutator(agent->getFramework(), "init");
    optional<shared_ptr<modelgbp::dmtree::Root> > root = 
        modelgbp::dmtree::Root::resolve(agent->getFramework(), URI::ROOT);
    if (root)
        root.get()->addGbpeTunnelEpUniverse();
    mutator.commit();

#ifdef HAVE_IFADDRS_H
    timer.reset(new deadline_timer(agent_io, milliseconds(0)));
    timer->async_wait(bind(&TunnelEpManager::on_timer, this, error));
#else<commit_msg>Fix uninitialized-memory-read errors reported by valgrind<commit_after>/* -*- C++ -*-; c-basic-offset: 4; indent-tabs-mode: nil */
/*
 * Implementation for TunnelEpManager class.
 *
 * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */

#include <config.h>
#ifdef HAVE_IFADDRS_H
#include <arpa/inet.h>
#include <ifaddrs.h>
#endif
#include <fstream>

#include <opflex/modb/Mutator.h>
#include <boost/foreach.hpp>
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>

#include "Agent.h"
#include "TunnelEpManager.h"
#include "logging.h"

namespace ovsagent {

using std::string;
using opflex::modb::URI;
using opflex::modb::Mutator;
using boost::optional;
using boost::shared_ptr;
using boost::unique_lock;
using boost::mutex;
using boost::uuids::to_string;
using boost::uuids::random_generator;
using boost::asio::deadline_timer;
using boost::asio::placeholders::error;
using boost::posix_time::milliseconds;
using boost::bind;
using boost::system::error_code;

TunnelEpManager::TunnelEpManager(Agent* agent_, long timer_interval_)
    : agent(agent_), 
      agent_io(agent_->getAgentIOService()), 
      timer_interval(timer_interval_), stopping(false) {

    boost::uuids::uuid tuuid;
    std::ifstream rndfile("/dev/urandom", std::ifstream::binary);
    if (!rndfile.is_open() ||
        !rndfile.read((char *)&tuuid, tuuid.size()).good()) {
        tuuid = random_generator()();       // use boost RNG as fallback
    }
    tunnelEpUUID = to_string(tuuid);
}

TunnelEpManager::~TunnelEpManager() {

}

void TunnelEpManager::start() {
    LOG(DEBUG) << "Starting tunnel endpoint manager";
    stopping = false;

    Mutator mutator(agent->getFramework(), "init");
    optional<shared_ptr<modelgbp::dmtree::Root> > root = 
        modelgbp::dmtree::Root::resolve(agent->getFramework(), URI::ROOT);
    if (root)
        root.get()->addGbpeTunnelEpUniverse();
    mutator.commit();

#ifdef HAVE_IFADDRS_H
    timer.reset(new deadline_timer(agent_io, milliseconds(0)));
    timer->async_wait(bind(&TunnelEpManager::on_timer, this, error));
#else
{'fraction_non_alphanumeric': 0.146374829001368, 'fraction_numerical': 0.004559963520291838, 'mean_word_length': 5.249287749287749, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5195374', 'n_tokens_mistral': 1484, 'n_tokens_neox': 1438, 'n_words': 308}