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[]; |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 65 | |
ys9693 | 16a9fce | 2020-01-19 13:50:02 +0200 | [diff] [blame] | 66 | // custom |
| 67 | selected: boolean; |
| 68 | filterTerm: string; |
| 69 | |
| 70 | constructor(capability?: Capability) { |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 71 | |
| 72 | if (capability) { |
ys9693 | 16a9fce | 2020-01-19 13:50:02 +0200 | [diff] [blame] | 73 | // server data |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 74 | this.name = capability.name; |
| 75 | this.ownerId = capability.ownerId; |
| 76 | this.ownerName = capability.ownerName; |
| 77 | this.type = capability.type; |
| 78 | this.uniqueId = capability.uniqueId; |
| 79 | this.capabilitySources = capability.capabilitySources; |
Tal Gitelman | 51d50f0 | 2017-12-10 18:55:03 +0200 | [diff] [blame] | 80 | this.leftOccurrences = capability.leftOccurrences; |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 81 | this.minOccurrences = capability.minOccurrences; |
| 82 | this.maxOccurrences = capability.maxOccurrences; |
| 83 | this.properties = capability.properties; |
| 84 | this.description = capability.description; |
| 85 | this.validSourceTypes = capability.validSourceTypes; |
| 86 | this.selected = capability.selected; |
| 87 | this.initFilterTerm(); |
| 88 | |
| 89 | } |
| 90 | } |
| 91 | |
ys9693 | 16a9fce | 2020-01-19 13:50:02 +0200 | [diff] [blame] | 92 | public getTitle(): string { |
Tal Gitelman | 51d50f0 | 2017-12-10 18:55:03 +0200 | [diff] [blame] | 93 | return this.ownerName + ': ' + this.name; |
| 94 | } |
| 95 | |
ys9693 | 16a9fce | 2020-01-19 13:50:02 +0200 | [diff] [blame] | 96 | public getFullTitle(): string { |
| 97 | const maxOccurrences: string = this.maxOccurrences === 'UNBOUNDED' ? '∞' : this.maxOccurrences; |
Tal Gitelman | 51d50f0 | 2017-12-10 18:55:03 +0200 | [diff] [blame] | 98 | return this.getTitle() + ': [' + this.minOccurrences + ', ' + maxOccurrences + ']'; |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 99 | } |
| 100 | |
ys9693 | 16a9fce | 2020-01-19 13:50:02 +0200 | [diff] [blame] | 101 | public toJSON = (): any => { |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 102 | this.selected = undefined; |
| 103 | this.filterTerm = undefined; |
| 104 | return this; |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 105 | } |
Tal Gitelman | 51d50f0 | 2017-12-10 18:55:03 +0200 | [diff] [blame] | 106 | |
| 107 | public isFulfilled() { |
| 108 | return parseInt(this.leftOccurrences) === 0; |
| 109 | } |
ys9693 | 16a9fce | 2020-01-19 13:50:02 +0200 | [diff] [blame] | 110 | |
| 111 | private initFilterTerm = (): void => { |
| 112 | this.filterTerm = this.name + ' ' + |
| 113 | (this.type ? (this.type.replace('tosca.capabilities.', '') + ' ' ) : '') + |
| 114 | (this.description || '') + ' ' + |
| 115 | (this.ownerName || '') + ' ' + |
| 116 | (this.validSourceTypes ? (this.validSourceTypes.join(',') + ' ') : '') + |
| 117 | this.minOccurrences + ',' + this.maxOccurrences; |
| 118 | if (this.properties && this.properties.length) { |
| 119 | _.forEach(this.properties, (prop: PropertyModel) => { |
| 120 | this.filterTerm += ' ' + prop.name + |
| 121 | ' ' + (prop.description || '') + |
| 122 | ' ' + prop.type + |
| 123 | (prop.schema && prop.schema.property ? (' ' + prop.schema.property.type) : ''); |
| 124 | }); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | export class CapabilityUI extends Capability { |
| 130 | isCreatedManually: boolean; |
| 131 | |
| 132 | constructor(input: Capability, componentUniqueId: string) { |
| 133 | super(input); |
| 134 | this.isCreatedManually = input.ownerId === componentUniqueId; |
| 135 | } |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | |