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 * Created by obarda on 2/3/2016.
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";
36 export class Resource extends Component {
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;
47 // Onboarding parameters
48 public csarUUID:string;
49 public csarVersion:string;
50 public csarPackageType:string;
51 public packageId:string;
53 constructor(componentService:IResourceService, $q:ng.IQService, component?:Resource) {
54 super(componentService, $q, component);
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;
73 this.resourceType = ResourceType.VF;
76 this.componentService = componentService;
77 this.iconSprite = "sprite-resource-icons";
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();
90 public getComponentSubType = ():string => {
91 return this.resourceType;
94 public isComplex = ():boolean => {
95 return this.resourceType === ResourceType.VF || this.resourceType === ResourceType.PNF || this.resourceType === ResourceType.CVFC || this.resourceType === ResourceType.CR;
98 public isVl = ():boolean => {
99 return ResourceType.VL == this.resourceType;
102 public isCsarComponent = ():boolean => {
103 return !!this.csarUUID;
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);
113 let onError = (error:any):void => {
114 deferred.reject(error);
118 if (this.importedFile) {
119 this.payloadData = this.importedFile.base64;
120 this.payloadName = this.importedFile.filename;
122 this.componentService.createComponent(this).then(onSuccess, onError);
123 return deferred.promise;
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);
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);
137 let onError = (error:any):void => {
138 deferred.reject(error);
141 this.componentService.updateResourceGroupProperties(this.uniqueId, module.uniqueId, properties).then(onSuccess, onError);
142 return deferred.promise;
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;
151 getTypeUrl():string {
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";
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;
176 temp.selectedCategory = undefined;
177 temp.importedFile = undefined;