blob: 4a4f821e0c287135d4bcc246a5102ba9d8b866cd [file] [log] [blame]
Michael Landodd603392017-07-12 00:54:52 +03001/*-
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 Landoed64b5e2017-06-09 03:19:04 +030021/**
22 * Created by obarda on 4/20/2016.
23 */
24'use strict';
Michael Landoa5445102018-03-04 14:53:33 +020025import * as _ from "lodash";
Michael Landoed64b5e2017-06-09 03:19:04 +030026import {PropertyModel} from "./properties";
Tal Gitelman51d50f02017-12-10 18:55:03 +020027import {Requirement} from "./requirement";
Michael Landoed64b5e2017-06-09 03:19:04 +030028
Tal Gitelman51d50f02017-12-10 18:55:03 +020029export interface RequirementCapabilityModel{};
Michael Landoed64b5e2017-06-09 03:19:04 +030030//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
32export 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 }
Mojahidul Islam82e531a2019-05-14 12:49:31 +053048
49 public static getFlattenedCapabilities(capabilitiesGroup: CapabilitiesGroup): Array<Capability> {
50 return _.reduce(
51 _.toArray(capabilitiesGroup),
52 (allCaps, capArr) => allCaps.concat(capArr),
53 []);
54 }
Michael Landoed64b5e2017-06-09 03:19:04 +030055}
56
Tal Gitelman51d50f02017-12-10 18:55:03 +020057export class Capability implements RequirementCapabilityModel{
Michael Landoed64b5e2017-06-09 03:19:04 +030058
59 //server data
60 name:string;
61 ownerId:string;
62 ownerName:string;
63 type:string;
64 uniqueId:string;
65 capabilitySources:Array<String>;
Tal Gitelman51d50f02017-12-10 18:55:03 +020066 leftOccurrences:string;
miriame41ee9cb2019-03-04 13:49:15 +020067 minOccurrences: number;
Michael Landoed64b5e2017-06-09 03:19:04 +030068 maxOccurrences:string;
Michael Landoed64b5e2017-06-09 03:19:04 +030069 description:string;
70 validSourceTypes:Array<string>;
Tal Gitelman51d50f02017-12-10 18:55:03 +020071 properties:Array<PropertyModel>;
Michael Landoed64b5e2017-06-09 03:19:04 +030072 //custom
73 selected:boolean;
74 filterTerm:string;
75
76 constructor(capability?:Capability) {
77
78 if (capability) {
79 //server data
80 this.name = capability.name;
81 this.ownerId = capability.ownerId;
82 this.ownerName = capability.ownerName;
83 this.type = capability.type;
84 this.uniqueId = capability.uniqueId;
85 this.capabilitySources = capability.capabilitySources;
Tal Gitelman51d50f02017-12-10 18:55:03 +020086 this.leftOccurrences = capability.leftOccurrences;
Michael Landoed64b5e2017-06-09 03:19:04 +030087 this.minOccurrences = capability.minOccurrences;
88 this.maxOccurrences = capability.maxOccurrences;
89 this.properties = capability.properties;
90 this.description = capability.description;
91 this.validSourceTypes = capability.validSourceTypes;
92 this.selected = capability.selected;
93 this.initFilterTerm();
94
95 }
96 }
97
Tal Gitelman51d50f02017-12-10 18:55:03 +020098 public getTitle():string {
99 return this.ownerName + ': ' + this.name;
100 }
101
Michael Landoed64b5e2017-06-09 03:19:04 +0300102 public getFullTitle():string {
103 let maxOccurrences:string = this.maxOccurrences === 'UNBOUNDED' ? '∞' : this.maxOccurrences;
Tal Gitelman51d50f02017-12-10 18:55:03 +0200104 return this.getTitle() + ': [' + this.minOccurrences + ', ' + maxOccurrences + ']';
Michael Landoed64b5e2017-06-09 03:19:04 +0300105 }
106
107 public toJSON = ():any => {
108 this.selected = undefined;
109 this.filterTerm = undefined;
110 return this;
111 };
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 }
Tal Gitelman51d50f02017-12-10 18:55:03 +0200129
130 public isFulfilled() {
131 return parseInt(this.leftOccurrences) === 0;
132 }
Michael Landoed64b5e2017-06-09 03:19:04 +0300133}
134
135