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 csarVersionId: string;
51 public csarPackageType:string;
52 public packageId:string;
54 constructor(componentService:IResourceService, $q:ng.IQService, component?:Resource) {
55 super(componentService, $q, component);
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;
75 this.resourceType = ResourceType.VF;
78 this.componentService = componentService;
79 this.iconSprite = "sprite-resource-icons";
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();
93 public getComponentSubType = ():string => {
94 return this.resourceType;
97 public isComplex = ():boolean => {
98 return this.resourceType === ResourceType.VF || this.resourceType === ResourceType.PNF || this.resourceType === ResourceType.CVFC || this.resourceType === ResourceType.CR;
101 public isVl = ():boolean => {
102 return ResourceType.VL == this.resourceType;
105 public isCsarComponent = ():boolean => {
106 return !!this.csarUUID;
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);
116 let onError = (error:any):void => {
117 deferred.reject(error);
121 if (this.importedFile) {
122 this.payloadData = this.importedFile.base64;
123 this.payloadName = this.importedFile.filename;
125 this.componentService.createComponent(this).then(onSuccess, onError);
126 return deferred.promise;
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);
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);
140 let onError = (error:any):void => {
141 deferred.reject(error);
144 this.componentService.updateResourceGroupProperties(this.uniqueId, module.uniqueId, properties).then(onSuccess, onError);
145 return deferred.promise;
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;
154 getTypeUrl():string {
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";
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;
179 temp.selectedCategory = undefined;
180 temp.importedFile = undefined;
181 temp.modules = undefined;
182 temp.groupInstances = undefined;
183 temp.policies = undefined;