2 # Copyright 2016 RIFT.IO Inc
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
18 import concurrent
.futures
21 from gi
.repository
import (
31 import rift
.mano
.utils
.juju_api
as juju
34 class ConfigAgentAccountNotFound(Exception):
37 class JujuClient(object):
38 def __init__(self
, log
, ip
, port
, user
, passwd
):
45 self
._api
= juju
.JujuApi(log
=log
,
47 user
=user
, secret
=passwd
)
50 def validate_account_creds(self
):
51 status
= RwcalYang
.CloudConnectionStatus()
53 env
= self
._api
._get
_env
()
54 except juju
.JujuEnvError
as e
:
55 msg
= "JujuClient: Invalid account credentials: %s", str(e
)
58 except ConnectionRefusedError
as e
:
59 msg
= "JujuClient: Wrong IP or Port: %s", str(e
)
62 except Exception as e
:
63 msg
= "JujuClient: Connection Failed: %s", str(e
)
67 status
.status
= "success"
68 status
.details
= "Connection was successful"
69 self
._log
.info("JujuClient: Connection Successful")
74 class ConfigAgentAccount(object):
75 def __init__(self
, log
, account_msg
):
77 self
._account
_msg
= account_msg
.deep_copy()
79 if account_msg
.account_type
== "juju":
80 self
._cfg
_agent
_client
_plugin
= JujuClient(
82 account_msg
.juju
.ip_address
,
83 account_msg
.juju
.port
,
84 account_msg
.juju
.user
,
85 account_msg
.juju
.secret
)
87 self
._cfg
_agent
_client
_plugin
= None
89 self
._status
= RwConfigAgentYang
.ConfigAgentAccount_ConnectionStatus(
91 details
="Connection status lookup not started"
94 self
._validate
_task
= None
98 return self
._account
_msg
.name
101 def account_msg(self
):
102 return self
._account
_msg
105 def account_type(self
):
106 return self
._account
_msg
.account_type
109 def connection_status(self
):
112 def update_from_cfg(self
, cfg
):
113 self
._log
.debug("Updating parent ConfigAgentAccount to %s", cfg
)
114 raise NotImplementedError("Update config agent account not yet supported")
117 def validate_cfg_agent_account_credentials(self
, loop
):
118 self
._log
.debug("Validating Config Agent Account %s, credential status %s", self
._account
_msg
, self
._status
)
120 self
._status
= RwConfigAgentYang
.ConfigAgentAccount_ConnectionStatus(
122 details
="Config Agent account connection validation in progress"
125 if self
._cfg
_agent
_client
_plugin
is None:
126 self
._status
= RwConfigAgentYang
.ConfigAgentAccount_ConnectionStatus(
128 details
="Config Agent account does not support validation of account creds"
132 status
= yield from loop
.run_in_executor(
134 self
._cfg
_agent
_client
_plugin
.validate_account_creds
136 self
._status
= RwConfigAgentYang
.ConfigAgentAccount_ConnectionStatus
.from_dict(status
.as_dict())
137 except Exception as e
:
138 self
._status
= RwConfigAgentYang
.ConfigAgentAccount_ConnectionStatus(
140 details
="Error - " + str(e
)
143 self
._log
.info("Got config agent account validation response: %s", self
._status
)
145 def start_validate_credentials(self
, loop
):
146 if self
._validate
_task
is not None:
147 self
._validate
_task
.cancel()
148 self
._validate
_task
= None
150 self
._validate
_task
= asyncio
.ensure_future(
151 self
.validate_cfg_agent_account_credentials(loop
),
155 class CfgAgentDtsOperdataHandler(object):
156 def __init__(self
, dts
, log
, loop
):
161 self
.cfg_agent_accounts
= {}
163 def add_cfg_agent_account(self
, account_msg
):
164 account
= ConfigAgentAccount(self
._log
, account_msg
)
165 self
.cfg_agent_accounts
[account
.name
] = account
166 self
._log
.info("ConfigAgent Operdata Handler added. Starting account validation")
168 account
.start_validate_credentials(self
._loop
)
170 def delete_cfg_agent_account(self
, account_name
):
171 del self
.cfg_agent_accounts
[account_name
]
172 self
._log
.info("ConfigAgent Operdata Handler deleted.")
174 def get_saved_cfg_agent_accounts(self
, cfg_agent_account_name
):
175 ''' Get Config Agent Account corresponding to passed name, or all saved accounts if name is None'''
176 saved_cfg_agent_accounts
= []
178 if cfg_agent_account_name
is None or cfg_agent_account_name
== "":
179 cfg_agent_accounts
= list(self
.cfg_agent_accounts
.values())
180 saved_cfg_agent_accounts
.extend(cfg_agent_accounts
)
181 elif cfg_agent_account_name
in self
.cfg_agent_accounts
:
182 account
= self
.cfg_agent_accounts
[cfg_agent_account_name
]
183 saved_cfg_agent_accounts
.append(account
)
185 errstr
= "Config Agent account {} does not exist".format(cfg_agent_account_name
)
186 raise KeyError(errstr
)
188 return saved_cfg_agent_accounts
191 def _register_show_status(self
):
192 def get_xpath(cfg_agent_name
=None):
193 return "D,/rw-config-agent:config-agent/account{}/connection-status".format(
194 "[name='%s']" % cfg_agent_name
if cfg_agent_name
is not None else ''
198 def on_prepare(xact_info
, action
, ks_path
, msg
):
199 path_entry
= RwConfigAgentYang
.ConfigAgentAccount
.schema().keyspec_to_entry(ks_path
)
200 cfg_agent_account_name
= path_entry
.key00
.name
201 self
._log
.debug("Got show cfg_agent connection status request: %s", ks_path
.create_string())
204 saved_accounts
= self
.get_saved_cfg_agent_accounts(cfg_agent_account_name
)
205 for account
in saved_accounts
:
206 connection_status
= account
.connection_status
207 self
._log
.debug("Responding to config agent connection status request: %s", connection_status
)
208 xact_info
.respond_xpath(
209 rwdts
.XactRspCode
.MORE
,
210 xpath
=get_xpath(account
.name
),
211 msg
=account
.connection_status
,
213 except KeyError as e
:
214 self
._log
.warning(str(e
))
215 xact_info
.respond_xpath(rwdts
.XactRspCode
.NA
)
218 xact_info
.respond_xpath(rwdts
.XactRspCode
.ACK
)
220 yield from self
._dts
.register(
222 handler
=rift
.tasklets
.DTS
.RegistrationHandler(
223 on_prepare
=on_prepare
),
224 flags
=rwdts
.Flag
.PUBLISHER
,
227 def _register_validate_rpc(self
):
229 return "/rw-config-agent:update-cfg-agent-status"
232 def on_prepare(xact_info
, action
, ks_path
, msg
):
233 if not msg
.has_field("cfg_agent_account"):
234 raise ConfigAgentAccountNotFound("Config Agent account name not provided")
236 cfg_agent_account_name
= msg
.cfg_agent_account
238 account
= self
.cfg_agent_accounts
[cfg_agent_account_name
]
240 raise ConfigAgentAccountNotFound("Config Agent account name %s not found" % cfg_agent_account_name
)
242 account
.start_validate_credentials(self
._loop
)
244 xact_info
.respond_xpath(rwdts
.XactRspCode
.ACK
)
246 yield from self
._dts
.register(
248 handler
=rift
.tasklets
.DTS
.RegistrationHandler(
249 on_prepare
=on_prepare
251 flags
=rwdts
.Flag
.PUBLISHER
,
256 yield from self
._register
_show
_status
()
257 yield from self
._register
_validate
_rpc
()
259 class ConfigAgentJob(object):
260 """A wrapper over the config agent job object, providing some
261 convenience functions.
263 YangData_Nsr_NsInstanceOpdata_Nsr_ConfigAgentJob contains
270 # The normalizes the state terms from Juju to our yang models
272 STATUS_MAP
= {"completed": "success",
273 "pending" : "pending",
274 "running" : "pending",
275 "failed" : "failure"}
277 def __init__(self
, nsr_id
, job
, tasks
=None):
280 nsr_id (uuid): ID of NSR record
281 job (YangData_Nsr_NsInstanceOpdata_Nsr_ConfigAgentJob): Gi object
282 tasks: List of asyncio.tasks. If provided the job monitor will
283 use it to monitor the tasks instead of the execution IDs
293 return self
._job
.job_id
298 return self
._job
.job_name
301 def job_status(self
):
302 """Status of the job (success|pending|failure)"""
303 return self
._job
.job_status
306 def job_status(self
, value
):
307 """Setter for job status"""
308 self
._job
.job_status
= value
317 """Xpath of the job"""
318 return ("D,/nsr:ns-instance-opdata" +
319 "/nsr:nsr[nsr:ns-instance-config-ref='{}']" +
320 "/nsr:config-agent-job[nsr:job-id='{}']"
321 ).format(self
.nsr_id
, self
.id)
325 """Registration handle for the job"""
330 """Setter for registration handle"""
334 def convert_rpc_input_to_job(nsr_id
, rpc_output
, tasks
):
335 """A helper function to convert the YangOutput_Nsr_ExecNsConfigPrimitive
336 to YangData_Nsr_NsInstanceOpdata_Nsr_ConfigAgentJob (NsrYang)
339 nsr_id (uuid): NSR ID
340 rpc_output (YangOutput_Nsr_ExecNsConfigPrimitive): RPC output
341 tasks (list): A list of asyncio.Tasks
346 # Shortcuts to prevent the HUUGE names.
347 CfgAgentJob
= NsrYang
.YangData_Nsr_NsInstanceOpdata_Nsr_ConfigAgentJob
348 CfgAgentVnfr
= NsrYang
.YangData_Nsr_NsInstanceOpdata_Nsr_ConfigAgentJob_Vnfr
349 CfgAgentPrimitive
= NsrYang
.YangData_Nsr_NsInstanceOpdata_Nsr_ConfigAgentJob_Vnfr_Primitive
350 CfgAgentPrimitiveParam
= NsrYang
.YangData_Nsr_NsInstanceOpdata_Nsr_ConfigAgentJob_Vnfr_Primitive_Parameter
352 job
= CfgAgentJob
.from_dict({
353 "job_id": rpc_output
.job_id
,
354 "job_name" : rpc_output
.name
,
355 "job_status": "pending",
356 "triggered_by": rpc_output
.triggered_by
,
357 "create_time": rpc_output
.create_time
,
358 "job_status_details": rpc_output
.job_status_details
if rpc_output
.job_status_details
is not None else None,
359 "parameter": [param
.as_dict() for param
in rpc_output
.parameter
],
360 "parameter_group": [pg
.as_dict() for pg
in rpc_output
.parameter_group
]
363 for vnfr
in rpc_output
.vnf_out_list
:
364 vnfr_job
= CfgAgentVnfr
.from_dict({
365 "id": vnfr
.vnfr_id_ref
,
366 "vnf_job_status": "pending",
369 for primitive
in vnfr
.vnf_out_primitive
:
370 vnf_primitive
= CfgAgentPrimitive
.from_dict({
371 "name": primitive
.name
,
372 "execution_status": ConfigAgentJob
.STATUS_MAP
[primitive
.execution_status
],
373 "execution_id": primitive
.execution_id
376 # Copy over the input param
377 for param
in primitive
.parameter
:
378 vnf_primitive
.parameter
.append(
379 CfgAgentPrimitiveParam
.from_dict({
384 vnfr_job
.primitive
.append(vnf_primitive
)
386 job
.vnfr
.append(vnfr_job
)
388 return ConfigAgentJob(nsr_id
, job
, tasks
)
391 class ConfigAgentJobMonitor(object):
392 """Job monitor: Polls the Juju controller and get the status.
394 If all Primitive are success, then vnf & nsr status will be "success"
395 If any one Primitive reaches a failed state then both vnf and nsr will fail.
399 def __init__(self
, dts
, log
, job
, executor
, loop
, config_plugin
):
404 job (ConfigAgentJob): ConfigAgentJob instance
405 executor (concurrent.futures): Executor for juju status api calls
406 loop (eventloop): Current event loop instance
407 config_plugin : Config plugin to be used.
412 self
.executor
= executor
413 self
.polling_period
= ConfigAgentJobMonitor
.POLLING_PERIOD
414 self
.config_plugin
= config_plugin
418 def _monitor_processes(self
, registration_handle
):
421 for process
in self
.job
.tasks
:
422 if isinstance(process
, asyncio
.subprocess
.Process
):
423 rc
= yield from process
.wait()
424 err
= yield from process
.stderr
.read()
428 rc
= yield from process
431 self
.log
.debug("Process {} returned rc: {}, err: {}".
432 format(process
, rc
, err
))
435 errs
+= "<error>{}</error>".format(err
)
439 self
.job
.job_status
= "success"
441 self
.job
.job_status
= "failure"
444 self
.job
.job
.job_status_details
= errs
446 registration_handle
.update_element(self
.job
.xpath
, self
.job
.job
)
448 def get_error_details(self
):
449 '''Get the error details from failed primitives'''
451 for vnfr
in self
.job
.job
.vnfr
:
452 if vnfr
.vnf_job_status
!= "failure":
455 for primitive
in vnfr
.primitive
:
456 if primitive
.execution_status
== "failure":
458 if primitive
.execution_error_details
:
459 errs
+= primitive
.execution_error_details
461 errs
+= '{}: Unknown error'.format(primitive
.name
)
467 def publish_action_status(self
):
469 Starts publishing the status for jobs/primitives
471 registration_handle
= yield from self
.dts
.register(
472 xpath
=self
.job
.xpath
,
473 handler
=rift
.tasklets
.DTS
.RegistrationHandler(),
474 flags
=(rwdts
.Flag
.PUBLISHER | rwdts
.Flag
.NO_PREP_READ
),
477 self
.log
.debug('preparing to publish job status for {}'.format(self
.job
.xpath
))
478 self
.job
.regh
= registration_handle
481 registration_handle
.create_element(self
.job
.xpath
, self
.job
.job
)
483 # If the config is done via a user defined script
484 if self
.job
.tasks
is not None:
485 yield from self
._monitor
_processes
(registration_handle
)
489 # Run until pending moves to either failure/success
490 while self
.job
.job_status
== "pending":
493 if curr
- prev
< self
.polling_period
:
494 pause
= self
.polling_period
- (curr
- prev
)
495 yield from asyncio
.sleep(pause
, loop
=self
.loop
)
500 for vnfr
in self
.job
.job
.vnfr
:
501 task
= self
.loop
.create_task(self
.get_vnfr_status(vnfr
))
504 # Exit, if no tasks are found
508 yield from asyncio
.wait(tasks
, loop
=self
.loop
)
510 job_status
= [task
.result() for task
in tasks
]
512 if "failure" in job_status
:
513 self
.job
.job_status
= "failure"
514 errs
= self
.get_error_details()
516 self
.job
.job
.job_status_details
= errs
517 elif "pending" in job_status
:
518 self
.job
.job_status
= "pending"
520 self
.job
.job_status
= "success"
522 # self.log.debug("Publishing job status: {} at {} for nsr id: {}".format(
523 # self.job.job_status,
527 registration_handle
.update_element(self
.job
.xpath
, self
.job
.job
)
530 except Exception as e
:
531 self
.log
.exception(e
)
536 def get_vnfr_status(self
, vnfr
):
537 """Schedules tasks for all containing primitives and updates it's own
541 vnfr : Vnfr job record containing primitives.
544 (str): "success|failure|pending"
549 for primitive
in vnfr
.primitive
:
550 if primitive
.execution_status
!= 'pending':
553 if primitive
.execution_id
== "":
554 # Actions which failed to queue can have empty id
555 job_status
.append(primitive
.execution_status
)
558 elif primitive
.execution_id
== "config":
559 # Config job. Check if service is active
560 task
= self
.loop
.create_task(self
.get_service_status(vnfr
.id, primitive
))
563 task
= self
.loop
.create_task(self
.get_primitive_status(primitive
))
568 yield from asyncio
.wait(tasks
, loop
=self
.loop
)
570 job_status
.extend([task
.result() for task
in tasks
])
571 if "failure" in job_status
:
572 vnfr
.vnf_job_status
= "failure"
575 elif "pending" in job_status
:
576 vnfr
.vnf_job_status
= "pending"
580 vnfr
.vnf_job_status
= "success"
584 def get_service_status(self
, vnfr_id
, primitive
):
586 status
= yield from self
.loop
.run_in_executor(
588 self
.config_plugin
.get_service_status
,
592 self
.log
.debug("Service status: {}".format(status
))
593 if status
in ['error', 'blocked']:
594 self
.log
.warning("Execution of config {} failed: {}".
595 format(primitive
.execution_id
, status
))
596 primitive
.execution_error_details
= 'Config failed'
598 elif status
in ['active']:
605 except Exception as e
:
606 self
.log
.exception(e
)
609 primitive
.execution_status
= status
610 return primitive
.execution_status
613 def get_primitive_status(self
, primitive
):
615 Queries the juju api and gets the status of the execution id.
618 primitive : Primitive containing the execution ID.
622 resp
= yield from self
.loop
.run_in_executor(
624 self
.config_plugin
.get_action_status
,
625 primitive
.execution_id
628 self
.log
.debug("Action status: {}".format(resp
))
629 status
= resp
['status']
630 if status
== 'failed':
631 self
.log
.warning("Execution of action {} failed: {}".
632 format(primitive
.execution_id
, resp
))
633 primitive
.execution_error_details
= resp
['message']
635 except Exception as e
:
636 self
.log
.exception(e
)
639 # Handle case status is None
641 primitive
.execution_status
= ConfigAgentJob
.STATUS_MAP
[status
]
643 primitive
.execution_status
= "failure"
645 return primitive
.execution_status
648 class CfgAgentJobDtsHandler(object):
649 """Dts Handler for CfgAgent"""
650 XPATH
= "D,/nsr:ns-instance-opdata/nsr:nsr/nsr:config-agent-job"
652 def __init__(self
, dts
, log
, loop
, nsm
, cfgm
):
659 cfgm : ConfigManager.
668 self
._nsr
_regh
= None
672 """ Return registration handle """
677 """ Return the NSManager manager instance """
682 """ Return the ConfigManager manager instance """
686 def cfg_job_xpath(nsr_id
, job_id
):
687 return ("D,/nsr:ns-instance-opdata" +
688 "/nsr:nsr[nsr:ns-instance-config-ref = '{}']" +
689 "/nsr:config-agent-job[nsr:job-id='{}']").format(nsr_id
, job_id
)
693 """ Register for NS monitoring read from dts """
696 def on_prepare(xact_info
, action
, ks_path
, msg
):
697 """ prepare callback from dts """
698 xpath
= ks_path
.to_xpath(RwNsrYang
.get_schema())
699 if action
== rwdts
.QueryAction
.READ
:
700 schema
= RwNsrYang
.YangData_Nsr_NsInstanceOpdata_Nsr
.schema()
701 path_entry
= schema
.keyspec_to_entry(ks_path
)
703 nsr_id
= path_entry
.key00
.ns_instance_config_ref
705 #print("###>>> self.nsm.nsrs:", self.nsm.nsrs)
707 if nsr_id
is None or nsr_id
== "":
708 nsrs
= list(self
.nsm
.nsrs
.values())
709 nsr_ids
= [nsr
.id for nsr
in nsrs
if nsr
is not None]
713 for nsr_id
in nsr_ids
:
714 jobs
= self
.cfgm
.get_job(nsr_id
)
717 xact_info
.respond_xpath(
718 rwdts
.XactRspCode
.MORE
,
719 CfgAgentJobDtsHandler
.cfg_job_xpath(nsr_id
, job
.id),
722 except Exception as e
:
723 self
._log
.exception("Caught exception:%s", str(e
))
724 xact_info
.respond_xpath(rwdts
.XactRspCode
.ACK
)
727 xact_info
.respond_xpath(rwdts
.XactRspCode
.NA
)
729 hdl
= rift
.tasklets
.DTS
.RegistrationHandler(on_prepare
=on_prepare
,)
730 with self
._dts
.group_create() as group
:
731 self
._regh
= group
.register(xpath
=CfgAgentJobDtsHandler
.XPATH
,
733 flags
=rwdts
.Flag
.PUBLISHER
,
737 def _terminate_nsr(self
, nsr_id
):
738 self
._log
.debug("NSR {} being terminated".format(nsr_id
))
739 jobs
= self
.cfgm
.get_job(nsr_id
)
741 path
= CfgAgentJobDtsHandler
.cfg_job_xpath(nsr_id
, job
.id)
742 with self
._dts
.transaction() as xact
:
743 self
._log
.debug("Deleting job: {}".format(path
))
744 job
.regh
.delete_element(path
)
745 self
._log
.debug("Deleted job: {}".format(path
))
747 # Remove the NSR id in manager
748 self
.cfgm
.del_nsr(nsr_id
)
752 return "D,/nsr:ns-instance-opdata/nsr:nsr"
755 def register_for_nsr(self
):
756 """ Register for NSR changes """
759 def on_prepare(xact_info
, query_action
, ks_path
, msg
):
760 """ This NSR is created """
761 self
._log
.debug("Received NSR instantiate on_prepare (%s:%s:%s)",
766 if (query_action
== rwdts
.QueryAction
.UPDATE
or
767 query_action
== rwdts
.QueryAction
.CREATE
):
769 elif query_action
== rwdts
.QueryAction
.DELETE
:
770 nsr_id
= msg
.ns_instance_config_ref
771 asyncio
.ensure_future(self
._terminate
_nsr
(nsr_id
), loop
=self
._loop
)
773 raise NotImplementedError(
774 "%s action on cm-state not supported",
777 xact_info
.respond_xpath(rwdts
.XactRspCode
.ACK
)
780 handler
= rift
.tasklets
.DTS
.RegistrationHandler(on_prepare
=on_prepare
)
781 self
._nsr
_regh
= yield from self
._dts
.register(self
.nsr_xpath
,
782 flags
=rwdts
.Flag
.SUBSCRIBER | rwdts
.Flag
.DELTA_READY
,
784 except Exception as e
:
785 self
._log
.error("Failed to register for NSR changes as %s", str(e
))
788 class ConfigAgentJobManager(object):
789 """A central class that manager all the Config Agent related data,
790 Including updating the status
792 TODO: Needs to support multiple config agents.
794 def __init__(self
, dts
, log
, loop
, nsm
):
800 nsm : NsmTasklet instance
807 self
.handler
= CfgAgentJobDtsHandler(dts
, log
, loop
, nsm
, self
)
808 self
.executor
= concurrent
.futures
.ThreadPoolExecutor(max_workers
=1)
810 def add_job(self
, rpc_output
, tasks
=None):
811 """Once an RPC is trigger add a now job
814 rpc_output (YangOutput_Nsr_ExecNsConfigPrimitive): Rpc output
815 rpc_input (YangInput_Nsr_ExecNsConfigPrimitive): Rpc input
816 tasks(list) A list of asyncio.Tasks
819 nsr_id
= rpc_output
.nsr_id_ref
821 job
= ConfigAgentJob
.convert_rpc_input_to_job(nsr_id
, rpc_output
, tasks
)
823 self
.log
.debug("Creating a job monitor for Job id: {}".format(
826 if nsr_id
not in self
.jobs
:
827 self
.jobs
[nsr_id
] = [job
]
829 self
.jobs
[nsr_id
].append(job
)
831 # If the tasks are none, assume juju actions
832 # TBD: This logic need to be revisited
833 ca
= self
.nsm
.config_agent_plugins
[0]
835 for agent
in self
.nsm
.config_agent_plugins
:
836 if agent
.agent_type
== 'juju':
840 # For every Job we will schedule a new monitoring process.
841 job_monitor
= ConfigAgentJobMonitor(
849 task
= self
.loop
.create_task(job_monitor
.publish_action_status())
851 def get_job(self
, nsr_id
):
852 """Get the job associated with the NSR Id, if present."""
854 return self
.jobs
[nsr_id
]
858 def del_nsr(self
, nsr_id
):
859 """Delete a NSR id from the jobs list"""
860 if nsr_id
in self
.jobs
:
861 self
.jobs
.pop(nsr_id
)
865 yield from self
.handler
.register()
866 yield from self
.handler
.register_for_nsr()