blob: 2f2e3a8de655220a65a56ac0946be4f9eda2a43e [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';
25import {PropertyModel} from "./properties";
Tal Gitelman51d50f02017-12-10 18:55:03 +020026import {Requirement} from "./requirement";
Michael Landoed64b5e2017-06-09 03:19:04 +030027
Tal Gitelman51d50f02017-12-10 18:55:03 +020028export interface RequirementCapabilityModel{};
Michael Landoed64b5e2017-06-09 03:19:04 +030029//this is an object contains keys, when each key has matching array.
30// for example: key = tosca.capabilities.network.Linkable and the match array is array of capabilities objects
31export class CapabilitiesGroup {
32 constructor(capabilityGroupObj?:CapabilitiesGroup) {
33 _.forEach(capabilityGroupObj, (capabilitiesArrayObj:Array<Capability>, instance) => {
34 this[instance] = [];
35 _.forEach(capabilitiesArrayObj, (capability:Capability):void => {
36 this[instance].push(new Capability(capability));
37 });
38 });
39 }
40
41 public findValueByKey(keySubstring:string):Array<Capability> {
42 let key:string = _.find(Object.keys(this), (key)=> {
43 return _.includes(key.toLowerCase(), keySubstring);
44 });
45 return this[key];
46 }
47}
48
Tal Gitelman51d50f02017-12-10 18:55:03 +020049export class Capability implements RequirementCapabilityModel{
Michael Landoed64b5e2017-06-09 03:19:04 +030050
51 //server data
52 name:string;
53 ownerId:string;
54 ownerName:string;
55 type:string;
56 uniqueId:string;
57 capabilitySources:Array<String>;
Tal Gitelman51d50f02017-12-10 18:55:03 +020058 leftOccurrences:string;
Michael Landoed64b5e2017-06-09 03:19:04 +030059 minOccurrences:string;
60 maxOccurrences:string;
Michael Landoed64b5e2017-06-09 03:19:04 +030061 description:string;
62 validSourceTypes:Array<string>;
Tal Gitelman51d50f02017-12-10 18:55:03 +020063 properties:Array<PropertyModel>;
Michael Landoed64b5e2017-06-09 03:19:04 +030064 //custom
65 selected:boolean;
66 filterTerm:string;
67
68 constructor(capability?:Capability) {
69
70 if (capability) {
71 //server data
72 this.name = capability.name;
73 this.ownerId = capability.ownerId;
74 this.ownerName = capability.ownerName;
75 this.type = capability.type;
76 this.uniqueId = capability.uniqueId;
77 this.capabilitySources = capability.capabilitySources;
Tal Gitelman51d50f02017-12-10 18:55:03 +020078 this.leftOccurrences = capability.leftOccurrences;
Michael Landoed64b5e2017-06-09 03:19:04 +030079 this.minOccurrences = capability.minOccurrences;
80 this.maxOccurrences = capability.maxOccurrences;
81 this.properties = capability.properties;
82 this.description = capability.description;
83 this.validSourceTypes = capability.validSourceTypes;
84 this.selected = capability.selected;
85 this.initFilterTerm();
86
87 }
88 }
89
Tal Gitelman51d50f02017-12-10 18:55:03 +020090 public getTitle():string {
91 return this.ownerName + ': ' + this.name;
92 }
93
Michael Landoed64b5e2017-06-09 03:19:04 +030094 public getFullTitle():string {
95 let maxOccurrences:string = this.maxOccurrences === 'UNBOUNDED' ? '∞' : this.maxOccurrences;
Tal Gitelman51d50f02017-12-10 18:55:03 +020096 return this.getTitle() + ': [' + this.minOccurrences + ', ' + maxOccurrences + ']';
Michael Landoed64b5e2017-06-09 03:19:04 +030097 }
98
99 public toJSON = ():any => {
100 this.selected = undefined;
101 this.filterTerm = undefined;
102 return this;
103 };
104
105 private initFilterTerm = ():void => {
106 this.filterTerm = this.name + " " +
107 (this.type ? (this.type.replace("tosca.capabilities.", "") + " " ) : "") +
108 (this.description || "") + " " +
109 (this.ownerName || "") + " " +
110 (this.validSourceTypes ? (this.validSourceTypes.join(',') + " ") : "") +
111 this.minOccurrences + "," + this.maxOccurrences;
112 if (this.properties && this.properties.length) {
113 _.forEach(this.properties, (prop:PropertyModel)=> {
114 this.filterTerm += " " + prop.name +
115 " " + (prop.description || "") +
116 " " + prop.type +
117 (prop.schema && prop.schema.property ? (" " + prop.schema.property.type) : "");
118 });
119 }
120 }
Tal Gitelman51d50f02017-12-10 18:55:03 +0200121
122 public isFulfilled() {
123 return parseInt(this.leftOccurrences) === 0;
124 }
Michael Landoed64b5e2017-06-09 03:19:04 +0300125}
126
127