2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
22 * Created by obarda on 4/20/2016.
25 import * as _ from 'lodash';
26 import { PropertyModel } from './properties';
28 export interface RequirementCapabilityModel {}
30 // this is an object contains keys, when each key has matching array.
31 // for example: key = tosca.capabilities.network.Linkable and the match array is array of capabilities objects
32 export class CapabilitiesGroup {
33 constructor(capabilityGroupObj?: CapabilitiesGroup) {
34 _.forEach(capabilityGroupObj, (capabilitiesArrayObj: Capability[], instance) => {
36 _.forEach(capabilitiesArrayObj, (capability: Capability): void => {
37 this[instance].push(new Capability(capability));
42 public static getFlattenedCapabilities(capabilitiesGroup: CapabilitiesGroup): Array<Capability> {
44 _.toArray(capabilitiesGroup),
45 (allCaps, capArr) => allCaps.concat(capArr),
50 export class Capability implements RequirementCapabilityModel {
58 capabilitySources: string[];
59 leftOccurrences: string;
60 minOccurrences: string | number;
61 maxOccurrences: string;
63 validSourceTypes: string[];
64 properties: PropertyModel[];
71 constructor(capability?: Capability) {
75 this.name = capability.name;
76 this.ownerId = capability.ownerId;
77 this.ownerName = capability.ownerName;
78 this.type = capability.type;
79 this.uniqueId = capability.uniqueId;
80 this.capabilitySources = capability.capabilitySources;
81 this.leftOccurrences = capability.leftOccurrences;
82 this.minOccurrences = capability.minOccurrences;
83 this.maxOccurrences = capability.maxOccurrences;
84 this.properties = capability.properties;
85 this.description = capability.description;
86 this.validSourceTypes = capability.validSourceTypes;
87 this.selected = capability.selected;
88 this.external = capability.external;
89 this.initFilterTerm();
94 public getTitle(): string {
95 return this.ownerName + ': ' + this.name;
98 public getFullTitle(): string {
99 const maxOccurrences: string = this.maxOccurrences === 'UNBOUNDED' ? '∞' : this.maxOccurrences;
100 return this.getTitle() + ': [' + this.minOccurrences + ', ' + maxOccurrences + ']';
103 public toJSON = (): any => {
104 this.selected = undefined;
105 this.filterTerm = undefined;
109 public isFulfilled() {
110 return parseInt(this.leftOccurrences) === 0;
113 private initFilterTerm = (): void => {
114 this.filterTerm = this.name + ' ' +
115 (this.type ? (this.type.replace('tosca.capabilities.', '') + ' ' ) : '') +
116 (this.description || '') + ' ' +
117 (this.ownerName || '') + ' ' +
118 (this.validSourceTypes ? (this.validSourceTypes.join(',') + ' ') : '') +
119 this.minOccurrences + ',' + this.maxOccurrences;
120 if (this.properties && this.properties.length) {
121 _.forEach(this.properties, (prop: PropertyModel) => {
122 this.filterTerm += ' ' + prop.name +
123 ' ' + (prop.description || '') +
125 (prop.schema && prop.schema.property ? (' ' + prop.schema.property.type) : '');
131 export class CapabilityUI extends Capability {
132 isCreatedManually: boolean;
134 constructor(input: Capability, componentUniqueId: string) {
136 this.isCreatedManually = input.ownerId === componentUniqueId;