1 import * as _ from "lodash";
2 import {Component, ViewChild} from '@angular/core';
4 import {Subscription} from "rxjs/Subscription";
6 import {TranslateService} from "app/ng2/shared/translator/translate.service";
7 import {WorkflowServiceNg2} from 'app/ng2/services/workflow.service';
14 WORKFLOW_ASSOCIATION_OPTIONS,
18 import {Tabs, Tab} from "app/ng2/components/ui/tabs/tabs.component";
19 import {DropdownValue} from "app/ng2/components/ui/form-components/dropdown/ui-element-dropdown.component";
20 import { IDropDownOption } from 'onap-ui-angular';
22 export class DropDownOption implements IDropDownOption {
26 constructor(value: string, label?: string) {
28 this.label = label || value;
32 class TypedDropDownOption extends DropDownOption {
35 constructor(value: string, label?: string, type?: number) {
41 export interface OperationCreatorInput {
42 allWorkflows: Array<any>,
43 inputOperation: OperationModel,
44 interfaces: Array<InterfaceModel>,
45 inputProperties: Array<InputBEModel>,
46 enableWorkflowAssociation: boolean,
48 interfaceTypes: { [interfaceType: string]: Array<string> },
49 validityChangedCallback: Function,
50 workflowIsOnline: boolean,
51 capabilities: Array<Capability>
55 selector: 'operation-creator',
56 templateUrl: './operation-creator.component.html',
57 styleUrls: ['./operation-creator.component.less'],
58 providers: [TranslateService]
61 export class OperationCreatorComponent implements OperationCreatorInput {
63 input: OperationCreatorInput;
64 inputOperation: OperationModel;
65 interfaces: Array<InterfaceModel>;
66 operation: OperationModel;
67 interfaceNames: Array<TypedDropDownOption> = [];
68 interfaceTypes: { [interfaceType: string]: Array<string> };
69 operationNames: Array<TypedDropDownOption> = [];
70 validityChangedCallback: Function;
71 capabilities: Array<Capability>;
73 allWorkflows: Array<any>;
74 workflows: Array<DropdownValue> = [];
75 workflowVersions: Array<DropdownValue> = [];
76 inputProperties: Array<InputBEModel> = [];
77 archivedWorkflowId: string = '&';
79 inputParameters: Array<OperationParameter> = [];
80 noAssignInputParameters: Array<OperationParameter> = [];
81 assignInputParameters: { [key: string]: { [key: string]: Array<OperationParameter>; }; } = {};
83 outputParameters: Array<OperationParameter> = [];
84 noAssignOutputParameters: Array<OperationParameter> = [];
85 assignOutputParameters: { [key: string]: { [key: string]: Array<OperationParameter>; }; } = {};
86 componentCapabilities: Array<Capability> = [];
88 tableParameters: Array<OperationParameter> = [];
89 operationOutputs: Array<OperationModel> = [];
91 associationOptions: Array<DropdownValue> = [];
92 workflowAssociationType: string;
94 enableWorkflowAssociation: boolean;
95 workflowIsOnline: boolean;
96 isEditMode: boolean = false;
97 isLoading: boolean = false;
100 propertyTooltipText: String;
102 TYPE_INPUT = 'Inputs';
103 TYPE_OUTPUT = 'Outputs';
105 INTERFACE_OTHER_HEADER = 'Local Interfaces';
106 INTERFACE_OTHER = 'Local';
108 @ViewChild('propertyInputTabs') propertyInputTabs: Tabs;
111 constructor(private workflowServiceNg2: WorkflowServiceNg2, private translateService: TranslateService) {
112 this.translateService.languageChangedObservable.subscribe(lang => {
113 this.propertyTooltipText = this.translateService.translate("OPERATION_PROPERTY_TOOLTIP_TEXT");
115 this.associationOptions = [
116 new DropDownOption(WORKFLOW_ASSOCIATION_OPTIONS.EXTERNAL, this.translateService.translate("EXTERNAL_WORKFLOW_ASSOCIATION")),
117 new DropDownOption(WORKFLOW_ASSOCIATION_OPTIONS.EXISTING, this.translateService.translate("EXISTING_WORKFLOW_ASSOCIATION")),
120 this.workflowAssociationType = this.operation.workflowAssociationType || WORKFLOW_ASSOCIATION_OPTIONS.EXTERNAL;
123 this.currentTab = this.TYPE_INPUT;
126 createInterfaceDropdown(type: string) {
128 const lastDot = label.lastIndexOf('.');
130 label = label.substr(lastDot + 1);
132 return new TypedDropDownOption(type, label);
136 this.interfaceNames = _.map(
137 _.keys(this.interfaceTypes),
138 type => this.createInterfaceDropdown(type)
140 this.interfaceNames.unshift(new TypedDropDownOption('Existing Interfaces', 'Existing Interfaces', 1));
141 this.interfaceNames = this.interfaceNames.concat([
142 new TypedDropDownOption(' ', ' ', 3),
143 new TypedDropDownOption(this.INTERFACE_OTHER_HEADER, this.INTERFACE_OTHER_HEADER, 1),
144 new TypedDropDownOption(this.INTERFACE_OTHER)
147 const inputOperation = this.inputOperation;
148 this.operation = new OperationModel(inputOperation || {});
150 this.operationOutputs = _.reduce(
152 (acc: Array<OperationModel>, interf) => [
156 op => op.uniqueId !== this.operation.uniqueId
161 if (this.enableWorkflowAssociation) {
162 if (this.workflowIsOnline) {
163 this.workflows = _.map(
167 if (workflow.archiving === this.workflowServiceNg2.WF_STATE_ACTIVE) {
170 if (workflow.archiving === this.workflowServiceNg2.WF_STATE_ARCHIVED &&
171 workflow.id === this.operation.workflowId) {
172 this.archivedWorkflowId = workflow.id;
178 (workflow: any) => new DropdownValue(workflow.id, workflow.name)
184 this.reconstructOperation();
185 this.filterCapabilities();
186 this.validityChanged();
190 reconstructOperation = () => {
192 const buildAndUpdate = () => {
197 const inputOperation = this.inputOperation;
198 if (inputOperation) {
199 this.onSelectInterface(new DropDownOption(this.operation.interfaceType));
201 this.operation.artifactFileName = this.operation.artifactFileName || this.operation.implementation.artifactName;
203 if (this.enableWorkflowAssociation && inputOperation.workflowVersionId && this.isUsingExistingWF(inputOperation)) {
204 this.assignInputParameters[this.operation.workflowId] = {[inputOperation.workflowVersionId]: []};
205 this.assignOutputParameters[this.operation.workflowId] = {[inputOperation.workflowVersionId]: []};
206 this.inputParameters = this.assignInputParameters[this.operation.workflowId][this.operation.workflowVersionId];
207 this.outputParameters = this.assignOutputParameters[this.operation.workflowId][this.operation.workflowVersionId];
209 const sub = this.onSelectWorkflow(new DropDownOption(inputOperation.workflowId), inputOperation.workflowVersionId);
213 this.operation.workflowVersionId = '-1';
214 setTimeout(() => this.operation.workflowVersionId = this.inputOperation.workflowVersionId);
220 this.inputParameters = this.noAssignInputParameters;
221 this.outputParameters = this.noAssignOutputParameters;
225 if (inputOperation.uniqueId) {
226 this.isEditMode = true;
232 filterCapabilities() {
233 this.componentCapabilities = _.filter(this.capabilities, (cap: Capability) => cap.properties);
236 buildParams = () => {
238 if (this.inputOperation.outputs) {
239 this.currentTab = this.TYPE_OUTPUT;
242 [...this.inputOperation.outputs.listToscaDataDefinition].sort((a, b) => a.name.localeCompare(b.name)),
243 (output: OperationParameter) => {
244 this.addParam({...output, required: Boolean(output.required)});
249 this.currentTab = this.TYPE_INPUT;
251 if (this.inputOperation.inputs) {
253 [...this.inputOperation.inputs.listToscaDataDefinition].sort((a, b) => a.name.localeCompare(b.name)),
254 (input: OperationParameter) => {
255 this.addParam({...input, required: Boolean(input.required)});
262 isInterfaceOther(): boolean {
263 return this.operation.interfaceType === this.INTERFACE_OTHER;
266 onSelectInterface(interf: IDropDownOption) {
267 if (interf && this.operation.interfaceType !== interf.value) {
268 this.operation.name = null;
270 this.operation.interfaceType = interf && interf.value;
271 this.operationNames = !this.operation.interfaceType ? [] : (
273 this.interfaceTypes[this.operation.interfaceType],
275 const curInterf = _.find(
277 interf => interf.type === this.operation.interfaceType
279 const existingOp = _.find(
280 curInterf && curInterf.operations || [],
281 op => op.name === name
283 const ddType = (existingOp && existingOp.uniqueId !== this.operation.uniqueId) ? 2 : 0;
284 return new TypedDropDownOption(name, name, ddType);
288 this.validityChanged();
291 onSelectOperationName(name: IDropDownOption) {
293 this.operation.name = name.value;
295 this.validityChanged();
299 this.validityChanged();
302 get descriptionValue() {
303 return this.operation.description;
306 set descriptionValue(v) {
307 this.operation.description = v || null;
308 this.validityChanged();
311 onSelectWorkflow(workflowId: DropDownOption, selectedVersionId?: string): Subscription {
313 if (_.isUndefined(workflowId) || !this.workflowIsOnline) {
317 if (this.operation.workflowId === workflowId.value && !selectedVersionId) {
321 this.operation.workflowId = workflowId.value;
322 if (!this.assignInputParameters[this.operation.workflowId]) {
323 this.assignInputParameters[this.operation.workflowId] = {};
324 this.assignOutputParameters[this.operation.workflowId] = {};
327 this.isLoading = true;
328 this.validityChanged();
329 return this.workflowServiceNg2.getWorkflowVersions(this.operation.workflowId).subscribe((versions: Array<any>) => {
330 this.isLoading = false;
332 this.workflowVersions = _.map(
334 versions, version => version.state === this.workflowServiceNg2.VERSION_STATE_CERTIFIED
335 ).sort((a, b) => a.name.localeCompare(b.name)),
337 if (!this.assignInputParameters[this.operation.workflowId][version.id] && version.id !== selectedVersionId) {
338 this.assignInputParameters[this.operation.workflowId][version.id] = _.map(version.inputs, (input: any) => {
339 return new OperationParameter({...input, type: input.type.toLowerCase(), required: Boolean(input.mandatory)});
341 .sort((a, b) => a.name.localeCompare(b.name));
343 this.assignOutputParameters[this.operation.workflowId][version.id] = _.map(version.outputs, (output: any) => {
344 return new OperationParameter({...output, type: output.type.toLowerCase(), required: Boolean(output.mandatory)});
346 .sort((a, b) => a.name.localeCompare(b.name));
348 return new DropdownValue(version.id, `V ${version.name}`);
351 if (!selectedVersionId && this.workflowVersions.length) {
352 this.operation.workflowVersionId = _.last(this.workflowVersions).value;
355 this.changeWorkflowVersion(new DropDownOption(this.operation.workflowVersionId));
356 this.validityChanged();
361 changeWorkflowVersion(versionId: DropDownOption) {
363 if (_.isUndefined(versionId) || !this.workflowIsOnline) {
367 this.operation.workflowVersionId = versionId.value;
368 this.inputParameters = this.assignInputParameters[this.operation.workflowId][this.operation.workflowVersionId];
369 this.outputParameters = this.assignOutputParameters[this.operation.workflowId][this.operation.workflowVersionId];
371 this.validityChanged();
375 toggleAssociateWorkflow(type: DropDownOption) {
377 if (_.isUndefined(type)) {
381 this.operation.workflowAssociationType = type.value;
382 this.workflowAssociationType = this.operation.workflowAssociationType;
384 if (!this.isUsingExistingWF()) {
385 this.inputParameters = this.noAssignInputParameters;
386 this.outputParameters = this.noAssignOutputParameters;
388 if (!this.operation.workflowId || !this.operation.workflowVersionId) {
389 this.inputParameters = [];
390 this.outputParameters = [];
392 this.inputParameters = this.assignInputParameters[this.operation.workflowId][this.operation.workflowVersionId];
393 this.outputParameters = this.assignOutputParameters[this.operation.workflowId][this.operation.workflowVersionId];
398 this.validityChanged();
402 onChangeArtifactFile(e: any) {
403 const file = e.target.files && e.target.files[0];
404 this.operation.artifactFileName = file && file.name;
406 if (!this.operation.artifactFileName) {
407 this.operation.artifactData = null;
408 this.validityChanged();
412 const reader = new FileReader();
413 reader.onloadend = () => {
414 this.isLoading = false;
415 const result = <String>reader.result;
416 this.operation.artifactData = result.substring(result.indexOf(',') + 1);
417 this.validityChanged();
420 this.isLoading = true;
421 reader.readAsDataURL(file);
424 tabChanged = (event) => {
426 this.currentTab = event.title;
433 switch (this.currentTab) {
434 case this.TYPE_INPUT:
435 this.tableParameters = this.inputParameters;
437 case this.TYPE_OUTPUT:
438 this.tableParameters = this.outputParameters;
444 addParam(param?: OperationParameter): void {
445 this.tableParameters.push(new OperationParameter(param || {required: false}));
446 this.validityChanged();
449 canAdd = (): boolean => {
452 if (this.currentTab === this.TYPE_INPUT) {
453 _.forEach(this.inputParameters, param => {
454 if (!param.name || !param.inputId) {
459 _.forEach(this.outputParameters, param => {
460 if (!param.name || !param.type) {
470 isParamsValid = (): boolean => {
473 _.forEach(this.inputParameters, param => {
474 if (!param.name || !param.inputId) {
478 _.forEach(this.outputParameters, param => {
479 if (!param.name || !param.type) {
488 onRemoveParam = (param: OperationParameter): void => {
489 let index = _.indexOf(this.tableParameters, param);
490 this.tableParameters.splice(index, 1);
491 this.validityChanged();
494 createParamLists = () => {
495 this.operation.createInputsList(this.inputParameters);
496 this.operation.createOutputsList(this.outputParameters);
499 isUsingExistingWF = (operation?: OperationModel): boolean => {
500 operation = operation || this.operation;
501 return operation.workflowAssociationType === WORKFLOW_ASSOCIATION_OPTIONS.EXISTING;
504 isUsingExternalWF = (operation?: OperationModel): boolean => {
505 operation = operation || this.operation;
506 return operation.workflowAssociationType === WORKFLOW_ASSOCIATION_OPTIONS.EXTERNAL;
509 shouldCreateWF = (operation?: OperationModel): boolean => {
510 operation = operation || this.operation;
511 return operation.workflowAssociationType === WORKFLOW_ASSOCIATION_OPTIONS.NEW;
514 checkFormValidForSubmit = (): boolean => {
515 return this.operation.name &&
516 (!this.isUsingExistingWF() || this.operation.workflowVersionId) &&
517 this.isParamsValid();
520 validityChanged = () => {
521 let validState = this.checkFormValidForSubmit();
522 this.validityChangedCallback(validState);