blob: b377ed9edd38e46789afc25d86574254087f8bfe [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[];
andre.schmid2152a9a2021-05-05 15:31:04 +010065 external: boolean;
Michael Landoed64b5e2017-06-09 03:19:04 +030066
ys969316a9fce2020-01-19 13:50:02 +020067 // custom
68 selected: boolean;
69 filterTerm: string;
70
71 constructor(capability?: Capability) {
Michael Landoed64b5e2017-06-09 03:19:04 +030072
73 if (capability) {
ys969316a9fce2020-01-19 13:50:02 +020074 // server data
Michael Landoed64b5e2017-06-09 03:19:04 +030075 this.name = capability.name;
76 this.ownerId = capability.ownerId;
77 this.ownerName = capability.ownerName;
78 this.type = capability.type;
79 this.uniqueId = capability.uniqueId;
80 this.capabilitySources = capability.capabilitySources;
Tal Gitelman51d50f02017-12-10 18:55:03 +020081 this.leftOccurrences = capability.leftOccurrences;
Michael Landoed64b5e2017-06-09 03:19:04 +030082 this.minOccurrences = capability.minOccurrences;
83 this.maxOccurrences = capability.maxOccurrences;
84 this.properties = capability.properties;
85 this.description = capability.description;
86 this.validSourceTypes = capability.validSourceTypes;
87 this.selected = capability.selected;
andre.schmid2152a9a2021-05-05 15:31:04 +010088 this.external = capability.external;
Michael Landoed64b5e2017-06-09 03:19:04 +030089 this.initFilterTerm();
90
91 }
92 }
93
ys969316a9fce2020-01-19 13:50:02 +020094 public getTitle(): string {
Tal Gitelman51d50f02017-12-10 18:55:03 +020095 return this.ownerName + ': ' + this.name;
96 }
97
ys969316a9fce2020-01-19 13:50:02 +020098 public getFullTitle(): string {
99 const maxOccurrences: string = this.maxOccurrences === 'UNBOUNDED' ? '∞' : this.maxOccurrences;
Tal Gitelman51d50f02017-12-10 18:55:03 +0200100 return this.getTitle() + ': [' + this.minOccurrences + ', ' + maxOccurrences + ']';
Michael Landoed64b5e2017-06-09 03:19:04 +0300101 }
102
ys969316a9fce2020-01-19 13:50:02 +0200103 public toJSON = (): any => {
Michael Landoed64b5e2017-06-09 03:19:04 +0300104 this.selected = undefined;
105 this.filterTerm = undefined;
106 return this;
Michael Landoed64b5e2017-06-09 03:19:04 +0300107 }
Tal Gitelman51d50f02017-12-10 18:55:03 +0200108
109 public isFulfilled() {
110 return parseInt(this.leftOccurrences) === 0;
111 }
ys969316a9fce2020-01-19 13:50:02 +0200112
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 }
129}
130
131export class CapabilityUI extends Capability {
132 isCreatedManually: boolean;
133
134 constructor(input: Capability, componentUniqueId: string) {
135 super(input);
136 this.isCreatedManually = input.ownerId === componentUniqueId;
137 }
Michael Landoed64b5e2017-06-09 03:19:04 +0300138}
139
140