Handle CSAR reading errors in Service Import
[sdc.git] / catalog-ui / src / app / view-models / workspace / tabs / general / general-view-model.ts
index 9d67b8e..77c9330 100644 (file)
@@ -41,6 +41,7 @@ import {IWorkspaceViewModelScope} from "app/view-models/workspace/workspace-view
 import {CATEGORY_SERVICE_METADATA_KEYS, PREVIOUS_CSAR_COMPONENT, DEFAULT_MODEL_NAME} from "../../../../utils/constants";
 import {Observable} from "rxjs";
 import {Model} from "../../../../models/model";
+import {SdcUiServices} from "onap-ui-angular/dist";
 
 export class Validation {
     componentNameValidationPattern:RegExp;
@@ -127,6 +128,7 @@ export class GeneralViewModel {
         'sdcConfig',
         '$state',
         'ModalsHandler',
+        'ModalServiceSdcUI',
         'EventListenerService',
         'Notification',
         'Sdc.Services.ProgressService',
@@ -155,6 +157,7 @@ export class GeneralViewModel {
                 private sdcConfig:IAppConfigurtaion,
                 private $state:ng.ui.IStateService,
                 private ModalsHandler:ModalsHandler,
+                private modalServiceSdcUI: SdcUiServices.ModalService,
                 private EventListenerService:EventListenerService,
                 private Notification:any,
                 private progressService:ProgressService,
@@ -277,28 +280,40 @@ export class GeneralViewModel {
             if (resource.resourceType === ResourceType.VF && !resource.csarUUID) {
                 this.$scope.isShowFileBrowse = true;
             }
-        } else if(this.$scope.component.isService()){
+        } else if (this.$scope.component.isService()) {
             let service: Service = <Service>this.$scope.component;
             console.log(service.name + ": " + service.csarUUID);
             if (service.importedFile) {
                 this.$scope.isShowFileBrowse = true;
                 (<Service>this.$scope.component).ecompGeneratedNaming = true;
                 let blob = this.FileUtils.base64toBlob(service.importedFile.base64, "zip");
-                new ServiceCsarReader().read(blob).then((serviceCsar) => {
-                    serviceCsar.serviceMetadata.contactId = this.cacheService.get("user").userId;
-                    (<Service>this.$scope.component).setComponentMetadata(serviceCsar.serviceMetadata);
-                    (<Service>this.$scope.component).model = serviceCsar.serviceMetadata.model;
-                    this.$scope.onModelChange();
-                    this.$scope.componentCategories.selectedCategory = serviceCsar.serviceMetadata.selectedCategory;
-                    this.$scope.onCategoryChange();
-                    serviceCsar.extraServiceMetadata.forEach((value: string, key: string) => {
-                        if(this.getMetadataKey(key)) {
-                            (<Service>this.$scope.component).categorySpecificMetadata[key] = value;
-                        }
+                new ServiceCsarReader().read(blob).then(
+                    (serviceCsar) => {
+                        serviceCsar.serviceMetadata.contactId = this.cacheService.get("user").userId;
+                        (<Service>this.$scope.component).setComponentMetadata(serviceCsar.serviceMetadata);
+                        (<Service>this.$scope.component).model = serviceCsar.serviceMetadata.model;
+                        this.$scope.onModelChange();
+                        this.$scope.componentCategories.selectedCategory = serviceCsar.serviceMetadata.selectedCategory;
+                        this.$scope.onCategoryChange();
+                        serviceCsar.extraServiceMetadata.forEach((value: string, key: string) => {
+                            if (this.getMetadataKey(key)) {
+                                (<Service>this.$scope.component).categorySpecificMetadata[key] = value;
+                            }
+                        });
+                        (<Service>this.$scope.component).derivedFromGenericType = serviceCsar.substitutionNodeType;
+                        this.$scope.onBaseTypeChange();
+                    },
+                    (error) => {
+                        const errorMsg = this.$filter('translate')('IMPORT_FAILURE_MESSAGE_TEXT');
+                        console.error(errorMsg, error);
+                        const errorDetails = {
+                            'Error': error.reason,
+                            'Details': error.message
+                        };
+                        this.modalServiceSdcUI.openErrorDetailModal('Error', this.$filter('translate')('IMPORT_FAILURE_MESSAGE_TEXT'),
+                            'error-modal', errorDetails);
+                        this.$state.go('dashboard');
                     });
-                    (<Service>this.$scope.component).derivedFromGenericType = serviceCsar.substitutionNodeType;
-                    this.$scope.onBaseTypeChange();
-                });
             }
             if (this.$scope.isEditMode() && service.serviceType == 'Service' && !service.csarUUID) {
                 this.$scope.isShowFileBrowse = true;