blob: 65428b398ea9a90b9c3017f070f50292438b2ec5 [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";
Tal Gitelman51d50f02017-12-10 18:55:03 +020026import {RequirementCapabilityModel} from "./capability";
Michael Landoa5445102018-03-04 14:53:33 +020027
Michael Landoed64b5e2017-06-09 03:19:04 +030028//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
30export 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 Gitelman51d50f02017-12-10 18:55:03 +020042export class Requirement implements RequirementCapabilityModel{
Michael Landoed64b5e2017-06-09 03:19:04 +030043
44 //server data
45 capability:string;
46 name:string;
47 ownerId:string;
48 ownerName:string;
Michael Landoa5445102018-03-04 14:53:33 +020049 parentName: string;
Michael Landoed64b5e2017-06-09 03:19:04 +030050 node:string;
51 uniqueId:string;
52 relationship:string;
Tal Gitelman51d50f02017-12-10 18:55:03 +020053 leftOccurrences:string;
Michael Landoed64b5e2017-06-09 03:19:04 +030054 minOccurrences:string;
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 Landoa5445102018-03-04 14:53:33 +020066 this.parentName = requirement.parentName;
Michael Landoed64b5e2017-06-09 03:19:04 +030067 this.node = requirement.node;
68 this.uniqueId = requirement.uniqueId;
69 this.relationship = requirement.relationship;
Tal Gitelman51d50f02017-12-10 18:55:03 +020070 this.leftOccurrences = requirement.leftOccurrences;
Michael Landoed64b5e2017-06-09 03:19:04 +030071 this.minOccurrences = requirement.minOccurrences;
72 this.maxOccurrences = requirement.maxOccurrences;
73 this.initFilterTerm();
74
75 }
76 }
77
Tal Gitelman51d50f02017-12-10 18:55:03 +020078 public getTitle():string {
79 return this.ownerName + ': ' + this.name;
80 }
81
Michael Landoed64b5e2017-06-09 03:19:04 +030082 public getFullTitle():string {
Tal Gitelman51d50f02017-12-10 18:55:03 +020083 return this.getTitle() + ': [' + this.minOccurrences + ', ' + this.maxOccurrences + ']';
Michael Landoed64b5e2017-06-09 03:19:04 +030084 }
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 Gitelman51d50f02017-12-10 18:55:03 +020099
100 public isFulfilled() {
101 return parseInt(this.leftOccurrences) === 0;
102 }
Michael Landoed64b5e2017-06-09 03:19:04 +0300103}
104
105