Internet of Things resource management service for OpenStack clouds.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

models.py 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. # -*- encoding: utf-8 -*-
  2. #
  3. # Copyright 2013 Hewlett-Packard Development Company, L.P.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  6. # not use this file except in compliance with the License. You may obtain
  7. # a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  14. # License for the specific language governing permissions and limitations
  15. # under the License.
  16. """
  17. SQLAlchemy models for iot data.
  18. """
  19. import json
  20. from oslo_config import cfg
  21. from oslo_db.sqlalchemy import models
  22. import six.moves.urllib.parse as urlparse
  23. from sqlalchemy import Boolean
  24. from sqlalchemy import Column
  25. from sqlalchemy import ForeignKey, Integer
  26. from sqlalchemy.ext.declarative import declarative_base
  27. from sqlalchemy import schema
  28. from sqlalchemy import String
  29. from sqlalchemy.types import TypeDecorator, TEXT
  30. from iotronic.common import paths
  31. sql_opts = [
  32. cfg.StrOpt('mysql_engine',
  33. default='InnoDB',
  34. help='MySQL engine to use.')
  35. ]
  36. _DEFAULT_SQL_CONNECTION = 'sqlite:///' + \
  37. paths.state_path_def('iotronic.sqlite')
  38. cfg.CONF.register_opts(sql_opts, 'database')
  39. def table_args():
  40. engine_name = urlparse.urlparse(cfg.CONF.database.connection).scheme
  41. if engine_name == 'mysql':
  42. return {'mysql_engine': cfg.CONF.database.mysql_engine,
  43. 'mysql_charset': "utf8"}
  44. return None
  45. class JsonEncodedType(TypeDecorator):
  46. """Abstract base type serialized as json-encoded string in db."""
  47. type = None
  48. impl = TEXT
  49. def process_bind_param(self, value, dialect):
  50. if value is None:
  51. # Save default value according to current type to keep the
  52. # interface the consistent.
  53. value = self.type()
  54. elif not isinstance(value, self.type):
  55. raise TypeError("%s supposes to store %s objects, but %s given"
  56. % (self.__class__.__name__,
  57. self.type.__name__,
  58. type(value).__name__))
  59. serialized_value = json.dumps(value)
  60. return serialized_value
  61. def process_result_value(self, value, dialect):
  62. if value is not None:
  63. value = json.loads(value)
  64. return value
  65. class JSONEncodedDict(JsonEncodedType):
  66. """Represents dict serialized as json-encoded string in db."""
  67. type = dict
  68. class JSONEncodedList(JsonEncodedType):
  69. """Represents list serialized as json-encoded string in db."""
  70. type = list
  71. class IotronicBase(models.TimestampMixin,
  72. models.ModelBase):
  73. metadata = None
  74. def as_dict(self):
  75. d = {}
  76. for c in self.__table__.columns:
  77. d[c.name] = self[c.name]
  78. return d
  79. def save(self, session=None):
  80. import iotronic.db.sqlalchemy.api as db_api
  81. if session is None:
  82. session = db_api.get_session()
  83. super(IotronicBase, self).save(session)
  84. Base = declarative_base(cls=IotronicBase)
  85. class Conductor(Base):
  86. """Represents a conductor service entry."""
  87. __tablename__ = 'conductors'
  88. __table_args__ = (
  89. schema.UniqueConstraint('hostname', name='uniq_conductors0hostname'),
  90. table_args()
  91. )
  92. id = Column(Integer, primary_key=True)
  93. hostname = Column(String(255), nullable=False)
  94. online = Column(Boolean, default=True)
  95. class WampAgent(Base):
  96. """Represents a wampagent service entry."""
  97. __tablename__ = 'wampagents'
  98. __table_args__ = (
  99. schema.UniqueConstraint('hostname', name='uniq_wampagentss0hostname'),
  100. table_args()
  101. )
  102. id = Column(Integer, primary_key=True)
  103. hostname = Column(String(255), nullable=False)
  104. wsurl = Column(String(255), nullable=False)
  105. online = Column(Boolean, default=True)
  106. ragent = Column(Boolean, default=False)
  107. class Board(Base):
  108. """Represents a Board."""
  109. __tablename__ = 'boards'
  110. __table_args__ = (
  111. schema.UniqueConstraint('uuid', name='uniq_boards0uuid'),
  112. schema.UniqueConstraint('code', name='uniq_boards0code'),
  113. table_args())
  114. id = Column(Integer, primary_key=True)
  115. uuid = Column(String(36))
  116. code = Column(String(25))
  117. status = Column(String(15), nullable=True)
  118. name = Column(String(255), nullable=True)
  119. type = Column(String(255))
  120. agent = Column(String(255), nullable=True)
  121. owner = Column(String(36))
  122. project = Column(String(36))
  123. mobile = Column(Boolean, default=False)
  124. config = Column(JSONEncodedDict)
  125. extra = Column(JSONEncodedDict)
  126. class Location(Base):
  127. """Represents a location of a board."""
  128. __tablename__ = 'locations'
  129. __table_args__ = (
  130. table_args())
  131. id = Column(Integer, primary_key=True)
  132. longitude = Column(String(18), nullable=True)
  133. latitude = Column(String(18), nullable=True)
  134. altitude = Column(String(18), nullable=True)
  135. board_id = Column(Integer, ForeignKey('boards.id'))
  136. class SessionWP(Base):
  137. """Represents a session of a board."""
  138. __tablename__ = 'sessions'
  139. __table_args__ = (
  140. schema.UniqueConstraint(
  141. 'session_id',
  142. name='uniq_session_id0session_id'),
  143. schema.UniqueConstraint(
  144. 'board_uuid',
  145. name='uniq_board_uuid0board_uuid'),
  146. table_args())
  147. id = Column(Integer, primary_key=True)
  148. valid = Column(Boolean, default=True)
  149. session_id = Column(String(15))
  150. board_uuid = Column(String(36))
  151. board_id = Column(Integer, ForeignKey('boards.id'))
  152. class Plugin(Base):
  153. """Represents a plugin."""
  154. __tablename__ = 'plugins'
  155. __table_args__ = (
  156. schema.UniqueConstraint('uuid', name='uniq_plugins0uuid'),
  157. table_args())
  158. id = Column(Integer, primary_key=True)
  159. uuid = Column(String(36))
  160. name = Column(String(36))
  161. owner = Column(String(36))
  162. public = Column(Boolean, default=False)
  163. code = Column(TEXT)
  164. callable = Column(Boolean)
  165. parameters = Column(JSONEncodedDict)
  166. extra = Column(JSONEncodedDict)
  167. class InjectionPlugin(Base):
  168. """Represents an plugin injection on board."""
  169. __tablename__ = 'injection_plugins'
  170. __table_args__ = (
  171. table_args())
  172. id = Column(Integer, primary_key=True)
  173. board_uuid = Column(String(36), ForeignKey('boards.uuid'))
  174. plugin_uuid = Column(String(36), ForeignKey('plugins.uuid'))
  175. onboot = Column(Boolean, default=False)
  176. status = Column(String(15))
  177. class Service(Base):
  178. """Represents a service."""
  179. __tablename__ = 'services'
  180. __table_args__ = (
  181. schema.UniqueConstraint('uuid', name='uniq_services0uuid'),
  182. table_args())
  183. id = Column(Integer, primary_key=True)
  184. uuid = Column(String(36))
  185. name = Column(String(36))
  186. project = Column(String(36))
  187. port = Column(Integer)
  188. protocol = Column(String(3))
  189. extra = Column(JSONEncodedDict)
  190. class ExposedService(Base):
  191. """Represents an exposed service on board."""
  192. __tablename__ = 'exposed_services'
  193. __table_args__ = (
  194. table_args())
  195. id = Column(Integer, primary_key=True)
  196. board_uuid = Column(String(36), ForeignKey('boards.uuid'))
  197. service_uuid = Column(String(36), ForeignKey('services.uuid'))
  198. public_port = Column(Integer)