@twin.org/dataspace-control-plane-service 0.0.3-next.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +119 -0
- package/dist/es/dataspaceControlPlanePolicyRequester.js +228 -0
- package/dist/es/dataspaceControlPlanePolicyRequester.js.map +1 -0
- package/dist/es/dataspaceControlPlaneRoutes.js +202 -0
- package/dist/es/dataspaceControlPlaneRoutes.js.map +1 -0
- package/dist/es/dataspaceControlPlaneService.js +1420 -0
- package/dist/es/dataspaceControlPlaneService.js.map +1 -0
- package/dist/es/index.js +11 -0
- package/dist/es/index.js.map +1 -0
- package/dist/es/models/IDataspaceControlPlaneServiceConfig.js +4 -0
- package/dist/es/models/IDataspaceControlPlaneServiceConfig.js.map +1 -0
- package/dist/es/models/IDataspaceControlPlaneServiceConstructorOptions.js +2 -0
- package/dist/es/models/IDataspaceControlPlaneServiceConstructorOptions.js.map +1 -0
- package/dist/es/models/INegotiationState.js +4 -0
- package/dist/es/models/INegotiationState.js.map +1 -0
- package/dist/es/restEntryPoints.js +15 -0
- package/dist/es/restEntryPoints.js.map +1 -0
- package/dist/es/schema.js +11 -0
- package/dist/es/schema.js.map +1 -0
- package/dist/es/utils/dataHelpers.js +22 -0
- package/dist/es/utils/dataHelpers.js.map +1 -0
- package/dist/es/utils/transferErrorUtils.js +78 -0
- package/dist/es/utils/transferErrorUtils.js.map +1 -0
- package/dist/types/dataspaceControlPlanePolicyRequester.d.ts +80 -0
- package/dist/types/dataspaceControlPlaneRoutes.d.ts +17 -0
- package/dist/types/dataspaceControlPlaneService.d.ts +153 -0
- package/dist/types/index.d.ts +8 -0
- package/dist/types/models/IDataspaceControlPlaneServiceConfig.d.ts +20 -0
- package/dist/types/models/IDataspaceControlPlaneServiceConstructorOptions.d.ts +69 -0
- package/dist/types/models/INegotiationState.d.ts +27 -0
- package/dist/types/restEntryPoints.d.ts +7 -0
- package/dist/types/schema.d.ts +4 -0
- package/dist/types/utils/dataHelpers.d.ts +1 -0
- package/dist/types/utils/transferErrorUtils.d.ts +39 -0
- package/docs/API.md +341 -0
- package/docs/changelog.md +31 -0
- package/docs/examples.md +1 -0
- package/docs/reference/classes/DataspaceControlPlanePolicyRequester.md +244 -0
- package/docs/reference/classes/DataspaceControlPlaneService.md +549 -0
- package/docs/reference/functions/generateRestRoutesDataspaceControlPlane.md +29 -0
- package/docs/reference/functions/initSchema.md +9 -0
- package/docs/reference/index.md +22 -0
- package/docs/reference/interfaces/IDataspaceControlPlaneServiceConfig.md +26 -0
- package/docs/reference/interfaces/IDataspaceControlPlaneServiceConstructorOptions.md +160 -0
- package/docs/reference/interfaces/INegotiationState.md +43 -0
- package/docs/reference/variables/restEntryPoints.md +7 -0
- package/docs/reference/variables/tagsDataspaceControlPlane.md +5 -0
- package/locales/en.json +93 -0
- package/package.json +70 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2024 IOTA Stiftung.
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
# TWIN Dataspace Control Plane Service
|
|
2
|
+
|
|
3
|
+
Dataspace Control Plane contract implementation and REST endpoint definitions, implementing the Eclipse Dataspace Protocol (DSP) specification.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```shell
|
|
8
|
+
npm install @twin.org/dataspace-control-plane-service
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Overview
|
|
12
|
+
|
|
13
|
+
The Control Plane manages DSP protocol operations including contract negotiation and transfer process management.
|
|
14
|
+
|
|
15
|
+
### Key Features
|
|
16
|
+
|
|
17
|
+
- **Transfer Process Management**: Create, start, complete, suspend, and terminate transfers
|
|
18
|
+
- **Contract Negotiation**: Negotiate agreements with providers via Policy Negotiation Point (PNP)
|
|
19
|
+
- **Agreement Validation**: Validate agreements via Policy Administration Point (PAP)
|
|
20
|
+
- **Dataset Validation**: Validate datasets via Federated Catalogue
|
|
21
|
+
- **Entity Storage**: Persists `TransferProcessEntity` to shared storage (accessible by Data Plane)
|
|
22
|
+
- **Token Generation**: Creates transfer tokens with configurable expiration
|
|
23
|
+
- **State Machine**: Enforces valid state transitions per DSP specification
|
|
24
|
+
|
|
25
|
+
### Constructor Options
|
|
26
|
+
|
|
27
|
+
- `loggingComponentType` (default: `logging`) - Logging component type
|
|
28
|
+
- `identityComponentType` (default: `identity`) - Identity component for token signing
|
|
29
|
+
- `identityAuthenticationComponentType` (default: `identity-authentication`) - Token validation component
|
|
30
|
+
- `transferProcessEntityStorageType` (default: `transfer-process-entity`) - Entity storage for transfer processes
|
|
31
|
+
- `policyAdministrationPointComponentType` (default: `policy-administration-point`) - Agreement lookup component
|
|
32
|
+
- `policyNegotiationPointComponentType` (default: `policy-negotiation-point`) - Contract negotiation component
|
|
33
|
+
- `federatedCatalogueComponentType` (default: `federated-catalogue`) - Dataset validation component
|
|
34
|
+
- `trustComponentType` (default: `trust`) - Trust verification component
|
|
35
|
+
|
|
36
|
+
### DSP Protocols
|
|
37
|
+
|
|
38
|
+
The Control Plane implements two separate DSP protocols, following the Eclipse Dataspace Protocol specification:
|
|
39
|
+
|
|
40
|
+
#### 1. Contract Negotiation Protocol
|
|
41
|
+
|
|
42
|
+
Negotiate agreements with providers before initiating transfers. Integrates with the Rights Management Policy Negotiation Point (PNP).
|
|
43
|
+
|
|
44
|
+
**REST API Endpoints:**
|
|
45
|
+
|
|
46
|
+
- `POST /control-plane/negotiations` - Initiate contract negotiation
|
|
47
|
+
- `GET /control-plane/negotiations/:negotiationId` - Get negotiation status
|
|
48
|
+
|
|
49
|
+
**Example Flow:**
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
# Step 1: Initiate negotiation
|
|
53
|
+
curl -X POST https://consumer.example.com/control-plane/negotiations \
|
|
54
|
+
-H "Authorization: Bearer ${TOKEN}" \
|
|
55
|
+
-H "Content-Type: application/json" \
|
|
56
|
+
-d '{
|
|
57
|
+
"offerId": "offer-from-catalog",
|
|
58
|
+
"providerEndpoint": "https://provider.example.com/negotiations"
|
|
59
|
+
}'
|
|
60
|
+
# Response: { agreement: {...}, agreementId: "...", negotiationId: "..." }
|
|
61
|
+
|
|
62
|
+
# Step 2: Check negotiation status (optional)
|
|
63
|
+
curl -X GET https://consumer.example.com/control-plane/negotiations/${NEGOTIATION_ID} \
|
|
64
|
+
-H "Authorization: Bearer ${TOKEN}"
|
|
65
|
+
# Response: { negotiationId: "...", state: "FINALIZED", ... }
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
**Service Methods:**
|
|
69
|
+
|
|
70
|
+
- `negotiateAgreement()` - Initiate contract negotiation (returns agreement)
|
|
71
|
+
- `getNegotiation()` - Retrieve negotiation state
|
|
72
|
+
|
|
73
|
+
**Flow**: Catalog → Negotiation → Agreement → Transfer
|
|
74
|
+
|
|
75
|
+
#### 2. Transfer Process Protocol
|
|
76
|
+
|
|
77
|
+
Manage the lifecycle of data transfers using negotiated agreements.
|
|
78
|
+
|
|
79
|
+
**REST API Endpoints:**
|
|
80
|
+
|
|
81
|
+
- `POST /control-plane/transfers/request` - Request a transfer
|
|
82
|
+
- `GET /control-plane/transfers/:pid` - Get transfer process state
|
|
83
|
+
- `POST /control-plane/transfers/:pid/start` - Start a transfer
|
|
84
|
+
- `POST /control-plane/transfers/:pid/complete` - Complete a transfer
|
|
85
|
+
- `POST /control-plane/transfers/:pid/suspend` - Suspend a transfer
|
|
86
|
+
- `POST /control-plane/transfers/:pid/terminate` - Terminate a transfer
|
|
87
|
+
|
|
88
|
+
**Service Methods:**
|
|
89
|
+
|
|
90
|
+
- `requestTransfer()` - Initiate a transfer request (requires existing agreement)
|
|
91
|
+
- `getTransferProcess()` - Retrieve transfer process state
|
|
92
|
+
- `startTransfer()` - Start an approved transfer
|
|
93
|
+
- `completeTransfer()` - Mark transfer as complete
|
|
94
|
+
- `suspendTransfer()` - Temporarily suspend a transfer
|
|
95
|
+
- `terminateTransfer()` - Terminate a transfer
|
|
96
|
+
|
|
97
|
+
### Shared Storage Architecture
|
|
98
|
+
|
|
99
|
+
The Control Plane writes `TransferProcessEntity` to entity storage, which is read by the Data Plane for `consumerPid` resolution:
|
|
100
|
+
|
|
101
|
+
```text
|
|
102
|
+
Control Plane Data Plane
|
|
103
|
+
│ │
|
|
104
|
+
│ set(TransferProcessEntity) │ get(consumerPid)
|
|
105
|
+
│ │
|
|
106
|
+
└───── Entity Storage ──────────┘
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
## Examples
|
|
110
|
+
|
|
111
|
+
Usage of the APIs is shown in the examples [docs/examples.md](docs/examples.md)
|
|
112
|
+
|
|
113
|
+
## Reference
|
|
114
|
+
|
|
115
|
+
Detailed reference documentation for the API can be found in [docs/reference/index.md](docs/reference/index.md)
|
|
116
|
+
|
|
117
|
+
## Changelog
|
|
118
|
+
|
|
119
|
+
The changes between each version can be found in [docs/changelog.md](docs/changelog.md)
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
// Copyright 2025 IOTA Stiftung.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
3
|
+
import { ComponentFactory } from "@twin.org/core";
|
|
4
|
+
import { DataspaceProtocolContractNegotiationStateType } from "@twin.org/standards-dataspace-protocol";
|
|
5
|
+
/**
|
|
6
|
+
* Policy Requester for Dataspace Control Plane.
|
|
7
|
+
*
|
|
8
|
+
* Handles contract negotiation callbacks from PNP and forwards state changes
|
|
9
|
+
* to the control plane service via the INegotiationCallback interface.
|
|
10
|
+
*
|
|
11
|
+
* Callback Flow:
|
|
12
|
+
* 1. Control Plane initiates negotiation via PNP.sendRequestToProvider()
|
|
13
|
+
* 2. PNP calls offer() → auto-accept, notify control plane
|
|
14
|
+
* 3. PNP calls agreement() → store agreement, notify control plane
|
|
15
|
+
* 4. PNP calls finalised() → notify control plane with agreementId
|
|
16
|
+
* 5. Control plane notifies upstream caller (e.g. supply-chain)
|
|
17
|
+
*/
|
|
18
|
+
export class DataspaceControlPlanePolicyRequester {
|
|
19
|
+
/**
|
|
20
|
+
* Runtime name for the class.
|
|
21
|
+
*/
|
|
22
|
+
static CLASS_NAME = "DataspaceControlPlanePolicyRequester";
|
|
23
|
+
/**
|
|
24
|
+
* The logging component.
|
|
25
|
+
* @internal
|
|
26
|
+
*/
|
|
27
|
+
_loggingComponent;
|
|
28
|
+
/**
|
|
29
|
+
* Callback interface for notifying the control plane of negotiation state changes.
|
|
30
|
+
* @internal
|
|
31
|
+
*/
|
|
32
|
+
_callback;
|
|
33
|
+
/**
|
|
34
|
+
* Active negotiations tracked in memory.
|
|
35
|
+
* Key: negotiationId
|
|
36
|
+
* Value: Negotiation state
|
|
37
|
+
* @internal
|
|
38
|
+
*/
|
|
39
|
+
_negotiations;
|
|
40
|
+
/**
|
|
41
|
+
* Create a new instance of DataspaceControlPlanePolicyRequester.
|
|
42
|
+
* @param loggingComponentType Optional logging component type.
|
|
43
|
+
* @param callback Optional callback interface for state change notifications.
|
|
44
|
+
*/
|
|
45
|
+
constructor(loggingComponentType, callback) {
|
|
46
|
+
this._loggingComponent = ComponentFactory.getIfExists(loggingComponentType ?? "logging");
|
|
47
|
+
this._callback = callback;
|
|
48
|
+
this._negotiations = new Map();
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Returns the class name of the component.
|
|
52
|
+
* @returns The class name of the component.
|
|
53
|
+
*/
|
|
54
|
+
className() {
|
|
55
|
+
return DataspaceControlPlanePolicyRequester.CLASS_NAME;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Register a negotiation for tracking.
|
|
59
|
+
* Called by the control plane service after initiating a negotiation via PNP.
|
|
60
|
+
* @param negotiationId The negotiation ID returned by PNP.sendRequestToProvider().
|
|
61
|
+
*/
|
|
62
|
+
trackNegotiation(negotiationId) {
|
|
63
|
+
const now = Date.now();
|
|
64
|
+
this._negotiations.set(negotiationId, {
|
|
65
|
+
negotiationId,
|
|
66
|
+
state: DataspaceProtocolContractNegotiationStateType.REQUESTED,
|
|
67
|
+
startedAt: now,
|
|
68
|
+
updatedAt: now
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Get all active negotiations (for stalled cleanup).
|
|
73
|
+
* @returns Map of negotiationId to negotiation state.
|
|
74
|
+
*/
|
|
75
|
+
getActiveNegotiations() {
|
|
76
|
+
return this._negotiations;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Remove a negotiation from tracking (for cleanup).
|
|
80
|
+
* @param negotiationId The negotiation ID to remove.
|
|
81
|
+
*/
|
|
82
|
+
removeNegotiation(negotiationId) {
|
|
83
|
+
this._negotiations.delete(negotiationId);
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* A policy has been offered by a provider.
|
|
87
|
+
* Called by PNP when provider sends an OfferMessage.
|
|
88
|
+
* @param negotiationId The id of the negotiation.
|
|
89
|
+
* @param offer The offer sent by the provider.
|
|
90
|
+
* @returns True if the offer was accepted, false otherwise.
|
|
91
|
+
*/
|
|
92
|
+
async offer(negotiationId, offer) {
|
|
93
|
+
await this._loggingComponent?.log({
|
|
94
|
+
level: "info",
|
|
95
|
+
source: DataspaceControlPlanePolicyRequester.CLASS_NAME,
|
|
96
|
+
ts: Date.now(),
|
|
97
|
+
message: "offerReceived",
|
|
98
|
+
data: {
|
|
99
|
+
negotiationId,
|
|
100
|
+
offerId: offer.uid
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
const negotiation = this._negotiations.get(negotiationId);
|
|
104
|
+
if (negotiation) {
|
|
105
|
+
negotiation.state = DataspaceProtocolContractNegotiationStateType.OFFERED;
|
|
106
|
+
negotiation.updatedAt = Date.now();
|
|
107
|
+
}
|
|
108
|
+
await this._callback?.onStateChanged(negotiationId, DataspaceProtocolContractNegotiationStateType.OFFERED, { offer });
|
|
109
|
+
return true;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* A policy agreement has been sent by a provider.
|
|
113
|
+
* Called by PNP when provider sends an AgreementMessage.
|
|
114
|
+
* @param negotiationId The id of the negotiation.
|
|
115
|
+
* @param agreement The agreement sent by the provider.
|
|
116
|
+
* @returns True if the agreement was accepted, false otherwise.
|
|
117
|
+
*/
|
|
118
|
+
async agreement(negotiationId, agreement) {
|
|
119
|
+
await this._loggingComponent?.log({
|
|
120
|
+
level: "info",
|
|
121
|
+
source: DataspaceControlPlanePolicyRequester.CLASS_NAME,
|
|
122
|
+
ts: Date.now(),
|
|
123
|
+
message: "agreementReceived",
|
|
124
|
+
data: {
|
|
125
|
+
negotiationId,
|
|
126
|
+
agreementId: agreement.uid
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
const negotiation = this._negotiations.get(negotiationId);
|
|
130
|
+
if (negotiation) {
|
|
131
|
+
negotiation.state = DataspaceProtocolContractNegotiationStateType.AGREED;
|
|
132
|
+
negotiation.agreement = agreement;
|
|
133
|
+
negotiation.updatedAt = Date.now();
|
|
134
|
+
}
|
|
135
|
+
await this._callback?.onStateChanged(negotiationId, DataspaceProtocolContractNegotiationStateType.AGREED, { agreement });
|
|
136
|
+
return true;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* A policy finalisation has been sent by a provider.
|
|
140
|
+
* Called by PNP when provider sends a FinalizedEvent.
|
|
141
|
+
* @param negotiationId The id of the negotiation.
|
|
142
|
+
* @returns Nothing.
|
|
143
|
+
*/
|
|
144
|
+
async finalised(negotiationId) {
|
|
145
|
+
await this._loggingComponent?.log({
|
|
146
|
+
level: "info",
|
|
147
|
+
source: DataspaceControlPlanePolicyRequester.CLASS_NAME,
|
|
148
|
+
ts: Date.now(),
|
|
149
|
+
message: "negotiationFinalized",
|
|
150
|
+
data: { negotiationId }
|
|
151
|
+
});
|
|
152
|
+
const negotiation = this._negotiations.get(negotiationId);
|
|
153
|
+
if (negotiation) {
|
|
154
|
+
negotiation.state = DataspaceProtocolContractNegotiationStateType.FINALIZED;
|
|
155
|
+
negotiation.updatedAt = Date.now();
|
|
156
|
+
const agreementId = negotiation.agreement?.uid;
|
|
157
|
+
this._negotiations.delete(negotiationId);
|
|
158
|
+
await this._loggingComponent?.log({
|
|
159
|
+
level: "info",
|
|
160
|
+
source: DataspaceControlPlanePolicyRequester.CLASS_NAME,
|
|
161
|
+
ts: Date.now(),
|
|
162
|
+
message: "negotiationCompleted",
|
|
163
|
+
data: {
|
|
164
|
+
negotiationId,
|
|
165
|
+
agreementId,
|
|
166
|
+
durationMs: Date.now() - negotiation.startedAt
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
if (agreementId) {
|
|
170
|
+
await this._callback?.onCompleted(negotiationId, agreementId);
|
|
171
|
+
}
|
|
172
|
+
else {
|
|
173
|
+
await this._callback?.onFailed(negotiationId, "negotiationFinalizedNoAgreement");
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
else {
|
|
177
|
+
await this._loggingComponent?.log({
|
|
178
|
+
level: "warn",
|
|
179
|
+
source: DataspaceControlPlanePolicyRequester.CLASS_NAME,
|
|
180
|
+
ts: Date.now(),
|
|
181
|
+
message: "unknownNegotiationFinalized",
|
|
182
|
+
data: { negotiationId }
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* A policy termination has been sent by a provider.
|
|
188
|
+
* Called by PNP when provider sends a TerminatedMessage or negotiation fails.
|
|
189
|
+
* @param negotiationId The id of the negotiation.
|
|
190
|
+
* @returns Nothing.
|
|
191
|
+
*/
|
|
192
|
+
async terminated(negotiationId) {
|
|
193
|
+
await this._loggingComponent?.log({
|
|
194
|
+
level: "warn",
|
|
195
|
+
source: DataspaceControlPlanePolicyRequester.CLASS_NAME,
|
|
196
|
+
ts: Date.now(),
|
|
197
|
+
message: "negotiationTerminated",
|
|
198
|
+
data: { negotiationId }
|
|
199
|
+
});
|
|
200
|
+
const negotiation = this._negotiations.get(negotiationId);
|
|
201
|
+
if (negotiation) {
|
|
202
|
+
negotiation.state = DataspaceProtocolContractNegotiationStateType.TERMINATED;
|
|
203
|
+
negotiation.updatedAt = Date.now();
|
|
204
|
+
this._negotiations.delete(negotiationId);
|
|
205
|
+
await this._loggingComponent?.log({
|
|
206
|
+
level: "info",
|
|
207
|
+
source: DataspaceControlPlanePolicyRequester.CLASS_NAME,
|
|
208
|
+
ts: Date.now(),
|
|
209
|
+
message: "negotiationRejected",
|
|
210
|
+
data: {
|
|
211
|
+
negotiationId,
|
|
212
|
+
durationMs: Date.now() - negotiation.startedAt
|
|
213
|
+
}
|
|
214
|
+
});
|
|
215
|
+
await this._callback?.onFailed(negotiationId, "negotiationTerminatedByProvider");
|
|
216
|
+
}
|
|
217
|
+
else {
|
|
218
|
+
await this._loggingComponent?.log({
|
|
219
|
+
level: "warn",
|
|
220
|
+
source: DataspaceControlPlanePolicyRequester.CLASS_NAME,
|
|
221
|
+
ts: Date.now(),
|
|
222
|
+
message: "unknownNegotiationTerminated",
|
|
223
|
+
data: { negotiationId }
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
//# sourceMappingURL=dataspaceControlPlanePolicyRequester.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dataspaceControlPlanePolicyRequester.js","sourceRoot":"","sources":["../../src/dataspaceControlPlanePolicyRequester.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAKlD,OAAO,EAAE,6CAA6C,EAAE,MAAM,wCAAwC,CAAC;AAIvG;;;;;;;;;;;;GAYG;A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I,CAAC,iBAAiB,EAAE,GAAG,CAAC;gBACjC,KAAK,EAAE,MAAM;gBACb,MAAM,EAAE,oCAAoC,CAAC,UAAU;gBACvD,EAAE,EAAE,IAAI,CAAC,GAAG,EAAE;gBACd,OAAO,EAAE,8BAA8B;gBACvC,IAAI,EAAE,EAAE,aAAa,EAAE;aACvB,CAAC,CAAC;QACJ,CAAC;IACF,CAAC","sourcesContent":["// Copyright 2025 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { ComponentFactory } from \"@twin.org/core\";\nimport type { INegotiationCallback } from \"@twin.org/dataspace-models\";\nimport type { ILoggingComponent } from \"@twin.org/logging-models\";\nimport { nameof } from \"@twin.org/nameof\";\nimport type { IPolicyRequester } from \"@twin.org/rights-management-models\";\nimport { DataspaceProtocolContractNegotiationStateType } from \"@twin.org/standards-dataspace-protocol\";\nimport type { IOdrlAgreement, IOdrlOffer } from \"@twin.org/standards-w3c-odrl\";\nimport type { INegotiationState } from \"./models/INegotiationState.js\";\n\n/**\n * Policy Requester for Dataspace Control Plane.\n *\n * Handles contract negotiation callbacks from PNP and forwards state changes\n * to the control plane service via the INegotiationCallback interface.\n *\n * Callback Flow:\n * 1. Control Plane initiates negotiation via PNP.sendRequestToProvider()\n * 2. PNP calls offer() → auto-accept, notify control plane\n * 3. PNP calls agreement() → store agreement, notify control plane\n * 4. PNP calls finalised() → notify control plane with agreementId\n * 5. Control plane notifies upstream caller (e.g. supply-chain)\n */\nexport class DataspaceControlPlanePolicyRequester implements IPolicyRequester {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<DataspaceControlPlanePolicyRequester>();\n\n\t/**\n\t * The logging component.\n\t * @internal\n\t */\n\tprivate readonly _loggingComponent?: ILoggingComponent;\n\n\t/**\n\t * Callback interface for notifying the control plane of negotiation state changes.\n\t * @internal\n\t */\n\tprivate readonly _callback?: INegotiationCallback;\n\n\t/**\n\t * Active negotiations tracked in memory.\n\t * Key: negotiationId\n\t * Value: Negotiation state\n\t * @internal\n\t */\n\tprivate readonly _negotiations: Map<string, INegotiationState>;\n\n\t/**\n\t * Create a new instance of DataspaceControlPlanePolicyRequester.\n\t * @param loggingComponentType Optional logging component type.\n\t * @param callback Optional callback interface for state change notifications.\n\t */\n\tconstructor(loggingComponentType?: string, callback?: INegotiationCallback) {\n\t\tthis._loggingComponent = ComponentFactory.getIfExists<ILoggingComponent>(\n\t\t\tloggingComponentType ?? \"logging\"\n\t\t);\n\t\tthis._callback = callback;\n\t\tthis._negotiations = new Map();\n\t}\n\n\t/**\n\t * Returns the class name of the component.\n\t * @returns The class name of the component.\n\t */\n\tpublic className(): string {\n\t\treturn DataspaceControlPlanePolicyRequester.CLASS_NAME;\n\t}\n\n\t/**\n\t * Register a negotiation for tracking.\n\t * Called by the control plane service after initiating a negotiation via PNP.\n\t * @param negotiationId The negotiation ID returned by PNP.sendRequestToProvider().\n\t */\n\tpublic trackNegotiation(negotiationId: string): void {\n\t\tconst now = Date.now();\n\t\tthis._negotiations.set(negotiationId, {\n\t\t\tnegotiationId,\n\t\t\tstate: DataspaceProtocolContractNegotiationStateType.REQUESTED,\n\t\t\tstartedAt: now,\n\t\t\tupdatedAt: now\n\t\t});\n\t}\n\n\t/**\n\t * Get all active negotiations (for stalled cleanup).\n\t * @returns Map of negotiationId to negotiation state.\n\t */\n\tpublic getActiveNegotiations(): Map<string, INegotiationState> {\n\t\treturn this._negotiations;\n\t}\n\n\t/**\n\t * Remove a negotiation from tracking (for cleanup).\n\t * @param negotiationId The negotiation ID to remove.\n\t */\n\tpublic removeNegotiation(negotiationId: string): void {\n\t\tthis._negotiations.delete(negotiationId);\n\t}\n\n\t/**\n\t * A policy has been offered by a provider.\n\t * Called by PNP when provider sends an OfferMessage.\n\t * @param negotiationId The id of the negotiation.\n\t * @param offer The offer sent by the provider.\n\t * @returns True if the offer was accepted, false otherwise.\n\t */\n\tpublic async offer(negotiationId: string, offer: IOdrlOffer): Promise<boolean> {\n\t\tawait this._loggingComponent?.log({\n\t\t\tlevel: \"info\",\n\t\t\tsource: DataspaceControlPlanePolicyRequester.CLASS_NAME,\n\t\t\tts: Date.now(),\n\t\t\tmessage: \"offerReceived\",\n\t\t\tdata: {\n\t\t\t\tnegotiationId,\n\t\t\t\tofferId: offer.uid\n\t\t\t}\n\t\t});\n\n\t\tconst negotiation = this._negotiations.get(negotiationId);\n\t\tif (negotiation) {\n\t\t\tnegotiation.state = DataspaceProtocolContractNegotiationStateType.OFFERED;\n\t\t\tnegotiation.updatedAt = Date.now();\n\t\t}\n\n\t\tawait this._callback?.onStateChanged(\n\t\t\tnegotiationId,\n\t\t\tDataspaceProtocolContractNegotiationStateType.OFFERED,\n\t\t\t{ offer }\n\t\t);\n\n\t\treturn true;\n\t}\n\n\t/**\n\t * A policy agreement has been sent by a provider.\n\t * Called by PNP when provider sends an AgreementMessage.\n\t * @param negotiationId The id of the negotiation.\n\t * @param agreement The agreement sent by the provider.\n\t * @returns True if the agreement was accepted, false otherwise.\n\t */\n\tpublic async agreement(negotiationId: string, agreement: IOdrlAgreement): Promise<boolean> {\n\t\tawait this._loggingComponent?.log({\n\t\t\tlevel: \"info\",\n\t\t\tsource: DataspaceControlPlanePolicyRequester.CLASS_NAME,\n\t\t\tts: Date.now(),\n\t\t\tmessage: \"agreementReceived\",\n\t\t\tdata: {\n\t\t\t\tnegotiationId,\n\t\t\t\tagreementId: agreement.uid\n\t\t\t}\n\t\t});\n\n\t\tconst negotiation = this._negotiations.get(negotiationId);\n\t\tif (negotiation) {\n\t\t\tnegotiation.state = DataspaceProtocolContractNegotiationStateType.AGREED;\n\t\t\tnegotiation.agreement = agreement;\n\t\t\tnegotiation.updatedAt = Date.now();\n\t\t}\n\n\t\tawait this._callback?.onStateChanged(\n\t\t\tnegotiationId,\n\t\t\tDataspaceProtocolContractNegotiationStateType.AGREED,\n\t\t\t{ agreement }\n\t\t);\n\n\t\treturn true;\n\t}\n\n\t/**\n\t * A policy finalisation has been sent by a provider.\n\t * Called by PNP when provider sends a FinalizedEvent.\n\t * @param negotiationId The id of the negotiation.\n\t * @returns Nothing.\n\t */\n\tpublic async finalised(negotiationId: string): Promise<void> {\n\t\tawait this._loggingComponent?.log({\n\t\t\tlevel: \"info\",\n\t\t\tsource: DataspaceControlPlanePolicyRequester.CLASS_NAME,\n\t\t\tts: Date.now(),\n\t\t\tmessage: \"negotiationFinalized\",\n\t\t\tdata: { negotiationId }\n\t\t});\n\n\t\tconst negotiation = this._negotiations.get(negotiationId);\n\t\tif (negotiation) {\n\t\t\tnegotiation.state = DataspaceProtocolContractNegotiationStateType.FINALIZED;\n\t\t\tnegotiation.updatedAt = Date.now();\n\n\t\t\tconst agreementId = negotiation.agreement?.uid;\n\t\t\tthis._negotiations.delete(negotiationId);\n\n\t\t\tawait this._loggingComponent?.log({\n\t\t\t\tlevel: \"info\",\n\t\t\t\tsource: DataspaceControlPlanePolicyRequester.CLASS_NAME,\n\t\t\t\tts: Date.now(),\n\t\t\t\tmessage: \"negotiationCompleted\",\n\t\t\t\tdata: {\n\t\t\t\t\tnegotiationId,\n\t\t\t\t\tagreementId,\n\t\t\t\t\tdurationMs: Date.now() - negotiation.startedAt\n\t\t\t\t}\n\t\t\t});\n\n\t\t\tif (agreementId) {\n\t\t\t\tawait this._callback?.onCompleted(negotiationId, agreementId);\n\t\t\t} else {\n\t\t\t\tawait this._callback?.onFailed(negotiationId, \"negotiationFinalizedNoAgreement\");\n\t\t\t}\n\t\t} else {\n\t\t\tawait this._loggingComponent?.log({\n\t\t\t\tlevel: \"warn\",\n\t\t\t\tsource: DataspaceControlPlanePolicyRequester.CLASS_NAME,\n\t\t\t\tts: Date.now(),\n\t\t\t\tmessage: \"unknownNegotiationFinalized\",\n\t\t\t\tdata: { negotiationId }\n\t\t\t});\n\t\t}\n\t}\n\n\t/**\n\t * A policy termination has been sent by a provider.\n\t * Called by PNP when provider sends a TerminatedMessage or negotiation fails.\n\t * @param negotiationId The id of the negotiation.\n\t * @returns Nothing.\n\t */\n\tpublic async terminated(negotiationId: string): Promise<void> {\n\t\tawait this._loggingComponent?.log({\n\t\t\tlevel: \"warn\",\n\t\t\tsource: DataspaceControlPlanePolicyRequester.CLASS_NAME,\n\t\t\tts: Date.now(),\n\t\t\tmessage: \"negotiationTerminated\",\n\t\t\tdata: { negotiationId }\n\t\t});\n\n\t\tconst negotiation = this._negotiations.get(negotiationId);\n\t\tif (negotiation) {\n\t\t\tnegotiation.state = DataspaceProtocolContractNegotiationStateType.TERMINATED;\n\t\t\tnegotiation.updatedAt = Date.now();\n\n\t\t\tthis._negotiations.delete(negotiationId);\n\n\t\t\tawait this._loggingComponent?.log({\n\t\t\t\tlevel: \"info\",\n\t\t\t\tsource: DataspaceControlPlanePolicyRequester.CLASS_NAME,\n\t\t\t\tts: Date.now(),\n\t\t\t\tmessage: \"negotiationRejected\",\n\t\t\t\tdata: {\n\t\t\t\t\tnegotiationId,\n\t\t\t\t\tdurationMs: Date.now() - negotiation.startedAt\n\t\t\t\t}\n\t\t\t});\n\n\t\t\tawait this._callback?.onFailed(negotiationId, \"negotiationTerminatedByProvider\");\n\t\t} else {\n\t\t\tawait this._loggingComponent?.log({\n\t\t\t\tlevel: \"warn\",\n\t\t\t\tsource: DataspaceControlPlanePolicyRequester.CLASS_NAME,\n\t\t\t\tts: Date.now(),\n\t\t\t\tmessage: \"unknownNegotiationTerminated\",\n\t\t\t\tdata: { negotiationId }\n\t\t\t});\n\t\t}\n\t}\n}\n"]}
|