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/tests/test_charm_info_utils.py b/osm_lcm/tests/test_charm_info_utils.py
new file mode 100644
index 0000000..fd1cd9b
--- /dev/null
+++ b/osm_lcm/tests/test_charm_info_utils.py
@@ -0,0 +1,158 @@
+# 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)
diff --git a/osm_lcm/tests/test_juju_paas_activities.py b/osm_lcm/tests/test_juju_paas_activities.py
new file mode 100644
index 0000000..7d94a2c
--- /dev/null
+++ b/osm_lcm/tests/test_juju_paas_activities.py
@@ -0,0 +1,77 @@
+#######################################################################################
+# 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.
+
+import asynctest
+from juju.errors import JujuError
+from osm_common.dataclasses.temporal_dataclasses import ModelInfo
+from osm_common.dbbase import DbException
+from osm_lcm.temporal.juju_paas_activities import JujuPaasConnector
+from n2vc.temporal_libjuju import ConnectionInfo
+from temporalio.testing import ActivityEnvironment
+from unittest.mock import Mock
+
+vim_id = "some-vim-uuid"
+namespace = "some-namespace"
+model_info = ModelInfo(vim_id, namespace)
+connection_info = ConnectionInfo(
+    "1.2.3.4:17070", "user", "password", "cacert", "cloud_name", "cloud_credentials"
+)
+
+
+class TestJujuPaasConnector(asynctest.TestCase):
+    def setUp(self):
+        self.db = Mock()
+        self.env = ActivityEnvironment()
+        self.juju_paas_connector = JujuPaasConnector(self.db)
+
+    @asynctest.mock.patch("osm_lcm.temporal.juju_paas_activities.Libjuju.add_model")
+    @asynctest.mock.patch(
+        "osm_lcm.temporal.juju_paas_activities.JujuPaasConnector._get_connection_info"
+    )
+    async def test_create_model_nominal_case(
+        self, mock_get_connection_info, mock_add_model
+    ):
+        mock_get_connection_info.return_value = connection_info
+        await self.env.run(self.juju_paas_connector.create_model, model_info)
+        mock_get_connection_info.assert_called_once_with(vim_id)
+        mock_add_model.assert_called_once_with(namespace)
+
+    @asynctest.mock.patch("osm_lcm.temporal.juju_paas_activities.Libjuju.add_model")
+    @asynctest.mock.patch(
+        "osm_lcm.temporal.juju_paas_activities.JujuPaasConnector._get_connection_info"
+    )
+    async def test_create_model_raises_juju_exception(
+        self, mock_get_connection_info, mock_add_model
+    ):
+        mock_get_connection_info.return_value = connection_info
+        mock_add_model.side_effect = JujuError()
+        with self.assertRaises(JujuError):
+            await self.env.run(self.juju_paas_connector.create_model, model_info)
+        mock_get_connection_info.assert_called_once_with(vim_id)
+        mock_add_model.assert_called_once_with(namespace)
+
+    @asynctest.mock.patch("osm_lcm.temporal.juju_paas_activities.Libjuju.add_model")
+    @asynctest.mock.patch(
+        "osm_lcm.temporal.juju_paas_activities.JujuPaasConnector._get_connection_info"
+    )
+    async def test_create_model_raises_db_exception(
+        self, mock_get_connection_info, mock_add_model
+    ):
+        mock_get_connection_info.side_effect = DbException("not found")
+        with self.assertRaises(DbException):
+            await self.env.run(self.juju_paas_connector.create_model, model_info)
+        mock_get_connection_info.assert_called_once_with(vim_id)
+        mock_add_model.assert_not_called()
diff --git a/osm_lcm/tests/test_ns_activities.py b/osm_lcm/tests/test_ns_activities.py
new file mode 100644
index 0000000..b12693b
--- /dev/null
+++ b/osm_lcm/tests/test_ns_activities.py
@@ -0,0 +1,59 @@
+#######################################################################################
+# 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.
+
+
+import asynctest
+from osm_common.dataclasses.temporal_dataclasses import ModelInfo, NsInstantiateInput
+from osm_common.dbbase import DbException
+from osm_lcm.temporal.ns_activities import NsDbActivity
+from temporalio.testing import ActivityEnvironment
+from unittest.mock import Mock
+
+
+vim_id = "some-vim-uuid"
+ns_id = "0123456789-9876543210"
+nsr = {
+    "_id": ns_id,
+    "datacenter": vim_id,
+}
+input = NsInstantiateInput(ns_id, "op_id")
+expected_namespace = "9-9876543210-ome-vim-uuid"
+expected_model_info = ModelInfo(vim_id, expected_namespace)
+
+
+class TestGetModelInfo(asynctest.TestCase):
+    def setUp(self):
+        self.db = Mock()
+        self.env = ActivityEnvironment()
+        self.ns_db_activity = NsDbActivity(self.db)
+
+    async def test_nominal_case(self):
+        self.db.get_one.return_value = nsr
+        model_info = await self.env.run(self.ns_db_activity.get_model_info, input)
+        self.assertEqual(model_info, expected_model_info)
+
+    async def test_db_raises_exception(self):
+        self.db.get_one.side_effect = DbException("not found")
+        with self.assertRaises(DbException):
+            model_info = await self.env.run(self.ns_db_activity.get_model_info, input)
+            self.assertIsNone(model_info)
+
+    async def test_no_datacenter_raises_exception(self):
+        nsr = {"_id": ns_id}
+        self.db.get_one.return_value = nsr
+        with self.assertRaises(TypeError):
+            model_info = await self.env.run(self.ns_db_activity.get_model_info, input)
+            self.assertIsNone(model_info)