Catalog alignment
[sdc.git] / catalog-ui / src / app / models / componentsInstances / componentInstance.ts
1 /*-
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
21 /**
22  * Created by obarda on 2/4/2016.
23  */
24 'use strict';
25 import * as _ from "lodash";
26 import {
27     ArtifactGroupModel,
28     CapabilitiesGroup,
29     RequirementsGroup,
30     PropertyModel,
31     InputModel,
32     Module
33 } from "../../models";
34 import {ResourceType, ComponentType} from "../../utils/constants";
35 import {Capability} from "../capability";
36 import {Requirement} from "../requirement";
37
38 export interface IComponentInstance {
39
40     componentUid:string;
41     componentName:string;
42     posX:number;
43     posY:number;
44     componentVersion:string;
45     description:string;
46     icon:string;
47     name:string;
48     normalizedName:string;
49     originType:string;
50     deploymentArtifacts:ArtifactGroupModel;
51     artifacts:ArtifactGroupModel;
52     propertyValueCounter:number;
53     uniqueId:string;
54     creationTime:number;
55     modificationTime:number;
56     capabilities:CapabilitiesGroup;
57     requirements:RequirementsGroup;
58     customizationUUID:string;
59     sourceModelInvariant:string;
60     sourceModelName:string;
61     sourceModelUid:string;
62     sourceModelUuid:string;
63     //custom properties
64     certified:boolean;
65     iconSprite:string;
66     inputs:Array<InputModel>;
67     properties:Array<PropertyModel>;
68     groupInstances:Array<Module>;
69     invariantName:string;
70     originArchived:boolean;
71 }
72 export class ComponentInstance implements IComponentInstance{
73
74     public componentUid:string;
75     public componentName:string;
76     public posX:number;
77     public posY:number;
78     public componentVersion:string;
79     public description:string;
80     public icon:string;
81     public name:string;
82     public normalizedName:string;
83     public originType:string;
84     public deploymentArtifacts:ArtifactGroupModel;
85     public artifacts:ArtifactGroupModel;
86     public propertyValueCounter:number;
87     public uniqueId:string;
88     public creationTime:number;
89     public modificationTime:number;
90     public capabilities:CapabilitiesGroup;
91     public requirements:RequirementsGroup;
92     public customizationUUID:string;
93     public sourceModelInvariant:string;
94     public sourceModelName:string;
95     public sourceModelUid:string;
96     public sourceModelUuid:string;
97     //custom properties
98     public certified:boolean;
99     public iconSprite:string;
100     public inputs:Array<InputModel>;
101     public properties:Array<PropertyModel>;
102     public groupInstances:Array<Module>;
103     public invariantName:string;
104     public originArchived:boolean;
105     public directives: string[];
106
107     DIRECTIVES_TYPES = {
108         SELECTABLE: 'selectable'
109     };
110
111     constructor(componentInstance?:ComponentInstance) {
112
113         if (componentInstance) {
114             this.componentUid = componentInstance.componentUid;
115             this.componentName = componentInstance.componentName;
116
117             this.componentVersion = componentInstance.componentVersion;
118             this.description = componentInstance.description;
119             this.icon = componentInstance.icon;
120             this.name = componentInstance.name;
121             this.normalizedName = componentInstance.normalizedName;
122             this.originType = componentInstance.originType;
123             this.deploymentArtifacts = new ArtifactGroupModel(componentInstance.deploymentArtifacts);
124             this.artifacts = new ArtifactGroupModel(componentInstance.artifacts);
125             this.uniqueId = componentInstance.uniqueId;
126             this.creationTime = componentInstance.creationTime;
127             this.modificationTime = componentInstance.modificationTime;
128             this.propertyValueCounter = componentInstance.propertyValueCounter;
129             this.capabilities = new CapabilitiesGroup(componentInstance.capabilities);
130             this.requirements = new RequirementsGroup(componentInstance.requirements);
131             this.certified = componentInstance.certified;
132             this.customizationUUID = componentInstance.customizationUUID;
133             this.updatePosition(componentInstance.posX, componentInstance.posY);
134             this.groupInstances = componentInstance.groupInstances;
135             this.invariantName = componentInstance.invariantName;
136             this.sourceModelInvariant = componentInstance.sourceModelInvariant;
137             this.sourceModelName = componentInstance.sourceModelName;
138             this.sourceModelUid = componentInstance.sourceModelUid;
139             this.sourceModelUuid = componentInstance.sourceModelUuid;
140             this.originArchived = componentInstance.originArchived;
141             this.directives = componentInstance.directives;
142         }
143     }
144
145     public isUcpe = ():boolean => {
146         if (this.originType === 'VF' && this.capabilities && this.capabilities['tosca.capabilities.Container'] && this.name.toLowerCase().indexOf('ucpe') > -1) {
147             return true;
148         }
149         return false;
150     };
151
152     public isVl = ():boolean => {
153         return this.originType === 'VL';
154     };
155
156     public isComplex = ():boolean => {
157         return this.originType === ResourceType.VF || this.originType === ResourceType.PNF || this.originType === ResourceType.CVFC || this.originType === ResourceType.CR;
158     }
159
160     public isServiceProxy = ():boolean => {
161         return this.originType === ComponentType.SERVICE_PROXY;
162     }
163
164     public getComponentUid = ():string => {
165         return this.isServiceProxy()? this.sourceModelUid : this.componentUid;
166     }
167
168     public setInstanceRC = ():void=> {
169         _.forEach(this.requirements, (requirementValue:Array<any>, requirementKey)=> {
170             _.forEach(requirementValue, (requirement)=> {
171                 if (!requirement.ownerName) {
172                     requirement['ownerId'] = this.uniqueId;
173                     requirement['ownerName'] = this.name;
174                 }
175             });
176         });
177         _.forEach(this.capabilities, (capabilityValue:Array<any>, capabilityKey)=> {
178             _.forEach(capabilityValue, (capability)=> {
179                 if (!capability.ownerName) {
180                     capability['ownerId'] = this.uniqueId;
181                     capability['ownerName'] = this.name;
182                 }
183             });
184         });
185     };
186
187     public updatePosition(posX:number, posY:number) {
188         this.posX = posX;
189         this.posY = posY;
190     }
191
192     public findRequirement(reqType:string, uniqueId:string, ownerId:string, name:string):Requirement|undefined {
193         let requirement:Requirement = undefined;
194         const searchGroup = (reqType) ? [this.requirements[reqType]] : this.requirements;
195         _.some(_.keys(searchGroup), (searchType) => {
196             requirement = _.find<Requirement>(searchGroup[searchType], (req:Requirement) => (
197                 req.uniqueId === uniqueId && req.ownerId === ownerId && req.name === name
198             ));
199             return requirement;
200         });
201         return requirement;
202     }
203
204     public findCapability(capType:string, uniqueId:string, ownerId:string, name:string):Capability|undefined {
205         let capability:Capability = undefined;
206         const searchGroup = (capType) ? [this.capabilities[capType]] : this.capabilities;
207         _.some(_.keys(searchGroup), (searchType) => {
208             capability = _.find<Capability>(searchGroup[searchType], (cap:Capability) => (
209                 cap.uniqueId === uniqueId && cap.ownerId === ownerId && cap.name === name
210             ));
211             return capability;
212         });
213         return capability;
214     }
215
216     public toJSON = ():any => {
217         let temp = angular.copy(this);
218         temp.certified = undefined;
219         temp.iconSprite = undefined;
220         temp.inputs = undefined;
221         temp.groupInstances = undefined;
222         temp.properties = undefined;
223         temp.requirements = undefined;
224         temp.capabilities = undefined;
225         return temp;
226     };
227
228     public get iconClass() {
229         return this.iconSprite + ' ' + this.icon;
230     }
231     public isDependent = () : boolean => {
232         return this.directives && this.directives.indexOf(this.DIRECTIVES_TYPES.SELECTABLE) !== -1;
233     }
234
235     public markAsDependent = () : void => {
236         this.directives.push(this.DIRECTIVES_TYPES.SELECTABLE);
237     }
238
239     public unmarkAsDependent = () : void => {
240         let index = this.directives.indexOf(this.DIRECTIVES_TYPES.SELECTABLE);
241         if(index >= 0) {
242             this.directives.splice(index, 1);
243         }
244     }
245 }