c3188baa4747a66aae7d603c85750d8cc59b0563
[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     public interfaces:any;
107
108     constructor(componentInstance?:ComponentInstance) {
109
110         if (componentInstance) {
111             this.componentUid = componentInstance.componentUid;
112             this.componentName = componentInstance.componentName;
113
114             this.componentVersion = componentInstance.componentVersion;
115             this.description = componentInstance.description;
116             this.icon = componentInstance.icon;
117             this.name = componentInstance.name;
118             this.normalizedName = componentInstance.normalizedName;
119             this.originType = componentInstance.originType;
120             this.deploymentArtifacts = new ArtifactGroupModel(componentInstance.deploymentArtifacts);
121             this.artifacts = new ArtifactGroupModel(componentInstance.artifacts);
122             this.uniqueId = componentInstance.uniqueId;
123             this.creationTime = componentInstance.creationTime;
124             this.modificationTime = componentInstance.modificationTime;
125             this.propertyValueCounter = componentInstance.propertyValueCounter;
126             this.capabilities = new CapabilitiesGroup(componentInstance.capabilities);
127             this.requirements = new RequirementsGroup(componentInstance.requirements);
128             this.certified = componentInstance.certified;
129             this.customizationUUID = componentInstance.customizationUUID;
130             this.updatePosition(componentInstance.posX, componentInstance.posY);
131             this.groupInstances = componentInstance.groupInstances;
132             this.invariantName = componentInstance.invariantName;
133             this.sourceModelInvariant = componentInstance.sourceModelInvariant;
134             this.sourceModelName = componentInstance.sourceModelName;
135             this.sourceModelUid = componentInstance.sourceModelUid;
136             this.sourceModelUuid = componentInstance.sourceModelUuid;
137             this.originArchived = componentInstance.originArchived;
138             this.directives = componentInstance.directives;
139             this.interfaces = componentInstance.interfaces;
140         }
141     }
142
143     public isUcpe = ():boolean => {
144         if (this.originType === 'VF' && this.capabilities && this.capabilities['tosca.capabilities.Container'] && this.name.toLowerCase().indexOf('ucpe') > -1) {
145             return true;
146         }
147         return false;
148     };
149
150     public isVl = ():boolean => {
151         return this.originType === 'VL';
152     };
153
154     public isComplex = ():boolean => {
155         return this.originType === ResourceType.VF || this.originType === ResourceType.PNF || this.originType === ResourceType.CVFC || this.originType === ResourceType.CR;
156     }
157
158     public isServiceProxy = ():boolean => {
159         return this.originType === ComponentType.SERVICE_PROXY;
160     }
161     
162     public isServiceSubstitution = ():boolean => {
163         return this.originType === ComponentType.SERVICE_SUBSTITUTION;
164     }
165
166     public isVFC = ():boolean => {
167         return this.originType === ResourceType.VFC;
168     }
169
170     public getComponentUid = ():string => {
171         return this.isServiceProxy() || this.isServiceSubstitution() ? this.sourceModelUid : this.componentUid;
172     }
173
174     public setInstanceRC = ():void=> {
175         _.forEach(this.requirements, (requirementValue:Array<any>, requirementKey)=> {
176             _.forEach(requirementValue, (requirement)=> {
177                 if (!requirement.ownerName) {
178                     requirement['ownerId'] = this.uniqueId;
179                     requirement['ownerName'] = this.name;
180                 }
181             });
182         });
183         _.forEach(this.capabilities, (capabilityValue:Array<any>, capabilityKey)=> {
184             _.forEach(capabilityValue, (capability)=> {
185                 if (!capability.ownerName) {
186                     capability['ownerId'] = this.uniqueId;
187                     capability['ownerName'] = this.name;
188                 }
189             });
190         });
191     };
192
193     public updatePosition(posX:number, posY:number) {
194         this.posX = posX;
195         this.posY = posY;
196     }
197
198     public findRequirement(reqType:string, uniqueId:string, ownerId:string, name:string):Requirement|undefined {
199         let requirement:Requirement = undefined;
200         const searchGroup = (reqType) ? [this.requirements[reqType]] : this.requirements;
201         _.some(_.keys(searchGroup), (searchType) => {
202             requirement = _.find<Requirement>(searchGroup[searchType], (req:Requirement) => (
203                 req.uniqueId === uniqueId && req.ownerId === ownerId && req.name === name
204             ));
205             return requirement;
206         });
207         return requirement;
208     }
209
210     public findCapability(capType:string, uniqueId:string, ownerId:string, name:string):Capability|undefined {
211         let capability:Capability = undefined;
212         const searchGroup = (capType) ? [this.capabilities[capType]] : this.capabilities;
213         _.some(_.keys(searchGroup), (searchType) => {
214             capability = _.find<Capability>(searchGroup[searchType], (cap:Capability) => (
215                 cap.uniqueId === uniqueId && cap.ownerId === ownerId && cap.name === name
216             ));
217             return capability;
218         });
219         return capability;
220     }
221
222     public toJSON = ():any => {
223         let temp = angular.copy(this);
224         temp.certified = undefined;
225         temp.iconSprite = undefined;
226         temp.inputs = undefined;
227         temp.groupInstances = undefined;
228         temp.properties = undefined;
229         temp.requirements = undefined;
230         temp.capabilities = undefined;
231         return temp;
232     };
233
234     public get iconClass() {
235         return this.iconSprite + ' ' + this.icon;
236     }
237     public isDependent = () : boolean => {
238        return Array.isArray(this.directives) && this.directives.length > 0;
239     }
240
241     public setDirectiveValue = (selectedDirectives: string[]) : void => {
242         this.directives = selectedDirectives;
243     }
244
245     public unmarkAsDependent = (directiveValues: string[]) : void => {
246         console.info("[START] this.directives: ", this.directives)
247         directiveValues.forEach(directive => {
248             let index = this.directives.indexOf(directive);
249             if(index >= 0) {
250                 this.directives.splice(index, directiveValues.length);
251             }
252         });
253         console.info("[STOP] this.directives: ", this.directives)
254     }
255 }