Add dcae-cli and component-json-schemas projects
[dcaegen2/platform/cli.git] / dcae-cli / dcae_cli / catalog / mock / tables.py
1 # ============LICENSE_START=======================================================
2 # org.onap.dcae
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
9 #
10 #      http://www.apache.org/licenses/LICENSE-2.0
11 #
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=========================================================
18 #
19 # ECOMP is a trademark and service mark of AT&T Intellectual Property.
20
21 # -*- coding: utf-8 -*-
22 '''
23 Provides a local mock catalog
24 '''
25 import uuid
26 import json
27 from datetime import datetime
28
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
33
34
35 datetime_now = datetime.utcnow
36
37 Base = declarative_base()
38
39
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')
44 )
45
46
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')
51 )
52
53
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')
58 )
59
60
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')
65 )
66
67
68 def generate_uuid():
69     return str(uuid.uuid4())
70
71
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)
81
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)
87
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)
91
92     publishes = relationship('Format', secondary=published)
93     subscribes = relationship('Format', secondary=subscribed)
94     provides = relationship('FormatPair', secondary=provided)
95     calls = relationship('FormatPair', secondary=called)
96
97     __tableargs__ = (UniqueConstraint(name, version), )
98
99     def __repr__(self):
100         return '<{:}>'.format((self.__class__.__name__, self.id, self.name, self.version))
101
102     def is_published(self):
103         return self.when_published is not None
104
105     def get_spec_as_dict(self):
106         return json.loads(self.spec)
107
108
109 class Format(Base):
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)
118
119     name = Column(String(), nullable=False)
120     version = Column(String(), nullable=False)
121     description = Column(Text(), nullable=False)
122     spec = Column(Text(), nullable=False)
123
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)
127
128     __tableargs__ = (UniqueConstraint(name, version), )
129
130     def __repr__(self):
131         return '<{:}>'.format((self.__class__.__name__, self.id, self.name, self.version))
132
133     def is_published(self):
134         return self.when_published is not None
135
136
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'))
142
143     req = relationship('Format', foreign_keys=req_id, uselist=False)
144     resp = relationship('Format', foreign_keys=resp_id, uselist=False)
145
146     __table_args__ = (UniqueConstraint(req_id, resp_id), )
147
148     def __repr__(self):
149         return '<{:}>'.format((self.__class__.__name__, self.id, self.req, self.resp))