Skip to content

Commit

Permalink
chore: PolicyDefinition UI Migration
Browse files Browse the repository at this point in the history
  • Loading branch information
SaadEGI committed Aug 30, 2023
1 parent 3cb98ce commit bd14dc3
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 126 deletions.
20 changes: 20 additions & 0 deletions src/app/core/services/api/edc-api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import {
EdcClient,
IdResponse,
IdResponseDto,
PolicyDefinitionPage,
TransferHistoryPage,
buildEdcClient,
} from '@sovity.de/edc-client';
import {AssetCreateRequest} from '@sovity.de/edc-client/dist/generated/models/AssetCreateRequest';
import {ContractDefinitionRequest} from '@sovity.de/edc-client/dist/generated/models/ContractDefinitionRequest';
import {PolicyDefinitionCreateRequest} from '@sovity.de/edc-client/dist/generated/models/PolicyDefinitionCreateRequest';
import {APP_CONFIG, AppConfig} from '../../config/app-config';
import {EDC_FAKE_BACKEND} from './fake-backend/edc-fake-backend';

Expand Down Expand Up @@ -68,6 +70,24 @@ export class EdcApiService {
);
}

createPolicyDefinition(
policyDefinitionCreateRequest: PolicyDefinitionCreateRequest,
): Observable<IdResponseDto> {
return from(
this.edcClient.uiApi.createPolicyDefinition({
policyDefinitionCreateRequest,
}),
);
}

getPolicyDefinitionPage(): Observable<PolicyDefinitionPage> {
return from(this.edcClient.uiApi.policyDefinitionPage());
}

deletePolicyDefinition(policyId: string): Observable<IdResponseDto> {
return from(this.edcClient.uiApi.deletePolicyDefinition({policyId}));
}

getContractAgreementPage(): Observable<ContractAgreementPage> {
return from(this.edcClient.uiApi.contractAgreementEndpoint());
}
Expand Down
19 changes: 19 additions & 0 deletions src/app/core/services/api/fake-backend/edc-fake-backend.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import {FetchAPI} from '@sovity.de/edc-client';
import {AssetCreateRequest} from '@sovity.de/edc-client/dist/generated/models/AssetCreateRequest';
import {ContractDefinitionRequest} from '@sovity.de/edc-client/dist/generated/models/ContractDefinitionRequest';
import {PolicyDefinitionCreateRequest} from '@sovity.de/edc-client/dist/generated/models/PolicyDefinitionCreateRequest';
import {assetPage, createAsset, deleteAsset} from './asset-fake-service';
import {
contractDefinitionPage,
createContractDefinition,
deleteContractDefinition,
} from './contract-definition-fake-service';
import {
createPolicyDefinition,
deletePolicyDefinition,
policyDefinitionPage,
} from './policy-definition-fake-service';
import {getBody, getMethod, getUrl} from './utils/request-utils';
import {ok} from './utils/response-utils';
import {UrlInterceptor} from './utils/url-interceptor';
Expand Down Expand Up @@ -45,5 +51,18 @@ export const EDC_FAKE_BACKEND: FetchAPI = async (
ok(deleteContractDefinition(contractDefinitionId)),
)

.url('pages/policy-definition-page')
.on('GET', () => ok(policyDefinitionPage()))

.url('pages/policy-definition-page/policy-definitions')
.on('POST', () =>
ok(createPolicyDefinition(body as PolicyDefinitionCreateRequest)),
)

.url('pages/policy-definition-page/policy-definitions/*')
.on('DELETE', (policyDefinitionId) =>
ok(deletePolicyDefinition(policyDefinitionId)),
)

.tryMatch();
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import {IdResponseDto, PolicyDefinitionDto} from '@sovity.de/edc-client';
import {PolicyDefinitionCreateRequest} from '@sovity.de/edc-client/dist/generated/models/PolicyDefinitionCreateRequest';
import {UiPolicyDto} from '@sovity.de/edc-client/dist/generated/models/UiPolicyDto';

export let policyDefinitions: PolicyDefinitionDto[] = [
{
policyDefinitionId: 'test-policy-definition-1',
uiPolicyDto: {
policyJsonLd: 'test-policy-json-ld-1',
constraints: [
{
left: 'test-left-1',
operator: 'EQ',
right: {type: 'STRING', value: 'test-asset-1'},
},
],
errors: ['test-error-1'],
},
},
];

export const policyDefinitionPage = (): PolicyDefinitionDto[] => {
return policyDefinitions;
};

export const createPolicyDefinition = (
request: PolicyDefinitionCreateRequest,
): IdResponseDto => {
policyDefinitions.push({
policyDefinitionId: request.policyDefinitionId!,
uiPolicyDto: request.uiPolicyDto! as UiPolicyDto,
});

return {
id: request.policyDefinitionId,
lastUpdatedDate: new Date(),
};
};

export const deletePolicyDefinition = (id: string): IdResponseDto => {
policyDefinitions = policyDefinitions.filter(
(it) => it.policyDefinitionId !== id,
);
return {id, lastUpdatedDate: new Date()};
};
91 changes: 18 additions & 73 deletions src/app/core/services/policy-definition-builder.ts
Original file line number Diff line number Diff line change
@@ -1,91 +1,36 @@
import {Injectable} from '@angular/core';
import {addDays} from 'date-fns';
import {PolicyDefinitionCreateRequest} from '@sovity.de/edc-client/dist/generated/models/PolicyDefinitionCreateRequest';
import {NewPolicyDialogFormValue} from '../../routes/connector-ui/policy-definition-page/new-policy-dialog/new-policy-dialog-form-model';
import {Permission, PolicyDefinition} from './api/legacy-managent-api-client';
import {
AtomicConstraint,
ExpressionLeftSideConstants,
Operator,
} from './api/policy-type-ext';
import {PolicyDefinitionUtils} from './policy-definition-utils';
import {PolicyDefinition} from './api/legacy-managent-api-client';

@Injectable({
providedIn: 'root',
})
export class PolicyDefinitionBuilder {
constructor(private policyDefinitionUtils: PolicyDefinitionUtils) {}

/**
* Build {@link PolicyDefinition} from {@link NewPolicyDialogFormValue}
*
* @param formValue {@link NewPolicyDialogFormValue}
*/
buildPolicyDefinition(formValue: NewPolicyDialogFormValue): PolicyDefinition {
return {
id: formValue.id?.trim()!,
policy: {
permissions: this.buildPolicyPermissions(formValue),
},
};
}

private buildPolicyPermissions(
formValue: NewPolicyDialogFormValue,
): Permission[] {
let policyType = formValue.policyType;
switch (policyType) {
case 'Time-Period-Restricted':
return this.buildTimePeriodRestrictionPermissions(formValue);
case 'Connector-Restricted-Usage':
return this.buildConnectorRestrictedUsagePermissions(formValue);
default:
throw new Error(`Unknown policyType: ${policyType}`);
}
}

private buildConnectorRestrictedUsagePermissions(
buildPolicyDefinition(
formValue: NewPolicyDialogFormValue,
): Permission[] {
return [
this.policyDefinitionUtils.buildPermission({
constraints: [
this.policyDefinitionUtils.buildAtomicConstraint(
'REFERRING_CONNECTOR',
'EQ',
formValue.connectorId!,
),
],
}),
];
}
): PolicyDefinitionCreateRequest {
const policyConstraints = formValue.policy?.constraints;

private buildTimePeriodRestrictionPermissions(
formValue: NewPolicyDialogFormValue,
): Permission[] {
const start = formValue.range!!.start!!;
const constraints: AtomicConstraint[] = [this.evaluationTime('GEQ', start)];
const end = formValue.range!!.end;
if (end) {
constraints.push(this.evaluationTime('LT', addDays(end, 1)));
return [
this.policyDefinitionUtils.buildPermission({
constraints,
}),
];
} else {
return [
this.policyDefinitionUtils.buildPermission({
constraints: [this.evaluationTime('GEQ', start)],
}),
];
}
}
const constraints = policyConstraints
? policyConstraints.map((constraint) => ({
left: constraint.left,
operator: constraint.operator,
right: constraint.right,
}))
: [];

private evaluationTime(operator: Operator, date: Date): AtomicConstraint {
return this.policyDefinitionUtils.buildAtomicConstraint(
ExpressionLeftSideConstants.PolicyEvaluationTime,
operator,
date.toISOString()!,
);
return {
policyDefinitionId: formValue.id?.trim() || '',
uiPolicyDto: {
constraints: constraints,
},
};
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {FormControl, FormGroup, ɵFormGroupValue} from '@angular/forms';
import {FormControl, ɵFormGroupValue} from '@angular/forms';
import {UiPolicyCreateRequest} from '@sovity.de/edc-client';

/**
* Form Value Type
Expand All @@ -11,14 +12,5 @@ export type NewPolicyDialogFormValue =
*/
export interface NewPolicyDialogFormModel {
id: FormControl<string>;
policyType: FormControl<PolicyType>;
range: FormGroup<{
start: FormControl<Date | null>;
end: FormControl<Date | null>;
}>;
connectorId: FormControl<string>;
policy: FormControl<UiPolicyCreateRequest>;
}

export type PolicyType =
| 'Time-Period-Restricted'
| 'Connector-Restricted-Usage';
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export class NewPolicyDialogForm {
/**
* Quick access to full value
*/

get value(): NewPolicyDialogFormValue {
return this.group.value;
}
Expand All @@ -29,41 +30,26 @@ export class NewPolicyDialogForm {

constructor(private formBuilder: FormBuilder) {}

buildFormGroup(): FormGroup<NewPolicyDialogFormModel> {
const newPolicyFormGroup: FormGroup<NewPolicyDialogFormModel> =
this.formBuilder.nonNullable.group({
id: ['', [Validators.required, noWhitespaceValidator]],
policyType: [
'Connector-Restricted-Usage' as PolicyType,
Validators.required,
],
range: this.formBuilder.group(
{
start: null as Date | null,
end: null as Date | null,
},
{validators: dateRangeRequired},
),
connectorId: ['', Validators.required],
});

switchDisabledControls<NewPolicyDialogFormValue>(
newPolicyFormGroup,
(value) => {
const timePeriodRestricted =
value.policyType === 'Time-Period-Restricted';
const connecterRestrictedUsage =
value.policyType === 'Connector-Restricted-Usage';

return {
id: true,
policyType: true,
range: timePeriodRestricted,
connectorId: connecterRestrictedUsage,
};
},
);
buildFormGroup(): FormGroup {
return this.formBuilder.nonNullable.group({
policyDefinitionId: ['', [Validators.required, noWhitespaceValidator]],
policyType: [
'Connector-Restricted-Usage' as PolicyType,
Validators.required,
],
uiPolicyDto: this.formBuilder.group({
constraints: this.formBuilder.array([this.buildConstraintForm()]),
}),
});
}

return newPolicyFormGroup;
buildConstraintForm(): FormGroup {
return this.formBuilder.group({
policyDefinitionId: true,
policyType: true,
left: ['', Validators.required],
operator: ['', Validators.required],
right: this.formBuilder.group({}),
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {Component, OnDestroy} from '@angular/core';
import {MatDialogRef} from '@angular/material/dialog';
import {Subject} from 'rxjs';
import {finalize, takeUntil} from 'rxjs/operators';
import {PolicyService} from '../../../../core/services/api/legacy-managent-api-client';
import {EdcApiService} from '../../../../core/services/api/edc-api.service';
import {AssetEntryBuilder} from '../../../../core/services/asset-entry-builder';
import {NotificationService} from '../../../../core/services/notification.service';
import {PolicyDefinitionBuilder} from '../../../../core/services/policy-definition-builder';
Expand All @@ -19,9 +19,9 @@ export class NewPolicyDialogComponent implements OnDestroy {
loading = false;

constructor(
private edcApiService: EdcApiService,
public form: NewPolicyDialogForm,
private notificationService: NotificationService,
private policyService: PolicyService,
private dialogRef: MatDialogRef<NewPolicyDialogComponent>,
public validationMessages: ValidationMessages,
private policyDefinitionBuilder: PolicyDefinitionBuilder,
Expand All @@ -33,8 +33,8 @@ export class NewPolicyDialogComponent implements OnDestroy {
this.policyDefinitionBuilder.buildPolicyDefinition(formValue);
this.form.group.disable();
this.loading = true;
this.policyService
.createPolicy(policyDefinition)
this.edcApiService
.createPolicyDefinition(policyDefinition)
.pipe(
takeUntil(this.ngOnDestroy$),
finalize(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {catchError, tap} from 'rxjs/operators';
import {ConfirmDialogModel} from '../../../../component-library/confirmation-dialog/confirmation-dialog/confirmation-dialog.component';
import {JsonDialogComponent} from '../../../../component-library/json-dialog/json-dialog/json-dialog.component';
import {JsonDialogData} from '../../../../component-library/json-dialog/json-dialog/json-dialog.data';
import {PolicyService} from '../../../../core/services/api/legacy-managent-api-client';
import {EdcApiService} from '../../../../core/services/api/edc-api.service';
import {NotificationService} from '../../../../core/services/notification.service';
import {PolicyCard} from './policy-card';

Expand All @@ -35,9 +35,9 @@ export class PolicyCardsComponent {
deleteDone = new EventEmitter();

constructor(
private edcApiService: EdcApiService,
private matDialog: MatDialog,
private notificationService: NotificationService,
private policyService: PolicyService,
) {}

onPolicyDetailClick(policyCard: PolicyCard) {
Expand All @@ -52,7 +52,7 @@ export class PolicyCardsComponent {
color: 'warn',
confirmation: ConfirmDialogModel.forDelete('policy', policyCard.id),
action: () =>
this.policyService.deletePolicy(policyCard.id).pipe(
this.edcApiService.deletePolicyDefinition(policyCard.id).pipe(
tap(() => {
this.notificationService.showInfo('Policy deleted!');
this.deleteDone.emit();
Expand Down

0 comments on commit bd14dc3

Please sign in to comment.