2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
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";
32 export class ComponentFactory {
35 'Sdc.Services.Components.ResourceService',
36 'Sdc.Services.Components.ServiceService',
37 'Sdc.Services.CacheService',
42 constructor(private ResourceService:ResourceService,
43 private ServiceService:ServiceService,
44 private cacheService:CacheService,
45 private $q:ng.IQService,
46 private ComponentServiceNg2: ComponentServiceNg2) {
49 public createComponent = (component:Component):Component => {
50 let newComponent:Component;
51 switch (component.componentType) {
54 newComponent = new Service(this.ServiceService, this.$q, <Service> component);
58 newComponent = new Resource(this.ResourceService, this.$q, <Resource> component);
65 public createService = (service:Service):Service => {
66 let newService:Service = new Service(this.ServiceService, this.$q, <Service> service);
70 public createResource = (resource:Resource):Resource => {
71 let newResource:Resource = new Resource(this.ResourceService, this.$q, <Resource> resource);
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;
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;
93 public createFromCsarComponent = (csar:ICsarComponent):Component => {
94 let newResource:Resource = <Resource>this.createEmptyComponent(ComponentType.RESOURCE);
95 newResource.name = csar.vspName;
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.
104 let availableCategories = angular.copy(this.cacheService.get('resourceCategories'));
106 _.each(availableCategories, (main:IMainCategory)=> {
107 if (main.subcategories) {
108 allSubs = allSubs.concat(main.subcategories);
112 let selectedCategory:IMainCategory = _.find(availableCategories, function (main:IMainCategory) {
113 return main.uniqueId === csar.category;
116 let selectedSubCategory:ISubCategory = _.find(allSubs, (sub:ISubCategory)=> {
117 return sub.uniqueId === csar.subCategory;
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);
129 // Fill the component with details from CSAR
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;
145 public createEmptyComponent = (componentType:string, resourceType?:string):Component => {
146 let newComponent:Component;
148 switch (componentType) {
149 case ComponentType.SERVICE_PROXY:
150 case ComponentType.SERVICE:
151 newComponent = new Service(this.ServiceService, this.$q);
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);
165 (<Resource> newComponent).resourceType = resourceType;
169 newComponent.componentType = componentType;
170 newComponent.tags = [];
171 newComponent.icon = DEFAULT_ICON;
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();
181 public createComponentOnServer = (componentObject:Component):ng.IPromise<Component> => {
182 let component:Component = this.createComponent(componentObject);
183 return component.createComponentOnServer();
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);
195 return deferred.promise;