Support for Nested/Hierarchical Services
[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 import {DirectivesEnum} from "../../ng2/components/logic/service-dependencies/directive-option";
38
39 export interface IComponentInstance {
40
41     componentUid:string;
42     componentName:string;
43     posX:number;
44     posY:number;
45     componentVersion:string;
46     description:string;
47     icon:string;
48     name:string;
49     normalizedName:string;
50     originType:string;
51     deploymentArtifacts:ArtifactGroupModel;
52     artifacts:ArtifactGroupModel;
53     propertyValueCounter:number;
54     uniqueId:string;
55     creationTime:number;
56     modificationTime:number;
57     capabilities:CapabilitiesGroup;
58     requirements:RequirementsGroup;
59     customizationUUID:string;
60     sourceModelInvariant:string;
61     sourceModelName:string;
62     sourceModelUid:string;
63     sourceModelUuid:string;
64     //custom properties
65     certified:boolean;
66     iconSprite:string;
67     inputs:Array<InputModel>;
68     properties:Array<PropertyModel>;
69     groupInstances:Array<Module>;
70     invariantName:string;
71     originArchived:boolean;
72 }
73 export class ComponentInstance implements IComponentInstance{
74
75     public componentUid:string;
76     public componentName:string;
77     public posX:number;
78     public posY:number;
79     public componentVersion:string;
80     public description:string;
81     public icon:string;
82     public name:string;
83     public normalizedName:string;
84     public originType:string;
85     public deploymentArtifacts:ArtifactGroupModel;
86     public artifacts:ArtifactGroupModel;
87     public propertyValueCounter:number;
88     public uniqueId:string;
89     public creationTime:number;
90     public modificationTime:number;
91     public capabilities:CapabilitiesGroup;
92     public requirements:RequirementsGroup;
93     public customizationUUID:string;
94     public sourceModelInvariant:string;
95     public sourceModelName:string;
96     public sourceModelUid:string;
97     public sourceModelUuid:string;
98     //custom properties
99     public certified:boolean;
100     public iconSprite:string;
101     public inputs:Array<InputModel>;
102     public properties:Array<PropertyModel>;
103     public groupInstances:Array<Module>;
104     public invariantName:string;
105     public originArchived:boolean;
106     public directives: string[];
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         }
140     }
141
142     public isUcpe = ():boolean => {
143         if (this.originType === 'VF' && this.capabilities && this.capabilities['tosca.capabilities.Container'] && this.name.toLowerCase().indexOf('ucpe') > -1) {
144             return true;
145         }
146         return false;
147     };
148
149     public isVl = ():boolean => {
150         return this.originType === 'VL';
151     };
152
153     public isComplex = ():boolean => {
154         return this.originType === ResourceType.VF || this.originType === ResourceType.PNF || this.originType === ResourceType.CVFC || this.originType === ResourceType.CR;
155     }
156
157     public isServiceProxy = ():boolean => {
158         return this.originType === ComponentType.SERVICE_PROXY;
159     }
160     
161     public isServiceSubstitution = ():boolean => {
162         return this.originType === ComponentType.SERVICE_SUBSTITUTION;
163     }
164
165     public isVFC = ():boolean => {
166         return this.originType === ResourceType.VFC;
167     }
168
169     public getComponentUid = ():string => {
170         return this.isServiceProxy() || this.isServiceSubstitution() ? this.sourceModelUid : this.componentUid;
171     }
172
173     public setInstanceRC = ():void=> {
174         _.forEach(this.requirements, (requirementValue:Array<any>, requirementKey)=> {
175             _.forEach(requirementValue, (requirement)=> {
176                 if (!requirement.ownerName) {
177                     requirement['ownerId'] = this.uniqueId;
178                     requirement['ownerName'] = this.name;
179                 }
180             });
181         });
182         _.forEach(this.capabilities, (capabilityValue:Array<any>, capabilityKey)=> {
183             _.forEach(capabilityValue, (capability)=> {
184                 if (!capability.ownerName) {
185                     capability['ownerId'] = this.uniqueId;
186                     capability['ownerName'] = this.name;
187                 }
188             });
189         });
190     };
191
192     public updatePosition(posX:number, posY:number) {
193         this.posX = posX;
194         this.posY = posY;
195     }
196
197     public findRequirement(reqType:string, uniqueId:string, ownerId:string, name:string):Requirement|undefined {
198         let requirement:Requirement = undefined;
199         const searchGroup = (reqType) ? [this.requirements[reqType]] : this.requirements;
200         _.some(_.keys(searchGroup), (searchType) => {
201             requirement = _.find<Requirement>(searchGroup[searchType], (req:Requirement) => (
202                 req.uniqueId === uniqueId && req.ownerId === ownerId && req.name === name
203             ));
204             return requirement;
205         });
206         return requirement;
207     }
208
209     public findCapability(capType:string, uniqueId:string, ownerId:string, name:string):Capability|undefined {
210         let capability:Capability = undefined;
211         const searchGroup = (capType) ? [this.capabilities[capType]] : this.capabilities;
212         _.some(_.keys(searchGroup), (searchType) => {
213             capability = _.find<Capability>(searchGroup[searchType], (cap:Capability) => (
214                 cap.uniqueId === uniqueId && cap.ownerId === ownerId && cap.name === name
215             ));
216             return capability;
217         });
218         return capability;
219     }
220
221     public toJSON = ():any => {
222         let temp = angular.copy(this);
223         temp.certified = undefined;
224         temp.iconSprite = undefined;
225         temp.inputs = undefined;
226         temp.groupInstances = undefined;
227         temp.properties = undefined;
228         temp.requirements = undefined;
229         temp.capabilities = undefined;
230         return temp;
231     };
232
233     public get iconClass() {
234         return this.iconSprite + ' ' + this.icon;
235     }
236     public isDependent = () : boolean => {
237        return Array.isArray(this.directives) && this.directives.length > 0;
238     }
239
240     public markAsSelect = () : void => {
241         this.directives.push(DirectivesEnum.SELECT);
242     }
243
244     public markAsSubstitute = () : void => {
245         this.directives.push(DirectivesEnum.SUBSTITUTE);
246     }
247
248     public unmarkAsDependent = (actualDirectiveValue: string) : void => {
249         console.info("[START] this.directives: ", this.directives)
250         let index = this.directives.indexOf(actualDirectiveValue);
251         if(index >= 0) {
252             this.directives.splice(index, 1);
253         }
254         console.info("[STOP] this.directives: ", this.directives)
255     }
256 }