Sync Integ to Master
[sdc.git] / catalog-ui / src / app / services / onboarding-service.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 {Component, IComponent} from "../models/components/component";
23 import {ICsarComponent} from "../models/csar-component";
24 import {IAppConfigurtaion, IApi} from "../models/app-config";
25 import {IFileDownload} from "../models/file-download";
26 import {Resource} from "../models/components/resource";
27 import {ComponentFactory} from "../utils/component-factory";
28
29 interface IOnboardingService {
30     getOnboardingComponents():ng.IPromise<Array<IComponent>>;
31     getComponentFromCsarUuid(csarUuid:string):ng.IPromise<Component>;
32     downloadOnboardingCsar(packageId:string):ng.IPromise<IFileDownload>;
33 }
34
35 export class OnboardingService implements IOnboardingService {
36
37     static '$inject' = ['$http', '$q', 'sdcConfig', 'ComponentFactory'];
38     private api:IApi;
39
40     constructor(private $http:ng.IHttpService,
41                 private $q:ng.IQService,
42                 private sdcConfig:IAppConfigurtaion,
43                 private ComponentFactory:ComponentFactory) {
44         this.api = sdcConfig.api;
45     }
46
47     getOnboardingVSPs = ():ng.IPromise<Array<ICsarComponent>> =>{
48         let defer = this.$q.defer<Array<ICsarComponent>>();
49         this.$http.get(this.api.GET_onboarding)
50             .then((response:any) => {
51                 defer.resolve(response.data.results);
52             },(response) => {
53                 defer.reject(response);
54             });
55
56         return defer.promise;
57     };
58
59     getOnboardingComponents = ():ng.IPromise<Array<IComponent>> => {
60         let defer = this.$q.defer<Array<IComponent>>();
61         this.getOnboardingVSPs().then((onboardingComponents:Array<ICsarComponent>) => {
62                 let componentsList:Array<IComponent> = new Array();
63
64                 onboardingComponents.forEach((obc:ICsarComponent) => {
65                     let component:Component = this.ComponentFactory.createFromCsarComponent(obc);
66                     componentsList.push(component);
67                 });
68
69                 defer.resolve(componentsList);
70             },(response) => {
71                 defer.reject(response);
72             });
73
74         return defer.promise;
75     };
76
77     downloadOnboardingCsar = (packageId:string):ng.IPromise<IFileDownload> => {
78         let defer = this.$q.defer<IFileDownload>();
79         this.$http({
80             url: this.api.GET_onboarding + "/" + packageId,
81             method: "get",
82             responseType: "blob"
83         })
84             .then((response:any) => {
85                 defer.resolve(response.data);
86             }, (err) => {
87                 defer.reject(err);
88             });
89
90         return defer.promise;
91     };
92
93     getComponentFromCsarUuid = (csarUuid:string):ng.IPromise<Component> => {
94         let defer = this.$q.defer<Component>();
95         this.$http.get(this.api.root + this.api.GET_component_from_csar_uuid.replace(':csar_uuid', csarUuid))
96             .then((response:any) => {
97                 let component:Resource;
98                 // If the status is 400, this means that the component not found.
99                 // I do not want to return error from server, because a popup will appear in client with the error.
100                 // So returning success (200) with status 400.
101                 if (response.data.status !== 400) {
102                     component = new Resource(null, this.$q, <Resource>response.data);
103                 }
104                 defer.resolve(component);
105             },(response) => {
106                 defer.reject(response.data);
107             });
108
109         return defer.promise;
110     };
111
112 }