Merge "Add additional unit tests and asserts for NS Heal."
[vfc/nfvo/lcm.git] / lcm / ns / ns_create.py
1 # Copyright 2016 ZTE Corporation.
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 import logging
15 import uuid
16
17 from lcm.pub.database.models import NSDModel, NSInstModel
18 from lcm.pub.exceptions import NSLCMException
19 from lcm.pub.utils.timeutil import now_time
20
21 logger = logging.getLogger(__name__)
22
23
24 class CreateNSService(object):
25     def __init__(self, nsd_id, ns_name, description):
26         self.nsd_id = nsd_id
27         self.ns_name = ns_name
28         self.description = description
29         self.ns_inst_id = ''
30         self.ns_package_id = ''
31
32     def do_biz(self):
33         self.check_nsd_valid()
34         self.check_ns_inst_name_exist()
35         self.create_ns_inst()
36         logger.debug("CreateNSService::do_biz::ns_inst_id=%s" % self.ns_inst_id)
37         return self.ns_inst_id
38
39     def check_nsd_valid(self):
40         logger.debug("CreateNSService::check_nsd_valid::nsd_id=%s" % self.nsd_id)
41         ns_package_info = NSDModel.objects.filter(nsd_id=self.nsd_id)
42         if not ns_package_info:
43             raise NSLCMException("nsd(%s) not exists." % self.nsd_id)
44         self.ns_package_id = ns_package_info[0].id
45         logger.debug("CreateNSService::check_nsd_valid::ns_package_id=%s" % self.ns_package_id)
46
47     def check_ns_inst_name_exist(self):
48         is_exist = NSInstModel.objects.filter(name=self.ns_name).exists()
49         logger.debug("CreateNSService::check_ns_inst_name_exist::is_exist=%s" % is_exist)
50         if is_exist:
51             raise NSLCMException("ns(%s) already existed." % self.ns_name)
52
53     def create_ns_inst(self):
54         self.ns_inst_id = str(uuid.uuid4())
55         logger.debug("CreateNSService::create_ns_inst::ns_inst_id=%s" % self.ns_inst_id)
56         NSInstModel(id=self.ns_inst_id, name=self.ns_name, nspackage_id=self.ns_package_id, 
57                     nsd_id=self.nsd_id, description=self.description, status='empty', 
58                     lastuptime=now_time()).save()
59