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.
20 import ncclient
.asyncio_manager
21 import tornado
.httpclient
as tornadoh
22 import asyncio
.subprocess
29 gi
.require_version('RwDts', '1.0')
30 gi
.require_version('RwYang', '1.0')
31 gi
.require_version('RwConmanYang', '1.0')
32 gi
.require_version('RwNsrYang', '1.0')
33 gi
.require_version('RwVnfrYang', '1.0')
35 from gi
.repository
import (
38 RwConmanYang
as conmanY
,
45 if sys
.version_info
< (3, 4, 4):
46 asyncio
.ensure_future
= asyncio
.async
48 def log_this_vnf(vnf_cfg
):
50 used_item_list
= ['nsr_name', 'vnfr_name', 'member_vnf_index', 'mgmt_ip_address']
51 for item
in used_item_list
:
53 if item
== 'mgmt_ip_address':
54 log_vnf
+= "({})".format(vnf_cfg
[item
])
56 log_vnf
+= "{}/".format(vnf_cfg
[item
])
59 class ConfigManagerROifConnectionError(Exception):
61 class ScriptError(Exception):
65 class ConfigManagerEvents(object):
66 def __init__(self
, dts
, log
, loop
, parent
):
71 self
._nsr
_xpath
= "/cm-state/cm-nsr"
78 def update_vnf_state(self
, vnf_cfg
, state
):
79 nsr_obj
= vnf_cfg
['nsr_obj']
80 yield from nsr_obj
.update_vnf_cm_state(vnf_cfg
['vnfr'], state
)
83 def apply_vnf_config(self
, vnf_cfg
):
84 self
._log
.debug("apply_vnf_config VNF:{}"
85 .format(log_this_vnf(vnf_cfg
)))
87 if vnf_cfg
['config_delay']:
88 yield from self
.update_vnf_state(vnf_cfg
, conmanY
.RecordState
.CFG_DELAY
)
89 yield from asyncio
.sleep(vnf_cfg
['config_delay'], loop
=self
._loop
)
91 # See if we are still alive!
92 if vnf_cfg
['nsr_obj'].being_deleted
:
93 # Don't do anything, just return
94 self
._log
.info("VNF : %s is being deleted, skipping configuration!",
95 log_this_vnf(vnf_cfg
))
98 yield from self
.update_vnf_state(vnf_cfg
, conmanY
.RecordState
.CFG_SEND
)
100 if vnf_cfg
['config_method'] == 'script':
101 self
._log
.info("Executing script for VNF cfg = %s, No action needed!", vnf_cfg
)
102 elif vnf_cfg
['config_method'] == 'juju':
103 self
._log
.info("Executing juju config for VNF cfg = %s!", vnf_cfg
)
104 jujuc
= ConfigManagerVNFjujuconf(self
._log
, self
._loop
, self
._parent
, vnf_cfg
)
105 yield from jujuc
.apply_edit_cfg()
107 self
._log
.error("Unknown configuration method(%s) received for %s",
108 vnf_cfg
['config_method'], vnf_cfg
['vnf_unique_name'])
109 yield from self
.update_vnf_state(vnf_cfg
, conmanY
.RecordState
.CFG_FAILED
)
113 yield from self
.update_vnf_state(vnf_cfg
, conmanY
.RecordState
.READY
)
114 self
._log
.info("Successfully applied configuration to VNF: %s",
115 log_this_vnf(vnf_cfg
))
116 except Exception as e
:
117 self
._log
.error("Applying configuration(%s) file(%s) to VNF: %s failed as: %s",
118 vnf_cfg
['config_method'],
120 log_this_vnf(vnf_cfg
),
127 class ConfigManagerVNFjujuconf(object):
129 def __init__(self
, log
, loop
, parent
, vnf_cfg
):
132 self
._parent
= parent
134 self
._vnf
_cfg
= vnf_cfg
137 def apply_edit_cfg(self
):
138 vnf_cfg
= self
._vnf
_cfg
139 self
._log
.debug("Attempting to apply juju conf to VNF: %s", log_this_vnf(vnf_cfg
))
142 vnf_cfg
['juju_script'],
143 '--server', vnf_cfg
['mgmt_ip_address'],
144 '--user', vnf_cfg
['user'],
145 '--password', vnf_cfg
['secret'],
146 '--port', str(vnf_cfg
['port']),
148 self
._log
.error("juju script command (%s)", args
)
150 proc
= yield from asyncio
.create_subprocess_exec(
152 stdout
=asyncio
.subprocess
.PIPE
)
153 juju_msg
= yield from proc
.stdout
.read()
154 rc
= yield from proc
.wait()
158 "Juju config returned error code : %s" % rc
161 self
._log
.debug("Juju config output (%s)", juju_msg
)
162 except Exception as e
:
163 self
._log
.error("Error (%s) while executing juju config", str(e
))