Provide capability to add user specified name for service role/function
[sdc.git] / catalog-ui / src / app / view-models / workspace / tabs / general / general-view-model.ts
index 9c103b4..a0e8ca9 100644 (file)
@@ -30,7 +30,9 @@ import {
     instantiationType,
     ModalsHandler,
     ResourceType,
-    ValidationUtils
+    ValidationUtils,
+    FileUtils,
+    ServiceCsarReader
 } from "app/utils";
 import {EventListenerService, ProgressService} from "app/services";
 import {CacheService, ElementService, ModelService, ImportVSPService, OnboardingService} from "app/services-ng2";
@@ -39,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;
@@ -82,6 +85,10 @@ export interface IGeneralScope extends IWorkspaceViewModelScope {
     instantiationTypes:Array<instantiationType>;
     isHiddenCategorySelected: boolean;
     isModelRequired: boolean;
+    othersFlag: boolean;
+    functionOption: string;
+    othersRoleFlag: boolean;
+    roleOption: string;
 
     save():Promise<any>;
     revert():void;
@@ -105,6 +112,8 @@ export interface IGeneralScope extends IWorkspaceViewModelScope {
     possibleToUpdateIcon():boolean;
     initModel():void;
     isVspImport(): boolean;
+    setServiceFunction(option:string):void;
+    setServiceRole(option:string):void;
 }
 
 // tslint:disable-next-line:max-classes-per-file
@@ -121,9 +130,11 @@ export class GeneralViewModel {
         'VendorModelNumberValidationPattern',
         'CommentValidationPattern',
         'ValidationUtils',
+        'FileUtils',
         'sdcConfig',
         '$state',
         'ModalsHandler',
+        'ModalServiceSdcUI',
         'EventListenerService',
         'Notification',
         'Sdc.Services.ProgressService',
@@ -148,9 +159,11 @@ export class GeneralViewModel {
                 private VendorModelNumberValidationPattern:RegExp,
                 private CommentValidationPattern:RegExp,
                 private ValidationUtils:ValidationUtils,
+                private FileUtils: FileUtils,
                 private sdcConfig:IAppConfigurtaion,
                 private $state:ng.ui.IStateService,
                 private ModalsHandler:ModalsHandler,
+                private modalServiceSdcUI: SdcUiServices.ModalService,
                 private EventListenerService:EventListenerService,
                 private Notification:any,
                 private progressService:ProgressService,
@@ -169,9 +182,6 @@ export class GeneralViewModel {
         this.initScope();
     }
 
-
-
-
     private initScopeValidation = ():void => {
         this.$scope.validation = new Validation();
         this.$scope.validation.componentNameValidationPattern = this.ComponentNameValidationPattern;
@@ -216,7 +226,9 @@ export class GeneralViewModel {
                 if(this.$stateParams.componentCsar && this.$scope.component.lifecycleState === 'NOT_CERTIFIED_CHECKIN' && !this.$scope.isCreateMode()) {
                     this.$scope.importedToscaBrowseFileText = this.$scope.originComponent.name + ' (' + (this.$scope.originComponent as Resource).csarVersion + ')';
                 } else {
-                    this.$scope.importedToscaBrowseFileText = onboardCsarFilesMap[csarUUID][csarVersion];
+                    if (onboardCsarFilesMap && onboardCsarFilesMap[csarUUID]) {
+                        this.$scope.importedToscaBrowseFileText = onboardCsarFilesMap[csarUUID][csarVersion];
+                    }
                 }
             }
         }
@@ -247,6 +259,8 @@ export class GeneralViewModel {
         this.$scope.progressService = this.progressService;
         this.$scope.componentCategories = new componentCategories();
         this.$scope.componentCategories.selectedCategory = this.$scope.component.selectedCategory;
+        this.$scope.othersFlag = false;
+        this.$scope.othersRoleFlag = false;
 
         // Init UIModel
         this.$scope.component.tags = _.without(this.$scope.component.tags, this.$scope.component.name);
@@ -271,13 +285,45 @@ 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) { // Component has imported file.
+            if (service.importedFile) {
                 this.$scope.isShowFileBrowse = true;
-                (<Service>this.$scope.component).serviceType = 'Service';
+                (<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;
+                            }
+                        });
+                        (<Service>this.$scope.component).derivedFromGenericType = serviceCsar.substitutionNodeType;
+                        this.$scope.onBaseTypeChange();
+                        this.setFunctionRole(service);
+                    },
+                    (error) => {
+                        const errorMsg = this.$filter('translate')('IMPORT_FAILURE_MESSAGE_TEXT');
+                        console.error(errorMsg, error);
+                        const errorDetails = {
+                            'Error': this.capitalize(error.reason),
+                            'Details': this.capitalize(error.message)
+                        };
+                        this.modalServiceSdcUI.openErrorDetailModal('Error', this.$filter('translate')('IMPORT_FAILURE_MESSAGE_TEXT'),
+                            'error-modal', errorDetails);
+                        this.$state.go('dashboard');
+                    });
             }
+
+            this.setFunctionRole(service);
+
             if (this.$scope.isEditMode() && service.serviceType == 'Service' && !service.csarUUID) {
                 this.$scope.isShowFileBrowse = true;
             }
@@ -305,7 +351,6 @@ export class GeneralViewModel {
             this.$scope.isShowOnboardingSelectionBrowse = false;
         }
 
-
         //init file extensions based on the file that was imported.
         if (this.$scope.component.isResource() && (<Resource>this.$scope.component).importedFile) {
             let fileName:string = (<Resource>this.$scope.component).importedFile.filename;
@@ -323,8 +368,6 @@ export class GeneralViewModel {
             //(<Resource>this.$scope.component).importedFile.filetype="csar";
         }
 
-
-
         this.$scope.setValidState(true);
 
         this.$scope.calculateUnique = (mainCategory:string, subCategory:string):string => {
@@ -341,7 +384,6 @@ export class GeneralViewModel {
             this.$scope.originComponent.contactId = this.$scope.component.contactId;
         }
 
-
         this.$scope.$on('$destroy', () => {
             this.EventListenerService.unRegisterObserver(EVENTS.ON_LIFECYCLE_CHANGE_WITH_SAVE);
             this.EventListenerService.unRegisterObserver(EVENTS.ON_LIFECYCLE_CHANGE);
@@ -349,6 +391,32 @@ export class GeneralViewModel {
 
     };
 
+    private capitalize(s) {
+        return s && s[0].toUpperCase() + s.slice(1);
+    }
+
+    private setFunctionRole = (service : Service) : void => {
+        if (service.serviceFunction) {
+            const functionList : string[] = this.$scope.getMetadataKeyValidValues('Service Function');
+            if (functionList.find(value => value == service.serviceFunction) != undefined) {
+                this.$scope.functionOption = service.serviceFunction;
+            } else {
+                this.$scope.functionOption = 'Others';
+                this.$scope.othersFlag = true;
+            }
+        }
+
+        if (service.serviceRole) {
+            const roleList : string[] = this.$scope.getMetadataKeyValidValues('Service Role');
+            if (roleList.find(value => value == service.serviceRole) != undefined) {
+                this.$scope.roleOption = service.serviceRole;
+            } else {
+                this.$scope.roleOption = 'Others';
+                this.$scope.othersRoleFlag = true;
+            }
+        }
+    }
+
     // Convert category string MainCategory_#_SubCategory to Array with one item (like the server except)
     private convertCategoryStringToOneArray = ():IMainCategory[] => {
         let tmp = this.$scope.component.selectedCategory.split("_#_");
@@ -499,15 +567,16 @@ export class GeneralViewModel {
                 return;
             }
 
-            if (!this.$scope.isCreateMode() && this.$scope.isVspImport()){
+            if (!this.$scope.isCreateMode() && this.$scope.isVspImport()) {
                 this.modelService.getModels().subscribe((modelsFound: Model[]) => {
                     modelsFound.sort().forEach(model => {
                         if (this.$scope.component.model != undefined) {
                             if (model.modelType == "NORMATIVE_EXTENSION") {
-                                this.$scope.component.model = model.derivedFrom;
+                                if (this.$scope.component.model === model.name) {
+                                    this.$scope.component.model = model.derivedFrom;
+                                }
                                 this.$scope.models.push(model.derivedFrom)
                             } else {
-                                this.$scope.component.model = model.name;
                                 this.$scope.models.push(model.name)
                             }
                         }
@@ -515,7 +584,9 @@ export class GeneralViewModel {
                 });
             } else {
                 this.modelService.getModelsOfType("normative").subscribe((modelsFound: Model[]) => {
-                    modelsFound.sort().forEach(model => {this.$scope.models.push(model.name)});
+                    modelsFound.sort().forEach(model => {
+                        this.$scope.models.push(model.name)
+                    });
                 });
             }
         };
@@ -616,8 +687,9 @@ export class GeneralViewModel {
             const onSuccess = (validation:IValidate) => {
                 this.$scope.editForm['componentName'].$setValidity('nameExist', validation.isValid);
                 if (validation.isValid) {
-                    // update breadcrumb after changing name
-                    this.updateComponentNameInBreadcrumbs();
+                    this.updateComponentNameInBreadcrumbs(); // update breadcrumb after changing name
+                } else {
+                    this.$scope.editForm['componentName'].$setDirty();
                 }
             };
 
@@ -648,7 +720,6 @@ export class GeneralViewModel {
             }
         };
 
-
         this.EventListenerService.registerObserverCallback(EVENTS.ON_LIFECYCLE_CHANGE_WITH_SAVE, (nextState) => {
             if (this.$state.current.data.unsavedChanges && this.$scope.isValidForm) {
                 this.$scope.save().then(() => {
@@ -723,6 +794,12 @@ export class GeneralViewModel {
         });
 
         this.$scope.onCategoryChange = (): void => {
+            if (!this.$scope.component.selectedCategory) {
+                this.$scope.editForm['category'].$setDirty();
+            }
+            if (!this.$scope.component.description) {
+                this.$scope.editForm['description'].$setDirty();
+            }
             this.$scope.component.selectedCategory = this.$scope.componentCategories.selectedCategory;
             if (this.$scope.component.selectedCategory) {
                 this.$scope.component.categories = this.convertCategoryStringToOneArray();
@@ -732,6 +809,12 @@ export class GeneralViewModel {
                         if (!this.$scope.component.categorySpecificMetadata[metadataKey.name]) {
                             this.$scope.component.categorySpecificMetadata[metadataKey.name] = metadataKey.defaultValue ? metadataKey.defaultValue : "";
                         }
+                        if (metadataKey.name === 'Service Role') {
+                            this.$scope.roleOption = this.$scope.component.categorySpecificMetadata[metadataKey.name];
+                        }
+                        if (metadataKey.name === 'Service Function') {
+                            this.$scope.functionOption = this.$scope.component.categorySpecificMetadata[metadataKey.name];
+                        }
                     }
                 }
                 if (this.$scope.component.categories[0].subcategories && this.$scope.component.categories[0].subcategories[0].metadataKeys) {
@@ -748,18 +831,16 @@ export class GeneralViewModel {
                         if (this.$scope.isCreateMode()) {
                             this.loadBaseTypes(data);
                         } else {
-                            let isValidForBaseType:boolean = false;
-                            data.baseTypes.forEach(baseType => {
-                                if (!this.$scope.component.derivedFromGenericType || baseType.toscaResourceName === this.$scope.component.derivedFromGenericType) {
-                                    isValidForBaseType = true;
-                                }
+                            let isValidForBaseType:boolean = data.baseTypes.some(baseType => {
+                                return !this.$scope.component.derivedFromGenericType ||
+                                    baseType.toscaResourceName === this.$scope.component.derivedFromGenericType;
                             });
                             this.$scope.editForm['category'].$setValidity('validForBaseType', isValidForBaseType);
                         }
                     });
                 }
             } else {
-                this.$scope.baseTypes = [];
+                this.clearBaseTypes();
             }
         };
 
@@ -769,6 +850,10 @@ export class GeneralViewModel {
             }
         };
 
+        this.$scope.getCategoryDisplayNameOrName = (mainCategory: any): string => {
+            return mainCategory.displayName ? mainCategory.displayName : mainCategory.name ;
+        }
+
         this.$scope.onBaseTypeChange = (): void => {
             if (!this.$scope.component.derivedFromGenericType) {
                 this.$scope.component.derivedFromGenericVersion = undefined;
@@ -793,6 +878,9 @@ export class GeneralViewModel {
         this.$scope.onModelChange = (): void => {
             if (this.$scope.componentType === ComponentType.SERVICE && this.$scope.component && this.$scope.categories) {
                 let modelName = this.$scope.component.model ? this.$scope.component.model : null;
+                this.$scope.component.categories = undefined;
+                this.$scope.component.selectedCategory = undefined;
+                this.$scope.componentCategories.selectedCategory = undefined;
                 this.filterCategoriesByModel(modelName);
                 this.filterBaseTypesByModelAndCategory(modelName)
             }
@@ -804,23 +892,49 @@ export class GeneralViewModel {
             }
         };
 
+        this.$scope.setServiceFunction = (option:string): void => {
+            if (option === 'Others') {
+                this.$scope.othersFlag = true;
+                (<Service>this.$scope.component).serviceFunction = '';
+            } else {
+                this.$scope.othersFlag = false;
+                (<Service>this.$scope.component).serviceFunction = option;
+            }
+
+        }
+
+        this.$scope.setServiceRole = (option:string): void => {
+            if (option === 'Others') {
+                this.$scope.othersRoleFlag = true;
+                (<Service>this.$scope.component).serviceRole = '';
+            } else {
+                this.$scope.othersRoleFlag = false;
+                (<Service>this.$scope.component).serviceRole = option;
+            }
+
+        }
+
         this.EventListenerService.registerObserverCallback(EVENTS.ON_LIFECYCLE_CHANGE, this.$scope.reload);
 
         this.$scope.isMetadataKeyMandatory = (key: string): boolean => {
-            let metadataKey = this.getMetadataKey(this.$scope.component.categories, key);
+            let metadataKey = this.getMetadataKey(key);
             return metadataKey && metadataKey.mandatory;
         }
 
         this.$scope.getMetadataKeyValidValues = (key: string): string[] => {
-            let metadataKey = this.getMetadataKey(this.$scope.component.categories, key);
+            let metadataKey = this.getMetadataKey(key);
             if (metadataKey) {
-                return metadataKey.validValues;
+                if (key == 'Service Function' || key == 'Service Role') {
+                    return metadataKey.validValues.concat("Others");
+                } else {
+                    return metadataKey.validValues;
+                }
             }
             return [];
         }
 
         this.$scope.getMetadataDisplayName = (key: string): string => {
-            let metadataKey = this.getMetadataKey(this.$scope.component.categories, key);
+            let metadataKey = this.getMetadataKey(key);
             if (metadataKey) {
                 return metadataKey.displayName ? metadataKey.displayName : metadataKey.name;
             }
@@ -828,7 +942,7 @@ export class GeneralViewModel {
         }
 
         this.$scope.isMetadataKeyForComponentCategory = (key: string): boolean => {
-            return this.getMetadataKey(this.$scope.component.categories, key) != null;
+            return this.getMetadataKey(key) != null;
         }
 
         this.$scope.isCategoryServiceMetadataKey = (key: string): boolean => {
@@ -836,38 +950,58 @@ export class GeneralViewModel {
         }
 
         this.$scope.isMetadataKeyForComponentCategoryService = (key: string, attribute: string): boolean => {
-            let metadatakey = this.getMetadataKey(this.$scope.component.categories, key);
+            let metadatakey = this.getMetadataKey(key);
             if (metadatakey && (!this.$scope.component[attribute] || !metadatakey.validValues.find(v => v === this.$scope.component[attribute]))) {
                 this.$scope.component[attribute] = metadatakey.defaultValue;
             }
             return metadatakey != null;
         }
+
+        this.$scope.isNotApplicableMetadataKeys = (key: string): boolean => {
+            return this.$scope.component.categories && this.$scope.component.categories[0].notApplicableMetadataKeys && this.$scope.component.categories[0].notApplicableMetadataKeys.some(item => item === key);
+        }
     }
 
     private filterCategoriesByModel(modelName:string) {
         // reload categories
         this.$scope.initCategories();
         this.$scope.categories = this.$scope.categories.filter(category =>
-            !modelName ? category.models.indexOf(DEFAULT_MODEL_NAME) !== -1 : category.models !== null && category.models.indexOf(modelName) !== -1);
+            !modelName ? !category.models || category.models.indexOf(DEFAULT_MODEL_NAME) !== -1 : category.models !== null && category.models.indexOf(modelName) !== -1);
     }
 
-
     private filterBaseTypesByModelAndCategory(modelName:string) {
         let categories = this.$scope.component.categories;
         if (categories) {
             this.elementService.getCategoryBaseTypes(categories[0].name, modelName).subscribe((data: ListBaseTypesResponse) => {
                 this.loadBaseTypes(data);
             });
+            return;
         }
+        this.clearBaseTypes();
     }
 
     private loadBaseTypes(baseTypeResponseList: ListBaseTypesResponse) {
         this.$scope.isBaseTypeRequired = baseTypeResponseList.required;
         this.$scope.baseTypes = [];
         this.$scope.baseTypeVersions = [];
+        let defaultBaseType = baseTypeResponseList.defaultBaseType;
         baseTypeResponseList.baseTypes.forEach(baseType => this.$scope.baseTypes.push(baseType.toscaResourceName));
-        if (this.$scope.isBaseTypeRequired) {
-            const baseType = baseTypeResponseList.baseTypes[0];
+        if (this.$scope.isBaseTypeRequired || defaultBaseType != null) {
+            let baseType = baseTypeResponseList.baseTypes[0];
+            if(defaultBaseType != null){
+                baseTypeResponseList.baseTypes.forEach(baseTypeObj => {
+                    if(baseTypeObj.toscaResourceName == defaultBaseType) {
+                        baseType = baseTypeObj;
+                    }
+                });
+            }
+            if((<Service>this.$scope.component).derivedFromGenericType) {
+                baseTypeResponseList.baseTypes.forEach(baseTypeObj => {
+                    if(baseTypeObj.toscaResourceName == (<Service>this.$scope.component).derivedFromGenericType) {
+                        baseType = baseTypeObj;
+                    }
+                });
+            }
             baseType.versions.reverse().forEach(version => this.$scope.baseTypeVersions.push(version));
             this.$scope.component.derivedFromGenericType = baseType.toscaResourceName;
             this.$scope.component.derivedFromGenericVersion = this.$scope.baseTypeVersions[0];
@@ -879,16 +1013,28 @@ export class GeneralViewModel {
         this.$scope.showBaseTypeVersions = false;
     }
 
+    private clearBaseTypes() {
+        this.$scope.isBaseTypeRequired = false;
+        this.$scope.baseTypes = [];
+        this.$scope.baseTypeVersions = [];
+        this.$scope.component.derivedFromGenericType = undefined;
+        this.$scope.component.derivedFromGenericVersion = undefined;
+        this.$scope.showBaseTypeVersions = false;
+    }
+
     private setUnsavedChanges = (hasChanges: boolean): void => {
         this.$state.current.data.unsavedChanges = hasChanges;
     }
 
-    private getMetadataKey(categories: IMainCategory[], key: string) : IMetadataKey {
-        let metadataKey = this.getSubcategoryMetadataKey(this.$scope.component.categories, key);
-        if (!metadataKey){
-            return this.getCategoryMetadataKey(this.$scope.component.categories, key);
+    private getMetadataKey(key: string) : IMetadataKey {
+        if (this.$scope.component.categories) {
+            let metadataKey = this.getSubcategoryMetadataKey(this.$scope.component.categories, key);
+            if (!metadataKey){
+                return this.getCategoryMetadataKey(this.$scope.component.categories, key);
+            }
+            return metadataKey;
         }
-        return metadataKey;
+        return null;
     }
 
     private getSubcategoryMetadataKey(categories: IMainCategory[], key: string) : IMetadataKey {