originalName fallbacks to customizationId or invariantId
[vid.git] / vid-webpack-master / src / app / drawingBoard / service-planning / objectsToTree / shared.tree.service.ts
index b8eddbb..d60bbd3 100644 (file)
@@ -14,6 +14,8 @@ import {undoUpgradeService, upgradeService} from "../../../shared/storeUtil/util
 import {VNFMethods} from "../../../shared/storeUtil/utils/vnf/vnf.actions";
 import {FeatureFlagsService, Features} from "../../../shared/services/featureFlag/feature-flags.service";
 import {Utils} from "../../../shared/utils/utils";
+import {Constants} from "../../../shared/utils/constants";
+import {NodeInstance} from "../../../shared/models/nodeInstance";
 
 @Injectable()
 export class SharedTreeService {
@@ -42,6 +44,51 @@ export class SharedTreeService {
     }
   }
 
+  /**
+   * Determines a consistent unique ID for a given right-tree
+   * node instance.
+   */
+  modelUniqueId = (nodeInstance: NodeInstance): string => {
+    return _.isNil(nodeInstance.modelInfo)
+      ? null
+      : (nodeInstance.modelInfo.modelCustomizationId || nodeInstance.modelInfo.modelInvariantId);
+  };
+
+  modelUniqueNameOrId = (instance): string =>
+    instance.originalName ? instance.originalName : this.modelUniqueId(instance);
+
+  /**
+   * Finds a model inside a full service model
+   * @param serviceModelFromHierarchy
+   * @param modelTypeName "vnfs" | "networks" | "vfModules" | "collectionResources" | ...
+   * @param modelUniqueNameOrId Either an entry name (i.e. "originalName"), modelCustomizationId or modelInvariantId.
+   *                      Note that modelInvariantId will work only where model lacks a modelCustomizationId.
+   * @param modeName An optional entry name (i.e. "originalName"); will not try to use as id
+   */
+  modelByIdentifiers = (serviceModelFromHierarchy, modelTypeName: string, modelUniqueNameOrId: string, modeName?: string): any => {
+    const logErrorAndReturnUndefined = () =>
+      console.info(`modelByIdentifiers: could not find a model matching query`, {
+        modelTypeName, modelUniqueNameOrId, modeName, serviceModelFromHierarchy
+      });
+
+    if (_.isNil(serviceModelFromHierarchy)) return logErrorAndReturnUndefined();
+
+    const modelsOfType = serviceModelFromHierarchy[modelTypeName];
+    if (_.isNil(modelsOfType)) return logErrorAndReturnUndefined();
+
+    const modelIfModelIdentifierIsEntryName = modelsOfType[modelUniqueNameOrId];
+    const modelIfModeNameExists = _.isNil(modeName) ? null : modelsOfType[modeName];
+
+    if (!_.isNil(modelIfModelIdentifierIsEntryName)) {
+      return modelIfModelIdentifierIsEntryName;
+    } else if (!_.isNil(modelIfModeNameExists)) {
+      return modelIfModeNameExists;
+    } else {
+      // try modelUniqueNameOrId as an id
+      return _.find(modelsOfType, o => (o.customizationUuid || o.invariantUuid) === modelUniqueNameOrId) || logErrorAndReturnUndefined()
+    }
+  };
+
   hasMissingData(instance, dynamicInputs: any, isEcompGeneratedNaming: boolean, requiredFields: string[]): boolean {
     if (!isEcompGeneratedNaming && _.isEmpty(instance.instanceName)) {
       return true;
@@ -108,8 +155,8 @@ export class SharedTreeService {
   /**********************************************
    * should return true if can delete
    **********************************************/
-  shouldShowDelete(node): boolean {
-    return this.shouldShowButtonGeneric(node, "delete")
+  shouldShowDelete(node, serviceModelId): boolean {
+    return this.shouldShowButtonGeneric(node, "delete", serviceModelId)
   }
 
   /**********************************************
@@ -162,7 +209,7 @@ export class SharedTreeService {
   shouldShowUpgrade(node, serviceModelId): boolean {
     if (FeatureFlagsService.getFlagState(Features.FLAG_FLASH_REPLACE_VF_MODULE, this._store) &&
       this.isThereAnUpdatedLatestVersion(serviceModelId)) {
-      return this.shouldShowButtonGeneric(node, VNFMethods.UPGRADE);
+      return this.shouldShowButtonGeneric(node, VNFMethods.UPGRADE, serviceModelId);
     }
     else {
       return false
@@ -170,12 +217,22 @@ export class SharedTreeService {
   }
 
   private isThereAnUpdatedLatestVersion(serviceModelId) : boolean{
-    let serviceInstance = this._store.getState().service.serviceInstance[serviceModelId];
+    let serviceInstance = this.getServiceInstance(serviceModelId);
     return !_.isNil(serviceInstance.latestAvailableVersion) && (Number(serviceInstance.modelInfo.modelVersion) < serviceInstance.latestAvailableVersion);
   }
 
-  private shouldShowButtonGeneric(node, method) {
+  private getServiceInstance(serviceModelId): any {
+    return this._store.getState().service.serviceInstance[serviceModelId];
+  }
+
+  shouldShowButtonGeneric(node, method, serviceModelId) {
     const mode = this._store.getState().global.drawingBoardStatus;
+    const isMacro = !(this.getServiceInstance(serviceModelId).isALaCarte);
+
+    if (isMacro) { //if macro action allowed only for service level
+      return false;
+    }
+
     if (!_.isNil(node) && !_.isNil(node.data) && !_.isNil(node.data.action) && !_.isNil(node.data.menuActions[method])) {
       if (mode !== DrawingBoardModes.EDIT || node.data.action === ServiceInstanceActions.Create) {
         return false;
@@ -238,7 +295,7 @@ export class SharedTreeService {
    **********************************************/
   shouldShowAddIcon(): boolean{
     const mode = this._store.getState().global.drawingBoardStatus;
-    return mode === DrawingBoardModes.EDIT || mode=== DrawingBoardModes.CREATE;
+    return mode === DrawingBoardModes.EDIT || mode=== DrawingBoardModes.CREATE || mode=== DrawingBoardModes.RECREATE;
   }
 
 
@@ -257,7 +314,7 @@ export class SharedTreeService {
    ************************************************/
   getExistingInstancesWithDeleteMode(node, serviceModelId: string, type: string): number {
     let counter = 0;
-    const existingInstances = this._store.getState().service.serviceInstance[serviceModelId][type];
+    const existingInstances = this.getServiceInstance(serviceModelId)[type];
     const modelUniqueId = node.data.modelUniqueId;
     if (!_.isNil(existingInstances)) {
       for (let instanceKey in existingInstances) {
@@ -368,4 +425,11 @@ export class SharedTreeService {
     const modelInfoItemsWithoutEmpty = _.filter(modelInfoItems, function(item){ return !item.values.every(_.isNil)});
     return new ComponentInfoModel(type, modelInfoItemsWithoutEmpty, [], instance != null);
   }
+
+  createMaximumToInstantiateModelInformationItem(model): ModelInformationItem {
+    return ModelInformationItem.createInstance(
+      "Max instances",
+      !_.isNil(model.max) ? String(model.max) : Constants.ModelInfo.UNLIMITED_DEFAULT
+    );
+  }
 }