summaryrefslogtreecommitdiffstats
path: root/share/newton_base/registration/registration.py
blob: fd2cf9aa001f8ab0684dc55a46ef83a80994a0d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
# Copyright (c) 2017-2018 Wind River Systems, Inc.
#
# 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 logging
import json
import uuid
import traceback

from keystoneauth1.exceptions import HttpError
from rest_framework import status
from rest_framework.response import Response
from rest_framework.views import APIView

from common.exceptions import VimDriverNewtonException
from common.msapi import extsys
from common.utils import restcall
from newton_base.util import VimDriverUtils

logger = logging.getLogger(__name__)


class Registry(APIView):

    def __init__(self):
        self.proxy_prefix = "multicloud"
        self.aai_base_url = "127.0.0.1"
        self._logger = logger

    def _get_list_resources(
            self, resource_url, service_type, session, viminfo,
            vimid, content_key):
        service = {
            'service_type': service_type,
            'interface': 'public'
        }

        # identity service should not filtered by region since it is might be first call
        # to figure out available region list
        if service_type != 'identity':
            service['region_name'] = viminfo['openstack_region_id']\
                if viminfo.get('openstack_region_id') else viminfo['cloud_region_id']

        self._logger.info("making request with URI:%s,%s" % (resource_url,service))
        resp = session.get(resource_url, endpoint_filter=service)
        self._logger.info("request returns with status %s" % resp.status_code)
        if resp.status_code == status.HTTP_200_OK:
            self._logger.debug("with content:%s" % resp.json())
            content = resp.json()
            return content.get(content_key)
        return  # failed to discover resources

    def _update_resoure(self, cloud_owner, cloud_region_id,
                        resoure_id, resource_info, resource_type):
        if cloud_owner and cloud_region_id:
            self._logger.debug(
                ("_update_resoure,vimid:%(cloud_owner)s"
                 "_%(cloud_region_id)s req_to_aai: %(resoure_id)s, "
                 "%(resource_type)s, %(resource_info)s")
                % {
                    "cloud_owner": cloud_owner,
                    "cloud_region_id": cloud_region_id,
                    "resoure_id": resoure_id,
                    "resource_type": resource_type,
                    "resource_info": resource_info,
                })

            #get the resource first
            resource_url = ("/cloud-infrastructure/cloud-regions/"
                     "cloud-region/%(cloud_owner)s/%(cloud_region_id)s/"
                     "%(resource_type)ss/%(resource_type)s/%(resoure_id)s"
                     % {
                         "cloud_owner": cloud_owner,
                         "cloud_region_id": cloud_region_id,
                         "resoure_id": resoure_id,
                         "resource_type": resource_type,
                     })

            # get cloud-region
            retcode, content, status_code = \
                restcall.req_to_aai(resource_url, "GET")

            # add resource-version
            if retcode == 0 and content:
                content = json.JSONDecoder().decode(content)
                #resource_info["resource-version"] = content["resource-version"]
                content.update(resource_info)
                resource_info = content

            #then update the resource
            retcode, content, status_code = \
                restcall.req_to_aai(resource_url, "PUT", content=resource_info)

            self._logger.debug(
                ("_update_resoure,vimid:%(cloud_owner)s"
                 "_%(cloud_region_id)s req_to_aai: %(resoure_id)s, "
                 "return %(retcode)s, %(content)s, %(status_code)s")
                % {
                    "cloud_owner": cloud_owner,
                    "cloud_region_id": cloud_region_id,
                    "resoure_id": resoure_id,
                    "retcode": retcode,
                    "content": content,
                    "status_code": status_code,
                })
            return retcode
        return 1  # unknown cloud owner,region_id

    def _discover_tenants(self, vimid="", session=None, viminfo=None):
        try:
            # iterate all projects and populate them into AAI
            cloud_owner, cloud_region_id = extsys.decode_vim_id(vimid)
            for tenant in self._get_list_resources(
                    "projects", "identity", session, viminfo, vimid,
                    "projects"):
                tenant_info = {
                    'tenant-id': tenant['id'],
                    'tenant-name': tenant['name'],
                }
                self._update_resoure(
                    cloud_owner, cloud_region_id, tenant['id'],
                    tenant_info, "tenant")

        except VimDriverNewtonException as e:
            self._logger.error("VimDriverNewtonException: status:%s, response:%s" % (e.http_status, e.content))
            return
        except HttpError as e:
            if e.http_status == status.HTTP_403_FORBIDDEN:
                ### get the tenant information from the token response
                try:
                    ### get tenant info from the session
                    tmp_auth_state = VimDriverUtils.get_auth_state(session)
                    tmp_auth_info = json.loads(tmp_auth_state)
                    tmp_auth_data = tmp_auth_info['body']
                    tenant = tmp_auth_data['token']['project']
                    tenant_info = {
                        'tenant-id': tenant['id'],
                        'tenant-name': tenant['name'],
                    }

                    self._update_resoure(
                        cloud_owner, cloud_region_id, tenant['id'],
                        tenant_info, "tenant")

                except Exception as ex:
                    self._logger.error(traceback.format_exc())
            else:
                self._logger.error("HttpError: status:%s, response:%s" % (e.http_status, e.response.json()))
            return
        except Exception as e:
            self._logger.error(traceback.format_exc())
            return

    def _discover_flavors(self, vimid="", session=None, viminfo=None):
        try:
            cloud_owner, cloud_region_id = extsys.decode_vim_id(vimid)
            for flavor in self._get_list_resources(
                    "/flavors/detail", "compute", session, viminfo, vimid,
                    "flavors"):
                flavor_info = {
                    'flavor-id': flavor['id'],
                    'flavor-name': flavor['name'],
                    'flavor-vcpus': flavor['vcpus'],
                    'flavor-ram': flavor['ram'],
                    'flavor-disk': flavor['disk'],
                    'flavor-ephemeral': flavor['OS-FLV-EXT-DATA:ephemeral'],
                    'flavor-swap': flavor['swap'],
                    'flavor-is-public': flavor['os-flavor-access:is_public'],
                    'flavor-disabled': flavor['OS-FLV-DISABLED:disabled'],
                }

                if flavor.get('links') and len(flavor['links']) > 0:
                    flavor_info['flavor-selflink'] = flavor['links'][0]['href'] or 'http://0.0.0.0'
                else:
                    flavor_info['flavor-selflink'] = 'http://0.0.0.0'

                # add hpa capabilities
                if (flavor['name'].find('onap.') == 0):
                    req_resouce = "/flavors/%s/os-extra_specs" % flavor['id']
                    extraResp = self._get_list_resources(req_resouce, "compute", session, viminfo, vimid, "extra_specs")

                    hpa_capabilities = self._get_hpa_capabilities(flavor, extraResp, viminfo)
                    flavor_info['hpa-capabilities'] = {'hpa-capability': hpa_capabilities}

                self._update_resoure(
                    cloud_owner, cloud_region_id, flavor['id'],
                    flavor_info, "flavor")

        except VimDriverNewtonException as e:
            self._logger.error("VimDriverNewtonException: status:%s, response:%s" % (e.http_status, e.content))
            return
        except HttpError as e:
            self._logger.error("HttpError: status:%s, response:%s" % (e.http_status, e.response.json()))
            return
        except Exception as e:
            self._logger.error(traceback.format_exc())
            return

    def _get_hpa_capabilities(self, flavor, extra_specs, viminfo):
        hpa_caps = []

        # Basic capabilties
        caps_dict = self._get_hpa_basic_capabilities(flavor)
        if len(caps_dict) > 0:
            self._logger.debug("basic_capabilities_info: %s" % caps_dict)
            hpa_caps.append(caps_dict)

        # cpupining capabilities
        caps_dict = self._get_cpupining_capabilities(extra_specs)
        if len(caps_dict) > 0:
            self._logger.debug("cpupining_capabilities_info: %s" % caps_dict)
            hpa_caps.append(caps_dict)

        # cputopology capabilities
        caps_dict = self._get_cputopology_capabilities(extra_specs)
        if len(caps_dict) > 0:
            self._logger.debug("cputopology_capabilities_info: %s" % caps_dict)
            hpa_caps.append(caps_dict)

        # hugepages capabilities
        caps_dict = self._get_hugepages_capabilities(extra_specs)
        if len(caps_dict) > 0:
            self._logger.debug("hugepages_capabilities_info: %s" % caps_dict)
            hpa_caps.append(caps_dict)

        # numa capabilities
        caps_dict = self._get_numa_capabilities(extra_specs)
        if len(caps_dict) > 0:
            self._logger.debug("numa_capabilities_info: %s" % caps_dict)
            hpa_caps.append(caps_dict)

        # storage capabilities
        caps_dict = self._get_storage_capabilities(flavor)
        if len(caps_dict) > 0:
            self._logger.debug("storage_capabilities_info: %s" % caps_dict)
            hpa_caps.append(caps_dict)

        # CPU instruction set extension capabilities
        caps_dict = self._get_instruction_set_capabilities(extra_specs)
        if len(caps_dict) > 0:
            self._logger.debug("instruction_set_capabilities_info: %s" % caps_dict)
            hpa_caps.append(caps_dict)

        # PCI passthrough capabilities
        caps_dict = self._get_pci_passthrough_capabilities(extra_specs)
        if len(caps_dict) > 0:
            self._logger.debug("pci_passthrough_capabilities_info: %s" % caps_dict)
            hpa_caps.append(caps_dict)

        # SRIOV-NIC capabilities
        caps_dict = self._get_sriov_nic_capabilities(extra_specs)
        if len(caps_dict) > 0:
            self._logger.debug("sriov_nic_capabilities_info: %s" % caps_dict)
            hpa_caps.append(caps_dict)

        # ovsdpdk capabilities
        caps_dict = self._get_ovsdpdk_capabilities(extra_specs, viminfo)
        if len(caps_dict) > 0:
            self._logger.debug("ovsdpdk_capabilities_info: %s" % caps_dict)
            hpa_caps.append(caps_dict)

        return hpa_caps

    def _get_hpa_basic_capabilities(self, flavor):
        basic_capability = {}
        feature_uuid = uuid.uuid4()

        basic_capability['hpa-capability-id'] = str(feature_uuid)
        basic_capability['hpa-feature'] = 'basicCapabilities'
        basic_capability['architecture'] = 'generic'
        basic_capability['hpa-version'] = 'v1'

        basic_capability['hpa-feature-attributes'] = []
        basic_capability['hpa-feature-attributes'].append({'hpa-attribute-key': 'numVirtualCpu',
                                               'hpa-attribute-value':
                                                   '{{\"value\":\"{0}\"}}'.format(flavor['vcpus'])
                                                           })
        basic_capability['hpa-feature-attributes'].append({'hpa-attribute-key':'virtualMemSize',
                                               'hpa-attribute-value':
                                                   '{{\"value\":\"{0}\",\"unit\":\"{1}\"}}'.format(flavor['ram'],"MB")
                                                           })

        return basic_capability

    def _get_cpupining_capabilities(self, extra_specs):
        cpupining_capability = {}
        feature_uuid = uuid.uuid4()

        if extra_specs.has_key('hw:cpu_policy') or extra_specs.has_key('hw:cpu_thread_policy'):
            cpupining_capability['hpa-capability-id'] = str(feature_uuid)
            cpupining_capability['hpa-feature'] = 'cpuPinning'
            cpupining_capability['architecture'] = 'generic'
            cpupining_capability['hpa-version'] = 'v1'

            cpupining_capability['hpa-feature-attributes'] = []
            if extra_specs.has_key('hw:cpu_thread_policy'):
                cpupining_capability['hpa-feature-attributes'].append({'hpa-attribute-key': 'logicalCpuThreadPinningPolicy',
                                                           'hpa-attribute-value':
                                                               '{{\"value\":\"{0}\"}}'.format(extra_specs['hw:cpu_thread_policy'])
                                                                       })
            if extra_specs.has_key('hw:cpu_policy'):
                cpupining_capability['hpa-feature-attributes'].append({'hpa-attribute-key':'logicalCpuPinningPolicy',
                                                           'hpa-attribute-value':
                                                               '{{\"value\":\"{0}\"}}'.format(extra_specs['hw:cpu_policy'])
                                                                       })

        return cpupining_capability

    def _get_cputopology_capabilities(self, extra_specs):
        cputopology_capability = {}
        feature_uuid = uuid.uuid4()

        if extra_specs.has_key('hw:cpu_sockets') or extra_specs.has_key('hw:cpu_cores') or extra_specs.has_key('hw:cpu_threads'):
            cputopology_capability['hpa-capability-id'] = str(feature_uuid)
            cputopology_capability['hpa-feature'] = 'cpuTopology'
            cputopology_capability['architecture'] = 'generic'
            cputopology_capability['hpa-version'] = 'v1'

            cputopology_capability['hpa-feature-attributes'] = []
            if extra_specs.has_key('hw:cpu_sockets'):
                cputopology_capability['hpa-feature-attributes'].append({'hpa-attribute-key': 'numCpuSockets',
                                                             'hpa-attribute-value':
                                                               '{{\"value\":\"{0}\"}}'.format(extra_specs['hw:cpu_sockets'])
                                                                         })
            if extra_specs.has_key('hw:cpu_cores'):
                cputopology_capability['hpa-feature-attributes'].append({'hpa-attribute-key': 'numCpuCores',
                                                             'hpa-attribute-value':
                                                               '{{\"value\":\"{0}\"}}'.format(extra_specs['hw:cpu_cores'])
                                                                         })
            if extra_specs.has_key('hw:cpu_threads'):
                cputopology_capability['hpa-feature-attributes'].append({'hpa-attribute-key': 'numCpuThreads',
                                                             'hpa-attribute-value':
                                                               '{{\"value\":\"{0}\"}}'.format(extra_specs['hw:cpu_threads'])
                                                                         })

        return cputopology_capability

    def _get_hugepages_capabilities(self, extra_specs):
        hugepages_capability = {}
        feature_uuid = uuid.uuid4()

        if extra_specs.has_key('hw:mem_page_size'):
            hugepages_capability['hpa-capability-id'] = str(feature_uuid)
            hugepages_capability['hpa-feature'] = 'hugePages'
            hugepages_capability['architecture'] = 'generic'
            hugepages_capability['hpa-version'] = 'v1'

            hugepages_capability['hpa-feature-attributes'] = []
            if extra_specs['hw:mem_page_size'] == 'large':
                hugepages_capability['hpa-feature-attributes'].append({'hpa-attribute-key': 'memoryPageSize',
                                                           'hpa-attribute-value':
                                                   '{{\"value\":\"{0}\",\"unit\":\"{1}\"}}'.format(2,"MB")
                                                                       })
            elif extra_specs['hw:mem_page_size'] == 'small':
                hugepages_capability['hpa-feature-attributes'].append({'hpa-attribute-key': 'memoryPageSize',
                                                           'hpa-attribute-value':
                                                   '{{\"value\":\"{0}\",\"unit\":\"{1}\"}}'.format(4,"KB")
                                                                       })
            elif extra_specs['hw:mem_page_size'] == 'any':
                self._logger.info("Currently HPA feature memoryPageSize did not support 'any' page!!")
            else :
                hugepages_capability['hpa-feature-attributes'].append({'hpa-attribute-key': 'memoryPageSize',
                                                           'hpa-attribute-value':
                                                   '{{\"value\":\"{0}\",\"unit\":\"{1}\"}}'.format(extra_specs['hw:mem_page_size'],"KB")
                                                                       })
        return hugepages_capability

    def _get_numa_capabilities(self, extra_specs):
        numa_capability = {}
        feature_uuid = uuid.uuid4()

        if extra_specs.has_key('hw:numa_nodes'):
            numa_capability['hpa-capability-id'] = str(feature_uuid)
            numa_capability['hpa-feature'] = 'numa'
            numa_capability['architecture'] = 'generic'
            numa_capability['hpa-version'] = 'v1'

            numa_capability['hpa-feature-attributes'] = []
            numa_capability['hpa-feature-attributes'].append({'hpa-attribute-key': 'numaNodes',
                                                  'hpa-attribute-value':
                                                      '{{\"value\":\"{0}\"}}'.format(extra_specs['hw:numa_nodes'] or 0)
                                                              })

            for num in range(0, int(extra_specs['hw:numa_nodes'])):
                numa_cpu_node = "hw:numa_cpus.%s" % num
                numa_mem_node = "hw:numa_mem.%s" % num
                numacpu_key = "numaCpu-%s" % num
                numamem_key = "numaMem-%s" % num

                if extra_specs.has_key(numa_cpu_node) and extra_specs.has_key(numa_mem_node):
                    numa_capability['hpa-feature-attributes'].append({'hpa-attribute-key': numacpu_key,
                                                          'hpa-attribute-value':
                                                               '{{\"value\":\"{0}\"}}'.format(extra_specs[numa_cpu_node])
                                                                      })
                    numa_capability['hpa-feature-attributes'].append({'hpa-attribute-key': numamem_key,
                                                          'hpa-attribute-value':
                                                   '{{\"value\":\"{0}\",\"unit\":\"{1}\"}}'.format(extra_specs[numa_mem_node],"MB")
                                                                      })

        return numa_capability

    def _get_storage_capabilities(self, flavor):
        storage_capability = {}
        feature_uuid = uuid.uuid4()

        storage_capability['hpa-capability-id'] = str(feature_uuid)
        storage_capability['hpa-feature'] = 'localStorage'
        storage_capability['architecture'] = 'generic'
        storage_capability['hpa-version'] = 'v1'

        storage_capability['hpa-feature-attributes'] = []
        storage_capability['hpa-feature-attributes'].append({'hpa-attribute-key': 'diskSize',
                                                       'hpa-attribute-value':
                                                   '{{\"value\":\"{0}\",\"unit\":\"{1}\"}}'.format(flavor['disk'] or 0,"GB")
                                                             })
        storage_capability['hpa-feature-attributes'].append({'hpa-attribute-key': 'swapMemSize',
                                                       'hpa-attribute-value':
                                                   '{{\"value\":\"{0}\",\"unit\":\"{1}\"}}'.format(flavor['swap'] or 0,"MB")
                                                             })
        storage_capability['hpa-feature-attributes'].append({'hpa-attribute-key': 'ephemeralDiskSize',
                                                       'hpa-attribute-value':
                                                   '{{\"value\":\"{0}\",\"unit\":\"{1}\"}}'.format(flavor['OS-FLV-EXT-DATA:ephemeral'] or 0,"GB")
                                                             })
        return storage_capability

    def _get_instruction_set_capabilities(self, extra_specs):
        instruction_capability = {}
        feature_uuid = uuid.uuid4()

        if extra_specs.has_key('hw:capabilities:cpu_info:features'):
            instruction_capability['hpa-capability-id'] = str(feature_uuid)
            instruction_capability['hpa-feature'] = 'instructionSetExtensions'
            instruction_capability['architecture'] = 'Intel64'
            instruction_capability['hpa-version'] = 'v1'

            instruction_capability['hpa-feature-attributes'] = []
            instruction_capability['hpa-feature-attributes'].append({'hpa-attribute-key': 'instructionSetExtensions',
                                                       'hpa-attribute-value':
                                                      '{{\"value\":\"{0}\"}}'.format(extra_specs['hw:capabilities:cpu_info:features'])
                                                                     })
        return instruction_capability

    def _get_pci_passthrough_capabilities(self, extra_specs):
        pci_passthrough_capability = {}
        feature_uuid = uuid.uuid4()

        if extra_specs.has_key('pci_passthrough:alias'):
            value1 = extra_specs['pci_passthrough:alias'].split(':')
            value2 = value1[0].split('-')

            pci_passthrough_capability['hpa-capability-id'] = str(feature_uuid)
            pci_passthrough_capability['hpa-feature'] = 'pciePassthrough'
            pci_passthrough_capability['architecture'] = str(value2[2])
            pci_passthrough_capability['hpa-version'] = 'v1'


            pci_passthrough_capability['hpa-feature-attributes'] = []
            pci_passthrough_capability['hpa-feature-attributes'].append({'hpa-attribute-key': 'pciCount',
                                                       'hpa-attribute-value':
                                                      '{{\"value\":\"{0}\"}}'.format(value1[1])
                                                                     })
            pci_passthrough_capability['hpa-feature-attributes'].append({'hpa-attribute-key': 'pciVendorId',
                                                       'hpa-attribute-value':
                                                      '{{\"value\":\"{0}\"}}'.format(value2[3])
                                                                     })
            pci_passthrough_capability['hpa-feature-attributes'].append({'hpa-attribute-key': 'pciDeviceId',
                                                       'hpa-attribute-value':
                                                      '{{\"value\":\"{0}\"}}'.format(value2[4])
                                                                     })

        return pci_passthrough_capability

    def _get_sriov_nic_capabilities(self, extra_specs):
        sriov_capability = {}
        feature_uuid = uuid.uuid4()

        if extra_specs.has_key('sriov_nic'):
            value1 = extra_specs['sriov_nic'].split(':')
            value2 = value1[0].split('-')

            sriov_capability['hpa-capability-id'] = str(feature_uuid)
            sriov_capability['hpa-feature'] = 'sriovNICNetwork'
            sriov_capability['architecture'] = str(value2[2])
            sriov_capability['hpa-version'] = 'v1'

            sriov_capability['hpa-feature-attributes'] = []
            sriov_capability['hpa-feature-attributes'].append({'hpa-attribute-key': 'pciCount',
                                          'hpa-attribute-value':
                                          '{{\"value\":\"{0}\"}}'.format(value1[1]) })
            sriov_capability['hpa-feature-attributes'].append({'hpa-attribute-key': 'pciVendorId',
                                          'hpa-attribute-value':
                                          '{{\"value\":\"{0}\"}}'.format(value2[3]) })
            sriov_capability['hpa-feature-attributes'].append({'hpa-attribute-key': 'pciDeviceId',
                                          'hpa-attribute-value':
                                          '{{\"value\":\"{0}\"}}'.format(value2[4]) })
            sriov_capability['hpa-feature-attributes'].append({'hpa-attribute-key': 'physicalNetwork',
                                          'hpa-attribute-value':
                                          '{{\"value\":\"{0}\"}}'.format(value2[5]) })

        return sriov_capability

    def _get_ovsdpdk_capabilities(self, extra_specs, viminfo):
        ovsdpdk_capability = {}
        feature_uuid = uuid.uuid4()

        cloud_extra_info_str = viminfo.get('cloud_extra_info')
        if not isinstance(cloud_extra_info_str, dict):
            try:
                cloud_extra_info_str = json.loads(cloud_extra_info_str)
            except Exception as ex:
                logger.error("Can not convert cloud extra info %s %s" % (
                             str(ex), cloud_extra_info_str))
                return {}
        if cloud_extra_info_str :
            cloud_dpdk_info = cloud_extra_info_str.get("ovsDpdk")
            if cloud_dpdk_info :
                ovsdpdk_capability['hpa-capability-id'] = str(feature_uuid)
                ovsdpdk_capability['hpa-feature'] = 'ovsDpdk'
                ovsdpdk_capability['architecture'] = 'Intel64'
                ovsdpdk_capability['hpa-version'] = 'v1'

                ovsdpdk_capability['hpa-feature-attributes'] = [
                    {
                        'hpa-attribute-key': str(cloud_dpdk_info.get("libname")),
                        'hpa-attribute-value': '{{\"value\":\"{0}\"}}'.format(cloud_dpdk_info.get("libversion"))
                    },]

        return ovsdpdk_capability

    # def update_image_metadata(self, cloud_owner, cloud_region_id, image_id, metadatainfo):
    #     '''
    #     populate image meta data
    #     :param cloud_owner:
    #     :param cloud_region_id:
    #     :param image_id:
    #     :param metadatainfo:
    #         metaname: string
    #         metaval: string
    #     :return:
    #     '''
    #
    #     if cloud_owner and cloud_region_id:
    #         retcode, content, status_code = \
    #             restcall.req_to_aai(
    #                 "/cloud-infrastructure/cloud-regions/cloud-region"
    #                 + "/%s/%s/images/image/%s/metadata/metadatum/%s"
    #                 % (cloud_owner, cloud_region_id, image_id, metadatainfo['metaname']),
    #                 "PUT", content=metadatainfo)
    #
    #         self._logger.debug("update_image,vimid:%s_%s req_to_aai: %s/%s, return %s, %s, %s"
    #                            % (cloud_owner,cloud_region_id,image_id,metadatainfo['metaname'],
    #                               retcode, content, status_code))
    #         return retcode
    #     return 1

    def _discover_images(self, vimid="", session=None, viminfo=None):
        try:
            cloud_owner, cloud_region_id = extsys.decode_vim_id(vimid)
            for image in self._get_list_resources(
                    "/v2/images", "image", session, viminfo, vimid,
                    "images"):
                image_info = {
                    'image-id': image['id'],
                    'image-name': image['name'],
                    'image-selflink': image['self'],

                    'image-os-distro': image.get('os_distro') or 'Unknown',
                    'image-os-version': image.get('os_version') or 'Unknown',
                    'application': image.get('application'),
                    'application-vendor': image.get('application_vendor'),
                    'application-version': image.get('application_version'),
                    'image-architecture': image.get('architecture'),
                }

                ret = self._update_resoure(
                    cloud_owner, cloud_region_id, image['id'], image_info,
                    "image")
                if ret != 0:
                    # failed to update image
                    self._logger.debug("failed to populate image info into AAI: %s, image id: %s, ret:%s"
                                       % (vimid, image_info['image-id'], ret))
                    continue

                schema = image['schema']
                if schema:
                    req_resource = schema
                    service = {'service_type': "image",
                               'interface': 'public',
                               'region_name': viminfo['openstack_region_id']
                               if viminfo.get('openstack_region_id')
                               else viminfo['cloud_region_id']
                               }

                    self._logger.info("making request with URI:%s" % req_resource)
                    resp = session.get(req_resource, endpoint_filter=service)
                    self._logger.info("request returns with status %s" % resp.status_code)
                    if resp.status_code == status.HTTP_200_OK:
                        self._logger.debug("with content:%s" % resp.json())
                        pass
                    content = resp.json()

                    # if resp.status_code == status.HTTP_200_OK:
                        # parse the schema? TBD
                        # self.update_image(cloud_owner, cloud_region_id, image_info)
                        #metadata_info = {}

        except VimDriverNewtonException as e:
            self._logger.error("VimDriverNewtonException: status:%s, response:%s" % (e.http_status, e.content))
            return
        except HttpError as e:
            self._logger.error("HttpError: status:%s, response:%s" % (e.http_status, e.response.json()))
            return
        except Exception as e:
            self._logger.error(traceback.format_exc())
            return

    def _discover_availability_zones(self, vimid="", session=None,
                                     viminfo=None):
        try:
            cloud_owner, cloud_region_id = extsys.decode_vim_id(vimid)
            for az in self._get_list_resources(
                    "/os-availability-zone/detail", "compute", session,
                    viminfo, vimid,
                    "availabilityZoneInfo"):
                az_info = {
                    'availability-zone-name': az['zoneName'],
                    'operational-status': az['zoneState']['available']
                    if az.get('zoneState') else '',
                    'hypervisor-type': '',
                }
                if az.get('hosts'):
                    for (k, v) in az['hosts'].items():
                        req_resource = "/os-hypervisors/detail?hypervisor_hostname_pattern=%s" % k
                        service = {'service_type': "compute",
                                   'interface': 'public',
                                   'region_name': viminfo['openstack_region_id']
                                   if viminfo.get('openstack_region_id')
                                   else viminfo['cloud_region_id']
                                   }

                        self._logger.info("making request with URI:%s" % req_resource)
                        resp = session.get(req_resource, endpoint_filter=service)
                        self._logger.info("request returns with status %s" % resp.status_code)
                        if resp.status_code == status.HTTP_200_OK:
                            self._logger.debug("with content:%s" % resp.json())
                            pass
                        content = resp.json()
                        if resp.status_code != status.HTTP_200_OK and not content[0]:
                            continue
                        az_info['hypervisor-type'] = content['hypervisors'][0]['hypervisor_type']\
                            if len(content.get('hypervisors')) else ''

                        break
                ret = self._update_resoure(
                    cloud_owner, cloud_region_id, az['zoneName'], az_info,
                    "availability-zone")
                if ret != 0:
                    # failed to update image
                    self._logger.debug("failed to populate az info into AAI: %s, az name: %s, ret:%s"
                                       % (vimid, az_info['availability-zone-name'], ret))

        except VimDriverNewtonException as e:
            self._logger.error("VimDriverNewtonException: status:%s, response:%s" % (e.http_status, e.content))
            return
        except HttpError as e:
            self._logger.error("HttpError: status:%s, response:%s" % (e.http_status, e.response.json()))
            return
        except Exception as e:
            self._logger.error(traceback.format_exc())
            return

    # def _discover_volumegroups(self, vimid="", session=None, viminfo=None):
    #     cloud_owner, cloud_region_id = extsys.decode_vim_id(vimid)
    #     for cg in self._get_list_resources(
    #             "/consistencygroups/detail", "volumev3", session,
    #             viminfo, vimid,
    #             "consistencygroups"):
    #         vg_info = {
    #             'volume-group-id': cg['id'],
    #             'volume-group-name': cg['name'],
    #             'vnf-type': '',
    #         }
    #
    #         ret = self._update_resoure(
    #             cloud_owner, cloud_region_id, cg['id'], vg_info,
    #             "volume-group")
    #         if ret != 0:
    #             # failed to update image
    #             self._logger.debug("failed to populate volumegroup info into AAI: %s, volume-group-id: %s, ret:%s"
    #                                % (vimid, vg_info['volume-group-id'], ret))

    def _discover_snapshots(self, vimid="", session=None, viminfo=None):
        try:
            cloud_owner, cloud_region_id = extsys.decode_vim_id(vimid)
            for ss in self._get_list_resources(
                    "/snapshots/detail", "volumev3", session,
                    viminfo, vimid,
                    "snapshots"):
                snapshot_info = {
                    'snapshot-id': ss['id'],
                    'snapshot-name': ss['name'],
                }
                if ss.get('metadata'):
                    snapshot_info['snapshot-architecture'] = ss['metadata'].get('architecture')
                    snapshot_info['application'] = ss['metadata'].get('architecture')
                    snapshot_info['snapshot-os-distro'] = ss['metadata'].get('os-distro')
                    snapshot_info['snapshot-os-version'] = ss['metadata'].get('os-version')
                    snapshot_info['application-vendor'] = ss['metadata'].get('vendor')
                    snapshot_info['application-version'] = ss['metadata'].get('version')
                    snapshot_info['snapshot-selflink'] = ss['metadata'].get('selflink')
                    snapshot_info['prev-snapshot-id'] = ss['metadata'].get('prev-snapshot-id')

                ret = self._update_resoure(
                    cloud_owner, cloud_region_id, ss['id'], snapshot_info,
                    "snapshot")
                if ret != 0:
                    # failed to update image
                    self._logger.debug("failed to populate snapshot info into AAI: %s, snapshot-id: %s, ret:%s"
                                       % (vimid, snapshot_info['snapshot-id'], ret))

        except VimDriverNewtonException as e:
            self._logger.error("VimDriverNewtonException: status:%s, response:%s" % (e.http_status, e.content))
            return
        except HttpError as e:
            self._logger.error("HttpError: status:%s, response:%s" % (e.http_status, e.response.json()))
            return
        except Exception as e:
            self._logger.error(traceback.format_exc())
            return

    # def _discover_servergroups(self, vimid="", session=None, viminfo=None):
    #     for sg in self._get_list_resources(
    #             "/os-server-groups", "compute", session,
    #             viminfo, vimid,
    #             "security groups"):

    def _update_pserver(self, cloud_owner, cloud_region_id, pserverinfo):
        '''
        populate pserver into AAI
        :param cloud_owner:
        :param cloud_region_id:
        :param pserverinfo:
            hostname: string
            in-maint: boolean

            pserver-name2: string
            pserver-id: string
            ptnii-equip-name: string
            number-of-cpus: integer
            disk-in-gigabytes: integer
            ram-in-megabytes: integer
            equip-type: string
            equip-vendor: string
            equip-model: string
            fqdn: string
            pserver-selflink: string
            ipv4-oam-address: string
            serial-number: string
            ipaddress-v4-loopback-0: string
            ipaddress-v6-loopback-0: string
            ipaddress-v4-aim: string
            ipaddress-v6-aim: string
            ipaddress-v6-oam: string
            inv-status: string
            internet-topology: string
            purpose: string
            prov-status: string
            management-option: string
            host-profile: string

        :return:
        '''

        if cloud_owner and cloud_region_id:
            resource_url = "/cloud-infrastructure/pservers/pserver/%s" \
                           % (pserverinfo['hostname'])

            # get cloud-region
            retcode, content, status_code = \
                restcall.req_to_aai(resource_url, "GET")

            # add resource-version to url
            if retcode == 0 and content:
                content = json.JSONDecoder().decode(content)
                #pserverinfo["resource-version"] = content["resource-version"]
                content.update(pserverinfo)
                pserverinfo = content


            retcode, content, status_code = \
                restcall.req_to_aai(resource_url, "PUT", content=pserverinfo)

            self._logger.debug("update_snapshot,vimid:%s_%s req_to_aai: %s, return %s, %s, %s"
                               % (cloud_owner,cloud_region_id, pserverinfo['hostname'], retcode, content, status_code))

            if retcode == 0:
                # relationship to cloud-region

                related_link = ("%s/cloud-infrastructure/cloud-regions/"
                                "cloud-region/%s/%s" % (
                                    self.aai_base_url, cloud_owner,
                                    cloud_region_id))

                relationship_data = \
                    {
                        'related-to': 'cloud-region',
                        'related-link': related_link,
                        'relationship-data': [
                            {
                                'relationship-key': 'cloud-region.cloud-owner',
                                'relationship-value': cloud_owner
                            },
                            {
                                'relationship-key': 'cloud-region.cloud-region-id',
                                'relationship-value': cloud_region_id
                            }
                        ],
                        "related-to-property": [
                            {
                                "property-key": "cloud-region.cloud-owner"
                            },
                            {
                                "property-key": "cloud-region.cloud-region-id"
                            }
                        ]
                    }

                retcode, content, status_code = \
                    restcall.req_to_aai("/cloud-infrastructure/pservers/pserver/%s/relationship-list/relationship"
                               % (pserverinfo['hostname']), "PUT", content=relationship_data)

                self._logger.debug("update_pserver,vimid:%s_%s req_to_aai: %s, return %s, %s, %s"
                                   % (cloud_owner, cloud_region_id, pserverinfo['hostname'], retcode, content,
                                      status_code))

            return retcode
        return 1  # unknown cloud owner,region_id

    def _discover_pservers(self, vimid="", session=None, viminfo=None):
        try:
            cloud_owner, cloud_region_id = extsys.decode_vim_id(vimid)
            for hypervisor in self._get_list_resources(
                    "/os-hypervisors/detail", "compute", session,
                    viminfo, vimid,
                    "hypervisors"):
                hypervisor_info = {
                    'hostname': hypervisor['hypervisor_hostname'],
                    'in-maint': hypervisor['state'],

                    'pserver-id': hypervisor.get('id'),
                    'ptnii-equip-name': hypervisor.get('id'),
                    'disk-in-gigabytes': hypervisor.get('local_gb'),
                    'ram-in-megabytes': hypervisor.get('memory_mb'),
                    'pserver-selflink': hypervisor.get('hypervisor_links'),
                    'ipv4-oam-address': hypervisor.get('host_ip'),
                }

    #            if hypervisor.get('cpu_info') and hypervisor['cpu_info'].get('topology'):
    #                cputopo = hypervisor['cpu_info'].get('topology')
    #                n_cpus = cputopo['cores'] * cputopo['threads'] * cputopo['sockets']
    #                hypervisor_info['number-of-cpus'] = n_cpus
                if hypervisor.get('cpu_info'):
                    cpu_info = json.loads(hypervisor['cpu_info'])
                    if cpu_info.get('topology'):
                        cputopo = cpu_info.get('topology')
                        n_cpus = cputopo['cores'] * cputopo['threads'] * cputopo['sockets']
                        hypervisor_info['number-of-cpus'] = n_cpus

                ret = self._update_pserver(cloud_owner, cloud_region_id,
                                          hypervisor_info)
                if ret != 0:
                    # failed to update image
                    self._logger.debug("failed to populate pserver info into AAI: %s, hostname: %s, ret:%s"
                                       % (vimid, hypervisor_info['hostname'], ret))

        except VimDriverNewtonException as e:
            self._logger.error("VimDriverNewtonException: status:%s, response:%s" % (e.http_status, e.content))
            return
        except HttpError as e:
            self._logger.error("HttpError: status:%s, response:%s" % (e.http_status, e.response.json()))
            return
        except Exception as e:
            self._logger.error(traceback.format_exc())
            return

    def _update_proxy_identity_endpoint(self, vimid):
        '''
        update cloud_region's identity url
        :param cloud_owner:
        :param cloud_region_id:
        :param url:
        :return:
        '''
        try:
            cloud_owner, cloud_region_id = extsys.decode_vim_id(vimid)
            if cloud_owner and cloud_region_id:
                resource_url = "/cloud-infrastructure/cloud-regions/cloud-region/%s/%s" \
                               % (cloud_owner, cloud_region_id)

                # get cloud-region
                retcode, content, status_code = \
                    restcall.req_to_aai(resource_url, "GET")

                # add resource-version to url
                if retcode == 0 and content:
                    viminfo = json.JSONDecoder().decode(content)
                    viminfo['identity-url'] = self.proxy_prefix + "/%s/identity/v2.0" % vimid \
                        if self.proxy_prefix[-3:] == "/v0" else \
                        self.proxy_prefix + "/%s/%s/identity/v2.0" % extsys.decode_vim_id(vimid)

                    retcode, content, status_code = \
                        restcall.req_to_aai("/cloud-infrastructure/cloud-regions/cloud-region/%s/%s"
                                   % (cloud_owner, cloud_region_id), "PUT", content=viminfo)

                    self._logger.debug("update_proxy_identity_endpoint,vimid:%s req_to_aai: %s, return %s, %s, %s"
                                       % (vimid, viminfo['identity-url'], retcode, content, status_code))
                else:
                    self._logger.debug("failure: update_proxy_identity_endpoint,vimid:%s req_to_aai: return %s, %s, %s"
                                       % (vimid, retcode, content, status_code))

        except VimDriverNewtonException as e:
            self._logger.error("VimDriverNewtonException: status:%s, response:%s" % (e.http_status, e.content))
            return
        except HttpError as e:
            self._logger.error("HttpError: status:%s, response:%s" % (e.http_status, e.response.json()))
            return
        except Exception as e:
            self._logger.error(traceback.format_exc())
            return

    def post(self, request, vimid=""):
        self._logger.info("registration with vimid: %s" % vimid)
        self._logger.debug("with data: %s" % request.data)

        try:
            # populate proxy identity url
            self._update_proxy_identity_endpoint(vimid)

            # prepare request resource to vim instance
            # get token:
            viminfo = VimDriverUtils.get_vim_info(vimid)
            if not viminfo:
                raise VimDriverNewtonException(
                    "There is no cloud-region with {cloud-owner}_{cloud-region-id}=%s in AAI" % vimid)

            # set the default tenant since there is no tenant info in the VIM yet
            sess = VimDriverUtils.get_session(
                viminfo, tenant_name=viminfo['tenant'])

            # step 1. discover all projects and populate into AAI
            self._discover_tenants(vimid, sess, viminfo)

            # discover all flavors
            self._discover_flavors(vimid, sess, viminfo)

            # discover all images
            self._discover_images(vimid, sess, viminfo)

            # discover all az
            self._discover_availability_zones(vimid, sess, viminfo)

            # discover all vg
            #self._discover_volumegroups(vimid, sess, viminfo)

            # discover all snapshots
            self._discover_snapshots(vimid, sess, viminfo)

            # discover all server groups
            #self.discover_servergroups(request, vimid, sess, viminfo)

            # discover all pservers
            self._discover_pservers(vimid, sess, viminfo)

            return Response(status=status.HTTP_202_ACCEPTED)

        except VimDriverNewtonException as e:
            return Response(data={'error': e.content}, status=e.status_code)
        except HttpError as e:
            self._logger.error("HttpError: status:%s, response:%s" % (e.http_status, e.response.json()))
            return Response(data=e.response.json(), status=e.http_status)
        except Exception as e:
            self._logger.error(traceback.format_exc())
            return Response(
                data={'error': str(e)},
                status=status.HTTP_500_INTERNAL_SERVER_ERROR)

    def delete(self, request, vimid=""):
        self._logger.debug("Registration--delete::data> %s" % request.data)
        self._logger.debug("Registration--delete::vimid > %s"% vimid)
        try:

            # prepare request resource to vim instance
            # get token:
            viminfo = VimDriverUtils.get_vim_info(vimid)
            if not viminfo:
                raise VimDriverNewtonException(
                    "There is no cloud-region with {cloud-owner}_{cloud-region-id}=%s in AAI" % vimid)

            cloud_owner, cloud_region_id = extsys.decode_vim_id(vimid)

            #get the resource first
            resource_url = ("/cloud-infrastructure/cloud-regions/"
                     "cloud-region/%(cloud_owner)s/%(cloud_region_id)s?depth=all"
                     % {
                         "cloud_owner": cloud_owner,
                         "cloud_region_id": cloud_region_id,
                     })

            # get cloud-region
            retcode, content, status_code = \
                restcall.req_to_aai(resource_url, "GET")

            # add resource-version
            if retcode == 0 and content:
                cloudregiondata = json.JSONDecoder().decode(content)

            # step 1. remove all tenants
            tenants = cloudregiondata.get("tenants", None)
            for tenant in tenants.get("tenant", []) if tenants else []:
                # common prefix
                aai_cloud_region = "/cloud-infrastructure/cloud-regions/cloud-region/%s/%s/tenants/tenant/%s" \
                                   % (cloud_owner, cloud_region_id, tenant['tenant-id'])

                # remove all vservers
                try:
                    # get list of vservers
                    vservers = tenant.get('vservers', {}).get('vserver', [])
                    for vserver in vservers:
                        try:
                            # iterate vport, except will be raised if no l-interface exist
                            for vport in vserver['l-interfaces']['l-interface']:
                                # delete vport
                                vport_delete_url = aai_cloud_region + "/vservers/vserver/%s/l-interfaces/l-interface/%s?resource-version=%s" \
                                                                      % (vserver['vserver-id'], vport['interface-name'],
                                                                         vport['resource-version'])
                                restcall.req_to_aai(vport_delete_url, "DELETE")
                        except Exception as e:
                            pass

                        try:
                            # delete vserver
                            vserver_delete_url = aai_cloud_region + "/vservers/vserver/%s?resource-version=%s" \
                                                                    % (
                                                                    vserver['vserver-id'], vserver['resource-version'])
                            restcall.req_to_aai(vserver_delete_url, "DELETE")
                        except Exception as e:
                            continue

                except Exception:
                    self._logger.error(traceback.format_exc())
                    pass

                resource_url = ("/cloud-infrastructure/cloud-regions/"
                     "cloud-region/%(cloud_owner)s/%(cloud_region_id)s/"
                     "%(resource_type)ss/%(resource_type)s/%(resoure_id)s/"
                     "?resource-version=%(resource-version)s"
                     % {
                         "cloud_owner": cloud_owner,
                         "cloud_region_id": cloud_region_id,
                         "resource_type": "tenant",
                         "resoure_id": tenant["tenant-id"],
                         "resource-version": tenant["resource-version"]
                     })
                # remove tenant
                retcode, content, status_code = \
                    restcall.req_to_aai(resource_url, "DELETE")

            # remove all flavors
            flavors = cloudregiondata.get("flavors", None)
            for flavor in flavors.get("flavor", []) if flavors else []:
                # iterate hpa-capabilities
                hpa_capabilities = flavor.get("hpa-capabilities", None)
                for hpa_capability in hpa_capabilities.get("hpa-capability", []) if hpa_capabilities else []:
                    resource_url = ("/cloud-infrastructure/cloud-regions/"
                                    "cloud-region/%(cloud_owner)s/%(cloud_region_id)s/"
                                    "%(resource_type)ss/%(resource_type)s/%(resoure_id)s/"
                                    "hpa-capabilities/hpa-capability/%(hpa-capability-id)s/"
                                    "?resource-version=%(resource-version)s"
                                    % {
                                        "cloud_owner": cloud_owner,
                                        "cloud_region_id": cloud_region_id,
                                        "resource_type": "flavor",
                                        "resoure_id": flavor["flavor-id"],
                                        "hpa-capability-id": hpa_capability["hpa-capability-id"],
                                        "resource-version": hpa_capability["resource-version"]
                                    })
                    # remove hpa-capability
                    retcode, content, status_code = \
                        restcall.req_to_aai(resource_url, "DELETE")

                # remove flavor
                resource_url = ("/cloud-infrastructure/cloud-regions/"
                     "cloud-region/%(cloud_owner)s/%(cloud_region_id)s/"
                     "%(resource_type)ss/%(resource_type)s/%(resoure_id)s/"
                     "?resource-version=%(resource-version)s"
                     % {
                         "cloud_owner": cloud_owner,
                         "cloud_region_id": cloud_region_id,
                         "resource_type": "flavor",
                         "resoure_id": flavor["flavor-id"],
                         "resource-version": flavor["resource-version"]
                     })

                retcode, content, status_code = \
                    restcall.req_to_aai(resource_url, "DELETE")

            # remove all images
            images = cloudregiondata.get("images", None)
            for image in images.get("image", []) if images else []:
                resource_url = ("/cloud-infrastructure/cloud-regions/"
                     "cloud-region/%(cloud_owner)s/%(cloud_region_id)s/"
                     "%(resource_type)ss/%(resource_type)s/%(resoure_id)s/"
                     "?resource-version=%(resource-version)s"
                     % {
                         "cloud_owner": cloud_owner,
                         "cloud_region_id": cloud_region_id,
                         "resource_type": "image",
                         "resoure_id": image["image-id"],
                         "resource-version": image["resource-version"]
                     })
                # remove image
                retcode, content, status_code = \
                    restcall.req_to_aai(resource_url, "DELETE")

            # remove all az

            # remove all vg

            # remove all snapshots
            snapshots = cloudregiondata.get("snapshots", None)
            for snapshot in snapshots.get("snapshot", []) if snapshots else []:
                resource_url = ("/cloud-infrastructure/cloud-regions/"
                     "cloud-region/%(cloud_owner)s/%(cloud_region_id)s/"
                     "%(resource_type)ss/%(resource_type)s/%(resoure_id)s/"
                     "?resource-version=%(resource-version)s"
                     % {
                         "cloud_owner": cloud_owner,
                         "cloud_region_id": cloud_region_id,
                         "resource_type": "snapshot",
                         "resoure_id": snapshot["snapshot-id"],
                         "resource-version": snapshot["resource-version"]
                     })
                # remove snapshot
                retcode, content, status_code = \
                    restcall.req_to_aai(resource_url, "DELETE")

            # remove all server groups

            # remove all pservers

            # remove cloud region itself
            resource_url = ("/cloud-infrastructure/cloud-regions/"
                 "cloud-region/%(cloud_owner)s/%(cloud_region_id)s"
                 "?resource-version=%(resource-version)s"
                 % {
                     "cloud_owner": cloud_owner,
                     "cloud_region_id": cloud_region_id,
                     "resource-version": cloudregiondata["resource-version"]
                 })
            # remove cloud region
            retcode, content, status_code = \
                restcall.req_to_aai(resource_url, "DELETE")

            #ret_code = VimDriverUtils.delete_vim_info(vimid)
            return Response(status=status.HTTP_204_NO_CONTENT if retcode==0 else status.HTTP_500_INTERNAL_SERVER_ERROR)
        except VimDriverNewtonException as e:
            return Response(data={'error': e.content}, status=e.status_code)
        except HttpError as e:
            self._logger.error("HttpError: status:%s, response:%s" % (e.http_status, e.response.json()))
            return Response(data=e.response.json(), status=e.http_status)
        except Exception as e:
            self._logger.error(traceback.format_exc())
            return Response(data={'error': str(e)},
                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)