3 # Copyright 2016 RIFT.IO Inc
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
19 @author Varun Prasad (varun.prasad@riftio.com)
27 from gi
.repository
import RwDts
as rwdts
28 from . import core
, ns_subscriber
, vnf_subscriber
31 class SubscriberStore(core
.SubscriberDtsHandler
):
32 """A convenience class that hold all the VNF and NS related config and Opdata
34 KEY
= enum
.Enum('KEY', 'NSR NSD VNFD VNFR')
36 def __init__(self
, log
, dts
, loop
, project
, callback
=None):
37 super().__init
__(log
, dts
, loop
, project
)
39 params
= (self
.log
, self
.dts
, self
.loop
, self
.project
)
41 self
._nsr
_sub
= ns_subscriber
.NsrCatalogSubscriber(*params
, callback
=self
.on_nsr_change
)
43 self
._nsd
_sub
= ns_subscriber
.NsdCatalogSubscriber(*params
)
45 self
._vnfr
_sub
= vnf_subscriber
.VnfrCatalogSubscriber(*params
, callback
=self
.on_vnfr_change
)
47 self
._vnfd
_sub
= vnf_subscriber
.VnfdCatalogSubscriber(*params
)
51 return list(self
._vnfd
_sub
.reg
.get_xact_elements())
55 return list(self
._nsd
_sub
.reg
.get_xact_elements())
59 return list(self
._vnfrs
.values())
63 return list(self
._nsrs
.values())
65 def _unwrap(self
, values
, id_name
):
69 self
.log
.exception("Unable to find the object with the given "
70 "ID {}".format(id_name
))
72 def get_nsr(self
, nsr_id
):
73 values
= [nsr
for nsr
in self
.nsr
if nsr
.ns_instance_config_ref
== nsr_id
]
74 return self
._unwrap
(values
, nsr_id
)
76 def get_nsd(self
, nsd_id
):
77 values
= [nsd
for nsd
in self
.nsd
if nsd
.id == nsd_id
]
78 return self
._unwrap
(values
, nsd_id
)
80 def get_vnfr(self
, vnfr_id
):
81 values
= [vnfr
for vnfr
in self
.vnfr
if vnfr
.id == vnfr_id
]
82 return self
._unwrap
(values
, vnfr_id
)
84 def get_vnfd(self
, vnfd_id
):
85 values
= [vnfd
for vnfd
in self
.vnfd
if vnfd
.id == vnfd_id
]
86 return self
._unwrap
(values
, vnfd_id
)
90 yield from self
._vnfd
_sub
.register()
91 yield from self
._nsd
_sub
.register()
92 yield from self
._vnfr
_sub
.register()
93 yield from self
._nsr
_sub
.register()
96 self
._log
.debug("De-register store for project {}".
97 format(self
._project
))
98 self
._vnfd
_sub
.deregister()
99 self
._nsd
_sub
.deregister()
100 self
._vnfr
_sub
.deregister()
101 self
._nsr
_sub
.deregister()
104 def refresh_store(self
, subsriber
, store
):
105 itr
= yield from self
.dts
.query_read(subsriber
.get_xpath())
109 result
= yield from res
110 result
= result
.result
111 store
[getattr(result
, subsriber
.key_name())] = result
113 def on_nsr_change(self
, msg
, action
):
114 if action
== rwdts
.QueryAction
.DELETE
:
115 if msg
.ns_instance_config_ref
in self
._nsrs
:
116 del self
._nsrs
[msg
.ns_instance_config_ref
]
119 self
.loop
.create_task(self
.refresh_store(self
._nsr
_sub
, self
._nsrs
))
121 def on_vnfr_change(self
, msg
, action
):
122 if action
== rwdts
.QueryAction
.DELETE
:
123 if msg
.id in self
._vnfrs
:
124 del self
._vnfrs
[msg
.id]
127 self
.loop
.create_task(self
.refresh_store(self
._vnfr
_sub
, self
._vnfrs
))