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