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, 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";
31 export class ComponentFactory {
34 'Sdc.Services.Components.ResourceService',
35 'Sdc.Services.Components.ServiceService',
36 'Sdc.Services.CacheService',
41 constructor(private ResourceService:ResourceService,
42 private ServiceService:ServiceService,
43 private cacheService:CacheService,
44 private $q:ng.IQService,
45 private ComponentServiceNg2: ComponentServiceNg2) {
48 public createComponent = (component:Component):Component => {
49 let newComponent:Component;
50 switch (component.componentType) {
53 newComponent = new Service(this.ServiceService, this.$q, <Service> component);
57 newComponent = new Resource(this.ResourceService, this.$q, <Resource> component);
64 public createService = (service:Service):Service => {
65 let newService:Service = new Service(this.ServiceService, this.$q, <Service> service);
69 public createResource = (resource:Resource):Resource => {
70 let newResource:Resource = new Resource(this.ResourceService, this.$q, <Resource> resource);
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;
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
92 public createFromCsarComponent = (csar:ICsarComponent):Component => {
93 let newResource:Resource = <Resource>this.createEmptyComponent(ComponentType.RESOURCE);
94 newResource.name = csar.vspName;
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.
103 let availableCategories = angular.copy(this.cacheService.get('resourceCategories'));
105 _.each(availableCategories, (main:IMainCategory)=> {
106 if (main.subcategories) {
107 allSubs = allSubs.concat(main.subcategories);
111 let selectedCategory:IMainCategory = _.find(availableCategories, function (main:IMainCategory) {
112 return main.uniqueId === csar.category;
115 let selectedSubCategory:ISubCategory = _.find(allSubs, (sub:ISubCategory)=> {
116 return sub.uniqueId === csar.subCategory;
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);
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
143 public createEmptyComponent = (componentType:string):Component => {
144 let newComponent:Component;
146 switch (componentType) {
147 case ComponentType.SERVICE_PROXY:
148 case ComponentType.SERVICE:
149 newComponent = new Service(this.ServiceService, this.$q);
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);
164 newComponent.componentType = componentType;
165 newComponent.tags = [];
166 newComponent.icon = DEFAULT_ICON;
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();
176 public createComponentOnServer = (componentObject:Component):ng.IPromise<Component> => {
177 let component:Component = this.createComponent(componentObject);
178 return component.createComponentOnServer();
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);
190 return deferred.promise;