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 {ComponentMetadata} from "../component-metadata";
31 import {ForwardingPath} from "app/models/forwarding-path";
33 export class Service extends Component {
35 public serviceApiArtifacts:ArtifactGroupModel;
36 public componentService:IServiceService;
37 public ecompGeneratedNaming:boolean;
38 public namingPolicy:string;
39 public serviceType:string;
40 public serviceRole:string;
41 public environmentContext:string;
42 public instantiationType:string;
43 public forwardingPaths:{ [key:string]:ForwardingPath } = {};
45 constructor(componentService:IServiceService, $q:ng.IQService, component?:Service) {
46 super(componentService, $q, component);
47 this.ecompGeneratedNaming = true;
49 this.serviceApiArtifacts = new ArtifactGroupModel(component.serviceApiArtifacts);
50 this.filterTerm = this.name + ' ' + this.description + ' ' + (this.tags ? this.tags.toString() : '') + ' ' + this.version;
51 this.ecompGeneratedNaming = component.ecompGeneratedNaming;
52 this.namingPolicy = component.namingPolicy;
53 this.serviceType = component.serviceType;
54 this.serviceRole = component.serviceRole;
55 this.instantiationType = component.instantiationType;
56 this.environmentContext = component.environmentContext;
57 if (component.categories && component.categories[0]) {
58 this.mainCategory = component.categories[0].name;
59 this.selectedCategory = this.mainCategory;
62 this.componentService = componentService;
63 this.iconSprite = "sprite-services-icons";
66 public getDistributionsList = ():ng.IPromise<Array<Distribution>> => {
67 return this.componentService.getDistributionsList(this.uuid);
70 public getDistributionsComponent = (distributionId:string):ng.IPromise<Array<DistributionComponent>> => {
71 return this.componentService.getDistributionComponents(distributionId);
74 public markAsDeployed = (distributionId:string):ng.IPromise<any> => {
75 return this.componentService.markAsDeployed(this.uniqueId, distributionId);
78 /* 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
79 * 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
81 public createInputsFormInstances = (instancesInputsMap:InstancesInputsOrPropertiesMapData, instancePropertiesMap:InstancesInputsOrPropertiesMapData):ng.IPromise<Array<InputModel>> => {
83 let deferred = this.$q.defer<Array<InputModel>>();
84 let onSuccess = (inputsCreated:Array<InputModel>):void => {
85 this.inputs = inputsCreated.concat(this.inputs);
86 deferred.resolve(inputsCreated);
88 let onFailed = (error:any):void => {
89 deferred.reject(error);
92 let propertiesAndInputsMap:InstancesInputsPropertiesMap = new InstancesInputsPropertiesMap(instancesInputsMap, instancePropertiesMap);
93 propertiesAndInputsMap = propertiesAndInputsMap.cleanUnnecessaryDataBeforeSending(); // We need to create a copy of the map, without the already selected inputs / properties, and to send the clean map
94 this.componentService.createInputsFromInstancesInputs(this.uniqueId, propertiesAndInputsMap).then(onSuccess, onFailed);
95 return deferred.promise;
98 // 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
99 public getServiceInputInputsAndProperties = (inputId:string):ng.IPromise<InputsAndProperties> => {
100 let deferred = this.$q.defer<InputsAndProperties>();
101 let onSuccess = (inputsAndProperties:InputsAndProperties):void => {
102 let input:InputModel = _.find(this.inputs, (input:InputModel) => {
103 return input.uniqueId === inputId;
105 input.inputs = inputsAndProperties.inputs;
106 input.properties = inputsAndProperties.properties;
107 deferred.resolve(inputsAndProperties);
109 let onFailed = (error:any):void => {
110 deferred.reject(error);
112 this.componentService.getComponentInputInputsAndProperties(this.uniqueId, inputId).then(onSuccess, onFailed);
113 return deferred.promise;
116 public deleteServiceInput = (inputId:string):ng.IPromise<InputModel> => {
117 let deferred = this.$q.defer<InputModel>();
119 let onSuccess = (deletedInput:InputModel):void => {
120 delete _.remove(this.inputs, {uniqueId: deletedInput.uniqueId})[0];
121 deferred.resolve(deletedInput);
124 let onFailed = (error:any):void => {
125 deferred.reject(error);
128 this.componentService.deleteComponentInput(this.uniqueId, inputId).then(onSuccess, onFailed);
129 return deferred.promise;
132 public getArtifactsByType = (artifactGroupType:string):ArtifactGroupModel => {
133 switch (artifactGroupType) {
134 case ArtifactGroupType.DEPLOYMENT:
135 return this.deploymentArtifacts;
136 case ArtifactGroupType.INFORMATION:
137 return this.artifacts;
138 case ArtifactGroupType.SERVICE_API:
139 return this.serviceApiArtifacts;
143 public updateGroupInstanceProperties = (resourceInstanceId:string, group:DisplayModule, properties:Array<PropertyModel>):ng.IPromise<Array<PropertyModel>> => {
145 let deferred = this.$q.defer<Array<PropertyModel>>();
146 let onSuccess = (updatedProperties:Array<PropertyModel>):void => {
147 _.forEach(updatedProperties, (property:PropertyModel) => { // Replace all updated properties on the we needed to update
148 _.extend(_.find(group.properties, {uniqueId: property.uniqueId}), property);
150 deferred.resolve(updatedProperties);
152 let onError = (error:any):void => {
153 deferred.reject(error);
156 this.componentService.updateGroupInstanceProperties(this.uniqueId, resourceInstanceId, group.groupInstanceUniqueId, properties).then(onSuccess, onError);
157 return deferred.promise;
160 getTypeUrl():string {
165 public setComponentMetadata(componentMetadata: ComponentMetadata) {
166 super.setComponentMetadata(componentMetadata);
167 this.ecompGeneratedNaming = componentMetadata.ecompGeneratedNaming;
168 this.namingPolicy = componentMetadata.namingPolicy;
169 this.serviceType = componentMetadata.serviceType;
170 this.serviceRole = componentMetadata.serviceRole;
171 this.environmentContext = componentMetadata.environmentContext;
172 this.instantiationType = componentMetadata.instantiationType;
173 this.setComponentDisplayData();
176 setComponentDisplayData():void {
177 this.filterTerm = this.name + ' ' + this.description + ' ' + (this.tags ? this.tags.toString() : '') + ' ' + this.version;
178 if (this.categories && this.categories[0]) {
179 this.mainCategory = this.categories[0].name;
180 this.selectedCategory = this.mainCategory;
182 this.iconSprite = "sprite-services-icons";
185 public toJSON = ():any => {
186 let temp = angular.copy(this);
187 temp.componentService = undefined;
188 temp.filterTerm = undefined;
189 temp.iconSprite = undefined;
190 temp.mainCategory = undefined;
191 temp.subCategory = undefined;
192 temp.selectedInstance = undefined;
193 temp.showMenu = undefined;
195 temp.selectedCategory = undefined;
196 temp.modules = undefined;
197 temp.groupInstances = undefined;