fix ui-ci tests
[sdc.git] / catalog-ui / src / app / utils / component-factory.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 'use strict';
22 import * as _ from "lodash";
23 import {DEFAULT_ICON, ResourceType, ComponentType} from "./constants";
24 import {ServiceService, ResourceService} from "app/services";
25 import {CacheService} from "app/services-ng2";
26 import {IMainCategory, ISubCategory, ICsarComponent, Component, Resource, Service} from "app/models";
27 import {ComponentMetadata} from "../models/component-metadata";
28 import {ComponentServiceNg2} from "../ng2/services/component-services/component.service";
29 import {ComponentGenericResponse} from "../ng2/services/responses/component-generic-response";
30
31
32 export class ComponentFactory {
33
34     static '$inject' = [
35         'Sdc.Services.Components.ResourceService',
36         'Sdc.Services.Components.ServiceService',
37         'Sdc.Services.CacheService',
38         '$q',
39         'ComponentServiceNg2'
40     ];
41
42     constructor(private ResourceService:ResourceService,
43                 private ServiceService:ServiceService,
44                 private cacheService:CacheService,
45                 private $q:ng.IQService,
46                 private ComponentServiceNg2: ComponentServiceNg2) {
47     }
48
49     public createComponent = (component:Component):Component => {
50         let newComponent:Component;
51         switch (component.componentType) {
52
53             case 'SERVICE':
54                 newComponent = new Service(this.ServiceService, this.$q, <Service> component);
55                 break;
56
57             case 'RESOURCE':
58                 newComponent = new Resource(this.ResourceService, this.$q, <Resource> component);
59                 break;
60
61         }
62         return newComponent;
63     };
64
65     public createService = (service:Service):Service => {
66         let newService:Service = new Service(this.ServiceService, this.$q, <Service> service);
67         return newService;
68     };
69
70     public createResource = (resource:Resource):Resource => {
71         let newResource:Resource = new Resource(this.ResourceService, this.$q, <Resource> resource);
72         return newResource;
73     };
74
75     public updateComponentFromCsar = (csarComponent:Resource, oldComponent: Resource): Component => {
76           _.pull(oldComponent.tags, oldComponent.name);
77           if (!oldComponent.isAlreadyCertified()) {
78             oldComponent.name = csarComponent.name;
79             oldComponent.categories = csarComponent.categories;
80             oldComponent.selectedCategory = csarComponent.selectedCategory;
81         }
82           oldComponent.vendorName = csarComponent.vendorName;
83           oldComponent.vendorRelease = csarComponent.vendorRelease;
84           oldComponent.csarUUID = csarComponent.csarUUID;
85           oldComponent.csarPackageType = csarComponent.csarPackageType;
86           oldComponent.csarVersion = csarComponent.csarVersion;
87           oldComponent.packageId = csarComponent.packageId;
88           oldComponent.description = csarComponent.description;
89           oldComponent.filterTerm = oldComponent.name +  ' '  + oldComponent.description + ' ' + oldComponent.vendorName + ' ' + oldComponent.csarVersion;
90           return oldComponent;
91     }
92
93     public createFromCsarComponent = (csar:ICsarComponent):Component => {
94         let newResource:Resource = <Resource>this.createEmptyComponent(ComponentType.RESOURCE);
95         newResource.name = csar.vspName;
96
97         /**
98          * Onboarding CSAR contains category and sub category that are uniqueId.
99          * Need to find the category and sub category and extract the name from them.
100          * First concat all sub categories to one array.
101          * Then find the selected sub category and category.
102          * @type {any}
103          */
104         let availableCategories = angular.copy(this.cacheService.get('resourceCategories'));
105         let allSubs = [];
106         _.each(availableCategories, (main:IMainCategory)=> {
107             if (main.subcategories) {
108                 allSubs = allSubs.concat(main.subcategories);
109             }
110         });
111
112         let selectedCategory:IMainCategory = _.find(availableCategories, function (main:IMainCategory) {
113             return main.uniqueId === csar.category;
114         });
115
116         let selectedSubCategory:ISubCategory = _.find(allSubs, (sub:ISubCategory)=> {
117             return sub.uniqueId === csar.subCategory;
118         });
119
120         // Build the categories and sub categories array (same format as component category)
121         let categories:Array<IMainCategory> = new Array();
122         let subcategories:Array<ISubCategory> = new Array();
123         if (selectedCategory && selectedSubCategory) {
124             subcategories.push(selectedSubCategory);
125             selectedCategory.subcategories = subcategories;
126             categories.push(selectedCategory);
127         }
128
129         // Fill the component with details from CSAR
130
131         newResource.categories = categories;
132         newResource.vendorName = csar.vendorName;
133         newResource.vendorRelease = csar.vendorRelease;
134         newResource.csarUUID = csar.packageId;
135         newResource.csarPackageType = csar.packageType;
136         newResource.csarVersion = csar.version;
137         newResource.packageId = csar.packageId;
138         newResource.description = csar.description;
139         newResource.resourceType = csar.resourceType;
140         newResource.selectedCategory = selectedCategory && selectedSubCategory ? selectedCategory.name + "_#_" + selectedSubCategory.name : '';
141         newResource.filterTerm = newResource.name +  ' '  + newResource.description + ' ' + newResource.vendorName + ' ' + newResource.csarVersion;
142         return newResource;
143     };
144
145     public createEmptyComponent = (componentType:string, resourceType?:string):Component => {
146         let newComponent:Component;
147
148         switch (componentType) {
149             case ComponentType.SERVICE_PROXY:
150             case ComponentType.SERVICE:
151                 newComponent = new Service(this.ServiceService, this.$q);
152                 break;
153
154             case ComponentType.RESOURCE:
155             case ResourceType.VF:
156             case ResourceType.VL:
157             case ResourceType.VFC:
158             case ResourceType.CP:
159             case ResourceType.CR:
160             case ResourceType.PNF:
161             case ResourceType.CVFC:
162             case ResourceType.CONFIGURATION:
163                 newComponent = new Resource(this.ResourceService, this.$q);
164                 if (resourceType){
165                     (<Resource> newComponent).resourceType = resourceType;
166                 }
167                 break;
168         }
169         newComponent.componentType = componentType;
170         newComponent.tags = [];
171         newComponent.icon = DEFAULT_ICON;
172         return newComponent;
173     };
174
175     public getComponentFromServer = (componentType:string, componentId:string):ng.IPromise<Component> => {
176         let newComponent:Component = this.createEmptyComponent(componentType);
177         newComponent.setUniqueId(componentId);
178         return newComponent.getComponent();
179     };
180
181     public createComponentOnServer = (componentObject:Component):ng.IPromise<Component> => {
182         let component:Component = this.createComponent(componentObject);
183         return component.createComponentOnServer();
184
185     };
186
187     public getComponentWithMetadataFromServer = (componentType:string, componentId:string):ng.IPromise<Component> => {
188         let deferred = this.$q.defer<Component>();
189         let component = this.createEmptyComponent(componentType);
190         component.setUniqueId(componentId);
191         this.ComponentServiceNg2.getComponentMetadata(component.uniqueId, component.componentType).subscribe((response:ComponentGenericResponse) => {
192             component.setComponentMetadata(response.metadata);
193             deferred.resolve(component);
194         });
195         return deferred.promise;
196     }
197 }