blob: 2a061bb5c1fbb57487d46b74691130dc58e89042 [file] [log] [blame]
Matteo Scandolo5e293c92017-08-08 13:05:23 -07001
2# Copyright 2017-present Open Networking Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16
Scott Baker8e6647a2016-06-20 17:16:20 -070017from core.models import User
Scott Bakerc15556d2017-05-06 09:55:01 -070018from services.rcord.models import CordSubscriberRoot
Scott Baker8e6647a2016-06-20 17:16:20 -070019
20from xosresource import XOSResource
21
22class XOSCORDUser(XOSResource):
23 provides = "tosca.nodes.CORDUser"
24
25 def get_model_class_name(self):
26 return "CORDUser"
27
28 def get_subscriber_root(self, throw_exception=True):
29 sub_name = self.get_requirement("tosca.relationships.SubscriberDevice", throw_exception=throw_exception)
30 sub = self.get_xos_object(CordSubscriberRoot, name=sub_name, throw_exception=throw_exception)
31 return sub
32
33 def get_existing_objs(self):
34 result = []
35 sub = self.get_subscriber_root(throw_exception=False)
36 if not sub:
37 return []
38 for user in sub.devices:
39 if user["name"] == self.obj_name:
40 result.append(user)
41 return result
42
43 def get_xos_args(self):
44 args = {"name": self.obj_name,
45 "level": self.get_property("level"),
46 "mac": self.get_property("mac")}
47 return args
48
49
50 def create(self):
51 xos_args = self.get_xos_args()
52 sub = self.get_subscriber_root()
53
54 sub.create_device(**xos_args)
55 sub.save()
56
57 self.info("Created CORDUser %s for Subscriber %s" % (self.obj_name, sub.name))
58
59 def update(self, obj):
60 pass
61
62 def delete(self, obj):
63 if (self.can_delete(obj)):
64 self.info("destroying CORDUser %s" % obj["name"])
65 sub = self.get_subscriber_root()
66 sub.delete_user(obj["id"])
67 sub.save()
68
69 def can_delete(self, obj):
70 return True
71