user upadate: add/revoke project access
[osm/LW-UI.git] / lib / osm / osm_parser.py
1 #
2 # Copyright 2017 CNIT - Consorzio Nazionale Interuniversitario per le Telecomunicazioni
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 #
16
17 import json
18 import pyaml
19 import yaml
20 from lib.util import Util
21 from lib.parser import Parser
22 import logging
23 import traceback
24 import glob
25 import os
26
27 logging.basicConfig(level=logging.DEBUG)
28 log = logging.getLogger('OsmParser')
29
30 class OsmParser(Parser):
31 """Parser methods for osm project type
32
33 """
34
35 def __init__(self):
36 super(OsmParser, self).__init__()
37
38 @classmethod
39 def importprojectdir(cls,dir_project, file_type):
40 """Imports all descriptor files under a given folder
41
42 this method is specific for Osm project type
43 """
44
45 project = {
46 'nsd':{},
47
48 'vnfd':{},
49
50 'positions': {}
51 }
52
53
54 for desc_type in project:
55 cur_type_path = os.path.join(dir_project, desc_type.upper())
56 log.debug(cur_type_path)
57 if os.path.isdir(cur_type_path):
58 for file in glob.glob(os.path.join(cur_type_path, '*.'+file_type)):
59 if file_type == 'json':
60 project[desc_type][os.path.basename(file).split('.')[0]] = Util.loadjsonfile(file)
61 elif file_type == 'yaml':
62 project[desc_type][os.path.basename(file).split('.')[0]] = Util.loadyamlfile(file)
63
64
65 for vertices_file in glob.glob(os.path.join(dir_project, '*.json')):
66 if os.path.basename(vertices_file) == 'vertices.json':
67 project['positions']['vertices'] = Util.loadjsonfile(vertices_file)
68
69 return project
70
71 @classmethod
72 def importprojectfiles(cls, file_dict):
73 """Imports descriptors (extracted from the new project POST)
74
75 The keys in the dictionary are the file types
76 """
77 project = {
78 'nsd':{},
79
80 'vnfd':{},
81
82 }
83 for desc_type in project:
84 if desc_type in file_dict:
85 files_desc_type = file_dict[desc_type]
86 for file in files_desc_type:
87 project[desc_type][os.path.splitext(file.name)[0]] = json.loads(file.read())
88
89 return project