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/4/2016.
25 import * as _ from "lodash";
26 import {IServiceService} from "../../services/components/service-service";
27 import {Component, PropertyModel, DisplayModule, InputsAndProperties, InputModel, InstancesInputsOrPropertiesMapData, InstancesInputsPropertiesMap,
28 Distribution, DistributionComponent, ArtifactGroupModel} from "../../models";
29 import {ArtifactGroupType} from "../../utils/constants";
30 import {FileUploadModel} from "../../directives/file-upload/file-upload";
31 import {ComponentMetadata} from "../component-metadata";
32 import {ForwardingPath} from "app/models/forwarding-path";
34 export class Service extends Component {
36 public serviceApiArtifacts:ArtifactGroupModel;
37 public componentService:IServiceService;
38 public ecompGeneratedNaming:boolean;
39 public namingPolicy:string;
40 public serviceType:string;
41 public serviceRole:string;
42 public serviceFunction:string;
43 public environmentContext:string;
44 public instantiationType:string;
45 public forwardingPaths:{ [key:string]:ForwardingPath } = {};
46 public payloadData: string;
47 public payloadName: string;
48 public importedFile: FileUploadModel;
50 // Onboarding parameters
51 public csarUUID: string;
52 public csarVersion: string;
53 public csarPackageType: string;
54 public packageId: string;
56 constructor(componentService:IServiceService, $q:ng.IQService, component?:Service) {
57 super(componentService, $q, component);
58 this.ecompGeneratedNaming = true;
60 this.serviceApiArtifacts = new ArtifactGroupModel(component.serviceApiArtifacts);
61 this.filterTerm = this.name + ' ' + this.description + ' ' + (this.tags ? this.tags.toString() : '') + ' ' + this.version;
62 this.ecompGeneratedNaming = component.ecompGeneratedNaming;
63 this.namingPolicy = component.namingPolicy;
64 this.serviceType = component.serviceType;
65 this.serviceRole = component.serviceRole;
66 this.serviceFunction = component.serviceFunction;
67 this.instantiationType = component.instantiationType;
68 this.environmentContext = component.environmentContext;
69 this.payloadData = component.payloadData ? component.payloadData : undefined;
70 this.payloadName = component.payloadName ? component.payloadName : undefined;
71 this.csarUUID = component.csarUUID;
72 this.csarVersion = component.csarVersion;
73 if (component.categories && component.categories[0]) {
74 this.mainCategory = component.categories[0].name;
75 this.selectedCategory = this.mainCategory;
76 this.importedFile = component.importedFile;
79 this.componentService = componentService;
80 this.iconSprite = "sprite-services-icons";
83 public importComponentOnServer = (): ng.IPromise<Component> => {
84 let deferred = this.$q.defer<Component>();
85 let onSuccess = (component: Service): void => {
86 this.payloadData = undefined;
87 this.payloadName = undefined;
88 deferred.resolve(component);
90 let onError = (error: any): void => {
91 deferred.reject(error);
95 if (this.importedFile) {
96 this.payloadData = this.importedFile.base64;
97 this.payloadName = this.importedFile.filename;
99 this.componentService.importComponent(this).then(onSuccess, onError);
100 return deferred.promise;
103 public getDistributionsList = ():ng.IPromise<Array<Distribution>> => {
104 return this.componentService.getDistributionsList(this.uuid);
107 public getDistributionsComponent = (distributionId:string):ng.IPromise<Array<DistributionComponent>> => {
108 return this.componentService.getDistributionComponents(distributionId);
111 public markAsDeployed = (distributionId:string):ng.IPromise<any> => {
112 return this.componentService.markAsDeployed(this.uniqueId, distributionId);
115 /* we need to change the name of the input to vfInstanceName + input name before sending to server in order to create the inputs on the service
116 * we also need to remove already selected inputs (the inputs that already create on server, and disabled in the view - but they are selected so they are still in the view model
118 public createInputsFormInstances = (instancesInputsMap:InstancesInputsOrPropertiesMapData, instancePropertiesMap:InstancesInputsOrPropertiesMapData):ng.IPromise<Array<InputModel>> => {
120 let deferred = this.$q.defer<Array<InputModel>>();
121 let onSuccess = (inputsCreated:Array<InputModel>):void => {
122 this.inputs = inputsCreated.concat(this.inputs);
123 deferred.resolve(inputsCreated);
125 let onFailed = (error:any):void => {
126 deferred.reject(error);
129 let propertiesAndInputsMap:InstancesInputsPropertiesMap = new InstancesInputsPropertiesMap(instancesInputsMap, instancePropertiesMap);
130 propertiesAndInputsMap = propertiesAndInputsMap.cleanUnnecessaryDataBeforeSending(); // We need to create a copy of the map, without the already selected inputs / properties, and to send the clean map
131 this.componentService.createInputsFromInstancesInputs(this.uniqueId, propertiesAndInputsMap).then(onSuccess, onFailed);
132 return deferred.promise;
135 // we need to change the name of the input to vfInstanceName + input name before sending to server in order to create the inputs on the service
136 public getServiceInputInputsAndProperties = (inputId:string):ng.IPromise<InputsAndProperties> => {
137 let deferred = this.$q.defer<InputsAndProperties>();
138 let onSuccess = (inputsAndProperties:InputsAndProperties):void => {
139 let input:InputModel = _.find(this.inputs, (input:InputModel) => {
140 return input.uniqueId === inputId;
142 input.inputs = inputsAndProperties.inputs;
143 input.properties = inputsAndProperties.properties;
144 deferred.resolve(inputsAndProperties);
146 let onFailed = (error:any):void => {
147 deferred.reject(error);
149 this.componentService.getComponentInputInputsAndProperties(this.uniqueId, inputId).then(onSuccess, onFailed);
150 return deferred.promise;
153 public deleteServiceInput = (inputId:string):ng.IPromise<InputModel> => {
154 let deferred = this.$q.defer<InputModel>();
156 let onSuccess = (deletedInput:InputModel):void => {
157 delete _.remove(this.inputs, {uniqueId: deletedInput.uniqueId})[0];
158 deferred.resolve(deletedInput);
161 let onFailed = (error:any):void => {
162 deferred.reject(error);
165 this.componentService.deleteComponentInput(this.uniqueId, inputId).then(onSuccess, onFailed);
166 return deferred.promise;
169 public getArtifactsByType = (artifactGroupType:string):ArtifactGroupModel => {
170 switch (artifactGroupType) {
171 case ArtifactGroupType.DEPLOYMENT:
172 return this.deploymentArtifacts;
173 case ArtifactGroupType.INFORMATION:
174 return this.artifacts;
175 case ArtifactGroupType.SERVICE_API:
176 return this.serviceApiArtifacts;
180 public updateGroupInstanceProperties = (resourceInstanceId:string, group:DisplayModule, properties:Array<PropertyModel>):ng.IPromise<Array<PropertyModel>> => {
182 let deferred = this.$q.defer<Array<PropertyModel>>();
183 let onSuccess = (updatedProperties:Array<PropertyModel>):void => {
184 _.forEach(updatedProperties, (property:PropertyModel) => { // Replace all updated properties on the we needed to update
185 _.extend(_.find(group.properties, {uniqueId: property.uniqueId}), property);
187 deferred.resolve(updatedProperties);
189 let onError = (error:any):void => {
190 deferred.reject(error);
193 this.componentService.updateGroupInstanceProperties(this.uniqueId, resourceInstanceId, group.groupInstanceUniqueId, properties).then(onSuccess, onError);
194 return deferred.promise;
197 getTypeUrl():string {
202 public setComponentMetadata(componentMetadata: ComponentMetadata) {
203 super.setComponentMetadata(componentMetadata);
204 this.ecompGeneratedNaming = componentMetadata.ecompGeneratedNaming;
205 this.namingPolicy = componentMetadata.namingPolicy;
206 this.serviceType = componentMetadata.serviceType;
207 this.serviceRole = componentMetadata.serviceRole;
208 this.serviceFunction = componentMetadata.serviceFunction;
209 this.environmentContext = componentMetadata.environmentContext;
210 this.instantiationType = componentMetadata.instantiationType;
211 this.setComponentDisplayData();
214 setComponentDisplayData():void {
215 this.filterTerm = this.name + ' ' + this.description + ' ' + (this.tags ? this.tags.toString() : '') + ' ' + this.version;
216 if (this.categories && this.categories[0]) {
217 this.mainCategory = this.categories[0].name;
218 this.selectedCategory = this.mainCategory;
220 this.iconSprite = "sprite-services-icons";
223 public isSubstituteCandidate(): boolean {
224 return !!this.derivedFromGenericType;
227 public toJSON = ():any => {
228 let temp = angular.copy(this);
229 temp.componentService = undefined;
230 temp.filterTerm = undefined;
231 temp.iconSprite = undefined;
232 temp.mainCategory = undefined;
233 temp.subCategory = undefined;
234 temp.selectedInstance = undefined;
235 temp.showMenu = undefined;
237 temp.selectedCategory = undefined;
238 temp.modules = undefined;
239 temp.groupInstances = undefined;
240 temp.policies = undefined;