text:
_LIBCPP_BEGIN_NAMESPACE_LFTS_PMR

// memory_resource

//memory_resource::~memory_resource() {}

// new_delete_resource()

class _LIBCPP_TYPE_VIS __new_delete_memory_resource_imp
    : public memory_resource
{
public:
    ~__new_delete_memory_resource_imp() = default;

protected:
    virtual void* do_allocate(size_t __size, size_t __align)
        { return __allocate(__size); }

    virtual void do_deallocate(void * __p, size_t, size_t)
        { _VSTD::__libcpp_deallocate(__p); }

    virtual bool do_is_equal(memory_resource const & __other) const _NOEXCEPT
        { return &__other == this; }
};

// null_memory_resource()

class _LIBCPP_TYPE_VIS __null_memory_resource_imp
    : public memory_resource
{
public:
    ~__null_memory_resource_imp() = default;

protected:
    virtual void* do_allocate(size_t, size_t) {
        __throw_bad_alloc();
    }
    virtual void do_deallocate(void *, size_t, size_t) {}
    virtual bool do_is_equal(memory_resource const & __other) const _NOEXCEPT
    { return &__other == this; }
};

namespace {

union ResourceInitHelper {
  struct {
    __new_delete_memory_resource_imp new_delete_res;
    __null_memory_resource_imp       null_res;
  } resources;
  char dummy;
  _LIBCPP_CONSTEXPR_AFTER_CXX11 ResourceInitHelper() : resources() {}
  ~ResourceInitHelper() {}
};
// When compiled in C++14 this initialization should be a constant expression.
// Only in C++11 is "init_priority" needed to ensure initialization order.
#if _LIBCPP_STD_VER > 11
_LIBCPP_SAFE_STATIC
#endif
ResourceInitHelper res_init  __attribute__((init_priority (101)));

} // end namespace

memory_resource * new_delete_resource() _NOEXCEPT {
    return &res_init.resources.new_delete_res;
}

memory_resource * null_memory_resource() _NOEXCEPT {
    return &res_init.resources.null_res;
}

// default_memory_resource()

static memory_resource *
__default_memory_resource(bool set = false, memory_resource * new_res = nullptr) _NOEXCEPT
{
#ifndef _LIBCPP_HAS_NO_ATOMIC_HEADER
    _LIBCPP_SAFE_STATIC static atomic<memory_resource*> __res =
        ATOMIC_VAR_INIT(&res_init.resources.new_delete_res);
    if (set) {
        new_res = new_res ? new_res : new_delete_resource();
        // TODO: Can a weaker ordering be used?
        return _VSTD::atomic_exchange_explicit(
            &__res, new_res, memory_order::memory_order_acq_rel);
    }
    else {
        return _VSTD::atomic_load_explicit(
            &__res, memory_order::memory_order_acquire);
    }
#elif !defined(_LIBCPP_HAS_NO_THREADS)
    _LIBCPP_SAFE_STATIC static memory_resource * res = &res_init.resources.new_delete_res;
    static mutex res_lock;
    if (set) {
        new_res = new_res ? new_res : new_delete_resource();
        lock_guard<mutex> guard(res_lock);
        memory_resource * old_res = res;
        res = new_res;
        return old_res;
    } else {
        lock_guard<mutex> guard(res_lock);
        return res;
    }
#else
    _LIBCPP_SAFE_STATIC static memory_resource* res = &res_init.resources.new_delete_res;
    if (set) {
        new_res = new_res ? new_res : new_delete_resource();
        memory_resource * old_res = res;
        res = new_res;
        return old_res;
    } else {
        return res;
    }
#endif
}

memory_resource * get_default_resource() _NOEXCEPT
{
    return __default_memory_resource();
}

memory_resource * set_default_resource(memory_resource * __new_res) _NOEXCEPT
{
    return __default_memory_resource(true, __new_res);
}

_LIBCPP_END_NAMESPACE_LFTS_PMR


text:
#include "../../Source/stdafx.h"
#include "../../Source/Variable.h"
#include "../../Source/iVariableManager.h"
#include "../../Source/values/String.h"
#include "../../Source/values/Int.h"
#include "../../Source/values/Void.h"

using namespace Swift;

class TestVariable : public CPPUNIT_NS::TestFixture {
public:
  CPPUNIT_TEST_SUITE(TestVariable);

  CPPUNIT_TEST(testGetSetValue);

  CPPUNIT_TEST_SUITE_END();

public:
  void setUp() { }
  void tearDown() { }

protected:
  void testGetSetValue() {
    iVariableManager* vm = new iVariableManager;
    vm->addVariable("day", "today");

    Variable* var = new Variable("day", vm);
    Variable* var2 = new Variable("time", vm);
    Variable* var3 = new Variable("year", vm);

    CPPUNIT_ASSERT(var->getValue() >> String() == "today");
    CPPUNIT_ASSERT(var2->getValue()->getID() == Values::Void::id);

    vm->addVariable("time", "6:00pm");

    CPPUNIT_ASSERT(var2->getValue() >> String() == "6:00pm");

    var->setValue(1050);

    CPPUNIT_ASSERT(var->getValue() >> int() == 1050);

    var3->setValue("2002");

    CPPUNIT_ASSERT(var3->getValue() >> String() == "2002");

    CPPUNIT_ASSERT(vm->hasVariable("year"));

    delete var;
    delete var2;
    delete var3;
    delete vm;
  }
};

CPPUNIT_TEST_SUITE_REGISTRATION(TestVariable);

redpajama_set_name: RedPajamaGithub


text:
/* jshint -W053 */

var assert = require('assert');

var R = require('..');

describe('is', function() {
    it('works with built-in types', function() {
        assert.strictEqual(R.is(Array, []), true);
        assert.strictEqual(R.is(Boolean, new Boolean(false)), true);
        assert.strictEqual(R.is(Date, new Date()), true);
        assert.strictEqual(R.is(Function, function() {}), true);
        assert.strictEqual(R.is(Number, new Number(0)), true);
        assert.strictEqual(R.is(Object, {}), true);
        assert.strictEqual(R.is(RegExp, /(?:)/), true);
        assert.strictEqual(R.is(String, new String('')), true);
    });

    it('works with user-defined types', function() {
        function Foo() {}
        function Bar() {}
        Bar.prototype = new Foo();

        var foo = new Foo();
        var bar = new Bar();

        assert.strictEqual(R.is(Foo, foo), true);
        assert.strictEqual(R.is(Bar, bar), true);
        assert.strictEqual(R.is(Foo, bar), true);
        assert.strictEqual(R.is(Bar, foo), false);
    });

    it('is curried', function() {
        var isArray = R.is(Array);
        assert.strictEqual(isArray([]), true);
        assert.strictEqual(isArray({}), false);
    });

    it('considers almost everything an object', function() {
        function Foo() {}
        var foo = new Foo();
        var isObject = R.is(Object);

        assert.strictEqual(isObject(foo), true);
        assert.strictEqual(isObject(function() { return arguments; }()), true);
        assert.strictEqual(isObject([]), true);
        assert.strictEqual(isObject(new Boolean(false)), true);
        assert.strictEqual(isObject(new Date()), true);
        assert.strictEqual(isObject(function() {}), true);
        assert.strictEqual(isObject(new Number(0)), true);
        assert.strictEqual(isObject(/(?:)/), true);
        assert.strictEqual(isObject(new String('')), true);

        assert.strictEqual(isObject(null), false);
        assert.strictEqual(isObject(undefined), false);
    });

    it('does not coerce', function() {
        assert.strictEqual(R.is(Boolean, 1), false);
        assert.strictEqual(R.is(Number, '1'), false);
        assert.strictEqual(R.is(Number, false), false);
    });

    it('recognizes primitives as their object equivalents', function() {
        assert.strictEqual(R.is(Boolean, false), true);
        assert.strictEqual(R.is(Number, 0), true);
        assert.strictEqual(R.is(String, ''), true);
    });

    it('does not consider primitives to be instances of Object', function() {
        assert.strictEqual(R.is(Object, false), false);
        assert.strictEqual(R.is(Object, 0), false);
        assert.strictEqual(R.is(Object, ''), false);
    });

    it('is curried', function() {
        assert(typeof R.is(String) === 'function');
        assert(R.is(String)('s'));
    });
});


text:
date: 2008-11-26 00:31:00+00:00
slug: fedora-10
title: Fedora 10 released
wordpress_id: 68
---

Fedora 10 released

I can hardly wait to test it out. Was released at 10 am EST today. Get your copies while they're hot!!

[Fedora torrents](http://torrent.fedoraproject.org/)


text:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class ScrollBackground : MonoBehaviour {

	public float speed = 0;
	public int sortingLayerID;

	// Use this for initialization
	void Start () {

	}

	// Update is called once per frame
	void Update ()
	{
		renderer.material.mainTextureOffset = new Vector2 ((Time.time * speed) % 1, 0f);
		renderer.sortingLayerID = sortingLayerID;
	}
}


text:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.experimental.parallaxstickylistview.app.ParallaxStickyHeaderCollectionFragment">

    <FrameLayout
        android:id="@+id/holycow"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:drawingCacheQuality="high">

        <ImageView
            android:id="@+id/imageview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/pattern"
            android:scaleType="centerCrop"
            android:layout_gravity="top"
            android:drawingCacheQuality="high" />

    </FrameLayout>

    <com.gokhanbarisaker.scrollplugins.views.ObservableScrollView
        android:id="@+id/scrollview"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingTop="200dp">

            <View
                android:id="@+id/parallax_reference"
                android:layout_width="0dp"
                android:layout_height="0dp" />

            <TextView
                android:id="@+id/header_dynamic"
                android:layout_width="match_parent"
                android:layout_height="48dp"
                android:text="HEADER"
                android:textSize="36sp"
                android:gravity="center"
                android:background="@android:color/holo_blue_bright"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/ipsum"
                android:textSize="18sp"
                android:padding="8dp"
                android:textColor="@android:color/primary_text_dark"
                android:background="@android:color/holo_green_dark" />
        </LinearLayout>

    </com.gokhanbarisaker.scrollplugins.views.ObservableScrollView>

    <TextView
        android:id="@+id/header_sticky"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:text="HEADER"
        android:textSize="36sp"
        android:gravity="center"
        android:visibility="gone"
        android:background="@android:color/holo_blue_bright"/>

</FrameLayout>


text:
<resources xmlns:tools="http://schemas.android.com/tools">

    <string name="app_name">FreshdeskSDK</string>
    <string name="mobihelp_activity_title_solution_list">Assistance</string>
    <string name="mobihelp_activity_title_tickets_list">Conversations</string>
    <string name="mobihelp_activity_title_feedback">Commencer une conversation</string>
    <string name="mobihelp_activity_title_feedback_only">Commentaires</string>
    <string name="mobihelp_activity_title_solution_article">Solution</string>
    <string name="mobihelp_label_contact_us">Nous contacter</string>
    <string name="mobihelp_label_contact_us_description_text">Si vous ne trouvez pas ce que vous recherchez, contactez-nous.</string>
    <string name="mobihelp_label_my_conversations">Mes conversations</string>
    <string name="mobihelp_label_support_solutions_list_label">Solutions</string>
    <string name="mobihelp_label_request_review">"Cliquez ici pour donner votre avis sur l'application dans le Play Store"</string>
    <string name="mobihelp_autoreply_message">"Merci pour vos commentaires. Notre représentant de l'assistance prendra contact avec vous sous peu."</string>
    <string name="mobihelp_default_anon_user_name">Utilisateur Mobihelp</string>

    <!-- UI Element hints -->
    <string name="mobihelp_hint_email">E-mail</string>
    <string name="mobihelp_hint_feedback">"Qu'en pensez-vous ?"</string>
    <string name="mobihelp_hint_name">Nom</string>
    <string name="mobihelp_hint_required_field">(obligatoire)</string>
    <string name="mobihelp_hint_optional_field">(facultatif)</string>
    <string name="mobihelp_hint_enter_text_here">Entrez votre message ici</string>
    <string name="mobihelp_hint_solutions_search_query_hint">Faire une recherche dans notre base de connaissances</string>

    <!-- Error Messages -->
    <string name="mobihelp_error_no_network_connection">Aucune connexion à Internet</string>
    <string name="mobihelp_error_name_not_present">Veuillez saisir votre nom</string>
    <string name="mobihelp_error_email_not_present">Veuillez saisir votre adresse électronique</string>
    <string name="mobihelp_error_email_not_valid">Veuillez saisir une adresse électronique valide</string>
    <string name="mobihelp_error_description_not_present">Veuillez saisir une description de votre problème</string>
    <string name="mobihelp_error_genreic_retry_message">Veuillez réessayer plus tard</string>

    <!-- Menu Items -->
    <string name="mobihelp_menu_item_title_search_solutions">Rechercher</string>
    <string name="mobihelp_menu_item_title_contact_us">Nous contacter</string>
    <string name="mobihelp_menu_item_title_submit">Envoyer</string>

    <!-- Misc -->
    <string name="mobihelp_content_description_send_message">Envoyer</string>
    <string name="mobihelp_content_description_device_screenshot">"Capture d'écran de l'appareil"</string>
    <string name="mobihelp_tickets_list_empty_message">Appuyez ici pour commencer une conversation</string>
    <string name="mobihelp_solutions_list_empty_message">Aucune solution disponible</string>
    <string name="mobihelp_message_ticket_submitted_thank_you">Merci ! Vos commentaires ont bien été envoyés</string>
    <string name="mobihelp_message_request_succeded">Envoi réussi</string>
    <string name="mobihelp_message_request_failed">"Échec de l'envoi. Veuillez essayer plus tard"</string>
    <string name="mobihelp_message_failed_to_load_solution_article">Impossible de charger cet article</string>
    <string name="mobihelp_message_support_agent">assistance</string>
    <string name="mobihelp_message_pending">envoi en cours...</string>

    <!-- Rating Dialog Title, Text & Button labels -->
    <string name="mobihelp_rating_dialog_text">Si vous aimez cette application, prenez quelques minutes pour la noter. Merci de votre soutien !</string>
    <string name="mobihelp_rating_dialog_title">Noter</string>
    <string name="mobihelp_rating_dialog_rate_button">Noter</string>
    <string name="mobihelp_rating_dialog_not_now_button">Pas maintenant</string>
    <string name="mobihelp_rating_dialog_feedback_button">Commentaires</string>

    <!-- Text used to describe the time each item in the conversation -->
    <string name="mobihelp_time_just_now">il y a quelques instants</string>
    <string name="mobihelp_time_n_mins_ago">il y a {mins} minutes</string>

    <plurals name="mobihelp_time_n_hrs_ago">
        <item quantity="one" tools:ignore="ImpliedQuantity">il y a {hours} heure</item>
        <item quantity="other">il y a {hours} heures</item>
    </plurals>

    <plurals name="mobihelp_time_n_weeks_ago">
        <item quantity="one" tools:ignore="ImpliedQuantity">il y a {weeks} semaine</item>
        <item quantity="other">il y a {weeks} semaines</item>
    </plurals>

    <plurals name="mobihelp_time_n_months_ago">
        <item quantity="one" tools:ignore="ImpliedQuantity">il y a {months} mois</item>
        <item quantity="other">il y a {months} mois</item>
    </plurals>

    <string name="mobihelp_time_a_day_ago">il y a 1 jour</string>
    <string name="mobihelp_time_n_days_ago">il y a {days} jours</string>
    <string name="mobihelp_time_more_than_a_year_ago">"il y a plus d'un an"</string>

</resources>


text:
from neutron.db import models_v2
from neutron_lib.db import model_base
from neutron_lib import exceptions as nexc
from neutron_lib.plugins import directory
from oslo_log import helpers as log
from oslo_utils import uuidutils
import sqlalchemy as sa
from sqlalchemy import orm

from gbpservice._i18n import _
from gbpservice.common import utils as gbp_utils
from gbpservice.neutron.db import api as db_api
from gbpservice.neutron.db.grouppolicy import group_policy_db as gpdb
from gbpservice.neutron.extensions import group_policy as gpolicy
from gbpservice.neutron.services.grouppolicy.common import exceptions
from gbpservice.neutron.services.grouppolicy.common import utils

def get_current_context():
    return gbp_utils.get_current_context()

class AddressScopeUpdateForL3PNotSupported(nexc.BadRequest):
    message = _("Address Scope update for L3 Policy is not supported.")

class PolicyTargetMapping(gpdb.PolicyTarget):
    """Mapping of PolicyTarget to Neutron Port."""
    __table_args__ = {'extend_existing': True}
    __mapper_args__ = {'polymorphic_identity': 'mapping'}
    # REVISIT(ivar): Set null on delete is a temporary workaround until Nova
    # bug 1158684 is fixed.
    port_id = sa.Column(sa.String(36), sa.ForeignKey('ports.id',
                                                     ondelete='SET NULL'),
                        nullable=True, unique=True)

class PTGToSubnetAssociation(model_base.BASEV2):
    """Many to many relation between PolicyTargetGroup and Subnets."""
    __tablename__ = 'gp_ptg_to_subnet_associations'
    policy_target_group_id = sa.Column(
        sa.String(36), sa.ForeignKey('gp_policy_target_groups.id'),
        primary_key=True)
    subnet_id = sa.Column(sa.String(36), sa.ForeignKey('subnets.id'),
                          primary_key=True)

class PolicyTargetGroupMapping(gpdb.PolicyTargetGroup):
    """Mapping of PolicyTargetGroup to set of Neutron Subnets."""
    __table_args__ = {'extend_existing': True}
    __mapper_args__ = {'polymorphic_identity': 'mapping'}
    subnets = orm.relationship(PTGToSubnetAssociation,
                               cascade='all', lazy="joined")

class L2PolicyMapping(gpdb.L2Policy):
    """Mapping of L2Policy to Neutron Network."""
    __table_args__ = {'extend_existing': True}
    __mapper_args__ = {'polymorphic_identity': 'mapping'}
    network_id = sa.Column(sa.String(36), sa.ForeignKey('networks.id'),
                           nullable=True, unique=True)

class L3PolicyRouterAssociation(model_base.BASEV2):
    """Models the many to many relation between L3Policies and Routers."""
    __tablename__ = 'gp_l3_policy_router_associations'
    l3_policy_id = sa.Column(sa.String(36), sa.ForeignKey('gp_l3_policies.id'),
                             primary_key=True)
    router_id = sa.Column(sa.String(36), sa.ForeignKey('routers.id'),
                          primary_key=True)

class L3PolicySubnetpoolV4Association(model_base.BASEV2):
    """Models one to many relation between a L3Policy and v4 Subnetpools."""
    __tablename__ = 'gp_l3_policy_subnetpool_v4_associations'
    l3_policy_id = sa.Column(sa.String(36), sa.ForeignKey('gp_l3_policies.id'),
                             primary_key=True)
    subnetpool_id = sa.Column(
        sa.String(36), sa.ForeignKey('subnetpools.id'), primary_key=True)

class L3PolicySubnetpoolV6Association(model_base.BASEV2):
    """Models one to many relation between a L3Policy and v6 Subnetpools."""
    __tablename__ = 'gp_l3_policy_subnetpool_v6_associations'
    l3_policy_id = sa.Column(sa.String(36), sa.ForeignKey('gp_l3_policies.id'),
                             primary_key=True)
    subnetpool_id = sa.Column(
        sa.String(36), sa.ForeignKey('subnetpools.id'), primary_key=True)

class L3PolicyMapping(gpdb.L3Policy):
    """Mapping of L3Policy to set of Neutron Resources."""
    __table_args__ = {'extend_existing': True}
    __mapper_args__ = {'polymorphic_identity': 'mapping'}
    routers = orm.relationship(L3PolicyRouterAssociation,
                               cascade='all', lazy="joined")
    address_scope_v4_id = sa.Column(
        sa.String(36), sa.ForeignKey('address_scopes.id'),
        nullable=True, unique=True)
    address_scope_v6_id = sa.Column(
        sa.String(36), sa.ForeignKey('address_scopes.id'),
        nullable=True, unique=True)
    subnetpools_v4 = orm.relationship(L3PolicySubnetpoolV4Association,
                                      cascade='all', lazy="joined")
    subnetpools_v6 = orm.relationship(L3PolicySubnetpoolV6Association,
                                      cascade='all', lazy="joined")

class ExternalSegmentMapping(gpdb.ExternalSegment):
    """Mapping of L2Policy to Neutron Network."""
    __table_args__ = {'extend_existing': True}
    __mapper_args__ = {'polymorphic_identity': 'mapping'}
    subnet_id = sa.Column(sa.String(36), sa.ForeignKey('subnets.id'),
                          nullable=True, unique=True)

class NATPoolMapping(gpdb.NATPool):
    """Mapping of NAT Pool to Neutron Subnet."""
    __table_args__ = {'extend_existing': True}
    __mapper_args__ = {'polymorphic_identity': 'mapping'}
    subnet_id = sa.Column(sa.String(36), sa.ForeignKey('subnets.id'),
                          nullable=True, unique=True)

class GroupPolicyMappingDbPlugin(gpdb.GroupPolicyDbPlugin):
    """Group Policy Mapping interface implementation using SQLAlchemy models.
    """

    def _make_policy_target_dict(self, pt, fields=None, **kwargs):
        res = super(GroupPolicyMappingDbPlugin,
                    self)._make_policy_target_dict(pt)
        res['port_id'] = pt.port_id
        res.update(kwargs)
        return db_api.resource_fields(res, fields)

    def _make_policy_target_group_dict(self, ptg, fields=None):
        res = super(GroupPolicyMappingDbPlugin,
                    self)._make_policy_target_group_dict(ptg)
        res['subnets'] = [subnet.subnet_id for subnet in ptg.subnets]
        return db_api.resource_fields(res, fields)

    def _get_subnetpools(self, id_list):
        context = get_current_context().elevated()
        with context.session.begin(subtransactions=True):
            filters = {'id': id_list}
            return db_api.get_collection_query(
                context, models_v2.SubnetPool, filters=filters).all()

    def _make_l2_policy_dict(self, l2p, fields=None):
        res = super(GroupPolicyMappingDbPlugin,
                    self)._make_l2_policy_dict(l2p)
        res['network_id'] = l2p.network_id
        return db_api.resource_fields(res, fields)

    def _make_l3_policy_dict(self, l3p, fields=None, ip_pool=None):
        res = super(GroupPolicyMappingDbPlugin,
                    self)._make_l3_policy_dict(l3p)
        res['routers'] = [router.router_id for router in l3p.routers]
        res['address_scope_v4_id'] = l3p.address_scope_v4_id
        res['address_scope_v6_id'] = l3p.address_scope_v6_id
        res['subnetpools_v4'] = [sp.subnetpool_id for sp in l3p.subnetpools_v4]
        res['subnetpools_v6'] = [sp.subnetpool_id for sp in l3p.subnetpools_v6]
        if ip_pool:
            res['ip_pool'] = ip_pool
        subnetpools = self._get_subnetpools(res['subnetpools_v4'] +
                                            res['subnetpools_v6'])
        pool_list = [prefix['cidr'] for pool in subnetpools
                     for prefix in pool['prefixes']]
        if pool_list:
            res['ip_pool'] = utils.convert_ip_pool_list_to_string(
                pool_list)
        return db_api.resource_fields(res, fields)

    def _make_external_segment_dict(self, es, fields=None):
        res = super(GroupPolicyMappingDbPlugin,
                    self)._make_external_segment_dict(es)
        res['subnet_id'] = es.subnet_id
        return db_api.resource_fields(res, fields)

    def _make_nat_pool_dict(self, np, fields=None):
        res = super(GroupPolicyMappingDbPlugin,
                    self)._make_nat_pool_dict(np)
        res['subnet_id'] = np.subnet_id
        return db_api.resource_fields(res, fields)

    def _set_port_for_policy_target(self, context, pt_id, port_id):
        with context.session.begin(subtransactions=True):
            pt_db = self._get_policy_target(context, pt_id)
            pt_db.port_id = port_id

    def _add_subnet_to_policy_target_group(self, context, ptg_id, subnet_id):
        with context.session.begin(subtransactions=True):
            ptg_db = self._get_policy_target_group(context, ptg_id)
            assoc = PTGToSubnetAssociation(policy_target_group_id=ptg_id,
                                           subnet_id=subnet_id)
            ptg_db.subnets.append(assoc)
        return [subnet.subnet_id for subnet in ptg_db.subnets]

    def _remove_subnets_from_policy_target_groups(self, context, subnet_ids):
        with context.session.begin(subtransactions=True):
            assocs = context.session.query(PTGToSubnetAssociation).filter(
                PTGToSubnetAssociation.subnet_id.in_(subnet_ids)).all()
            for assoc in assocs:
                context.session.delete(assoc)

    def _remove_subnets_from_policy_target_group(self, context, ptg_id):
        with context.session.begin(subtransactions=True):
            ptg_db = self._get_policy_target_group(context, ptg_id)
            assocs = (context.session.query(PTGToSubnetAssociation).
                      filter_by(policy_target_group_id=ptg_id).all())
            ptg_db.update({'subnets': []})
            for assoc in assocs:
                context.session.delete(assoc)
        return []

    def _set_network_for_l2_policy(self, context, l2p_id, network_id):
        with context.session.begin(subtransactions=True):
            l2p_db = self._get_l2_policy(context, l2p_id)
            l2p_db.network_id = network_id

    def _set_address_scope_for_l3_policy_by_id(
        self, context, l3p_id, address_scope_id, ip_version=4):
        with context.session.begin(subtransactions=True):
            l3p_db = self._get_l3_policy(context, l3p_id)
            self._set_address_scope_for_l3_policy(
                context, l3p_db, address_scope_id, ip_version)

    def _set_address_scope_for_l3_policy(self, context, l3p_db,
                                         address_scope_id, ip_version=4):
        if not address_scope_id:
            return
        # TODO(Sumit): address_scope_id validation
        with context.session.begin(subtransactions=True):
            if ip_version == 4:
                l3p_db.address_scope_v4_id = address_scope_id
            else:
                l3p_db.address_scope_v6_id = address_scope_id

    def _add_subnetpool_to_l3_policy_by_id(self, context, l3p_id,
                                           subnetpool_id, ip_version=4):
        with context.session.begin(subtransactions=True):
            l3p_db = self._get_l3_policy(context, l3p_id)
            return self._add_subnetpool_to_l3_policy(
                context, l3p_db, subnetpool_id, ip_version)

    def _add_subnetpool_to_l3_policy(self, context, l3p_db,
                                     subnetpool_id, ip_version=4):
        # TODO(Sumit): subnetpool_id validation
        with context.session.begin(subtransactions=True):
            if ip_version == 4:
                assoc = L3PolicySubnetpoolV4Association(
                    l3_policy_id=l3p_db['id'], subnetpool_id=subnetpool_id)
                l3p_db.subnetpools_v4.append(assoc)
            else:
                assoc = L3PolicySubnetpoolV6Association(
                    l3_policy_id=l3p_db['id'], subnetpool_id=subnetpool_id)
                l3p_db.subnetpools_v6.append(assoc)
        return {4: [sp.subnetpool_id for sp in l3p_db.subnetpools_v4],
                6: [sp.subnetpool_id for sp in l3p_db.subnetpools_v6]}

    def _add_subnetpools_to_l3_policy_by_id(self, context, l3p_id,
                                            subnetpools, ip_version=4):
        with context.session.begin(subtransactions=True):
            l3p_db = self._get_l3_policy(context, l3p_id)
            self._add_subnetpools_to_l3_policy(
                context, l3p_db, subnetpools, ip_version)

    def _add_subnetpools_to_l3_policy(self, context, l3p_db,
                                      subnetpools, ip_version=4):
        for sp in subnetpools or []:
            self._add_subnetpool_to_l3_policy(
                context, l3p_db, sp, ip_version=ip_version)

    def _remove_subnetpool_from_l3_policy(self, context, l3p_id,
                                          subnetpool_id, ip_version=4):
        with context.session.begin(subtransactions=True):
            l3p_db = self._get_l3_policy(context, l3p_id)
            if ip_version == 4:
                assoc = (context.session.query(
                    L3PolicySubnetpoolV4Association).filter_by(
                        l3_policy_id=l3p_id,
                        subnetpool_id=subnetpool_id).one())
                l3p_db.subnetpools_v4.remove(assoc)
            else:
                assoc = (context.session.query(
                    L3PolicySubnetpoolV6Association).filter_by(
                        l3_policy_id=l3p_id,
                        subnetpool_id=subnetpool_id).one())
                l3p_db.subnetpools_v6.remove(assoc)
            context.session.delete(assoc)
        return {4: [sp.subnetpool_id for sp in l3p_db.subnetpools_v4],
                6: [sp.subnetpool_id for sp in l3p_db.subnetpools_v6]}

    def _update_subnetpools_for_l3_policy(self, context, l3p_id,
                                          subnetpools, ip_version=4):
        # Add/remove associations for changes in subnetpools
        # TODO(Sumit): Before disassociating a subnetpool, check that
        # there is no PT present on a subnet which belongs to that subnetpool
        if not subnetpools:
            return
        with context.session.begin(subtransactions=True):
            l3p_db = self._get_l3_policy(context, l3p_id)
            new_subnetpools = set(subnetpools)
            if ip_version == 4:
                old_subnetpools = set(sp.subnetpool_id
                                      for sp in l3p_db.subnetpools_v4)
            else:
                old_subnetpools = set(sp.subnetpool_id
                                      for sp in l3p_db.subnetpools_v6)
            for sp in new_subnetpools - old_subnetpools:
                if ip_version == 4:
                    assoc = L3PolicySubnetpoolV4Association(
                        l3_policy_id=l3p_id, subnetpool_id=sp)
                    l3p_db.subnetpools_v4.append(assoc)
                else:
                    assoc = L3PolicySubnetpoolV6Association(
                        l3_policy_id=l3p_id, subnetpool_id=sp)
                    l3p_db.subnetpools_v6.append(assoc)
            for sp in old_subnetpools - new_subnetpools:
                if ip_version == 4:
                    assoc = (context.session.query(
                        L3PolicySubnetpoolV4Association).filter_by(
                            l3_policy_id=l3p_id, subnetpool_id=sp).one())
                    l3p_db.subnetpools_v4.remove(assoc)
                else:
                    assoc = (context.session.query(
                        L3PolicySubnetpoolV6Association).filter_by(
                            l3_policy_id=l3p_id, subnetpool_id=sp).one())
                    l3p_db.subnetpools_v6.remove(assoc)
                context.session.delete(assoc)

    def _add_router_to_l3_policy(self, context, l3p_id, router_id):
        with context.session.begin(subtransactions=True):
            l3p_db = self._get_l3_policy(context, l3p_id)
            assoc = L3PolicyRouterAssociation(l3_policy_id=l3p_id,
                                              router_id=router_id)
            l3p_db.routers.append(assoc)
        return [router.router_id for router in l3p_db.routers]

    def _remove_router_from_l3_policy(self, context, l3p_id, router_id):
        with context.session.begin(subtransactions=True):
            l3p_db = self._get_l3_policy(context, l3p_id)
            assoc = (context.session.query(L3PolicyRouterAssociation).
                     filter_by(l3_policy_id=l3p_id, router_id=router_id).
                     one())
            l3p_db.routers.remove(assoc)
            context.session.delete(assoc)
        return [router.router_id for router in l3p_db.routers]

    def _set_subnet_to_es(self, context, es_id, subnet_id):
        with context.session.begin(subtransactions=True):
            es_db = self._get_external_segment(context, es_id)
            es_db.subnet_id = subnet_id

    def _set_subnet_to_np(self, context, np_id, subnet_id):
        with context.session.begin(subtransactions=True):
            np_db = self._get_nat_pool(context, np_id)
            np_db.subnet_id = subnet_id

    def _update_ess_for_l3p(self, context, l3p_id, ess):
        with context.session.begin(subtransactions=True):
            l3p_db = self._get_l3_policy(context, l3p_id)
            self._set_ess_for_l3p(context, l3p_db, ess)

    def _get_nat_pool(self, context, nat_pool_id):
        return self._find_gbp_resource(
            context, NATPoolMapping, nat_pool_id,
            gpolicy.NATPoolNotFound)

    def _get_l3p_ptgs(self, context, l3p_id):
        return super(GroupPolicyMappingDbPlugin, self)._get_l3p_ptgs(
            context, l3p_id, l3p_klass=L3PolicyMapping,
            ptg_klass=PolicyTargetGroupMapping, l2p_klass=L2PolicyMapping)

    def get_l3p_id_from_router_id(self, context, router_id):
        mapping = context.session.query(L3PolicyRouterAssociation).filter_by(
            router_id=router_id).first()
        if mapping:
            return mapping['l3_policy_id']

    def _set_db_np_subnet(self, context, nat_pool, subnet_id):
        with context.session.begin(subtransactions=True):
            nat_pool['subnet_id'] = subnet_id
            db_np = self._get_nat_pool(context, nat_pool['id'])
            db_np.subnet_id = nat_pool['subnet_id']
            context.session.merge(db_np)

    def _get_ptgs_for_subnet(self, context, subnet_id):
        return [x['policy_target_group_id'] for x in
            context.session.query(PTGToSubnetAssociation).filter_by(
                subnet_id=subnet_id)]

    def _validate_pt_port_exta_attributes(self, context, pt):
        attributes = pt.get('port_attributes')
        if attributes:
            # Check network ID not overridden
            if 'network_id' in attributes:
                raise exceptions.InvalidPortExtraAttributes(
                    attribute='network_id', reason='read only attribute')
            if 'fixed_ips' in attributes:
                ptg = self.get_policy_target_group(
                    context, pt['policy_target_group_id'])
                subnets = ptg['subnets']
                for fixed_ip in attributes.get('fixed_ips'):
                    if ('subnet_id' in fixed_ip and
                            fixed_ip['subnet_id'] not in subnets):
                        raise exceptions.InvalidPortExtraAttributes(
                            attribute='fixed_ips:subnet_id',
                            reason='subnet not in PTG')
            if 'allowed_address_pairs' in attributes:
                # REVISIT(ivar); Could be allowed with certain restrictions,
                # but we don't have a use case for it right now
                raise exceptions.InvalidPortExtraAttributes(
                    attribute='allowed_address_pairs',
                    reason='read only attribute')

    @log.log_method_call
    def create_policy_target(self, context, policy_target):
        pt = policy_target['policy_target']
        tenant_id = self._get_tenant_id_for_create(context, pt)
        with db_api.CONTEXT_WRITER.using(context):
            self._validate_pt_port_exta_attributes(context, pt)
            pt_db = PolicyTargetMapping(
                id=uuidutils.generate_uuid(),
                tenant_id=tenant_id,
                name=pt['name'],
                description=pt['description'],
                policy_target_group_id=pt['policy_target_group_id'],
                port_id=pt['port_id'],
                cluster_id=pt['cluster_id'])
            context.session.add(pt_db)
            return self._make_policy_target_dict(
                pt_db, port_attributes=pt.get('port_attributes', {}))

    @log.log_method_call
    def get_policy_targets_count(self, context, filters=None):
        return db_api.get_collection_count(context, PolicyTargetMapping,
                                           filters=filters)

    @log.log_method_call
    def get_policy_targets(self, context, filters=None, fields=None,
                           sorts=None, limit=None, marker=None,
                           page_reverse=False):
        plugin = directory.get_plugin()
        marker_obj = db_api.get_marker_obj(plugin, context,
                                           'policy_target', limit,
                                           marker)
        return db_api.get_collection(context, PolicyTargetMapping,
                                     self._make_policy_target_dict,
                                     filters=filters, fields=fields,
                                     sorts=sorts, limit=limit,
                                     marker_obj=marker_obj,
                                     page_reverse=page_reverse)

    @log.log_method_call
    def create_policy_target_group(self, context, policy_target_group):
        ptg = policy_target_group['policy_target_group']
        tenant_id = self._get_tenant_id_for_create(context, ptg)
        with db_api.CONTEXT_WRITER.using(context):
            if ptg['service_management']:
                self._validate_service_management_ptg(context, tenant_id)
            uuid = ptg.get('id')
            if not uuid:
                uuid = uuidutils.generate_uuid()
            ptg_db = PolicyTargetGroupMapping(
                id=uuid, tenant_id=tenant_id,
                name=ptg['name'], description=ptg['description'],
                l2_policy_id=ptg['l2_policy_id'],
                application_policy_group_id=ptg.get(
                    'application_policy_group_id', None),
                network_service_policy_id=ptg['network_service_policy_id'],
                shared=ptg.get('shared', False),
                service_management=ptg.get('service_management', False))
            context.session.add(ptg_db)
            if 'subnets' in ptg:
                for subnet in ptg['subnets']:
                    assoc = PTGToSubnetAssociation(
                        policy_target_group_id=ptg_db.id,
                        subnet_id=subnet
                    )
                    ptg_db.subnets.append(assoc)
            self._process_policy_rule_sets_for_ptg(context, ptg_db, ptg)
            return self._make_policy_target_group_dict(ptg_db)

    @log.log_method_call
    def update_policy_target_group(self, context, policy_target_group_id,
                                   policy_target_group):
        ptg = policy_target_group['policy_target_group']
        with db_api.CONTEXT_WRITER.using(context):
            ptg_db = self._get_policy_target_group(
                context, policy_target_group_id)
            self._process_policy_rule_sets_for_ptg(context, ptg_db, ptg)
            if 'subnets' in ptg:
                # Add/remove associations for changes in subnets.
                new_subnets = set(ptg['subnets'])
                old_subnets = set(subnet.subnet_id
                                  for subnet in ptg_db.subnets)
                for subnet in new_subnets - old_subnets:
                    assoc = PTGToSubnetAssociation(
                        policy_target_group_id=policy_target_group_id,
                        subnet_id=subnet)
                    ptg_db.subnets.append(assoc)
                for subnet in old_subnets - new_subnets:
                    assoc = (
                        context.session.query(
                            PTGToSubnetAssociation).filter_by(
                                policy_target_group_id=policy_target_group_id,
                                subnet_id=subnet).one())
                    ptg_db.subnets.remove(assoc)
                    context.session.delete(assoc)
                # Don't update ptg_db.subnets with subnet IDs.
                del ptg['subnets']
            ptg_db.update(ptg)
            return self._make_policy_target_group_dict(ptg_db)

    @log.log_method_call
    def get_policy_target_groups_count(self, context, filters=None):
        return db_api.get_collection_count(context, PolicyTargetGroupMapping,
                                           filters=filters)

    def _get_policy_target_groups(self, context, filters=None, fields=None,
                           sorts=None, limit=None, marker=None,
                           page_reverse=False):
        plugin = directory.get_plugin()
        marker_obj = db_api.get_marker_obj(plugin, context,
                                           'policy_target_group',
                                           limit, marker)
        return db_api.get_collection(context, PolicyTargetGroupMapping,
                                     self._make_policy_target_group_dict,
                                     filters=filters, fields=fields,
                                     sorts=sorts, limit=limit,
                                     marker_obj=marker_obj,
                                     page_reverse=page_reverse)

    @log.log_method_call
    def get_policy_target_groups(self, context, filters=None, fields=None,
                           sorts=None, limit=None, marker=None,
                           page_reverse=False):
        return self._get_policy_target_groups(
            context, filters, fields, sorts, limit, marker, page_reverse)

    @log.log_method_call
    def create_l2_policy(self, context, l2_policy):
        l2p = l2_policy['l2_policy']
        tenant_id = self._get_tenant_id_for_create(context, l2p)
        with db_api.CONTEXT_WRITER.using(context):
            l2p_db = L2PolicyMapping(id=uuidutils.generate_uuid(),
                                     tenant_id=tenant_id,
                                     name=l2p['name'],
                                     description=l2p['description'],
                                     l3_policy_id=l2p['l3_policy_id'],
                                     network_id=l2p['network_id'],
                                     inject_default_route=l2p.get(
                                         'inject_default_route', True),
                                     shared=l2p.get('shared', False))
            context.session.add(l2p_db)
            return self._make_l2_policy_dict(l2p_db)

    @log.log_method_call
    def get_l2_policies(self, context, filters=None, fields=None,
                        sorts=None, limit=None, marker=None,
                        page_reverse=False):
        plugin = directory.get_plugin()
        marker_obj = db_api.get_marker_obj(plugin, context,
                                           'l2_policy', limit,
                                           marker)
        return db_api.get_collection(context, L2PolicyMapping,
                                     self._make_l2_policy_dict,
                                     filters=filters, fields=fields,
                                     sorts=sorts, limit=limit,
                                     marker_obj=marker_obj,
                                     page_reverse=page_reverse)

    @log.log_method_call
    def get_l2_policies_count(self, context, filters=None):
        return db_api.get_collection_count(context, L2PolicyMapping,
                                           filters=filters)

    @log.log_method_call
    def create_l3_policy(self, context, l3_policy):
        l3p = l3_policy['l3_policy']
        self.validate_ip_pool(l3p.get('ip_pool', None), l3p['ip_version'])
        # TODO(Sumit): Check consistency of explicit subnetpool's
        # address_scopes
        tenant_id = self._get_tenant_id_for_create(context, l3p)
        self.validate_subnet_prefix_length(l3p['ip_version'],
                                           l3p['subnet_prefix_length'],
                                           l3p.get('ip_pool', None))
        with db_api.CONTEXT_WRITER.using(context):
            l3p_db = L3PolicyMapping(
                id=uuidutils.generate_uuid(),
                tenant_id=tenant_id,
                name=l3p['name'],
                ip_version=l3p['ip_version'],
                ip_pool=l3p['ip_pool'],
                subnet_prefix_length=l3p['subnet_prefix_length'],
                description=l3p['description'],
                shared=l3p.get('shared', False))

            self._set_address_scope_for_l3_policy(
                context, l3p_db, l3p.get('address_scope_v4_id'),
                ip_version=4)
            self._set_address_scope_for_l3_policy(
                context, l3p_db, l3p.get('address_scope_v6_id'),
                ip_version=6)
            self._add_subnetpools_to_l3_policy(
                context, l3p_db, l3p.get('subnetpools_v4'), ip_version=4)
            self._add_subnetpools_to_l3_policy(
                context, l3p_db, l3p.get('subnetpools_v6'), ip_version=6)

            if 'routers' in l3p:
                for router in l3p['routers']:
                    assoc = L3PolicyRouterAssociation(
                        l3_policy_id=l3p_db.id,
                        router_id=router
                    )
                    l3p_db.routers.append(assoc)
            if 'external_segments' in l3p:
                self._set_ess_for_l3p(context, l3p_db,
                                      l3p['external_segments'])
            context.session.add(l3p_db)
            return self._make_l3_policy_dict(l3p_db, ip_pool=l3p['ip_pool'])

    @log.log_method_call
    def update_l3_policy(self, context, l3_policy_id, l3_policy):
        l3p = l3_policy['l3_policy']

        if 'address_scope_v4_id' in l3p or 'address_scope_v6_id' in l3p:
            raise AddressScopeUpdateForL3PNotSupported()

        with db_api.CONTEXT_WRITER.using(context):
            l3p_db = self._get_l3_policy(context, l3_policy_id)

            self._update_subnetpools_for_l3_policy(context, l3_policy_id,
                                                   l3p.get('subnetpools_v4'),
                                                   ip_version=4)
            l3p.pop('subnetpools_v4', None)
            self._update_subnetpools_for_l3_policy(context, l3_policy_id,
                                                   l3p.get('subnetpools_v6'),
                                                   ip_version=6)
            l3p.pop('subnetpools_v6', None)

            if 'subnet_prefix_length' in l3p:
                self.validate_subnet_prefix_length(l3p_db.ip_version,
                                                   l3p['subnet_prefix_length'],
                                                   l3p_db.ip_pool)

            if 'routers' in l3p:
                # Add/remove associations for changes in routers.
                new_routers = set(l3p['routers'])
                old_routers = set(router.router_id
                                  for router in l3p_db.routers)
                for router in new_routers - old_routers:
                    assoc = L3PolicyRouterAssociation(
                        l3_policy_id=l3_policy_id, router_id=router)
                    l3p_db.routers.append(assoc)
                for router in old_routers - new_routers:
                    assoc = (context.session.query(L3PolicyRouterAssociation).
                             filter_by(l3_policy_id=l3_policy_id,
                                       router_id=router).
                             one())
                    l3p_db.routers.remove(assoc)
                    context.session.delete(assoc)
                # Don't update l3p_db.routers with router IDs.
                del l3p['routers']
            if 'external_segments' in l3p:
                self._set_ess_for_l3p(context, l3p_db,
                                      l3p['external_segments'])
                del l3p['external_segments']
            l3p_db.update(l3p)
            return self._make_l3_policy_dict(l3p_db)

    @log.log_method_call
    def create_external_segment(self, context, external_segment):
        es = external_segment['external_segment']
        tenant_id = self._get_tenant_id_for_create(context, es)
        with db_api.CONTEXT_WRITER.using(context):
            es_db = ExternalSegmentMapping(
                id=uuidutils.generate_uuid(), tenant_id=tenant_id,
                name=es['name'], description=es['description'],
                shared=es.get('shared', False), ip_version=es['ip_version'],
                cidr=es['cidr'],
                port_address_translation=es['port_address_translation'],
                subnet_id=es['subnet_id'])
            context.session.add(es_db)
            if 'external_routes' in es:
                self._process_segment_ers(context, es_db, es)
            return self._make_external_segment_dict(es_db)

    @log.log_method_call
    def get_external_segments(self, context, filters=None, fields=None,
                              sorts=None, limit=None, marker=None,
                              page_reverse=False):
        plugin = directory.get_plugin()
        marker_obj = db_api.get_marker_obj(plugin, context,
                                           'external_segment', limit,
                                           marker)
        return db_api.get_collection(context, ExternalSegmentMapping,
                                     self._make_external_segment_dict,
                                     filters=filters, fields=fields,
                                     sorts=sorts, limit=limit,
                                     marker_obj=marker_obj,
                                     page_reverse=page_reverse)

    def get_external_segments_count(self, context, filters=None):
        return db_api.get_collection_count(context, ExternalSegmentMapping,
                                           filters=filters)

    @log.log_method_call
    def create_nat_pool(self, context, nat_pool):
        np = nat_pool['nat_pool']
        tenant_id = self._get_tenant_id_for_create(context, np)
        with db_api.CONTEXT_WRITER.using(context):
            np_db = NATPoolMapping(
                id=uuidutils.generate_uuid(), tenant_id=tenant_id,
                name=np['name'], description=np['description'],
                shared=np.get('shared', False), ip_version=np['ip_version'],
                ip_pool=np['ip_pool'],
                external_segment_id=np['external_segment_id'],
                subnet_id=np.get('subnet_id'))
            context.session.add(np_db)
            return self._make_nat_pool_dict(np_db)

    @log.log_method_call
    def get_nat_pools(self, context, filters=None, fields=None,
                      sorts=None, limit=None, marker=None,
                      page_reverse=False):
        plugin = directory.get_plugin()
        marker_obj = db_api.get_marker_obj(plugin, context,
                                           'nat_pool', limit,
                                           marker)
        return db_api.get_collection(context, NATPoolMapping,
                                     self._make_nat_pool_dict,
                                     filters=filters, fields=fields,
                                     sorts=sorts, limit=limit,
                                     marker_obj=marker_obj,
                                     page_reverse=page_reverse)