Sync Integ to Master
[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 {ArtifactGroupModel, CapabilitiesGroup,RequirementsGroup, PropertyModel, InputModel, Module} from "../../models";
27 import {ResourceType,ComponentType} from "../../utils/constants";
28 import {Capability} from "../capability";
29 import {Requirement} from "../requirement";
30
31 export class ComponentInstance {
32
33     public componentUid:string;
34     public componentName:string;
35     public posX:number;
36     public posY:number;
37     public componentVersion:string;
38     public description:string;
39     public icon:string;
40     public name:string;
41     public normalizedName:string;
42     public originType:string;
43     public deploymentArtifacts:ArtifactGroupModel;
44     public artifacts:ArtifactGroupModel;
45     public propertyValueCounter:number;
46     public uniqueId:string;
47     public creationTime:number;
48     public modificationTime:number;
49     public capabilities:CapabilitiesGroup;
50     public requirements:RequirementsGroup;
51     public customizationUUID:string;
52     public sourceModelInvariant:string;
53     public sourceModelName:string;
54     public sourceModelUid:string;
55     public sourceModelUuid:string;
56     //custom properties
57     public certified:boolean;
58     public iconSprite:string;
59     public inputs:Array<InputModel>;
60     public properties:Array<PropertyModel>;
61     public groupInstances:Array<Module>;
62     public invariantName:string;
63
64     constructor(componentInstance?:ComponentInstance) {
65
66         if (componentInstance) {
67             this.componentUid = componentInstance.componentUid;
68             this.componentName = componentInstance.componentName;
69
70             this.componentVersion = componentInstance.componentVersion;
71             this.description = componentInstance.description;
72             this.icon = componentInstance.icon;
73             this.name = componentInstance.name;
74             this.normalizedName = componentInstance.normalizedName;
75             this.originType = componentInstance.originType;
76             this.deploymentArtifacts = new ArtifactGroupModel(componentInstance.deploymentArtifacts);
77             this.artifacts = new ArtifactGroupModel(componentInstance.artifacts);
78             this.uniqueId = componentInstance.uniqueId;
79             this.creationTime = componentInstance.creationTime;
80             this.modificationTime = componentInstance.modificationTime;
81             this.propertyValueCounter = componentInstance.propertyValueCounter;
82             this.capabilities = new CapabilitiesGroup(componentInstance.capabilities);
83             this.requirements = new RequirementsGroup(componentInstance.requirements);
84             this.certified = componentInstance.certified;
85             this.customizationUUID = componentInstance.customizationUUID;
86             this.updatePosition(componentInstance.posX, componentInstance.posY);
87             this.groupInstances = componentInstance.groupInstances;
88             this.invariantName = componentInstance.invariantName;
89             this.sourceModelInvariant = componentInstance.sourceModelInvariant;
90             this.sourceModelName = componentInstance.sourceModelName;
91             this.sourceModelUid = componentInstance.sourceModelUid;
92             this.sourceModelUuid = componentInstance.sourceModelUuid;
93         }
94     }
95
96     public isUcpe = ():boolean => {
97         if (this.originType === 'VF' && this.capabilities && this.capabilities['tosca.capabilities.Container'] && this.name.toLowerCase().indexOf('ucpe') > -1) {
98             return true;
99         }
100         return false;
101     };
102
103     public isVl = ():boolean => {
104         return this.originType === 'VL';
105     };
106
107     public isComplex = () : boolean => {
108         return this.originType === ResourceType.VF || this.originType === ResourceType.PNF || this.originType === ResourceType.CVFC || this.originType === ResourceType.CR ;
109     }
110
111     public isServiceProxy = () :boolean => {
112         return this.originType === ComponentType.SERVICE_PROXY;
113     }
114
115     public setInstanceRC = ():void=> {
116         _.forEach(this.requirements, (requirementValue:Array<any>, requirementKey)=> {
117             _.forEach(requirementValue, (requirement)=> {
118                 if (!requirement.ownerName) {
119                     requirement['ownerId'] = this.uniqueId;
120                     requirement['ownerName'] = this.name;
121                 }
122             });
123         });
124         _.forEach(this.capabilities, (capabilityValue:Array<any>, capabilityKey)=> {
125             _.forEach(capabilityValue, (capability)=> {
126                 if (!capability.ownerName) {
127                     capability['ownerId'] = this.uniqueId;
128                     capability['ownerName'] = this.name;
129                 }
130             });
131         });
132     };
133
134     public updatePosition(posX:number, posY:number) {
135         this.posX = posX;
136         this.posY = posY;
137     }
138
139     public findRequirement(reqType:string, uniqueId:string, ownerId:string, name:string):Requirement|undefined {
140         let requirement:Requirement = undefined;
141         const searchGroup = (reqType) ? [this.requirements[reqType]] : this.requirements;
142         _.some(_.keys(searchGroup), (searchType) => {
143             requirement = _.find<Requirement>(searchGroup[searchType], (req:Requirement) => (
144                 req.uniqueId === uniqueId && req.ownerId === ownerId && req.name === name
145             ));
146             return requirement;
147         });
148         return requirement;
149     }
150
151     public findCapability(capType:string, uniqueId:string, ownerId:string, name:string):Capability|undefined {
152         let capability:Capability = undefined;
153         const searchGroup = (capType) ? [this.capabilities[capType]] : this.capabilities;
154         _.some(_.keys(searchGroup), (searchType) => {
155             capability = _.find<Capability>(searchGroup[searchType], (cap:Capability) => (
156                 cap.uniqueId === uniqueId && cap.ownerId === ownerId && cap.name === name
157             ));
158             return capability;
159         });
160         return capability;
161     }
162
163     public toJSON = ():any => {
164         let temp = angular.copy(this);
165         temp.certified = undefined;
166         temp.iconSprite = undefined;
167         temp.inputs = undefined;
168         temp.groupInstances = undefined;
169         temp.properties = undefined;
170         temp.requirements = undefined;
171         temp.capabilities = undefined;
172         return temp;
173     };
174 }