Docs: Fix issue of the docs; Update 'sol0018' to 'sol018'
[pti/o2.git] / o2dms / adapter / orm.py
1 # Copyright (C) 2021 Wind River Systems, Inc.
2 #
3 #  Licensed under the Apache License, Version 2.0 (the "License");
4 #  you may not use this file except in compliance with the License.
5 #  You may obtain a copy of the License at
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
9 #  Unless required by applicable law or agreed to in writing, software
10 #  distributed under the License is distributed on an "AS IS" BASIS,
11 #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 #  See the License for the specific language governing permissions and
13 #  limitations under the License.
14
15 from retry import retry
16 from sqlalchemy import (
17     Table,
18     MetaData,
19     Column,
20     Integer,
21     String,
22     Text,
23     # Date,
24     DateTime,
25     # ForeignKey,
26     # engine,
27     # event,
28     exc
29 )
30
31 from sqlalchemy.orm import mapper
32 from o2dms.domain import dms as dmsModel
33
34 from o2common.helper import o2logging
35 logger = o2logging.get_logger(__name__)
36
37 metadata = MetaData()
38
39 nfDeploymentDesc = Table(
40     "nfDeploymentDesc",
41     metadata,
42     Column("updatetime", DateTime),
43     Column("createtime", DateTime),
44     Column("hash", String(255)),
45     Column("version_number", Integer),
46
47     Column("id", String(255), primary_key=True),
48     Column("deploymentManagerId", String(255)),
49     Column("name", String(255)),
50     Column("description", String(255)),
51     Column("inputParams", Text()),
52     Column("outputParams", String(255)),
53     Column("artifactRepoUrl", String(255)),
54     Column("artifactName", String(255)),
55     # Column("extensions", String(1024))
56 )
57
58 nfDeployment = Table(
59     "nfDeployment",
60     metadata,
61     Column("updatetime", DateTime),
62     Column("createtime", DateTime),
63     Column("hash", String(255)),
64     Column("version_number", Integer),
65
66     Column("id", String(255), primary_key=True),
67     Column("deploymentManagerId", String(255)),
68     Column("name", String(255)),
69     Column("description", String(255)),
70     Column("descriptorId", String(255)),
71     Column("parentDeploymentId", String(255)),
72     Column("status", Integer)
73 )
74
75 nfOCloudVResource = Table(
76     "nfOcloudVRes",
77     metadata,
78     Column("updatetime", DateTime),
79     Column("createtime", DateTime),
80     Column("hash", String(255)),
81     Column("version_number", Integer),
82
83     Column("id", String(255), primary_key=True),
84     Column("deploymentManagerId", String(255)),
85     Column("name", String(255)),
86     Column("description", String(255)),
87     Column("descriptorId", String(255)),
88     Column("vresourceType", String(255)),
89     Column("status", Integer),
90     Column("metadata", String(2048)),
91     Column("nfDeploymentId", String(255))
92 )
93
94
95 @retry((exc.IntegrityError), tries=3, delay=2)
96 def wait_for_metadata_ready(engine):
97     # wait for mapper ready
98     metadata.create_all(engine, checkfirst=True)
99     logger.info("metadata is ready")
100
101
102 def start_o2dms_mappers(engine=None):
103     logger.info("Starting O2 DMS mappers")
104
105     mapper(dmsModel.NfDeploymentDesc, nfDeploymentDesc)
106     mapper(dmsModel.NfDeployment, nfDeployment)
107     mapper(dmsModel.NfOCloudVResource, nfOCloudVResource)
108
109     if engine is not None:
110         wait_for_metadata_ready(engine)