RIFT-14481 uptime for vlr, vnfr, nsr
[osm/SO.git] / rwlaunchpad / plugins / rwvnfm / rift / tasklets / rwvnfmtasklet / rwvnfmtasklet.py
index 17e6fbf..36f0351 100755 (executable)
@@ -1,4 +1,4 @@
-# 
+#
 #   Copyright 2016 RIFT.IO Inc
 #
 #   Licensed under the Apache License, Version 2.0 (the "License");
@@ -308,6 +308,14 @@ class VirtualDeploymentUnitRecord(object):
                     return conn_point.ip_address
         return "0.0.0.0"
 
+    def cp_mac_addr(self, cp_name):
+        """ Find mac address by connection point name """
+        if self._vm_resp is not None:
+            for conn_point in self._vm_resp.connection_points:
+                if conn_point.name == cp_name:
+                    return conn_point.mac_addr
+        return "00:00:00:00:00:00"
+
     def cp_id(self, cp_name):
         """ Find connection point id  by connection point name """
         if self._vm_resp is not None:
@@ -408,7 +416,8 @@ class VirtualDeploymentUnitRecord(object):
             icp_list.append({"name": cp.name,
                              "id": cp.id,
                              "type_yang": "VPORT",
-                             "ip_address": self.cp_ip_addr(cp.id)})
+                             "ip_address": self.cp_ip_addr(cp.id),
+                             "mac_address": self.cp_mac_addr(cp.id)})
 
             ii_list.append({"name": intf.name,
                             "vdur_internal_connection_point_ref": cp.id,
@@ -423,7 +432,10 @@ class VirtualDeploymentUnitRecord(object):
             ei_list.append({"name": cp,
                             "vnfd_connection_point_ref": cp,
                             "virtual_interface": {}})
-            self._vnfr.update_cp(cp, self.cp_ip_addr(cp), self.cp_id(cp))
+            self._vnfr.update_cp(cp,
+                                 self.cp_ip_addr(cp),
+                                 self.cp_mac_addr(cp),
+                                 self.cp_id(cp))
 
         vdur_dict["external_interface"] = ei_list
 
@@ -564,7 +576,7 @@ class VirtualDeploymentUnitRecord(object):
 
             if (vlr.has_field('ip_profile_params')) and (vlr.ip_profile_params.has_field('security_group')):
                 cp_info['security_group'] = vlr.ip_profile_params.security_group
-                
+
             cp_list.append(cp_info)
 
         for intf, cp, vlr in self._int_intf:
@@ -1227,6 +1239,7 @@ class VirtualNetworkFunctionRecord(object):
         vnfr_dict.update(vnfd_copy_dict)
 
         vnfr_msg = RwVnfrYang.YangData_Vnfr_VnfrCatalog_Vnfr.from_dict(vnfr_dict)
+        vnfr_msg.uptime = int(time.time()) - self._create_time
         vnfr_msg.mgmt_interface = mgmt_intf
 
         # Add all the VLRs  to  VNFR
@@ -1311,7 +1324,7 @@ class VirtualNetworkFunctionRecord(object):
         for ivld_msg in self.vnfd.msg.internal_vld:
             self._log.debug("Creating internal vld:"
                             " %s, int_cp_ref = %s",
-                            ivld_msg, ivld_msg.internal_connection_point_ref
+                            ivld_msg, ivld_msg.internal_connection_point
                             )
             vlr = InternalVirtualLinkRecord(dts=self._dts,
                                             log=self._log,
@@ -1322,14 +1335,14 @@ class VirtualNetworkFunctionRecord(object):
                                             )
             self._vlrs.append(vlr)
 
-            for int_cp in ivld_msg.internal_connection_point_ref:
-                if int_cp in self._vlr_by_cp:
+            for int_cp in ivld_msg.internal_connection_point:
+                if int_cp.id_ref in self._vlr_by_cp:
                     msg = ("Connection point %s already "
-                           " bound %s" % (int_cp, self._vlr_by_cp[int_cp]))
+                           " bound %s" % (int_cp.id_ref, self._vlr_by_cp[int_cp.id_ref]))
                     raise InternalVirtualLinkRecordError(msg)
                 self._log.debug("Setting vlr %s to internal cp = %s",
-                                vlr, int_cp)
-                self._vlr_by_cp[int_cp] = vlr
+                                vlr, int_cp.id_ref)
+                self._vlr_by_cp[int_cp.id_ref] = vlr
 
     @asyncio.coroutine
     def instantiate_vls(self, xact, restart_mode=False):
@@ -1651,13 +1664,14 @@ class VirtualNetworkFunctionRecord(object):
         # Update the VNFR with the changed status
         yield from self.publish(None)
 
-    def update_cp(self, cp_name, ip_address, cp_id):
+    def update_cp(self, cp_name, ip_address, mac_addr, cp_id):
         """Updated the connection point with ip address"""
         for cp in self._cprs:
             if cp.name == cp_name:
                 self._log.debug("Setting ip address and id for cp %s, cpr %s with ip %s id %s",
                                 cp_name, cp, ip_address, cp_id)
                 cp.ip_address = ip_address
+                cp.mac_address = mac_addr
                 cp.connection_point_id = cp_id
                 return
 
@@ -1760,6 +1774,10 @@ class VirtualNetworkFunctionRecord(object):
 
         self._log.debug("VNFR-ID %s: Instantiation Done", self._vnfr_id)
 
+        # create task updating uptime for this vnfr
+        self._log.debug("VNFR-ID %s: Starting task to update uptime", self._vnfr_id)
+        self._loop.create_task(self.vnfr_uptime_update(xact))
+
     @asyncio.coroutine
     def terminate(self, xact):
         """ Terminate this virtual network function """
@@ -1797,6 +1815,19 @@ class VirtualNetworkFunctionRecord(object):
         self._log.debug("Terminated  VNF id %s", self.vnfr_id)
         self.set_state(VirtualNetworkFunctionRecordState.TERMINATED)
 
+    @asyncio.coroutine
+    def vnfr_uptime_update(self, xact):
+        while True:
+            # Return when vnfr state is FAILED or TERMINATED etc
+            if self._state not in [VirtualNetworkFunctionRecordState.INIT,
+                                   VirtualNetworkFunctionRecordState.VL_INIT_PHASE,
+                                   VirtualNetworkFunctionRecordState.VM_INIT_PHASE,
+                                   VirtualNetworkFunctionRecordState.READY]:
+                return
+            yield from self.publish(xact)
+            yield from asyncio.sleep(2, loop=self._loop)
+
+
 
 class VnfdDtsHandler(object):
     """ DTS handler for VNFD config changes """
@@ -1964,7 +1995,7 @@ class VnfrConsoleOperdataDtsHandler(object):
                     if not vdur._state == VDURecordState.READY:
                         self._log.debug("VDUR state is not READY. current state is {}".format(vdur._state))
                         xact_info.respond_xpath(rsp_code=rwdts.XactRspCode.ACK)
-                        return 
+                        return
                     with self._dts.transaction() as new_xact:
                         resp = yield from vdur.read_resource(new_xact)
                         vdur_console = RwVnfrYang.YangData_RwVnfr_VnfrConsole_Vnfr_Vdur()
@@ -1979,7 +2010,7 @@ class VnfrConsoleOperdataDtsHandler(object):
                     vdur_console = RwVnfrYang.YangData_RwVnfr_VnfrConsole_Vnfr_Vdur()
                     vdur_console.id = self._vdur_id
                     vdur_console.console_url = 'none'
-                      
+
                 xact_info.respond_xpath(rsp_code=rwdts.XactRspCode.ACK,
                                             xpath=self.vnfr_vdu_console_xpath,
                                             msg=vdur_console)
@@ -1987,8 +2018,8 @@ class VnfrConsoleOperdataDtsHandler(object):
                 #raise VnfRecordError("Not supported operation %s" % action)
                 self._log.error("Not supported operation %s" % action)
                 xact_info.respond_xpath(rsp_code=rwdts.XactRspCode.ACK)
-                return 
-                 
+                return
+
 
         self._log.debug("Registering for VNFR VDU using xpath: %s",
                         self.vnfr_vdu_console_xpath)
@@ -2602,10 +2633,6 @@ class VnfManager(object):
         """ update the Virtual Network Function descriptor """
         self._log.debug("Update virtual network function descriptor - %s", vnfd)
 
-        # Hack to remove duplicates from leaf-lists - to be fixed by RIFT-6511
-        for ivld in vnfd.internal_vld:
-            ivld.internal_connection_point_ref = list(set(ivld.internal_connection_point_ref))
-
         if vnfd.id not in self._vnfds:
             self._log.debug("No VNFD found - creating VNFD id = %s", vnfd.id)
             self.create_vnfd(vnfd)