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 |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 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'; |
Michael Lando | a544510 | 2018-03-04 14:53:33 +0200 | [diff] [blame] | 25 | import * as _ from "lodash"; |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 26 | import {PropertyModel} from "./properties"; |
Tal Gitelman | 51d50f0 | 2017-12-10 18:55:03 +0200 | [diff] [blame] | 27 | import {Requirement} from "./requirement"; |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 28 | |
Tal Gitelman | 51d50f0 | 2017-12-10 18:55:03 +0200 | [diff] [blame] | 29 | export interface RequirementCapabilityModel{}; |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 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:Array<Capability>, instance) => { |
| 35 | this[instance] = []; |
| 36 | _.forEach(capabilitiesArrayObj, (capability:Capability):void => { |
| 37 | this[instance].push(new Capability(capability)); |
| 38 | }); |
| 39 | }); |
| 40 | } |
| 41 | |
| 42 | public findValueByKey(keySubstring:string):Array<Capability> { |
| 43 | let key:string = _.find(Object.keys(this), (key)=> { |
| 44 | return _.includes(key.toLowerCase(), keySubstring); |
| 45 | }); |
| 46 | return this[key]; |
| 47 | } |
| 48 | } |
| 49 | |
Tal Gitelman | 51d50f0 | 2017-12-10 18:55:03 +0200 | [diff] [blame] | 50 | export class Capability implements RequirementCapabilityModel{ |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 51 | |
| 52 | //server data |
| 53 | name:string; |
| 54 | ownerId:string; |
| 55 | ownerName:string; |
| 56 | type:string; |
| 57 | uniqueId:string; |
| 58 | capabilitySources:Array<String>; |
Tal Gitelman | 51d50f0 | 2017-12-10 18:55:03 +0200 | [diff] [blame] | 59 | leftOccurrences:string; |
miriame | 41ee9cb | 2019-03-04 13:49:15 +0200 | [diff] [blame] | 60 | minOccurrences: number; |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 61 | maxOccurrences:string; |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 62 | description:string; |
| 63 | validSourceTypes:Array<string>; |
Tal Gitelman | 51d50f0 | 2017-12-10 18:55:03 +0200 | [diff] [blame] | 64 | properties:Array<PropertyModel>; |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 65 | //custom |
| 66 | selected:boolean; |
| 67 | filterTerm:string; |
| 68 | |
| 69 | constructor(capability?:Capability) { |
| 70 | |
| 71 | if (capability) { |
| 72 | //server data |
| 73 | this.name = capability.name; |
| 74 | this.ownerId = capability.ownerId; |
| 75 | this.ownerName = capability.ownerName; |
| 76 | this.type = capability.type; |
| 77 | this.uniqueId = capability.uniqueId; |
| 78 | this.capabilitySources = capability.capabilitySources; |
Tal Gitelman | 51d50f0 | 2017-12-10 18:55:03 +0200 | [diff] [blame] | 79 | this.leftOccurrences = capability.leftOccurrences; |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 80 | this.minOccurrences = capability.minOccurrences; |
| 81 | this.maxOccurrences = capability.maxOccurrences; |
| 82 | this.properties = capability.properties; |
| 83 | this.description = capability.description; |
| 84 | this.validSourceTypes = capability.validSourceTypes; |
| 85 | this.selected = capability.selected; |
| 86 | this.initFilterTerm(); |
| 87 | |
| 88 | } |
| 89 | } |
| 90 | |
Tal Gitelman | 51d50f0 | 2017-12-10 18:55:03 +0200 | [diff] [blame] | 91 | public getTitle():string { |
| 92 | return this.ownerName + ': ' + this.name; |
| 93 | } |
| 94 | |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 95 | public getFullTitle():string { |
| 96 | let maxOccurrences:string = this.maxOccurrences === 'UNBOUNDED' ? '∞' : this.maxOccurrences; |
Tal Gitelman | 51d50f0 | 2017-12-10 18:55:03 +0200 | [diff] [blame] | 97 | return this.getTitle() + ': [' + this.minOccurrences + ', ' + maxOccurrences + ']'; |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | public toJSON = ():any => { |
| 101 | this.selected = undefined; |
| 102 | this.filterTerm = undefined; |
| 103 | return this; |
| 104 | }; |
| 105 | |
| 106 | private initFilterTerm = ():void => { |
| 107 | this.filterTerm = this.name + " " + |
| 108 | (this.type ? (this.type.replace("tosca.capabilities.", "") + " " ) : "") + |
| 109 | (this.description || "") + " " + |
| 110 | (this.ownerName || "") + " " + |
| 111 | (this.validSourceTypes ? (this.validSourceTypes.join(',') + " ") : "") + |
| 112 | this.minOccurrences + "," + this.maxOccurrences; |
| 113 | if (this.properties && this.properties.length) { |
| 114 | _.forEach(this.properties, (prop:PropertyModel)=> { |
| 115 | this.filterTerm += " " + prop.name + |
| 116 | " " + (prop.description || "") + |
| 117 | " " + prop.type + |
| 118 | (prop.schema && prop.schema.property ? (" " + prop.schema.property.type) : ""); |
| 119 | }); |
| 120 | } |
| 121 | } |
Tal Gitelman | 51d50f0 | 2017-12-10 18:55:03 +0200 | [diff] [blame] | 122 | |
| 123 | public isFulfilled() { |
| 124 | return parseInt(this.leftOccurrences) === 0; |
| 125 | } |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | |