OSMENG-992 - Implement create model activity

in NS Workflow.

An exception is raised if model already exists

Change-Id: I84dd89850b28287dfefb1abc0d158cc72cd4eb34
Signed-off-by: Patricia Reinoso <patricia.reinoso@canonical.com>
Signed-off-by: Mark Beierl <mark.beierl@canonical.com>
diff --git a/osm_lcm/temporal/juju_paas_activities.py b/osm_lcm/temporal/juju_paas_activities.py
index ecd59e6..3c61ec7 100644
--- a/osm_lcm/temporal/juju_paas_activities.py
+++ b/osm_lcm/temporal/juju_paas_activities.py
@@ -18,15 +18,15 @@
 from n2vc.temporal_libjuju import ConnectionInfo, Libjuju
 from osm_common.temporal_constants import (
     ACTIVITY_TEST_VIM_CONNECTIVITY,
-    ACTIVITY_CREATE_MODEL_IF_DOESNT_EXIST,
+    ACTIVITY_CREATE_MODEL,
     ACTIVITY_DEPLOY_CHARM,
     ACTIVITY_CHECK_CHARM_STATUS,
 )
 from osm_common.dataclasses.temporal_dataclasses import (
-    TestVimConnectivityInput,
-    CreateModelInput,
-    VduInstantiateInput,
     CharmInfo,
+    ModelInfo,
+    TestVimConnectivityInput,
+    VduInstantiateInput,
 )
 
 
@@ -116,19 +116,17 @@
         finally:
             await libjuju.disconnect_controller(controller)
 
-    @activity.defn(name=ACTIVITY_CREATE_MODEL_IF_DOESNT_EXIST)
-    async def create_model_if_doesnt_exist(
-        self, create_model_input: CreateModelInput
-    ) -> None:
-        # TODO: OSM-991
-        """Connects to Juju Controller. Create a new model if model_name does not exist
+    @activity.defn(name=ACTIVITY_CREATE_MODEL)
+    async def create_model(self, create_model_input: ModelInfo) -> None:
+        """Connects to Juju Controller. Creates a new model.
 
         Collaborators:
             DB Read:            vim_accounts
             Juju Controller:    Connect and create model.
 
         Raises  (Retryable):
-            ApplicationError    If Juju controller is not reachable
+            ApplicationError    If Juju controller is not reachable.
+                                If the model already exists.
 
         Activity Lifecycle:
             This activity should complete relatively quickly (in a few seconds).
diff --git a/osm_lcm/temporal/ns_activities.py b/osm_lcm/temporal/ns_activities.py
index 1a2925c..6834eec 100644
--- a/osm_lcm/temporal/ns_activities.py
+++ b/osm_lcm/temporal/ns_activities.py
@@ -18,12 +18,14 @@
 from time import time
 from temporalio import activity
 from osm_common.temporal_constants import (
-    ACTIVITY_UPDATE_NS_STATE,
     ACTIVITY_CHECK_NS_INSTANTIATION_FINISHED,
-    ACTIVITY_PREPARE_VNF_RECORDS,
     ACTIVITY_DEPLOY_NS,
+    ACTIVITY_GET_MODEL_INFO,
+    ACTIVITY_PREPARE_VNF_RECORDS,
+    ACTIVITY_UPDATE_NS_STATE,
 )
 from osm_common.dataclasses.temporal_dataclasses import (
+    ModelInfo,
     NsInstantiateInput,
     UpdateNsStateInput,
     VduInstantiateInput,
@@ -45,7 +47,7 @@
 
     async def deploy_vnf(self, vnfr: dict):
         vim_id = vnfr.get("vim-account-id")
-        model_name = vnfr.get("namespace")
+        model_name = "model-name"
         vnfd_id = vnfr["vnfd-id"]
         vnfd = self.db.get_one("vnfds", {"_id": vnfd_id})
         sw_image_descs = vnfd.get("sw-image-desc")
@@ -106,6 +108,32 @@
         for vnfr in vnfrs:
             self._prepare_vnf_record(vnfr)
 
+    @activity.defn(name=ACTIVITY_GET_MODEL_INFO)
+    async def get_model_info(
+        self, ns_instantiate_input: NsInstantiateInput
+    ) -> ModelInfo:
+        """Returns a ModelInfo. Contains VIM ID and model name.
+
+        Collaborators:
+            DB Read:           nsrs
+
+        Raises  (Retryable):
+            DbException         If the target DB record does not exist or DB is not reachable.
+
+        Activity Lifecycle:
+            This activity will not report a heartbeat due to its
+            short-running nature.
+
+            As this is a direct DB update, it is not recommended to have
+            any specific retry policy
+
+        """
+        ns_uuid = ns_instantiate_input.ns_uuid
+        nsr = self.db.get_one("nsrs", {"_id": ns_uuid})
+        vim_uuid = nsr.get("datacenter")
+        model_name = self._get_namespace(ns_uuid, vim_uuid)
+        return ModelInfo(vim_uuid, model_name)
+
     def _get_namespace(self, ns_id: str, vim_id: str) -> str:
         """The NS namespace is the combination if the NS ID and the VIM ID."""
         return ns_id[-12:] + "-" + vim_id[-12:]
diff --git a/osm_lcm/temporal/ns_workflows.py b/osm_lcm/temporal/ns_workflows.py
index bf0be65..bef6766 100644
--- a/osm_lcm/temporal/ns_workflows.py
+++ b/osm_lcm/temporal/ns_workflows.py
@@ -15,20 +15,24 @@
 # limitations under the License.
 
 from temporalio import workflow
+from temporalio.converter import value_to_type
 from temporalio.exceptions import ActivityError
 
 from osm_common.dataclasses.temporal_dataclasses import (
+    ModelInfo,
     NsInstantiateInput,
     NsLcmOperationInput,
     NsState,
     UpdateNsStateInput,
 )
 from osm_common.temporal_constants import (
-    WORKFLOW_NS_INSTANTIATE,
-    ACTIVITY_UPDATE_NS_STATE,
+    ACTIVITY_CREATE_MODEL,
     ACTIVITY_CHECK_NS_INSTANTIATION_FINISHED,
     ACTIVITY_DEPLOY_NS,
+    ACTIVITY_GET_MODEL_INFO,
+    ACTIVITY_UPDATE_NS_STATE,
     LCM_TASK_QUEUE,
+    WORKFLOW_NS_INSTANTIATE,
 )
 
 from osm_lcm.temporal.lcm_workflows import LcmOperationWorkflow
@@ -51,7 +55,26 @@
 
         ns_state = UpdateNsStateInput(input.ns_uuid, NsState.INSTANTIATED, "Done")
         try:
-            # TODO: Create the model here OSM-991
+            model_info = value_to_type(
+                ModelInfo,
+                await workflow.execute_activity(
+                    activity=ACTIVITY_GET_MODEL_INFO,
+                    arg=input,
+                    activity_id=f"{ACTIVITY_GET_MODEL_INFO}-{input.ns_uuid}",
+                    task_queue=LCM_TASK_QUEUE,
+                    schedule_to_close_timeout=LcmOperationWorkflow.default_schedule_to_close_timeout,
+                    retry_policy=LcmOperationWorkflow.no_retry_policy,
+                ),
+            )
+
+            await workflow.execute_activity(
+                activity=ACTIVITY_CREATE_MODEL,
+                arg=model_info,
+                activity_id=f"{ACTIVITY_CREATE_MODEL}-{input.ns_uuid}",
+                task_queue=LCM_TASK_QUEUE,
+                schedule_to_close_timeout=LcmOperationWorkflow.default_schedule_to_close_timeout,
+                retry_policy=LcmOperationWorkflow.no_retry_policy,
+            )
 
             # TODO: Change this to a workflow OSM-990
             await workflow.execute_activity(
diff --git a/osm_lcm/temporal/tests/test_charm_info_utils.py b/osm_lcm/temporal/tests/test_charm_info_utils.py
deleted file mode 100644
index fd1cd9b..0000000
--- a/osm_lcm/temporal/tests/test_charm_info_utils.py
+++ /dev/null
@@ -1,158 +0,0 @@
-# Copyright ETSI Contributors and Others.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-#     http://www.apache.org/licenses/LICENSE-2.0
-#
-#     Unless required by applicable law or agreed to in writing, software
-#     distributed under the License is distributed on an "AS IS" BASIS,
-#     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-#     See the License for the specific language governing permissions and
-#     limitations under the License.
-
-from unittest import TestCase
-from osm_lcm.temporal.juju_paas_activities import CharmInfoUtils
-from osm_common.dataclasses.temporal_dataclasses import CharmInfo
-import yaml
-
-nsr_id = "ea958ba5-4e58-4405-bf42-6e3be15d4c3a"
-vim_id = "70b47595-fafa-4f63-904b-fc3ada60eebb"
-expected_default_ns_model = "6e3be15d4c3a-fc3ada60eebb"
-
-vdu_nominal = """
----
-vdu:
-   - id: test-vdu-id
-     name: test-vdu-name
-     int-cpd:
-       - id: internal
-         int-virtual-link-desc: network1
-       - id: mgmt
-     virtual-compute-desc: compute-id
-     virtual-storage-desc:
-       - storage-id
-     sw-image-desc: image-test2
-     configurable-properties:
-       - key: "track"
-         value: "latest"
-       - key: "channel"
-         value: "edge"
-"""
-
-vdu_no_channel = """
----
-vdu:
-   - id: test-vdu-id
-     name: test-vdu-name
-     int-cpd:
-       - id: internal
-         int-virtual-link-desc: network1
-       - id: mgmt
-     virtual-compute-desc: compute-id
-     virtual-storage-desc:
-       - storage-id
-     sw-image-desc: image-test2
-     configurable-properties:
-       - key: "track"
-         value: "latest"
-       - key: "key"
-         value: "edge"
-"""
-
-vdu_invalid_image = """
----
-vdu:
-   - id: test-vdu-id
-     name: test-vdu-name
-     int-cpd:
-       - id: internal
-         int-virtual-link-desc: network1
-       - id: mgmt
-     virtual-compute-desc: compute-id
-     virtual-storage-desc:
-       - storage-id
-     sw-image-desc: invalid_image
-     configurable-properties:
-       - key: "track"
-         value: "latest"
-       - key: "key"
-         value: "edge"
-"""
-
-vdu_no_sw_image_desc = """
----
-vdu:
-   - id: test-vdu-id
-     name: test-vdu-name
-     int-cpd:
-       - id: internal
-         int-virtual-link-desc: network1
-       - id: mgmt
-     virtual-compute-desc: compute-id
-     virtual-storage-desc:
-       - storage-id
-     sw-image-desc: invalid_image
-     configurable-properties:
-       - key: "track"
-         value: "latest"
-       - key: "key"
-         value: "edge"
-"""
-
-sw_image_desc = """
----
-sw-image-desc:
-  - id: image-test1
-    name: charm-name1
-    image: ch:mysql
-    version: "1.0"
-  - id: image-test2
-    name: charm-name2
-    image: ch:my-charm
-    version: "1.0"
-"""
-
-
-class TestCharmInfoUtils(TestCase):
-    def setUp(self):
-        self.charm_info_utils = CharmInfoUtils()
-
-    def get_loaded_descriptor(self, descriptor):
-        return yaml.load(descriptor, Loader=yaml.Loader)
-
-    def test_get_charm_info_nominal_case(self):
-        vdu_descriptor = self.get_loaded_descriptor(vdu_nominal).get("vdu")
-        sw_image_descs = self.get_loaded_descriptor(sw_image_desc).get("sw-image-desc")
-        result = self.charm_info_utils.get_charm_info(vdu_descriptor[0], sw_image_descs)
-        expected = CharmInfo("test-vdu-id", "edge", "ch:my-charm")
-        self.assertEqual(result, expected)
-
-    def test_get_charm_info_no_channel(self):
-        vdu_descriptor = self.get_loaded_descriptor(vdu_no_channel).get("vdu")
-        sw_image_descs = self.get_loaded_descriptor(sw_image_desc).get("sw-image-desc")
-        result = self.charm_info_utils.get_charm_info(vdu_descriptor[0], sw_image_descs)
-        expected = CharmInfo("test-vdu-id", None, "ch:my-charm")
-        self.assertEqual(result, expected)
-
-    def test_get_charm_info_invalid_image(self):
-        vdu_descriptor = self.get_loaded_descriptor(vdu_invalid_image).get("vdu")
-        sw_image_descs = self.get_loaded_descriptor(sw_image_desc).get("sw-image-desc")
-        result = self.charm_info_utils.get_charm_info(vdu_descriptor[0], sw_image_descs)
-        expected = CharmInfo("test-vdu-id", None, None)
-        self.assertEqual(result, expected)
-
-    def test_get_charm_info_no_sw_image_desc(self):
-        vdu_descriptor = self.get_loaded_descriptor(vdu_no_sw_image_desc).get("vdu")
-        sw_image_descs = self.get_loaded_descriptor(sw_image_desc).get("sw-image-desc")
-        result = self.charm_info_utils.get_charm_info(vdu_descriptor[0], sw_image_descs)
-        expected = CharmInfo("test-vdu-id", None, None)
-        self.assertEqual(result, expected)
-
-    def test_get_charm_info_empty_sw_image_descs(self):
-        vdu_descriptor = self.get_loaded_descriptor(vdu_nominal).get("vdu")
-        sw_image_descs = []
-        result = self.charm_info_utils.get_charm_info(vdu_descriptor[0], sw_image_descs)
-        expected = CharmInfo("test-vdu-id", "edge", None)
-        self.assertEqual(result, expected)