1 # ============LICENSE_START=======================================================
3 # ================================================================================
4 # Copyright (c) 2017 AT&T Intellectual Property. All rights reserved.
5 # ================================================================================
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 # ============LICENSE_END=========================================================
19 # ECOMP is a trademark and service mark of AT&T Intellectual Property.
21 # -*- coding: utf-8 -*-
23 Provides a local mock catalog
27 from datetime import datetime
29 from sqlalchemy import UniqueConstraint, Table, Column, String, DateTime, ForeignKey, Boolean, Enum, Text
30 from sqlalchemy.ext.declarative import declarative_base
31 from sqlalchemy.orm import relationship
32 from sqlalchemy.schema import PrimaryKeyConstraint
35 datetime_now = datetime.utcnow
37 Base = declarative_base()
40 published = Table('published', Base.metadata,
41 Column('component_id', String, ForeignKey('components.id', ondelete='CASCADE'), nullable=False),
42 Column('format_id', String, ForeignKey('formats.id', ondelete='CASCADE'), nullable=False),
43 PrimaryKeyConstraint('component_id', 'format_id')
47 subscribed = Table('subscribed', Base.metadata,
48 Column('component_id', String, ForeignKey('components.id', ondelete='CASCADE'), nullable=False),
49 Column('format_id', String, ForeignKey('formats.id', ondelete='CASCADE'), nullable=False),
50 PrimaryKeyConstraint('component_id', 'format_id')
54 provided = Table('provided', Base.metadata,
55 Column('component_id', String, ForeignKey('components.id', ondelete='CASCADE'), nullable=False),
56 Column('pair_id', String, ForeignKey('format_pairs.id', ondelete='CASCADE'), nullable=False),
57 PrimaryKeyConstraint('component_id', 'pair_id')
61 called = Table('called', Base.metadata,
62 Column('component_id', String, ForeignKey('components.id', ondelete='CASCADE'), nullable=False),
63 Column('pair_id', String, ForeignKey('format_pairs.id', ondelete='CASCADE'), nullable=False),
64 PrimaryKeyConstraint('component_id', 'pair_id')
69 return str(uuid.uuid4())
72 class Component(Base):
73 __tablename__ = 'components'
74 id = Column(String, primary_key=True, default=generate_uuid)
75 created = Column(DateTime, default=datetime_now, nullable=False)
76 modified = Column(DateTime, default=datetime_now, onupdate=datetime_now, nullable=False)
77 owner = Column(String, nullable=False)
78 # To be used for tracking and debugging
79 cli_version = Column(String, nullable=False)
80 schema_path = Column(String, nullable=False)
82 name = Column(String(), nullable=False)
83 component_type = Column(Enum('docker', 'cdap', name='component_types'), nullable=False)
84 version = Column(String(), nullable=False)
85 description = Column(Text(), nullable=False)
86 spec = Column(Text(), nullable=False)
88 when_added = Column(DateTime, default=datetime_now, nullable=True)
89 when_published = Column(DateTime, default=None, nullable=True)
90 when_revoked = Column(DateTime, default=None, nullable=True)
92 publishes = relationship('Format', secondary=published)
93 subscribes = relationship('Format', secondary=subscribed)
94 provides = relationship('FormatPair', secondary=provided)
95 calls = relationship('FormatPair', secondary=called)
97 __tableargs__ = (UniqueConstraint(name, version), )
100 return '<{:}>'.format((self.__class__.__name__, self.id, self.name, self.version))
102 def is_published(self):
103 return self.when_published is not None
105 def get_spec_as_dict(self):
106 return json.loads(self.spec)
110 __tablename__ = 'formats'
111 id = Column(String, primary_key=True, default=generate_uuid)
112 created = Column(DateTime, default=datetime_now, nullable=False)
113 modified = Column(DateTime, default=datetime_now, onupdate=datetime_now, nullable=False)
114 owner = Column(String, nullable=False)
115 # To be used for tracking and debugging
116 cli_version = Column(String, nullable=False)
117 schema_path = Column(String, nullable=False)
119 name = Column(String(), nullable=False)
120 version = Column(String(), nullable=False)
121 description = Column(Text(), nullable=True)
122 spec = Column(Text(), nullable=False)
124 when_added = Column(DateTime, default=datetime_now, nullable=True)
125 when_published = Column(DateTime, default=None, nullable=True)
126 when_revoked = Column(DateTime, default=None, nullable=True)
128 __tableargs__ = (UniqueConstraint(name, version), )
131 return '<{:}>'.format((self.__class__.__name__, self.id, self.name, self.version))
133 def is_published(self):
134 return self.when_published is not None
137 class FormatPair(Base):
138 __tablename__ = 'format_pairs'
139 id = Column(String, primary_key=True, default=generate_uuid)
140 req_id = Column(String, ForeignKey('formats.id', ondelete='CASCADE'))
141 resp_id = Column(String, ForeignKey('formats.id', ondelete='CASCADE'))
143 req = relationship('Format', foreign_keys=req_id, uselist=False)
144 resp = relationship('Format', foreign_keys=resp_id, uselist=False)
146 __table_args__ = (UniqueConstraint(req_id, resp_id), )
149 return '<{:}>'.format((self.__class__.__name__, self.id, self.req, self.resp))