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