blob: f365dc4940510718c30d9311b689028541750db8 [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
ys969316a9fce2020-01-19 13:50:02 +020010 *
Michael Landodd603392017-07-12 00:54:52 +030011 * http://www.apache.org/licenses/LICENSE-2.0
ys969316a9fce2020-01-19 13:50:02 +020012 *
Michael Landodd603392017-07-12 00:54:52 +030013 * 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';
ys969316a9fce2020-01-19 13:50:02 +020025import * as _ from 'lodash';
26import { PropertyModel } from './properties';
Michael Landoed64b5e2017-06-09 03:19:04 +030027
ys969316a9fce2020-01-19 13:50:02 +020028export interface RequirementCapabilityModel {}
29
30// this is an object contains keys, when each key has matching array.
Michael Landoed64b5e2017-06-09 03:19:04 +030031// for example: key = tosca.capabilities.network.Linkable and the match array is array of capabilities objects
32export class CapabilitiesGroup {
ys969316a9fce2020-01-19 13:50:02 +020033 constructor(capabilityGroupObj?: CapabilitiesGroup) {
34 _.forEach(capabilityGroupObj, (capabilitiesArrayObj: Capability[], instance) => {
Michael Landoed64b5e2017-06-09 03:19:04 +030035 this[instance] = [];
ys969316a9fce2020-01-19 13:50:02 +020036 _.forEach(capabilitiesArrayObj, (capability: Capability): void => {
Michael Landoed64b5e2017-06-09 03:19:04 +030037 this[instance].push(new Capability(capability));
38 });
39 });
40 }
ys969316a9fce2020-01-19 13:50:02 +020041
Mojahidul Islam82e531a2019-05-14 12:49:31 +053042 public static getFlattenedCapabilities(capabilitiesGroup: CapabilitiesGroup): Array<Capability> {
43 return _.reduce(
44 _.toArray(capabilitiesGroup),
45 (allCaps, capArr) => allCaps.concat(capArr),
46 []);
47 }
Michael Landoed64b5e2017-06-09 03:19:04 +030048}
49
ys969316a9fce2020-01-19 13:50:02 +020050export class Capability implements RequirementCapabilityModel {
Michael Landoed64b5e2017-06-09 03:19:04 +030051
ys969316a9fce2020-01-19 13:50:02 +020052 // 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 Landoed64b5e2017-06-09 03:19:04 +030065
ys969316a9fce2020-01-19 13:50:02 +020066 // custom
67 selected: boolean;
68 filterTerm: string;
69
70 constructor(capability?: Capability) {
Michael Landoed64b5e2017-06-09 03:19:04 +030071
72 if (capability) {
ys969316a9fce2020-01-19 13:50:02 +020073 // server data
Michael Landoed64b5e2017-06-09 03:19:04 +030074 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 Gitelman51d50f02017-12-10 18:55:03 +020080 this.leftOccurrences = capability.leftOccurrences;
Michael Landoed64b5e2017-06-09 03:19:04 +030081 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
ys969316a9fce2020-01-19 13:50:02 +020092 public getTitle(): string {
Tal Gitelman51d50f02017-12-10 18:55:03 +020093 return this.ownerName + ': ' + this.name;
94 }
95
ys969316a9fce2020-01-19 13:50:02 +020096 public getFullTitle(): string {
97 const maxOccurrences: string = this.maxOccurrences === 'UNBOUNDED' ? '∞' : this.maxOccurrences;
Tal Gitelman51d50f02017-12-10 18:55:03 +020098 return this.getTitle() + ': [' + this.minOccurrences + ', ' + maxOccurrences + ']';
Michael Landoed64b5e2017-06-09 03:19:04 +030099 }
100
ys969316a9fce2020-01-19 13:50:02 +0200101 public toJSON = (): any => {
Michael Landoed64b5e2017-06-09 03:19:04 +0300102 this.selected = undefined;
103 this.filterTerm = undefined;
104 return this;
Michael Landoed64b5e2017-06-09 03:19:04 +0300105 }
Tal Gitelman51d50f02017-12-10 18:55:03 +0200106
107 public isFulfilled() {
108 return parseInt(this.leftOccurrences) === 0;
109 }
ys969316a9fce2020-01-19 13:50:02 +0200110
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
129export 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 Landoed64b5e2017-06-09 03:19:04 +0300136}
137
138