re base code
[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     public originArchived:boolean;
64
65     constructor(componentInstance?:ComponentInstance) {
66
67         if (componentInstance) {
68             this.componentUid = componentInstance.componentUid;
69             this.componentName = componentInstance.componentName;
70
71             this.componentVersion = componentInstance.componentVersion;
72             this.description = componentInstance.description;
73             this.icon = componentInstance.icon;
74             this.name = componentInstance.name;
75             this.normalizedName = componentInstance.normalizedName;
76             this.originType = componentInstance.originType;
77             this.deploymentArtifacts = new ArtifactGroupModel(componentInstance.deploymentArtifacts);
78             this.artifacts = new ArtifactGroupModel(componentInstance.artifacts);
79             this.uniqueId = componentInstance.uniqueId;
80             this.creationTime = componentInstance.creationTime;
81             this.modificationTime = componentInstance.modificationTime;
82             this.propertyValueCounter = componentInstance.propertyValueCounter;
83             this.capabilities = new CapabilitiesGroup(componentInstance.capabilities);
84             this.requirements = new RequirementsGroup(componentInstance.requirements);
85             this.certified = componentInstance.certified;
86             this.customizationUUID = componentInstance.customizationUUID;
87             this.updatePosition(componentInstance.posX, componentInstance.posY);
88             this.groupInstances = componentInstance.groupInstances;
89             this.invariantName = componentInstance.invariantName;
90             this.sourceModelInvariant = componentInstance.sourceModelInvariant;
91             this.sourceModelName = componentInstance.sourceModelName;
92             this.sourceModelUid = componentInstance.sourceModelUid;
93             this.sourceModelUuid = componentInstance.sourceModelUuid;
94             this.originArchived = componentInstance.originArchived;
95         }
96     }
97
98     public isUcpe = ():boolean => {
99         if (this.originType === 'VF' && this.capabilities && this.capabilities['tosca.capabilities.Container'] && this.name.toLowerCase().indexOf('ucpe') > -1) {
100             return true;
101         }
102         return false;
103     };
104
105     public isVl = ():boolean => {
106         return this.originType === 'VL';
107     };
108
109     public isComplex = () : boolean => {
110         return this.originType === ResourceType.VF || this.originType === ResourceType.PNF || this.originType === ResourceType.CVFC || this.originType === ResourceType.CR ;
111     }
112
113     public isServiceProxy = () :boolean => {
114         return this.originType === ComponentType.SERVICE_PROXY;
115     }
116
117     public setInstanceRC = ():void=> {
118         _.forEach(this.requirements, (requirementValue:Array<any>, requirementKey)=> {
119             _.forEach(requirementValue, (requirement)=> {
120                 if (!requirement.ownerName) {
121                     requirement['ownerId'] = this.uniqueId;
122                     requirement['ownerName'] = this.name;
123                 }
124             });
125         });
126         _.forEach(this.capabilities, (capabilityValue:Array<any>, capabilityKey)=> {
127             _.forEach(capabilityValue, (capability)=> {
128                 if (!capability.ownerName) {
129                     capability['ownerId'] = this.uniqueId;
130                     capability['ownerName'] = this.name;
131                 }
132             });
133         });
134     };
135
136     public updatePosition(posX:number, posY:number) {
137         this.posX = posX;
138         this.posY = posY;
139     }
140
141     public findRequirement(reqType:string, uniqueId:string, ownerId:string, name:string):Requirement|undefined {
142         let requirement:Requirement = undefined;
143         const searchGroup = (reqType) ? [this.requirements[reqType]] : this.requirements;
144         _.some(_.keys(searchGroup), (searchType) => {
145             requirement = _.find<Requirement>(searchGroup[searchType], (req:Requirement) => (
146                 req.uniqueId === uniqueId && req.ownerId === ownerId && req.name === name
147             ));
148             return requirement;
149         });
150         return requirement;
151     }
152
153     public findCapability(capType:string, uniqueId:string, ownerId:string, name:string):Capability|undefined {
154         let capability:Capability = undefined;
155         const searchGroup = (capType) ? [this.capabilities[capType]] : this.capabilities;
156         _.some(_.keys(searchGroup), (searchType) => {
157             capability = _.find<Capability>(searchGroup[searchType], (cap:Capability) => (
158                 cap.uniqueId === uniqueId && cap.ownerId === ownerId && cap.name === name
159             ));
160             return capability;
161         });
162         return capability;
163     }
164
165     public toJSON = ():any => {
166         let temp = angular.copy(this);
167         temp.certified = undefined;
168         temp.iconSprite = undefined;
169         temp.inputs = undefined;
170         temp.groupInstances = undefined;
171         temp.properties = undefined;
172         temp.requirements = undefined;
173         temp.capabilities = undefined;
174         return temp;
175     };
176
177     public get iconClass() {
178         return this.iconSprite + ' ' + this.icon;
179     }
180 }