fix ui-ci tests
[sdc.git] / catalog-ui / src / app / ng2 / services / onboarding.service.ts
1 /**
2  * Created by rc2122 on 6/4/2018.
3  */
4 /*-
5  * ============LICENSE_START=======================================================
6  * SDC
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
13  *
14  *      http://www.apache.org/licenses/LICENSE-2.0
15  *
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=========================================================
22  */
23
24 'use strict';
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";
37
38 export interface OnboardingComponents {
39     listCount: number;
40     results: Array<ICsarComponent>
41
42
43 @Injectable()
44 export class OnboardingService {
45     private api:IApi;
46
47     constructor(protected http: HttpClient,
48                 private cacheService:CacheService,
49                 @Inject(SdcConfigToken) sdcConfig:ISdcConfig,
50                 private componentFactory: ComponentFactory) {
51                 this.api = sdcConfig.api;
52     }
53
54     getOnboardingVSPs = (): Observable<Array<ICsarComponent>> =>{
55         return this.http.get<OnboardingComponents>(this.api.GET_onboarding).map((onboardingVSPs) =>{
56             return onboardingVSPs.results
57         });
58     }
59
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);
66             });
67             return componentsMetadataList;
68         });
69     };
70     
71     public createFromCsarComponent = (csar:ICsarComponent): ComponentMetadata => {
72         let newMetadata = new ComponentMetadata();  
73         newMetadata.name = csar.vspName;
74
75         /**
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.
80          * @type {any}
81          */
82         let availableCategories = angular.copy(this.cacheService.get('resourceCategories'));
83         let allSubs = [];
84         _.each(availableCategories, (main:IMainCategory)=> {
85             if (main.subcategories) {
86                 allSubs = allSubs.concat(main.subcategories);
87             }
88         });
89
90         let selectedCategory:IMainCategory = _.find(availableCategories, function (main:IMainCategory) {
91             return main.uniqueId === csar.category;
92         });
93
94         let selectedSubCategory:ISubCategory = _.find(allSubs, (sub:ISubCategory)=> {
95             return sub.uniqueId === csar.subCategory;
96         });
97
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);
105         }
106
107         // Fill the component with details from CSAR
108
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";
124         return newMetadata;
125     };
126
127     downloadOnboardingCsar = (packageId:string):Observable<HttpResponse<Blob>> => {
128         return this.http.get(this.api.GET_onboarding + "/" + packageId, {observe: 'response', responseType: 'blob'});
129     };
130
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;
141                 }
142             });
143     };
144
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);
149         return newResource;
150     }
151 }