Pyangbind checks for Network Slices descriptors
[osm/NBI.git] / osm_nbi / descriptor_topics.py
index 71f51e2..e9339e0 100644 (file)
@@ -1,5 +1,18 @@
 # -*- coding: utf-8 -*-
 
+# 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 tarfile
 import yaml
 import json
@@ -11,6 +24,7 @@ from validation import ValidationError, pdu_new_schema, pdu_edit_schema
 from base_topic import BaseTopic, EngineException, get_iterable
 from osm_im.vnfd import vnfd as vnfd_im
 from osm_im.nsd import nsd as nsd_im
+from osm_im.nst import nst as nst_im
 from pyangbind.lib.serialise import pybindJSONDecoder
 import pyangbind.lib.pybindJSON as pybindJSON
 
@@ -365,6 +379,11 @@ class DescriptorTopic(BaseTopic):
                 pybindJSONDecoder.load_ietf_json({'nsd:nsd-catalog': {'nsd': [data]}}, None, None, obj=mynsd,
                                                  path_helper=True, skip_unknown=force)
                 out = pybindJSON.dumps(mynsd, mode="ietf")
+            elif item == "nsts":
+                mynst = nst_im()
+                pybindJSONDecoder.load_ietf_json({'nst': [data]}, None, None, obj=mynst,
+                                                 path_helper=True, skip_unknown=force)
+                out = pybindJSON.dumps(mynst, mode="ietf")
             else:
                 raise EngineException("Not possible to validate '{}' item".format(item),
                                       http_code=HTTPStatus.INTERNAL_SERVER_ERROR)
@@ -726,20 +745,24 @@ class NstTopic(DescriptorTopic):
             return {}
         clean_indata = indata
 
-        if clean_indata.get('nst:nst'):
-            clean_indata = clean_indata['nst:nst']
-        elif clean_indata.get('nst'):
-            clean_indata = clean_indata['nst']
         if clean_indata.get('nst'):
             if not isinstance(clean_indata['nst'], list) or len(clean_indata['nst']) != 1:
                 raise EngineException("'nst' must be a list only one element")
             clean_indata = clean_indata['nst'][0]
+        elif clean_indata.get('nst:nst'):
+            if not isinstance(clean_indata['nst:nst'], list) or len(clean_indata['nst:nst']) != 1:
+                raise EngineException("'nst:nst' must be a list only one element")
+            clean_indata = clean_indata['nst:nst'][0]
         return clean_indata
 
     def _validate_input_edit(self, indata, force=False):
         # TODO validate with pyangbind, serialize
         return indata
 
+    def _validate_input_new(self, indata, force=False):
+        indata = self.pyangbind_validation("nsts", indata, force)
+        return indata.copy()
+
     def _check_descriptor_dependencies(self, session, descriptor):
         """
         Check that the dependent descriptors exist on a new descriptor or edition
@@ -777,13 +800,15 @@ class NstTopic(DescriptorTopic):
         # Get Network Slice Template from Database
         _filter = self._get_project_filter(session, write=False, show_all=False)
         _filter["_id"] = _id
-        nst = self.db.get_one("nst", _filter)
+        nst = self.db.get_one("nsts", _filter)
         
         # Search NSIs using NST via nst-ref
         _filter = self._get_project_filter(session, write=False, show_all=False)
         _filter["nst-ref"] = nst["id"]
-        if self.db.get_list("nsis", _filter):
-            raise EngineException("There is some NSIS that depends on this NST", http_code=HTTPStatus.CONFLICT)
+        nsis_list = self.db.get_list("nsis", _filter)
+        for nsi_item in nsis_list:
+            if nsi_item["_admin"].get("nsiState") != "TERMINATED":
+                raise EngineException("There is some NSIS that depends on this NST", http_code=HTTPStatus.CONFLICT)
 
 
 class PduTopic(BaseTopic):