Skip to content

Commit

Permalink
Move Inhertiance into own class.
Browse files Browse the repository at this point in the history
Signed-off-by: DominikVoigt <dominik.ingo.voigt@gmail.com>
  • Loading branch information
DominikVoigt committed Jan 27, 2020
1 parent 4b928cb commit b3b252b
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 128 deletions.
Expand Up @@ -58,6 +58,7 @@ import { WineryRepositoryConfigurationService } from '../../../../tosca-manageme
import { RequirementDefinitionModel } from '../models/requirementDefinitonModel';
import { CapabilityDefinitionModel } from '../models/capabilityDefinitionModel';
import { WineryRowData } from '../../../../tosca-management/src/app/wineryTableModule/wineryTable.component';
import { InheritanceUtils } from '../models/InheritanceUtils';

@Component({
selector: 'winery-canvas',
Expand Down Expand Up @@ -396,7 +397,7 @@ export class CanvasComponent implements OnInit, OnDestroy, OnChanges, AfterViewI
try {
// request all valid requirement types for that node type for display as name select options in
// the modal
const data = TopologyTemplateUtil.getRequirementDefinitionsOfNodeType(currentNodeData.type, this.entityTypes);
const data = InheritanceUtils.getRequirementDefinitionsOfNodeType(currentNodeData.type, this.entityTypes);
this.requirements.reqDefinitionNames = [];
this.requirements.reqDefinitionName = '';

Expand Down Expand Up @@ -478,7 +479,7 @@ export class CanvasComponent implements OnInit, OnDestroy, OnChanges, AfterViewI
try {
// request all valid capability types for that node type for display as name select options in
// the modal
const data = TopologyTemplateUtil.getCapabilityDefinitionsOfNodeType(currentNodeData.type, this.entityTypes);
const data = InheritanceUtils.getCapabilityDefinitionsOfNodeType(currentNodeData.type, this.entityTypes);
this.capabilities.capDefinitionNames = [];
this.capabilities.capDefinitionName = '';
for (const capType of data) {
Expand Down Expand Up @@ -927,7 +928,7 @@ export class CanvasComponent implements OnInit, OnDestroy, OnChanges, AfterViewI
&& nt.requirements.requirement.some(req => req.id === reqId));
const requirementModel: RequirementModel = sourceNodeTemplate.requirements.requirement
.find(req => req.id === reqId);
const requirementDefinition: RequirementDefinitionModel = TopologyTemplateUtil
const requirementDefinition: RequirementDefinitionModel = InheritanceUtils
.getRequirementDefinitionsOfNodeType(sourceNodeTemplate.type, this.entityTypes)
.find(reqDef => reqDef.name === requirementModel.name);
requirementModel.capability = requirementDefinition.capability;
Expand Down Expand Up @@ -1869,15 +1870,15 @@ export class CanvasComponent implements OnInit, OnDestroy, OnChanges, AfterViewI
if (this.configuration.isYaml()) {
this.newNode.requirements = { requirement: [] };
this.newNode.capabilities = { capability: [] };
const reqData = TopologyTemplateUtil.getRequirementDefinitionsOfNodeType(this.newNode.type, this.entityTypes);
const reqData = InheritanceUtils.getRequirementDefinitionsOfNodeType(this.newNode.type, this.entityTypes);
if (reqData) {
reqData.forEach(reqDef => {
const reqModel = RequirementModel.fromRequirementDefinition(reqDef);
reqModel.id = `${this.newNode.id}_req_${reqModel.name}`;
this.newNode.requirements.requirement.push(reqModel);
});
}
const capData = TopologyTemplateUtil.getCapabilityDefinitionsOfNodeType(this.newNode.type, this.entityTypes);
const capData = InheritanceUtils.getCapabilityDefinitionsOfNodeType(this.newNode.type, this.entityTypes);
if (capData) {
capData.forEach(capDef => {
const capModel = CapabilityModel.fromCapabilityDefinitionModel(capDef);
Expand Down Expand Up @@ -2186,7 +2187,6 @@ export class CanvasComponent implements OnInit, OnDestroy, OnChanges, AfterViewI
const capabilityId: string = info.targetId.substring(info.targetId.indexOf('.') + 1);
const currentTypeValid = this.entityTypes.relationshipTypes.some(relType => relType.qName === this.selectedRelationshipType.qName);
const currentSourceIdValid = this.allNodeTemplates.some(node => node.id === sourceNode);

if (sourceNode && currentTypeValid && currentSourceIdValid) {
const prefix = this.backendService.configuration.relationshipPrefix;
const relName = this.selectedRelationshipType.name;
Expand All @@ -2204,10 +2204,10 @@ export class CanvasComponent implements OnInit, OnDestroy, OnChanges, AfterViewI
const targetNodeTypeString: string = this.allNodeTemplates
.filter(nodeTemplate => nodeTemplate.id === info.targetId.substring(0, info.targetId.indexOf('.')))
.map(nodeTemplate => nodeTemplate.type)[0];
const capDef: CapabilityDefinitionModel = TopologyTemplateUtil
const capDef: CapabilityDefinitionModel = InheritanceUtils
.getCapabilityDefinitionsOfNodeType(targetNodeTypeString, this.entityTypes)
.filter(current => current.name === capModel.name)[0];
const reqDef: RequirementDefinitionModel = TopologyTemplateUtil
const reqDef: RequirementDefinitionModel = InheritanceUtils
.getRequirementDefinitionsOfNodeType(sourceNodeTypeString, this.entityTypes)
.filter(current => current.name === reqModel.name)[0];

Expand Down Expand Up @@ -2314,7 +2314,7 @@ export class CanvasComponent implements OnInit, OnDestroy, OnChanges, AfterViewI
*/
matchType(requiredType: string, targetElementType: string, targetElementTypeSet: EntityType[]) {
if (requiredType) {
const typeAncestry = TopologyTemplateUtil.getInheritanceAncestry(targetElementType, targetElementTypeSet);
const typeAncestry = InheritanceUtils.getInheritanceAncestry(targetElementType, targetElementTypeSet);
return typeAncestry.some(type => type.qName === requiredType);
} else {
// if there is no required type, we assume all target types are valid!
Expand Down Expand Up @@ -2386,7 +2386,7 @@ export class CanvasComponent implements OnInit, OnDestroy, OnChanges, AfterViewI

handleYamlPolicySelected($event: WineryRowData) {
this.selectedYamlPolicy = this.entityTypes.yamlPolicies.find(policy => policy.name === (<TPolicy>$event.row).name);
this.selectedYamlPolicy.properties = TopologyTemplateUtil.getActiveKVPropertiesOfTemplateElement(this.selectedYamlPolicy.properties,
this.selectedYamlPolicy.properties = InheritanceUtils.getEffectiveKVPropertiesOfTemplateElement(this.selectedYamlPolicy.properties,
this.selectedYamlPolicy.policyType, this.entityTypes.policyTypes);
}

Expand Down
@@ -0,0 +1,132 @@
/*******************************************************************************
* Copyright (c) 2020 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache Software License 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*******************************************************************************/

import { EntityTypesModel } from './entityTypesModel';
import { CapabilityDefinitionModel } from './capabilityDefinitionModel';
import { RequirementDefinitionModel } from './requirementDefinitonModel';
import { EntityType, TPolicyType } from './ttopology-template';
import { QName } from './qname';
import { TopologyTemplateUtil } from './topologyTemplateUtil';

export class InheritanceUtils {

static getCapabilityDefinitionsOfNodeType(nodeType: string, entityTypes: EntityTypesModel): CapabilityDefinitionModel[] {
const listOfEffectiveCapabilityDefinitions: CapabilityDefinitionModel[] = [];
const listOfBequeathingNodeTypes = this.getInheritanceAncestry(nodeType, entityTypes.unGroupedNodeTypes);
for (const currentNodeType of listOfBequeathingNodeTypes) {
if (currentNodeType.full.serviceTemplateOrNodeTypeOrNodeTypeImplementation[0].capabilityDefinitions &&
currentNodeType.full.serviceTemplateOrNodeTypeOrNodeTypeImplementation[0].capabilityDefinitions.capabilityDefinition) {
for (const capabilityDefinition of currentNodeType.full.serviceTemplateOrNodeTypeOrNodeTypeImplementation[0]
.capabilityDefinitions.capabilityDefinition) {
if (!listOfEffectiveCapabilityDefinitions
.some(value => value.name === capabilityDefinition.name)) {
listOfEffectiveCapabilityDefinitions.push(capabilityDefinition);
}
}
}
}
return listOfEffectiveCapabilityDefinitions;
}

static getRequirementDefinitionsOfNodeType(nodeType: string, entityTypes: EntityTypesModel): RequirementDefinitionModel[] {
const listOfEffectiveRequirementDefinitions: RequirementDefinitionModel[] = [];
const listOfBequeathingNodeTypes = this.getInheritanceAncestry(nodeType, entityTypes.unGroupedNodeTypes);
for (const currentNodeType of listOfBequeathingNodeTypes) {
if (currentNodeType.full.serviceTemplateOrNodeTypeOrNodeTypeImplementation[0].requirementDefinitions &&
currentNodeType.full.serviceTemplateOrNodeTypeOrNodeTypeImplementation[0].requirementDefinitions.requirementDefinition) {
for (const requirementDefinition of currentNodeType.full.serviceTemplateOrNodeTypeOrNodeTypeImplementation[0]
.requirementDefinitions.requirementDefinition) {
if (!listOfEffectiveRequirementDefinitions
.some(value => value.name === requirementDefinition.name)) {
listOfEffectiveRequirementDefinitions.push(requirementDefinition);
}
}
}
}
return listOfEffectiveRequirementDefinitions;
}

static getParent(element: EntityType, entities: EntityType[]): EntityType {
if (this.hasParentType(element)) {
const parentQName = element.full.serviceTemplateOrNodeTypeOrNodeTypeImplementation[0].derivedFrom.type;
return entities.find(entity => entity.qName === parentQName);
}
return null;
}

static getInheritanceAncestry(entityType: string, entityTypes: EntityType[]): EntityType[] {
const entity = entityTypes.find(type => type.qName === entityType);
const result = [];

if (entity) {
result.push(entity);
let parent = this.getParent(entity, entityTypes);

while (parent) {
result.push(parent);
parent = this.getParent(parent, entityTypes);
}
}

return result;
}

static hasParentType(element: EntityType): boolean {
return (element && element.full
&& element.full.serviceTemplateOrNodeTypeOrNodeTypeImplementation[0]
&& element.full.serviceTemplateOrNodeTypeOrNodeTypeImplementation[0].derivedFrom
);
}

/**
* Gets the active set of allowed target node types for this YAML policy type
* i.e., returns the targets array which is the lowest possible.
* @param policyTypeQName
* @param policyTypes
*/
static getEffectiveTargetsOfYamlPolicyType(policyTypeQName: string, policyTypes: TPolicyType[]): string[] {
const hierarchy = this.getInheritanceAncestry(policyTypeQName, policyTypes);
let result = [];

for (const type of hierarchy) {
if ((<TPolicyType>type).targets) {
result = (<TPolicyType>type).targets;
break;
}
}

return result;
}

static getEffectiveKVPropertiesOfTemplateElement(templateElementProperties: any, typeQName: string, entityTypes: EntityType[]): any {
const typeName = new QName(typeQName).localName;
const defaultTypeProperties = TopologyTemplateUtil.getDefaultPropertiesFromEntityTypes(typeName, entityTypes);
const result = {};

if (defaultTypeProperties && defaultTypeProperties.kvproperties) {
Object.keys(defaultTypeProperties.kvproperties).forEach(currentPropKey => {

if (templateElementProperties && templateElementProperties.kvproperties &&
Object.keys(templateElementProperties.kvproperties).some(tempPropertyKey => tempPropertyKey === currentPropKey)) {
result[currentPropKey] = templateElementProperties.kvproperties[currentPropKey];
} else {
result[currentPropKey] = defaultTypeProperties.kvproperties[currentPropKey];
}
});
}

return { kvproperties: result };
}

}

0 comments on commit b3b252b

Please sign in to comment.