Sync Integ to Master
[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 * as _ from "lodash";
26 import {ComponentType} from "../../utils/constants";
27 import {ComponentMetadata} from "../component-metadata";
28 import {PolicyMetadata} from "../policy-metadata";
29 import {GroupMetadata} from "../group-metadata";
30 import {RequirementsGroup} from "../requirement";
31 import {CapabilitiesGroup} from "../capability";
32
33 export enum LeftPaletteMetadataTypes {
34     Component,
35     Group,
36     Policy
37 }
38
39 export class LeftPaletteComponent {
40     uniqueId:string;
41     type:string;
42     displayName:string;
43     version:string;
44     mainCategory:string;
45     subCategory:string;
46     iconClass:string;
47     componentSubType:string;
48     searchFilterTerms:string;
49     certifiedIconClass:string;
50     icon:string;
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) {
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
98         if (component.categories && component.categories[0] && component.categories[0].subcategories && component.categories[0].subcategories[0]) {
99             this.mainCategory = component.categories[0].name;
100             this.subCategory = component.categories[0].subcategories[0].name;
101         } else {
102             this.mainCategory = 'Generic';
103             this.subCategory = 'Generic';
104         }
105
106         this.componentSubType = component.resourceType ? component.resourceType: 'SERVICE';
107
108         this.initDisplayName(component.name);
109         this.searchFilterTerms = (this.displayName + ' ' + component.description + ' ' + component.tags.join(' ')).toLowerCase() + ' ' + component.version;
110         this.initIconSprite(component.icon);
111         this.certifiedIconClass = component.lifecycleState != 'CERTIFIED' ? 'non-certified' : '';
112         if (component.icon === 'vl' || component.icon === 'cp') {
113             this.certifiedIconClass = this.certifiedIconClass + " " + 'smaller-icon';
114         }
115     }
116
117     private initGroup(group:GroupMetadata): void {
118         this.categoryType = LeftPaletteMetadataTypes.Group;
119
120         this.uniqueId = group.uniqueId;
121         this.displayName = group.type;
122         this.mainCategory = "Groups";
123         this.subCategory = "Groups";
124         this.iconClass = "sprite-group-icons group";
125         this.version = group.version;
126
127         this.type = group.type;
128         this.componentSubType = 'GROUP';
129
130         this.searchFilterTerms = this.displayName + ' ' + group.description + ' ' + group.version;
131     }
132
133     private initPolicy(policy:PolicyMetadata): void {
134         this.categoryType = LeftPaletteMetadataTypes.Policy;
135
136         this.uniqueId = policy.uniqueId;
137         this.displayName = policy.type;
138         this.mainCategory = "Policies";
139         this.subCategory = "Policies";
140         this.iconClass = "sprite-policy-icons policy";
141         this.version = policy.version;
142
143         this.type = policy.type;
144         this.componentSubType = 'POLICY';
145
146         this.searchFilterTerms = this.displayName + ' ' + policy.description + ' ' + policy.version;
147     }
148
149     public initDisplayName = (name:string):void => {
150         let newName =
151             _.last(_.last(_.last(_.last(_.last(_.last(_.last(_.last(name.split('tosca.nodes.'))
152                 .split('network.')).split('relationships.')).split('org.openecomp.')).split('resource.nfv.'))
153                 .split('nodes.module.')).split('cp.')).split('vl.'));
154         if (newName) {
155             this.displayName = newName;
156         } else {
157             this.displayName = name;
158         }
159     };
160
161     public initIconSprite = (icon:string):void => {
162         switch (this.componentSubType) {
163             case ComponentType.SERVICE:
164                 this.iconClass = "sprite-services-icons " + icon;
165                 break;
166             default:
167                 this.iconClass = "sprite-resource-icons " + icon;
168         }
169     }
170
171     public getComponentSubType = ():string => {
172         return this.componentSubType;
173     };
174 }