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"; |
Tal Gitelman | 51d50f0 | 2017-12-10 18:55:03 +0200 | [diff] [blame] | 26 | import {RequirementCapabilityModel} from "./capability"; |
Michael Lando | a544510 | 2018-03-04 14:53:33 +0200 | [diff] [blame] | 27 | |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 28 | //this is an object contains keys, when each key has matching array. |
| 29 | // for example: key = tosca.capabilities.network. and the match array is array of requirements objects |
| 30 | export class RequirementsGroup { |
| 31 | [key: string]: Array<Requirement>; |
| 32 | constructor(requirementGroupObj?:RequirementsGroup) { |
| 33 | _.forEach(requirementGroupObj, (requirementsArrayObj:Array<Requirement>, instance) => { |
| 34 | this[instance] = []; |
| 35 | _.forEach(requirementsArrayObj, (requirement:Requirement):void => { |
| 36 | this[instance].push(new Requirement(requirement)); |
| 37 | }); |
| 38 | }); |
| 39 | } |
| 40 | } |
| 41 | |
Tal Gitelman | 51d50f0 | 2017-12-10 18:55:03 +0200 | [diff] [blame] | 42 | export class Requirement implements RequirementCapabilityModel{ |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 43 | |
| 44 | //server data |
| 45 | capability:string; |
| 46 | name:string; |
| 47 | ownerId:string; |
| 48 | ownerName:string; |
Michael Lando | a544510 | 2018-03-04 14:53:33 +0200 | [diff] [blame] | 49 | parentName: string; |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 50 | node:string; |
| 51 | uniqueId:string; |
| 52 | relationship:string; |
Tal Gitelman | 51d50f0 | 2017-12-10 18:55:03 +0200 | [diff] [blame] | 53 | leftOccurrences:string; |
miriame | 41ee9cb | 2019-03-04 13:49:15 +0200 | [diff] [blame] | 54 | minOccurrences: number; |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 55 | maxOccurrences:string; |
| 56 | //custom |
| 57 | filterTerm:string; |
| 58 | |
| 59 | constructor(requirement?:Requirement) { |
| 60 | |
| 61 | if (requirement) { |
| 62 | this.capability = requirement.capability; |
| 63 | this.name = requirement.name; |
| 64 | this.ownerId = requirement.ownerId; |
| 65 | this.ownerName = requirement.ownerName; |
Michael Lando | a544510 | 2018-03-04 14:53:33 +0200 | [diff] [blame] | 66 | this.parentName = requirement.parentName; |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 67 | this.node = requirement.node; |
| 68 | this.uniqueId = requirement.uniqueId; |
| 69 | this.relationship = requirement.relationship; |
Tal Gitelman | 51d50f0 | 2017-12-10 18:55:03 +0200 | [diff] [blame] | 70 | this.leftOccurrences = requirement.leftOccurrences; |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 71 | this.minOccurrences = requirement.minOccurrences; |
| 72 | this.maxOccurrences = requirement.maxOccurrences; |
| 73 | this.initFilterTerm(); |
| 74 | |
| 75 | } |
| 76 | } |
| 77 | |
Tal Gitelman | 51d50f0 | 2017-12-10 18:55:03 +0200 | [diff] [blame] | 78 | public getTitle():string { |
| 79 | return this.ownerName + ': ' + this.name; |
| 80 | } |
| 81 | |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 82 | public getFullTitle():string { |
Tal Gitelman | 51d50f0 | 2017-12-10 18:55:03 +0200 | [diff] [blame] | 83 | return this.getTitle() + ': [' + this.minOccurrences + ', ' + this.maxOccurrences + ']'; |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | public toJSON = ():any => { |
| 87 | this.filterTerm = undefined; |
| 88 | return this; |
| 89 | }; |
| 90 | |
| 91 | private initFilterTerm = ():void => { |
| 92 | this.filterTerm = (this.name + " ") + |
| 93 | (this.ownerName + " " ) + |
| 94 | (this.capability ? (this.capability.substring("tosca.capabilities.".length) + " " ) : "") + |
| 95 | (this.node ? (this.node.substring("tosca.nodes.".length) + " ") : "") + |
| 96 | (this.relationship ? (this.relationship.substring("tosca.relationships.".length) + " ") : "") + |
| 97 | this.minOccurrences + "," + this.maxOccurrences; |
| 98 | } |
Tal Gitelman | 51d50f0 | 2017-12-10 18:55:03 +0200 | [diff] [blame] | 99 | |
| 100 | public isFulfilled() { |
| 101 | return parseInt(this.leftOccurrences) === 0; |
| 102 | } |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | |