Skip to content

Commit

Permalink
feat(RouteConfiguration): Add support for RouteConfiguration entity
Browse files Browse the repository at this point in the history
This commit adds support for RouteConfiguration entity.

fix: #492

Temp: Remove constructor parameter type from AbstractCamelVisualEntity

Temp: Make OnException inherit from AbstractCamelVisualEntity

Temp: Restore getArray util function

Temp: Remove duplicated methods in OnException

Temp: Remove duplicated methods in CamelRouteConfiguration

Temp: Refactor getNodeLabel

Temp: Rework errorHandler node label

Temp: Remove more duplicated methods in OnException

Temp: Add intercept, interceptFrom, interceptSendToEndpoint and onCompletion
  • Loading branch information
lordrip committed Apr 16, 2024
1 parent 33e03dd commit 5cf87de
Show file tree
Hide file tree
Showing 12 changed files with 782 additions and 226 deletions.
10 changes: 10 additions & 0 deletions packages/ui/src/models/camel/camel-route-resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@ import { createCamelPropertiesSorter, isDefined } from '../../utils';
import { AddStepMode } from '../visualization/base-visual-entity';
import { CamelRouteVisualEntity, isCamelFrom, isCamelRoute } from '../visualization/flows';
import { CamelErrorHandlerVisualEntity } from '../visualization/flows/camel-error-handler-visual-entity';
import { CamelInterceptFromVisualEntity } from '../visualization/flows/camel-intercept-from-visual-entity';
import { CamelInterceptSendToEndpointVisualEntity } from '../visualization/flows/camel-intercept-send-to-endpoint-visual-entity';
import { CamelInterceptVisualEntity } from '../visualization/flows/camel-intercept-visual-entity';
import { CamelOnCompletionVisualEntity } from '../visualization/flows/camel-on-completion-visual-entity';
import { CamelOnExceptionVisualEntity } from '../visualization/flows/camel-on-exception-visual-entity';
import { CamelRestConfigurationVisualEntity } from '../visualization/flows/camel-rest-configuration-visual-entity';
import { CamelRouteConfigurationVisualEntity } from '../visualization/flows/camel-route-configuration-visual-entity';
import { NonVisualEntity } from '../visualization/flows/non-visual-entity';
import { CamelComponentFilterService } from '../visualization/flows/support/camel-component-filter.service';
import { CamelRouteVisualEntityData } from '../visualization/flows/support/camel-component-types';
Expand All @@ -18,8 +23,13 @@ import { SourceSchemaType } from './source-schema-type';
export class CamelRouteResource implements CamelResource, BeansAwareResource {
static readonly SUPPORTED_ENTITIES = [
CamelOnExceptionVisualEntity,
CamelOnCompletionVisualEntity,
CamelErrorHandlerVisualEntity,
CamelRestConfigurationVisualEntity,
CamelRouteConfigurationVisualEntity,
CamelInterceptVisualEntity,
CamelInterceptFromVisualEntity,
CamelInterceptSendToEndpointVisualEntity,
] as const;
static readonly PARAMETERS_ORDER = ['id', 'description', 'uri', 'parameters', 'steps'];
readonly sortFn = createCamelPropertiesSorter(CamelRouteResource.PARAMETERS_ORDER) as (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ exports[`AbstractCamelVisualEntity getNodeInteraction should return the correct
"canHaveSpecialChildren": false,
"canRemoveFlow": false,
"canRemoveStep": true,
"canReplaceStep": true,
"canReplaceStep": false,
}
`;

Expand All @@ -32,7 +32,7 @@ exports[`AbstractCamelVisualEntity getNodeInteraction should return the correct
"canHaveSpecialChildren": false,
"canRemoveFlow": false,
"canRemoveStep": true,
"canReplaceStep": true,
"canReplaceStep": false,
}
`;

Expand All @@ -44,7 +44,7 @@ exports[`AbstractCamelVisualEntity getNodeInteraction should return the correct
"canHaveSpecialChildren": false,
"canRemoveFlow": false,
"canRemoveStep": true,
"canReplaceStep": true,
"canReplaceStep": false,
}
`;

Expand All @@ -68,7 +68,7 @@ exports[`AbstractCamelVisualEntity getNodeInteraction should return the correct
"canHaveSpecialChildren": false,
"canRemoveFlow": false,
"canRemoveStep": true,
"canReplaceStep": true,
"canReplaceStep": false,
}
`;

Expand All @@ -80,7 +80,7 @@ exports[`AbstractCamelVisualEntity getNodeInteraction should return the correct
"canHaveSpecialChildren": false,
"canRemoveFlow": false,
"canRemoveStep": true,
"canReplaceStep": true,
"canReplaceStep": false,
}
`;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-case-declarations */
import { ProcessorDefinition, RouteDefinition } from '@kaoto-next/camel-catalog/types';
import { ProcessorDefinition } from '@kaoto-next/camel-catalog/types';
import { SchemaService } from '../../../components/Form/schema.service';
import { ROOT_PATH, getArrayProperty, getValue, isDefined, setValue } from '../../../utils';
import { ROOT_PATH, getArrayProperty, getValue, setValue } from '../../../utils';
import { NodeIconResolver } from '../../../utils/node-icon-resolver';
import { DefinedComponent } from '../../camel-catalog-index';
import { EntityType } from '../../camel/entities';
Expand All @@ -20,12 +20,13 @@ import { CamelProcessorStepsProperties, CamelRouteVisualEntityData } from './sup
import { CamelStepsService } from './support/camel-steps.service';
import { ModelValidationService } from './support/validators/model-validation.service';

export abstract class AbstractCamelVisualEntity implements BaseVisualCamelEntity {
constructor(public route: RouteDefinition) {}
export abstract class AbstractCamelVisualEntity<T extends object> implements BaseVisualCamelEntity {
constructor(public route: T) {}

abstract id: string;
abstract type: EntityType;
abstract setId(id: string): void;
abstract toJSON(): unknown;
protected abstract getRootUri(): string | undefined;

getId(): string {
Expand Down Expand Up @@ -68,17 +69,11 @@ export abstract class AbstractCamelVisualEntity implements BaseVisualCamelEntity
return SchemaService.OMIT_FORM_FIELDS;
}

toJSON() {
return { route: this.route };
}

updateModel(path: string | undefined, value: unknown): void {
if (!path) return;
const updatedValue = CamelComponentSchemaService.getUriSerializedDefinition(path, value);

setValue(this.route, path, updatedValue);

if (isDefined(this.route.id)) this.id = this.route.id;
}

/**
Expand Down Expand Up @@ -106,12 +101,7 @@ export abstract class AbstractCamelVisualEntity implements BaseVisualCamelEntity
(options.data as CamelRouteVisualEntityData).processorName as keyof ProcessorDefinition,
);

/** Replace the root `from` step */
if (options.mode === AddStepMode.ReplaceStep && options.data.path === 'from' && isDefined(this.route.from)) {
const fromValue = CamelComponentDefaultService.getDefaultFromDefinitionValue(options.definedComponent);
Object.assign(this.route.from, fromValue);
return;
} else if (options.mode === AddStepMode.InsertChildStep || options.mode === AddStepMode.InsertSpecialChildStep) {
if (options.mode === AddStepMode.InsertChildStep || options.mode === AddStepMode.InsertSpecialChildStep) {
this.insertChildStep(options, stepsProperties, defaultValue);
return;
}
Expand Down Expand Up @@ -144,15 +134,6 @@ export abstract class AbstractCamelVisualEntity implements BaseVisualCamelEntity

removeStep(path?: string): void {
if (!path) return;
/**
* If there's only one path segment, it means the target is the `from` property of the route
* therefore we replace it with an empty object
*/
if (path === 'from') {
setValue(this.route, 'from.uri', '');
return;
}

const pathArray = path.split('.');
const last = pathArray[pathArray.length - 1];
const penultimate = pathArray[pathArray.length - 2];
Expand Down Expand Up @@ -207,6 +188,9 @@ export abstract class AbstractCamelVisualEntity implements BaseVisualCamelEntity
const canHavePreviousStep = CamelComponentSchemaService.canHavePreviousStep(
(data as CamelRouteVisualEntityData).processorName,
);
const canReplaceStep = CamelComponentSchemaService.canReplaceStep(
(data as CamelRouteVisualEntityData).processorName,
);
const canHaveChildren = stepsProperties.find((property) => property.type === 'branch') !== undefined;
const canHaveSpecialChildren = Object.keys(stepsProperties).length > 1;

Expand All @@ -215,7 +199,7 @@ export abstract class AbstractCamelVisualEntity implements BaseVisualCamelEntity
canHaveNextStep: canHavePreviousStep,
canHaveChildren,
canHaveSpecialChildren,
canReplaceStep: true,
canReplaceStep,
canRemoveStep: true,
canRemoveFlow: data.path === ROOT_PATH,
};
Expand Down Expand Up @@ -254,7 +238,7 @@ export abstract class AbstractCamelVisualEntity implements BaseVisualCamelEntity
}

private insertChildStep(
options: Parameters<AbstractCamelVisualEntity['addStep']>[0],
options: Parameters<AbstractCamelVisualEntity<object>['addStep']>[0],
stepsProperties: CamelProcessorStepsProperties[],
defaultValue: ProcessorDefinition = {},
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { ErrorHandler, NoErrorHandler } from '@kaoto-next/camel-catalog/types';
import { ErrorHandlerBuilderDeserializer, NoErrorHandler } from '@kaoto-next/camel-catalog/types';
import { useSchemasStore } from '../../../store';
import { errorHandlerSchema } from '../../../stubs/error-handler';
import { CamelErrorHandlerVisualEntity } from './camel-error-handler-visual-entity';
import { EntityType } from '../../camel/entities';
import { CamelErrorHandlerVisualEntity } from './camel-error-handler-visual-entity';

describe('CamelErrorHandlerVisualEntity', () => {
const ERROR_HANDLER_ID_REGEXP = /^errorHandler-[a-zA-Z0-9]{4}$/;
let errorHandlerDef: { errorHandler: ErrorHandler };
let errorHandlerDef: { errorHandler: ErrorHandlerBuilderDeserializer };

beforeAll(() => {
useSchemasStore.getState().setSchema('errorHandler', {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import { InterceptFrom, ProcessorDefinition } from '@kaoto-next/camel-catalog/types';
import { getCamelRandomId } from '../../../camel-utils/camel-random-id';
import { isDefined } from '../../../utils';
import { EntityType } from '../../camel/entities/base-entity';
import {
BaseVisualCamelEntity,
IVisualizationNode,
IVisualizationNodeData,
NodeInteraction,
} from '../base-visual-entity';
import { AbstractCamelVisualEntity } from './abstract-camel-visual-entity';
import { CamelComponentSchemaService } from './support/camel-component-schema.service';
import { CamelRouteVisualEntityData } from './support/camel-component-types';
import { CamelStepsService } from './support/camel-steps.service';
import { ModelValidationService } from './support/validators/model-validation.service';

export class CamelInterceptFromVisualEntity
extends AbstractCamelVisualEntity<{ interceptFrom: InterceptFrom }>
implements BaseVisualCamelEntity
{
id: string;
interceptFromDef: { interceptFrom: Exclude<InterceptFrom, string> };
readonly type = EntityType.InterceptFrom;
private static readonly ROOT_PATH = 'interceptFrom';

constructor(interceptFromRaw: { interceptFrom: InterceptFrom }) {
let interceptFromDef: { interceptFrom: Exclude<InterceptFrom, string> };
if (typeof interceptFromRaw.interceptFrom === 'string') {
interceptFromDef = {
interceptFrom: {
id: getCamelRandomId(CamelInterceptFromVisualEntity.ROOT_PATH),
uri: interceptFromRaw.interceptFrom,
},
};
} else {
interceptFromDef = { interceptFrom: interceptFromRaw.interceptFrom };
}

super(interceptFromDef);
this.interceptFromDef = interceptFromDef;
const id = interceptFromDef.interceptFrom.id ?? getCamelRandomId(CamelInterceptFromVisualEntity.ROOT_PATH);
this.id = id;
interceptFromDef.interceptFrom.id = id;
}

static isApplicable(interceptFromDef: unknown): interceptFromDef is { interceptFrom: InterceptFrom } {
if (!isDefined(interceptFromDef) || Array.isArray(interceptFromDef) || typeof interceptFromDef !== 'object') {
return false;
}

const objectKeys = Object.keys(interceptFromDef!);

return (
objectKeys.length === 1 &&
this.ROOT_PATH in interceptFromDef! &&
(typeof interceptFromDef.interceptFrom === 'object' || typeof interceptFromDef.interceptFrom === 'string')
);
}

getId(): string {
return this.id;
}

setId(id: string): void {
this.id = id;
this.interceptFromDef.interceptFrom.id = id;
}

getNodeInteraction(data: IVisualizationNodeData): NodeInteraction {
const stepsProperties = CamelComponentSchemaService.getProcessorStepsProperties(
(data as CamelRouteVisualEntityData).processorName as keyof ProcessorDefinition,
);
const canHavePreviousStep = CamelComponentSchemaService.canHavePreviousStep(
(data as CamelRouteVisualEntityData).processorName,
);
const canHaveChildren = stepsProperties.find((property) => property.type === 'branch') !== undefined;
const canHaveSpecialChildren = Object.keys(stepsProperties).length > 1;
const canReplaceStep = data.path !== CamelInterceptFromVisualEntity.ROOT_PATH;
const canRemoveStep = data.path !== CamelInterceptFromVisualEntity.ROOT_PATH;

return {
canHavePreviousStep,
canHaveNextStep: canHavePreviousStep,
canHaveChildren,
canHaveSpecialChildren,
canReplaceStep,
canRemoveStep,
canRemoveFlow: data.path === CamelInterceptFromVisualEntity.ROOT_PATH,
};
}

getNodeValidationText(path?: string | undefined): string | undefined {
const componentVisualSchema = this.getComponentSchema(path);
if (!componentVisualSchema) return undefined;

return ModelValidationService.validateNodeStatus(componentVisualSchema);
}

toVizNode(): IVisualizationNode<IVisualizationNodeData> {
const interceptFromGroupNode = CamelStepsService.getVizNodeFromProcessor(
CamelInterceptFromVisualEntity.ROOT_PATH,
{ processorName: CamelInterceptFromVisualEntity.ROOT_PATH as keyof ProcessorDefinition },
this.interceptFromDef,
);
interceptFromGroupNode.data.entity = this;
interceptFromGroupNode.data.isGroup = true;

return interceptFromGroupNode;
}

toJSON(): { interceptFrom: InterceptFrom } {
return { interceptFrom: this.interceptFromDef.interceptFrom };
}

protected getRootUri(): string | undefined {
return undefined;
}
}
Loading

0 comments on commit 5cf87de

Please sign in to comment.