2 * Created by rc2122 on 6/4/2018.
5 * ============LICENSE_START=======================================================
7 * ================================================================================
8 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
9 * ================================================================================
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 * ============LICENSE_END=========================================================
25 import {Inject, Injectable} from "@angular/core";
26 import {SdcConfigToken, ISdcConfig} from "app/ng2/config/sdc-config.config";
27 import {Observable} from "rxjs/Observable";
28 import { HttpClient, HttpResponse } from "@angular/common/http";
29 import { ComponentFactory } from "../../utils/component-factory";
30 import { DEFAULT_ICON, ComponentType } from "../../utils/constants";
31 import { ICsarComponent } from "../../models/csar-component";
32 import { IApi } from "../../models/app-config";
33 import { CacheService } from "./cache.service";
34 import { IComponentMetadata, ComponentMetadata } from "../../models/component-metadata";
35 import { IMainCategory, ISubCategory } from "../../models/category";
36 import { Resource } from "../../models/components/resource";
38 export interface OnboardingComponents {
40 results: Array<ICsarComponent>
44 export class OnboardingService {
47 constructor(protected http: HttpClient,
48 private cacheService:CacheService,
49 @Inject(SdcConfigToken) sdcConfig:ISdcConfig,
50 private componentFactory: ComponentFactory) {
51 this.api = sdcConfig.api;
54 getOnboardingVSPs = (): Observable<Array<ICsarComponent>> =>{
55 return this.http.get<OnboardingComponents>(this.api.GET_onboarding).map((onboardingVSPs) =>{
56 return onboardingVSPs.results
60 getOnboardingComponents = ():Observable<Array<IComponentMetadata>> => {
61 return this.getOnboardingVSPs().map((onboardingComponents) => {
62 let componentsMetadataList: Array<IComponentMetadata> = new Array();
63 onboardingComponents.forEach((obc:ICsarComponent) => {
64 let componentMetaData: ComponentMetadata = this.createFromCsarComponent(obc);
65 componentsMetadataList.push(componentMetaData);
67 return componentsMetadataList;
71 public createFromCsarComponent = (csar:ICsarComponent): ComponentMetadata => {
72 let newMetadata = new ComponentMetadata();
73 newMetadata.name = csar.vspName;
76 * Onboarding CSAR contains category and sub category that are uniqueId.
77 * Need to find the category and sub category and extract the name from them.
78 * First concat all sub categories to one array.
79 * Then find the selected sub category and category.
82 let availableCategories = angular.copy(this.cacheService.get('resourceCategories'));
84 _.each(availableCategories, (main:IMainCategory)=> {
85 if (main.subcategories) {
86 allSubs = allSubs.concat(main.subcategories);
90 let selectedCategory:IMainCategory = _.find(availableCategories, function (main:IMainCategory) {
91 return main.uniqueId === csar.category;
94 let selectedSubCategory:ISubCategory = _.find(allSubs, (sub:ISubCategory)=> {
95 return sub.uniqueId === csar.subCategory;
98 // Build the categories and sub categories array (same format as component category)
99 let categories:Array<IMainCategory> = new Array();
100 let subcategories:Array<ISubCategory> = new Array();
101 if (selectedCategory && selectedSubCategory) {
102 subcategories.push(selectedSubCategory);
103 selectedCategory.subcategories = subcategories;
104 categories.push(selectedCategory);
107 // Fill the component with details from CSAR
109 newMetadata.categories = categories;
110 newMetadata.vendorName = csar.vendorName;
111 newMetadata.vendorRelease = csar.vendorRelease;
112 newMetadata.csarUUID = csar.packageId;
113 newMetadata.csarPackageType = csar.packageType;
114 newMetadata.csarVersion = csar.version;
115 newMetadata.packageId = csar.packageId;
116 newMetadata.description = csar.description;
117 newMetadata.selectedCategory = selectedCategory && selectedSubCategory ? selectedCategory.name + "_#_" + selectedSubCategory.name : '';
118 newMetadata.filterTerm = newMetadata.name + ' ' + newMetadata.description + ' ' + newMetadata.vendorName + ' ' + newMetadata.csarVersion;
119 newMetadata.resourceType = csar.resourceType;
120 newMetadata.componentType = ComponentType.RESOURCE;
121 newMetadata.tags = [];
122 newMetadata.icon = DEFAULT_ICON;
123 newMetadata.iconSprite = "sprite-resource-icons";
127 downloadOnboardingCsar = (packageId:string):Observable<HttpResponse<Blob>> => {
128 return this.http.get(this.api.GET_onboarding + "/" + packageId, {observe: 'response', responseType: 'blob'});
131 getComponentFromCsarUuid = (csarUuid:string):Observable<ComponentMetadata> => {
132 return this.http.get<ComponentMetadata>(this.api.root + this.api.GET_component_from_csar_uuid.replace(':csar_uuid', csarUuid))
133 .map((response: any) => {
134 // If the status is 400, this means that the component not found.
135 // I do not want to return error from server, because a popup will appear in client with the error.
136 // So returning success (200) with status 400.
137 if (response.status !== 400) {
138 let componentMetadata = new ComponentMetadata();
139 componentMetadata = response;
140 return componentMetadata;
145 //TODO remove when workspace page convert to angular5
146 convertMetaDataToComponent(componentMetadata: ComponentMetadata) {
147 let newResource: Resource = <Resource>this.componentFactory.createEmptyComponent(ComponentType.RESOURCE);
148 newResource.setComponentMetadata(componentMetadata);