Move 'launchpad' directory to 'RIFT_VAR_ROOT' from 'RIFT_ARTIFACTS'
[osm/SO.git] / rwcm / plugins / rwconman / rift / tasklets / rwconmantasklet / rwconman_events.py
1
2 #
3 # Copyright 2016 RIFT.IO Inc
4 #
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
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
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.
16 #
17
18
19 import ncclient
20 import ncclient.asyncio_manager
21 import tornado.httpclient as tornadoh
22 import asyncio.subprocess
23 import asyncio
24 import time
25 import sys
26 import os, stat
27
28 import gi
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')
34
35 from gi.repository import (
36 RwDts as rwdts,
37 RwYang,
38 RwConmanYang as conmanY,
39 RwNsrYang as nsrY,
40 RwVnfrYang as vnfrY,
41 )
42
43 import rift.tasklets
44
45 if sys.version_info < (3, 4, 4):
46 asyncio.ensure_future = asyncio.async
47
48 def log_this_vnf(vnf_cfg):
49 log_vnf = ""
50 used_item_list = ['nsr_name', 'vnfr_name', 'member_vnf_index', 'mgmt_ip_address']
51 for item in used_item_list:
52 if item in vnf_cfg:
53 if item == 'mgmt_ip_address':
54 log_vnf += "({})".format(vnf_cfg[item])
55 else:
56 log_vnf += "{}/".format(vnf_cfg[item])
57 return log_vnf
58
59 class ConfigManagerROifConnectionError(Exception):
60 pass
61 class ScriptError(Exception):
62 pass
63
64
65 class ConfigManagerEvents(object):
66 def __init__(self, dts, log, loop, parent):
67 self._dts = dts
68 self._log = log
69 self._loop = loop
70 self._parent = parent
71 self._nsr_xpath = "/cm-state/cm-nsr"
72
73 @asyncio.coroutine
74 def register(self):
75 pass
76
77 @asyncio.coroutine
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)
81
82 @asyncio.coroutine
83 def apply_vnf_config(self, vnf_cfg):
84 self._log.debug("apply_vnf_config VNF:{}"
85 .format(log_this_vnf(vnf_cfg)))
86
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)
90
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))
96 return True
97
98 yield from self.update_vnf_state(vnf_cfg, conmanY.RecordState.CFG_SEND)
99 try:
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()
106 else:
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)
110 return True
111
112 #Update VNF state
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'],
119 vnf_cfg['cfg_file'],
120 log_this_vnf(vnf_cfg),
121 str(e))
122 #raise
123 return False
124
125 return True
126
127 class ConfigManagerVNFjujuconf(object):
128
129 def __init__(self, log, loop, parent, vnf_cfg):
130 self._log = log
131 self._loop = loop
132 self._parent = parent
133 self._manager = None
134 self._vnf_cfg = vnf_cfg
135
136 #@asyncio.coroutine
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))
140 try:
141 args = ['python3',
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']),
147 vnf_cfg['cfg_file']]
148 self._log.error("juju script command (%s)", args)
149
150 proc = yield from asyncio.create_subprocess_exec(
151 *args,
152 stdout=asyncio.subprocess.PIPE)
153 juju_msg = yield from proc.stdout.read()
154 rc = yield from proc.wait()
155
156 if rc != 0:
157 raise ScriptError(
158 "Juju config returned error code : %s" % rc
159 )
160
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))
164 raise