re base code
[sdc.git] / catalog-ui / src / app / models / components / displayComponent.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  * Created by obarda on 7/5/2016.
22  */
23
24 'use strict';
25 import {ComponentType} from "../../utils/constants";
26 import {ComponentMetadata} from "../component-metadata";
27 import {PolicyMetadata} from "../policy-metadata";
28 import {GroupMetadata} from "../group-metadata";
29 import {RequirementsGroup} from "../requirement";
30 import {CapabilitiesGroup} from "../capability";
31
32 export enum LeftPaletteMetadataTypes {
33     Component,
34     Group,
35     Policy
36 }
37
38 export class LeftPaletteComponent {
39     uniqueId:string;
40     type:string;
41     displayName:string;
42     version:string;
43     mainCategory:string;
44     subCategory:string;
45     iconClass:string;
46     componentSubType:string;
47     searchFilterTerms:string;
48     certifiedIconClass:string;
49     icon:string;
50     isDraggable:boolean;
51     isRequirmentAndCapabilitiesLoaded:boolean;
52
53     uuid:string;
54     name:string;
55     lifecycleState:string;
56     allVersions:any;
57     componentType:string;
58     systemName:string;
59
60     invariantUUID:string;
61
62     capabilities:CapabilitiesGroup;
63     requirements:RequirementsGroup;
64
65     categoryType:LeftPaletteMetadataTypes;
66
67     constructor(metadataType: LeftPaletteMetadataTypes, item: ComponentMetadata | PolicyMetadata | GroupMetadata) {
68         if (metadataType === LeftPaletteMetadataTypes.Policy) {
69             this.initPolicy(item as PolicyMetadata);
70             return;
71         }
72
73         if (metadataType === LeftPaletteMetadataTypes.Group) {
74             this.initGroup(item as GroupMetadata);
75             return;
76         }
77
78         if (metadataType === LeftPaletteMetadataTypes.Component) {
79             this.initComponent(item as ComponentMetadata);
80             return;
81         }
82     }
83
84     private initComponent(component:ComponentMetadata): void {
85         this.categoryType = LeftPaletteMetadataTypes.Component;
86
87         this.icon = component.icon;
88         this.version = component.version;
89         this.uniqueId = component.uniqueId;
90         this.isRequirmentAndCapabilitiesLoaded = false;
91         this.uuid = component.uuid;
92         this.name = component.name;
93         this.allVersions = component.allVersions;
94         this.componentType = component.componentType;
95         this.systemName = component.systemName;
96         this.invariantUUID = component.invariantUUID;
97         this.isDraggable = true;
98
99         if (component.categories && component.categories[0] && component.categories[0].subcategories && component.categories[0].subcategories[0]) {
100             this.mainCategory = component.categories[0].name;
101             this.subCategory = component.categories[0].subcategories[0].name;
102         } else {
103             this.mainCategory = 'Generic';
104             this.subCategory = 'Generic';
105         }
106
107         this.componentSubType = component.resourceType ? component.resourceType: 'SERVICE';
108
109         this.initDisplayName(component.name);
110         this.searchFilterTerms = (this.displayName + ' ' + component.description + ' ' + component.tags.join(' ')).toLowerCase() + ' ' + component.version;
111         this.initIconSprite(component.icon);
112         this.certifiedIconClass = component.lifecycleState != 'CERTIFIED' ? 'non-certified' : '';
113         if (component.icon === 'vl' || component.icon === 'cp') {
114             this.certifiedIconClass = this.certifiedIconClass + " " + 'smaller-icon';
115         }
116     }
117
118     private initGroup(group:GroupMetadata): void {
119         this.categoryType = LeftPaletteMetadataTypes.Group;
120
121         this.uniqueId = group.uniqueId;
122         this.displayName = group.name;
123         this.mainCategory = "Groups";
124         this.subCategory = "Groups";
125         this.iconClass = "sprite-group-icons group";
126         this.version = group.version;
127
128         this.type = group.type;
129         this.componentSubType = 'GROUP';
130
131         this.searchFilterTerms = this.type + ' ' + group.name + ' ' + group.version;
132         this.isDraggable = false;
133     }
134
135     private initPolicy(policy:PolicyMetadata): void {
136         this.categoryType = LeftPaletteMetadataTypes.Policy;
137
138         this.uniqueId = policy.uniqueId;
139         this.displayName = policy.name;
140         this.mainCategory = "Policies";
141         this.subCategory = "Policies";
142         this.iconClass = "sprite-policy-icons policy";
143         this.version = policy.version;
144
145         this.type = policy.type;
146         this.componentSubType = 'POLICY';
147
148         this.searchFilterTerms = this.type + ' ' + policy.name + ' ' + policy.version;
149         this.isDraggable = false;
150     }
151
152     public initDisplayName = (name:string):void => {
153         let newName =
154             _.last(_.last(_.last(_.last(_.last(_.last(_.last(_.last(name.split('tosca.nodes.'))
155                 .split('network.')).split('relationships.')).split('org.openecomp.')).split('resource.nfv.'))
156                 .split('nodes.module.')).split('cp.')).split('vl.'));
157         if (newName) {
158             this.displayName = newName;
159         } else {
160             this.displayName = name;
161         }
162     };
163
164     public initIconSprite = (icon:string):void => {
165         switch (this.componentSubType) {
166             case ComponentType.SERVICE:
167                 this.iconClass = "sprite-services-icons " + icon;
168                 break;
169             default:
170                 this.iconClass = "sprite-resource-icons " + icon;
171         }
172     }
173
174     public getComponentSubType = ():string => {
175         return this.componentSubType;
176     };
177 }