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