Michael Lando | dd60339 | 2017-07-12 00:54:52 +0300 | [diff] [blame] | 1 | /*- |
| 2 | * ============LICENSE_START======================================================= |
| 3 | * SDC |
| 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 |
ys9693 | 16a9fce | 2020-01-19 13:50:02 +0200 | [diff] [blame] | 10 | * |
Michael Lando | dd60339 | 2017-07-12 00:54:52 +0300 | [diff] [blame] | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
ys9693 | 16a9fce | 2020-01-19 13:50:02 +0200 | [diff] [blame] | 12 | * |
Michael Lando | dd60339 | 2017-07-12 00:54:52 +0300 | [diff] [blame] | 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========================================================= |
| 19 | */ |
| 20 | |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 21 | /** |
| 22 | * Created by obarda on 4/20/2016. |
| 23 | */ |
| 24 | 'use strict'; |
ys9693 | 16a9fce | 2020-01-19 13:50:02 +0200 | [diff] [blame] | 25 | import * as _ from 'lodash'; |
| 26 | import { PropertyModel } from './properties'; |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 27 | |
ys9693 | 16a9fce | 2020-01-19 13:50:02 +0200 | [diff] [blame] | 28 | export interface RequirementCapabilityModel {} |
| 29 | |
| 30 | // this is an object contains keys, when each key has matching array. |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 31 | // for example: key = tosca.capabilities.network.Linkable and the match array is array of capabilities objects |
| 32 | export class CapabilitiesGroup { |
ys9693 | 16a9fce | 2020-01-19 13:50:02 +0200 | [diff] [blame] | 33 | constructor(capabilityGroupObj?: CapabilitiesGroup) { |
| 34 | _.forEach(capabilityGroupObj, (capabilitiesArrayObj: Capability[], instance) => { |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 35 | this[instance] = []; |
ys9693 | 16a9fce | 2020-01-19 13:50:02 +0200 | [diff] [blame] | 36 | _.forEach(capabilitiesArrayObj, (capability: Capability): void => { |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 37 | this[instance].push(new Capability(capability)); |
| 38 | }); |
| 39 | }); |
| 40 | } |
ys9693 | 16a9fce | 2020-01-19 13:50:02 +0200 | [diff] [blame] | 41 | |
Mojahidul Islam | 82e531a | 2019-05-14 12:49:31 +0530 | [diff] [blame] | 42 | public static getFlattenedCapabilities(capabilitiesGroup: CapabilitiesGroup): Array<Capability> { |
| 43 | return _.reduce( |
| 44 | _.toArray(capabilitiesGroup), |
| 45 | (allCaps, capArr) => allCaps.concat(capArr), |
| 46 | []); |
| 47 | } |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 48 | } |
| 49 | |
ys9693 | 16a9fce | 2020-01-19 13:50:02 +0200 | [diff] [blame] | 50 | export class Capability implements RequirementCapabilityModel { |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 51 | |
ys9693 | 16a9fce | 2020-01-19 13:50:02 +0200 | [diff] [blame] | 52 | // server data |
| 53 | name: string; |
| 54 | ownerId: string; |
| 55 | ownerName: string; |
| 56 | type: string; |
| 57 | uniqueId: string; |
| 58 | capabilitySources: string[]; |
| 59 | leftOccurrences: string; |
| 60 | minOccurrences: string | number; |
| 61 | maxOccurrences: string; |
| 62 | description: string; |
| 63 | validSourceTypes: string[]; |
| 64 | properties: PropertyModel[]; |
andre.schmid | 2152a9a | 2021-05-05 15:31:04 +0100 | [diff] [blame] | 65 | external: boolean; |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 66 | |
ys9693 | 16a9fce | 2020-01-19 13:50:02 +0200 | [diff] [blame] | 67 | // custom |
| 68 | selected: boolean; |
| 69 | filterTerm: string; |
| 70 | |
| 71 | constructor(capability?: Capability) { |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 72 | |
| 73 | if (capability) { |
ys9693 | 16a9fce | 2020-01-19 13:50:02 +0200 | [diff] [blame] | 74 | // server data |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 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; |
Tal Gitelman | 51d50f0 | 2017-12-10 18:55:03 +0200 | [diff] [blame] | 81 | this.leftOccurrences = capability.leftOccurrences; |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 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; |
andre.schmid | 2152a9a | 2021-05-05 15:31:04 +0100 | [diff] [blame] | 88 | this.external = capability.external; |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 89 | this.initFilterTerm(); |
| 90 | |
| 91 | } |
| 92 | } |
| 93 | |
ys9693 | 16a9fce | 2020-01-19 13:50:02 +0200 | [diff] [blame] | 94 | public getTitle(): string { |
Tal Gitelman | 51d50f0 | 2017-12-10 18:55:03 +0200 | [diff] [blame] | 95 | return this.ownerName + ': ' + this.name; |
| 96 | } |
| 97 | |
ys9693 | 16a9fce | 2020-01-19 13:50:02 +0200 | [diff] [blame] | 98 | public getFullTitle(): string { |
| 99 | const maxOccurrences: string = this.maxOccurrences === 'UNBOUNDED' ? '∞' : this.maxOccurrences; |
Tal Gitelman | 51d50f0 | 2017-12-10 18:55:03 +0200 | [diff] [blame] | 100 | return this.getTitle() + ': [' + this.minOccurrences + ', ' + maxOccurrences + ']'; |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 101 | } |
| 102 | |
ys9693 | 16a9fce | 2020-01-19 13:50:02 +0200 | [diff] [blame] | 103 | public toJSON = (): any => { |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 104 | this.selected = undefined; |
| 105 | this.filterTerm = undefined; |
| 106 | return this; |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 107 | } |
Tal Gitelman | 51d50f0 | 2017-12-10 18:55:03 +0200 | [diff] [blame] | 108 | |
| 109 | public isFulfilled() { |
| 110 | return parseInt(this.leftOccurrences) === 0; |
| 111 | } |
ys9693 | 16a9fce | 2020-01-19 13:50:02 +0200 | [diff] [blame] | 112 | |
| 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 || '') + |
| 124 | ' ' + prop.type + |
| 125 | (prop.schema && prop.schema.property ? (' ' + prop.schema.property.type) : ''); |
| 126 | }); |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | export class CapabilityUI extends Capability { |
| 132 | isCreatedManually: boolean; |
| 133 | |
| 134 | constructor(input: Capability, componentUniqueId: string) { |
| 135 | super(input); |
| 136 | this.isCreatedManually = input.ownerId === componentUniqueId; |
| 137 | } |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | |