Set lock_path correctly.
[openstack-build/neutron-build.git] / neutron / tests / unit / dummy_plugin.py
1 # Copyright 2012 OpenStack Foundation.
2 # All Rights Reserved.
3 #
4 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
5 #    not use this file except in compliance with the License. You may obtain
6 #    a copy of the License at
7 #
8 #         http://www.apache.org/licenses/LICENSE-2.0
9 #
10 #    Unless required by applicable law or agreed to in writing, software
11 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 #    License for the specific language governing permissions and limitations
14 #    under the License.
15
16 from oslo_utils import uuidutils
17
18 from neutron.api import extensions
19 from neutron.api.v2 import base
20 from neutron.common import exceptions
21 from neutron.db import servicetype_db
22 from neutron.extensions import servicetype
23 from neutron import manager
24 from neutron.plugins.common import constants
25 from neutron.services import service_base
26
27
28 RESOURCE_NAME = "dummy"
29 COLLECTION_NAME = "%ss" % RESOURCE_NAME
30
31 # Attribute Map for dummy resource
32 RESOURCE_ATTRIBUTE_MAP = {
33     COLLECTION_NAME: {
34         'id': {'allow_post': False, 'allow_put': False,
35                'validate': {'type:uuid': None},
36                'is_visible': True},
37         'name': {'allow_post': True, 'allow_put': True,
38                  'validate': {'type:string': None},
39                  'is_visible': True, 'default': ''},
40         'tenant_id': {'allow_post': True, 'allow_put': False,
41                       'required_by_policy': True,
42                       'is_visible': True},
43         'service_type': {'allow_post': True,
44                          'allow_put': False,
45                          'validate': {'type:servicetype_ref': None},
46                          'is_visible': True,
47                          'default': None}
48     }
49 }
50
51
52 class Dummy(object):
53
54     @classmethod
55     def get_name(cls):
56         return "dummy"
57
58     @classmethod
59     def get_alias(cls):
60         return "dummy"
61
62     @classmethod
63     def get_description(cls):
64         return "Dummy stuff"
65
66     @classmethod
67     def get_updated(cls):
68         return "2012-11-20T10:00:00-00:00"
69
70     @classmethod
71     def get_resources(cls):
72         """Returns Extended Resource for dummy management."""
73         n_mgr = manager.NeutronManager.get_instance()
74         dummy_inst = n_mgr.get_service_plugins()['DUMMY']
75         controller = base.create_resource(
76             COLLECTION_NAME, RESOURCE_NAME, dummy_inst,
77             RESOURCE_ATTRIBUTE_MAP[COLLECTION_NAME])
78         return [extensions.ResourceExtension(COLLECTION_NAME,
79                                              controller)]
80
81
82 class DummyServicePlugin(service_base.ServicePluginBase):
83     """This is a simple plugin for managing instances of a fictional 'dummy'
84         service. This plugin is provided as a proof-of-concept of how
85         advanced service might leverage the service type extension.
86         Ideally, instances of real advanced services, such as load balancing
87         or VPN will adopt a similar solution.
88     """
89
90     supported_extension_aliases = ['dummy', servicetype.EXT_ALIAS]
91     path_prefix = "/dummy_svc"
92     agent_notifiers = {'dummy': 'dummy_agent_notifier'}
93
94     def __init__(self):
95         self.svctype_mgr = servicetype_db.ServiceTypeManager.get_instance()
96         self.dummys = {}
97
98     def get_plugin_type(self):
99         return constants.DUMMY
100
101     def get_plugin_description(self):
102         return "Neutron Dummy Service Plugin"
103
104     def get_dummys(self, context, filters, fields):
105         return self.dummys.values()
106
107     def get_dummy(self, context, id, fields):
108         try:
109             return self.dummys[id]
110         except KeyError:
111             raise exceptions.NotFound()
112
113     def create_dummy(self, context, dummy):
114         d = dummy['dummy']
115         d['id'] = uuidutils.generate_uuid()
116         self.dummys[d['id']] = d
117         self.svctype_mgr.increase_service_type_refcount(context,
118                                                         d['service_type'])
119         return d
120
121     def update_dummy(self, context, id, dummy):
122         pass
123
124     def delete_dummy(self, context, id):
125         try:
126             svc_type_id = self.dummys[id]['service_type']
127             del self.dummys[id]
128             self.svctype_mgr.decrease_service_type_refcount(context,
129                                                             svc_type_id)
130         except KeyError:
131             raise exceptions.NotFound()