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 {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";
29 interface IOnboardingService {
30 getOnboardingComponents():ng.IPromise<Array<IComponent>>;
31 getComponentFromCsarUuid(csarUuid:string):ng.IPromise<Component>;
32 downloadOnboardingCsar(packageId:string):ng.IPromise<IFileDownload>;
35 export class OnboardingService implements IOnboardingService {
37 static '$inject' = ['$http', '$q', 'sdcConfig', 'ComponentFactory'];
40 constructor(private $http:ng.IHttpService,
41 private $q:ng.IQService,
42 private sdcConfig:IAppConfigurtaion,
43 private ComponentFactory:ComponentFactory) {
44 this.api = sdcConfig.api;
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);
53 defer.reject(response);
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();
64 onboardingComponents.forEach((obc:ICsarComponent) => {
65 let component:Component = this.ComponentFactory.createFromCsarComponent(obc);
66 componentsList.push(component);
69 defer.resolve(componentsList);
71 defer.reject(response);
77 downloadOnboardingCsar = (packageId:string):ng.IPromise<IFileDownload> => {
78 let defer = this.$q.defer<IFileDownload>();
80 url: this.api.GET_onboarding + "/" + packageId,
84 .then((response:any) => {
85 defer.resolve(response.data);
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);
104 defer.resolve(component);
106 defer.reject(response.data);
109 return defer.promise;