blob: db6c72699a05dec6d9579a9a79b1e47aad60e5a3 [file] [log] [blame]
Michael Landodd603392017-07-12 00:54:52 +03001/*-
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
Michael Landoed64b5e2017-06-09 03:19:04 +030021'use strict';
22import {Component, IComponent} from "../models/components/component";
23import {ICsarComponent} from "../models/csar-component";
24import {IAppConfigurtaion, IApi} from "../models/app-config";
25import {IFileDownload} from "../models/file-download";
26import {Resource} from "../models/components/resource";
27import {ComponentFactory} from "../utils/component-factory";
28
29interface IOnboardingService {
30 getOnboardingComponents():ng.IPromise<Array<IComponent>>;
31 getComponentFromCsarUuid(csarUuid:string):ng.IPromise<Component>;
32 downloadOnboardingCsar(packageId:string):ng.IPromise<IFileDownload>;
33}
34
35export 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 getOnboardingComponents = ():ng.IPromise<Array<IComponent>> => {
48 let defer = this.$q.defer<Array<IComponent>>();
49 this.$http.get(this.api.GET_onboarding)
50 .then((response:any) => {
51 let onboardingComponents:Array<ICsarComponent> = response.data.results;
52 let componentsList:Array<IComponent> = new Array();
53
54 onboardingComponents.forEach((obc:ICsarComponent) => {
55 let component:Component = this.ComponentFactory.createFromCsarComponent(obc);
56 componentsList.push(component);
57 });
58
59 defer.resolve(componentsList);
60 },(response) => {
61 defer.reject(response);
62 });
63
64 return defer.promise;
65 };
66
67 downloadOnboardingCsar = (packageId:string):ng.IPromise<IFileDownload> => {
68 let defer = this.$q.defer();
69 this.$http({
70 url: this.api.GET_onboarding + "/" + packageId,
71 method: "get",
72 responseType: "blob"
73 })
74 .then((response:any) => {
75 defer.resolve(response.data);
76 }, (err) => {
77 defer.reject(err);
78 });
79
80 return defer.promise;
81 };
82
83 getComponentFromCsarUuid = (csarUuid:string):ng.IPromise<Component> => {
84 let defer = this.$q.defer<Component>();
85 this.$http.get(this.api.root + this.api.GET_component_from_csar_uuid.replace(':csar_uuid', csarUuid))
86 .then((response:any) => {
87 let component:Resource;
88 // If the status is 400, this means that the component not found.
89 // I do not want to return error from server, because a popup will appear in client with the error.
90 // So returning success (200) with status 400.
91 if (response.data.status !== 400) {
92 component = new Resource(null, this.$q, <Resource>response.data);
93 }
94 defer.resolve(component);
95 },(response) => {
96 defer.reject(response.data);
97 });
98
99 return defer.promise;
100 };
101
102}