[SDC] rebase 1710 code
[sdc.git] / catalog-ui / src / app / models / components / resource.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 /**
22  * Created by obarda on 2/3/2016.
23  */
24 'use strict';
25 import {InstancesInputsOrPropertiesMapData} from "../instance-inputs-properties-map";
26 import {PropertyModel} from "../properties";
27 import {DisplayModule} from "../modules/base-module";
28 import {InputModel} from "../inputs";
29 import {ResourceType} from "../../utils/constants";
30 import {Component} from "./component";
31 import {FileUploadModel} from "../../directives/file-upload/file-upload";
32 import {IResourceService} from "../../services/components/resource-service";
33 import {ComponentMetadata} from "../component-metadata";
34
35 export class Resource extends Component {
36
37     public interfaces:any;
38     public derivedFrom:Array<string>;
39     public componentService:IResourceService;
40     public resourceType:string;
41     public payloadData:string;
42     public payloadName:string;
43     public importedFile:FileUploadModel;
44     public resourceVendorModelNumber:string;
45
46     // Onboarding parameters
47     public csarUUID:string;
48     public csarVersion:string;
49     public csarPackageType:string;
50     public packageId:string;
51
52     constructor(componentService:IResourceService, $q:ng.IQService, component?:Resource) {
53         super(componentService, $q, component);
54         if (component) {
55
56             this.interfaces = component.interfaces;
57             this.derivedFrom = component.derivedFrom;
58             this.payloadData = component.payloadData ? component.payloadData : undefined;
59             this.payloadName = component.payloadName ? component.payloadName : undefined;
60             this.resourceType = component.resourceType;
61             this.csarUUID = component.csarUUID;
62             this.csarVersion = component.csarVersion;
63             this.resourceVendorModelNumber = component.resourceVendorModelNumber;
64             this.filterTerm = this.name + ' ' + this.description + ' ' + (this.tags ? this.tags.toString() : '') + ' ' + this.version + ' ' + this.resourceType;
65             if (component.categories && component.categories[0] && component.categories[0].subcategories && component.categories[0].subcategories[0]) {
66                 component.mainCategory = component.categories[0].name;
67                 component.subCategory = component.categories[0].subcategories[0].name;
68                 this.selectedCategory = component.mainCategory + "_#_" + component.subCategory;
69                 this.importedFile = component.importedFile;
70             }
71         } else {
72             this.resourceType = ResourceType.VF;
73         }
74
75         this.componentService = componentService;
76         this.iconSprite = "sprite-resource-icons";
77     }
78
79     public setComponentMetadata(componentMetadata: ComponentMetadata) {
80         super.setComponentMetadata(componentMetadata);
81         this.resourceType = componentMetadata.resourceType;
82         this.csarUUID = componentMetadata.csarUUID;
83         this.csarVersion = componentMetadata.csarVersion;
84         this.derivedFrom = componentMetadata.derivedFrom;
85         this.resourceVendorModelNumber = componentMetadata.resourceVendorModelNumber;
86         this.setComponentDisplayData();
87     };
88
89     public getComponentSubType = ():string => {
90         return this.resourceType;
91     };
92
93     public isComplex = ():boolean => {
94         return this.resourceType === ResourceType.VF || this.resourceType === ResourceType.PNF || this.resourceType === ResourceType.CVFC;
95     };
96
97     public isVl = ():boolean => {
98         return ResourceType.VL == this.resourceType;
99     };
100
101     public isCsarComponent = ():boolean => {
102         return !!this.csarUUID;
103     };
104
105     public createComponentOnServer = ():ng.IPromise<Component> => {
106         let deferred = this.$q.defer();
107         let onSuccess = (component:Resource):void => {
108             this.payloadData = undefined;
109             this.payloadName = undefined;
110             deferred.resolve(component);
111         };
112         let onError = (error:any):void => {
113             deferred.reject(error);
114         };
115
116         this.handleTags();
117         if (this.importedFile) {
118             this.payloadData = this.importedFile.base64;
119             this.payloadName = this.importedFile.filename;
120         }
121         this.componentService.createComponent(this).then(onSuccess, onError);
122         return deferred.promise;
123     };
124
125
126     public updateResourceGroupProperties = (module:DisplayModule, properties:Array<PropertyModel>):ng.IPromise<Array<PropertyModel>> => {
127         let deferred = this.$q.defer();
128         let onSuccess = (updatedProperties:Array<PropertyModel>):void => {
129             _.forEach(updatedProperties, (property:PropertyModel) => { // Replace all updated properties on the module we needed to update
130                 _.extend(_.find(module.properties, {uniqueId: property.uniqueId}), property);
131
132             });
133             //_.extend(_.findWhere(this.groups, {uniqueId: module.uniqueId }), module); // replace the module on the component so all data will be updates if the module sent to the function is a copy
134             deferred.resolve(updatedProperties);
135         };
136         let onError = (error:any):void => {
137             deferred.reject(error);
138         };
139
140         this.componentService.updateResourceGroupProperties(this.uniqueId, module.uniqueId, properties).then(onSuccess, onError);
141         return deferred.promise;
142     };
143
144     // For now we only implement the logic in service level
145     public createInputsFormInstances = (instanceInputsPropertiesMap:InstancesInputsOrPropertiesMapData):ng.IPromise<Array<InputModel>> => {
146         let deferred = this.$q.defer();
147         return deferred.promise;
148     };
149
150     getTypeUrl():string {
151         return 'resources/';
152     }
153
154
155     setComponentDisplayData():void {
156         this.filterTerm = this.name + ' ' + this.description + ' ' + (this.tags ? this.tags.toString() : '') + ' ' + this.version + ' ' + this.resourceType;
157         if (this.categories && this.categories[0] && this.categories[0].subcategories && this.categories[0].subcategories[0]) {
158             this.mainCategory = this.categories[0].name;
159             this.subCategory = this.categories[0].subcategories[0].name;
160             this.selectedCategory = this.mainCategory + "_#_" + this.subCategory;
161             this.iconSprite = "sprite-resource-icons";
162         }
163     };
164
165     public toJSON = ():any => {
166         this.componentService = undefined;
167         this.filterTerm = undefined;
168         this.iconSprite = undefined;
169         this.mainCategory = undefined;
170         this.subCategory = undefined;
171         this.selectedInstance = undefined;
172         this.showMenu = undefined;
173         this.$q = undefined;
174         this.selectedCategory = undefined;
175         this.importedFile = undefined;
176         return this;
177     };
178 }
179
180