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 forwardingPaths:{ [key:string]:ForwardingPath } = {};
44 constructor(componentService:IServiceService, $q:ng.IQService, component?:Service) {
45 super(componentService, $q, component);
46 this.ecompGeneratedNaming = true;
48 this.serviceApiArtifacts = new ArtifactGroupModel(component.serviceApiArtifacts);
49 this.filterTerm = this.name + ' ' + this.description + ' ' + (this.tags ? this.tags.toString() : '') + ' ' + this.version;
50 this.ecompGeneratedNaming = component.ecompGeneratedNaming;
51 this.namingPolicy = component.namingPolicy;
52 this.serviceType = component.serviceType;
53 this.serviceRole = component.serviceRole;
54 this.environmentContext = component.environmentContext;
55 if (component.categories && component.categories[0]) {
56 this.mainCategory = component.categories[0].name;
57 this.selectedCategory = this.mainCategory;
60 this.componentService = componentService;
61 this.iconSprite = "sprite-services-icons";
64 public getDistributionsList = ():ng.IPromise<Array<Distribution>> => {
65 return this.componentService.getDistributionsList(this.uuid);
68 public getDistributionsComponent = (distributionId:string):ng.IPromise<Array<DistributionComponent>> => {
69 return this.componentService.getDistributionComponents(distributionId);
72 public markAsDeployed = (distributionId:string):ng.IPromise<any> => {
73 return this.componentService.markAsDeployed(this.uniqueId, distributionId);
76 /* 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
77 * 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
79 public createInputsFormInstances = (instancesInputsMap:InstancesInputsOrPropertiesMapData, instancePropertiesMap:InstancesInputsOrPropertiesMapData):ng.IPromise<Array<InputModel>> => {
81 let deferred = this.$q.defer();
82 let onSuccess = (inputsCreated:Array<InputModel>):void => {
83 this.inputs = inputsCreated.concat(this.inputs);
84 deferred.resolve(inputsCreated);
86 let onFailed = (error:any):void => {
87 deferred.reject(error);
90 let propertiesAndInputsMap:InstancesInputsPropertiesMap = new InstancesInputsPropertiesMap(instancesInputsMap, instancePropertiesMap);
91 propertiesAndInputsMap = propertiesAndInputsMap.cleanUnnecessaryDataBeforeSending(); // We need to create a copy of the map, without the already selected inputs / properties, and to send the clean map
92 this.componentService.createInputsFromInstancesInputs(this.uniqueId, propertiesAndInputsMap).then(onSuccess, onFailed);
93 return deferred.promise;
96 // 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
97 public getServiceInputInputsAndProperties = (inputId:string):ng.IPromise<Array<InputModel>> => {
98 let deferred = this.$q.defer();
99 let onSuccess = (inputsAndProperties:InputsAndProperties):void => {
100 let input:InputModel = _.find(this.inputs, (input:InputModel) => {
101 return input.uniqueId === inputId;
103 input.inputs = inputsAndProperties.inputs;
104 input.properties = inputsAndProperties.properties;
105 deferred.resolve(inputsAndProperties);
107 let onFailed = (error:any):void => {
108 deferred.reject(error);
110 this.componentService.getComponentInputInputsAndProperties(this.uniqueId, inputId).then(onSuccess, onFailed);
111 return deferred.promise;
114 public deleteServiceInput = (inputId:string):ng.IPromise<InputModel> => {
115 let deferred = this.$q.defer();
117 let onSuccess = (deletedInput:InputModel):void => {
118 delete _.remove(this.inputs, {uniqueId: deletedInput.uniqueId})[0];
119 deferred.resolve(deletedInput);
122 let onFailed = (error:any):void => {
123 deferred.reject(error);
126 this.componentService.deleteComponentInput(this.uniqueId, inputId).then(onSuccess, onFailed);
127 return deferred.promise;
130 public getArtifactsByType = (artifactGroupType:string):ArtifactGroupModel => {
131 switch (artifactGroupType) {
132 case ArtifactGroupType.DEPLOYMENT:
133 return this.deploymentArtifacts;
134 case ArtifactGroupType.INFORMATION:
135 return this.artifacts;
136 case ArtifactGroupType.SERVICE_API:
137 return this.serviceApiArtifacts;
141 public updateGroupInstanceProperties = (resourceInstanceId:string, group:DisplayModule, properties:Array<PropertyModel>):ng.IPromise<Array<PropertyModel>> => {
143 let deferred = this.$q.defer();
144 let onSuccess = (updatedProperties:Array<PropertyModel>):void => {
145 _.forEach(updatedProperties, (property:PropertyModel) => { // Replace all updated properties on the we needed to update
146 _.extend(_.find(group.properties, {uniqueId: property.uniqueId}), property);
148 deferred.resolve(updatedProperties);
150 let onError = (error:any):void => {
151 deferred.reject(error);
154 this.componentService.updateGroupInstanceProperties(this.uniqueId, resourceInstanceId, group.groupInstanceUniqueId, properties).then(onSuccess, onError);
155 return deferred.promise;
158 getTypeUrl():string {
163 public setComponentMetadata(componentMetadata: ComponentMetadata) {
164 super.setComponentMetadata(componentMetadata);
165 this.ecompGeneratedNaming = componentMetadata.ecompGeneratedNaming;
166 this.namingPolicy = componentMetadata.namingPolicy;
167 this.serviceType = componentMetadata.serviceType;
168 this.serviceRole = componentMetadata.serviceRole;
169 this.environmentContext = componentMetadata.environmentContext;
170 this.setComponentDisplayData();
173 setComponentDisplayData():void {
174 this.filterTerm = this.name + ' ' + this.description + ' ' + (this.tags ? this.tags.toString() : '') + ' ' + this.version;
175 if (this.categories && this.categories[0]) {
176 this.mainCategory = this.categories[0].name;
177 this.selectedCategory = this.mainCategory;
179 this.iconSprite = "sprite-services-icons";