blob: c65e9731b04a5cec8ab06bd5d8ddb6a5c7ddf191 [file] [log] [blame]
Constantin Ziesche687f8882020-10-02 16:17:44 +02001/*******************************************************************************
2* Copyright (c) 2020 Robert Bosch GmbH
3* Author: Constantin Ziesche (constantin.ziesche@bosch.com)
4*
5* This program and the accompanying materials are made available under the
6* terms of the Eclipse Public License 2.0 which is available at
7* http://www.eclipse.org/legal/epl-2.0
8*
9* SPDX-License-Identifier: EPL-2.0
10*******************************************************************************/
11using System.Collections.Generic;
12using Microsoft.AspNetCore.Mvc;
13using BaSyx.Models.Core.AssetAdministrationShell.Generics;
14using BaSyx.Utils.ResultHandling;
15using BaSyx.API.Components;
16using BaSyx.Models.Core.AssetAdministrationShell.Implementations;
17using BaSyx.Models.Communication;
18
19namespace BaSyx.API.Http.Controllers
20{
21 /// <summary>
22 /// The Asset Administration Shell Repository Controller
23 /// </summary>
24 public class AssetAdministrationShellRepositoryController : Controller
25 {
26 private readonly IAssetAdministrationShellRepositoryServiceProvider serviceProvider;
27
28 /// <summary>
29 /// The constructor for the Asset Administration Shell Repository Controller
30 /// </summary>
31 /// <param name="assetAdministrationShellRepositoryServiceProvider"></param>
32 public AssetAdministrationShellRepositoryController(IAssetAdministrationShellRepositoryServiceProvider assetAdministrationShellRepositoryServiceProvider)
33 {
34 serviceProvider = assetAdministrationShellRepositoryServiceProvider;
35 }
36
37 /// <summary>
38 /// Retrieves all Asset Administration Shells from the Asset Administration Shell repository
39 /// </summary>
40 /// <returns></returns>
41 /// <response code="200">Returns a list of found Asset Administration Shells</response>
42 [HttpGet("shells", Name = "GetAllAssetAdministrationShells")]
43 [Produces("application/json")]
44 [ProducesResponseType(typeof(List<BaSyx.Models.Core.AssetAdministrationShell.Implementations.AssetAdministrationShell>), 200)]
45 public IActionResult GetAllAssetAdministrationShells()
46 {
47 var result = serviceProvider.RetrieveAssetAdministrationShells();
48 return result.CreateActionResult(CrudOperation.Retrieve);
49 }
50 /// <summary>
51 /// Retrieves a specific Asset Administration Shell from the Asset Administration Shell repository
52 /// </summary>
53 /// <param name="aasId">The Asset Administration Shell's unique id</param>
54 /// <returns></returns>
55 /// <response code="200">Returns the requested Asset Administration Shell</response>
56 /// <response code="404">No Asset Administration Shell found</response>
57 [HttpGet("shells/{aasId}")]
58 [HttpGet("shells/{aasId}/aas", Name = "GetAssetAdministrationShellById")]
59 [Produces("application/json")]
60 [ProducesResponseType(typeof(BaSyx.Models.Core.AssetAdministrationShell.Implementations.AssetAdministrationShell), 200)]
61 public IActionResult GetAssetAdministrationShellById(string aasId)
62 {
63 if (string.IsNullOrEmpty(aasId))
64 return ResultHandling.NullResult(nameof(aasId));
65
66 var result = serviceProvider.RetrieveAssetAdministrationShell(aasId);
67 return result.CreateActionResult(CrudOperation.Retrieve);
68 }
69
70 /// <summary>
71 /// Creates or updates a Asset Administration Shell at the Asset Administration Shell repository
72 /// </summary>
73 /// <param name="aasId">The Asset Administration Shell's unique id</param>
74 /// <param name="aas">The Asset Administration Shell</param>
75 /// <returns></returns>
76 /// <response code="201">Asset Administration Shell created successfully</response>
77 /// <response code="400">Bad Request</response>
78 [HttpPut("shells/{aasId}", Name = "PutAssetAdministrationShell")]
79 [Produces("application/json")]
80 [Consumes("application/json")]
81 [ProducesResponseType(typeof(BaSyx.Models.Core.AssetAdministrationShell.Implementations.AssetAdministrationShell), 201)]
82 public IActionResult PutAssetAdministrationShell(string aasId, [FromBody] IAssetAdministrationShell aas)
83 {
84 if (string.IsNullOrEmpty(aasId))
85 return ResultHandling.NullResult(nameof(aasId));
86 if (aas == null)
87 return ResultHandling.NullResult(nameof(aas));
88
89 var result = serviceProvider.CreateAssetAdministrationShell(aas);
90 return result.CreateActionResult(CrudOperation.Create);
91 }
92 /// <summary>
93 /// Deletes a specific Asset Administration Shell at the Asset Administration Shell repository
94 /// </summary>
95 /// <param name="aasId">The Asset Administration Shell's unique id</param>
96 /// <returns></returns>
97 /// <response code="200">Asset Administration Shell deleted successfully</response>
98 [HttpDelete("shells/{aasId}", Name = "DeleteAssetAdministrationShellById")]
99 [Produces("application/json")]
100 [ProducesResponseType(typeof(Result), 200)]
101 public IActionResult DeleteAssetAdministrationShellById(string aasId)
102 {
103 if (string.IsNullOrEmpty(aasId))
104 return ResultHandling.NullResult(nameof(aasId));
105
106 var result = serviceProvider.DeleteAssetAdministrationShell(aasId);
107 return result.CreateActionResult(CrudOperation.Delete);
108 }
109
110 #region AssetAdministrationShell-Services
111
112 /// <summary>
113 /// Retrieves all Submodels from the Asset Administration Shell
114 /// </summary>
115 /// <param name="aasId">The Asset Administration Shell's unique id</param>
116 /// <returns></returns>
117 /// <response code="200">Returns a list of found Submodels</response>
118 /// <response code="404">No Submodel Service Providers found</response>
119 [HttpGet("shells/{aasId}/aas/submodels", Name = "ShellRepo_GetSubmodelsFromShell")]
120 [Produces("application/json")]
121 [ProducesResponseType(typeof(Result), 200)]
122 public IActionResult ShellRepo_GetSubmodelsFromShell(string aasId)
123 {
124 if (IsNullOrNotFound(aasId, out IActionResult result, out IAssetAdministrationShellServiceProvider provider))
125 return result;
126
127 var service = new AssetAdministrationShellController(provider);
128 return service.GetSubmodelsFromShell();
129 }
130
131 /// <summary>
132 /// Creates or updates a Submodel to an existing Asset Administration Shell
133 /// </summary>
134 /// <param name="aasId">The Asset Administration Shell's unique id</param>
135 /// <param name="submodelIdShort">The Submodel's short id</param>
136 /// <param name="submodel">The serialized Submodel object</param>
137 /// <returns></returns>
138 /// <response code="201">Submodel created successfully</response>
139 /// <response code="400">Bad Request</response>
140 [HttpPut("shells/{aasId}/aas/submodels/{submodelIdShort}", Name = "ShellRepo_PutSubmodelToShell")]
141 [Produces("application/json")]
142 [Consumes("application/json")]
143 [ProducesResponseType(typeof(Submodel), 201)]
144 [ProducesResponseType(typeof(Result), 400)]
145 public IActionResult ShellRepo_PutSubmodelToShell(string aasId, string submodelIdShort, [FromBody] ISubmodel submodel)
146 {
147 if (IsNullOrNotFound(aasId, out IActionResult result, out IAssetAdministrationShellServiceProvider provider))
148 return result;
149
150 var service = new AssetAdministrationShellController(provider);
151 return service.PutSubmodelToShell(submodelIdShort, submodel);
152 }
153
154 /// <summary>
155 /// Retrieves the Submodel from the Asset Administration Shell
156 /// </summary>
157 /// <param name="aasId">The Asset Administration Shell's unique id</param>
158 /// <param name="submodelIdShort">The Submodel's short id</param>
159 /// <returns></returns>
160 /// <response code="200">Submodel retrieved successfully</response>
161 /// <response code="404">No Submodel Service Provider found</response>
162 [HttpGet("shells/{aasId}/aas/submodels/{submodelIdShort}")]
163 [HttpGet("shells/{aasId}/aas/submodels/{submodelIdShort}/submodel", Name = "ShellRepo_GetSubmodelFromShellByIdShort")]
164 [Produces("application/json")]
165 [ProducesResponseType(typeof(Submodel), 200)]
166 [ProducesResponseType(typeof(Result), 400)]
167 [ProducesResponseType(typeof(Result), 404)]
168 public IActionResult ShellRepo_GetSubmodelFromShellByIdShort(string aasId, string submodelIdShort)
169 {
170 if (IsNullOrNotFound(aasId, out IActionResult result, out IAssetAdministrationShellServiceProvider provider))
171 return result;
172
173 var service = new AssetAdministrationShellController(provider);
174 return service.GetSubmodelFromShellByIdShort(submodelIdShort);
175 }
176
177 /// <summary>
178 /// Deletes a specific Submodel from the Asset Administration Shell
179 /// </summary>
180 /// <param name="aasId">The Asset Administration Shell's unique id</param>
181 /// <param name="submodelIdShort">The Submodel's short id</param>
182 /// <returns></returns>
183 /// <response code="204">Submodel deleted successfully</response>
184 /// <response code="400">Bad Request</response>
185 [HttpDelete("shells/{aasId}/aas/submodels/{submodelIdShort}", Name = "ShellRepo_DeleteSubmodelFromShellByIdShort")]
186 [Produces("application/json")]
187 [ProducesResponseType(typeof(Result), 400)]
188 public IActionResult ShellRepo_DeleteSubmodelFromShellByIdShort(string aasId, string submodelIdShort)
189 {
190 if (IsNullOrNotFound(aasId, out IActionResult result, out IAssetAdministrationShellServiceProvider provider))
191 return result;
192
193 var service = new AssetAdministrationShellController(provider);
194 return service.DeleteSubmodelFromShellByIdShort(submodelIdShort);
195 }
196
197 /// <summary>
198 /// Retrieves the minimized version of a Submodel, i.e. only the values of SubmodelElements are serialized and returned
199 /// </summary>
200 /// <param name="aasId">The Asset Administration Shell's unique id</param>
201 /// <param name="submodelIdShort">The Submodel's short id</param>
202 /// <returns></returns>
203 /// <response code="200">Success</response>
204 /// <response code="404">Submodel not found</response>
205 [HttpGet("shells/{aasId}/aas/submodels/{submodelIdShort}/submodel/values", Name = "ShellRepo_GetSubmodelValues")]
206 [Produces("application/json")]
207 [ProducesResponseType(typeof(Result), 404)]
208 public IActionResult ShellRepo_GetSubmodelValues(string aasId, string submodelIdShort)
209 {
210 if (IsNullOrNotFound(aasId, out IActionResult result, out IAssetAdministrationShellServiceProvider provider))
211 return result;
212
213 var service = new AssetAdministrationShellController(provider);
214 return service.Shell_GetSubmodelValues(submodelIdShort);
215 }
216
217 /// <summary>
218 /// Retrieves all Submodel-Elements from the Submodel
219 /// </summary>
220 /// <param name="aasId">The Asset Administration Shell's unique id</param>
221 /// <param name="submodelIdShort">The Submodel's short id</param>
222 /// <returns></returns>
223 /// <response code="200">Returns a list of found Submodel-Elements</response>
224 /// <response code="404">Submodel not found</response>
225 [HttpGet("shells/{aasId}/aas/submodels/{submodelIdShort}/submodel/submodelElements", Name = "ShellRepo_GetSubmodelElements")]
226 [Produces("application/json")]
227 [ProducesResponseType(typeof(SubmodelElement[]), 200)]
228 [ProducesResponseType(typeof(Result), 404)]
229 public IActionResult ShellRepo_GetSubmodelElements(string aasId, string submodelIdShort)
230 {
231 if (IsNullOrNotFound(aasId, out IActionResult result, out IAssetAdministrationShellServiceProvider provider))
232 return result;
233
234 var service = new AssetAdministrationShellController(provider);
235 return service.Shell_GetSubmodelElements(submodelIdShort);
236 }
237
238 /// <summary>
239 /// Creates or updates a Submodel-Element at the Submodel
240 /// </summary>
241 /// <param name="aasId">The Asset Administration Shell's unique id</param>
242 /// <param name="submodelIdShort">The Submodel's short id</param>
243 /// <param name="seIdShortPath">The Submodel-Element's IdShort-Path</param>
244 /// <param name="submodelElement">The Submodel-Element object</param>
245 /// <returns></returns>
246 /// <response code="201">Submodel-Element created successfully</response>
247 /// <response code="400">Bad Request</response>
248 /// <response code="404">Submodel not found</response>
249 [HttpPut("shells/{aasId}/aas/submodels/{submodelIdShort}/submodel/submodelElements/{*seIdShortPath}", Name = "ShellRepo_PutSubmodelElement")]
250 [Produces("application/json")]
251 [Consumes("application/json")]
252 [ProducesResponseType(typeof(SubmodelElement), 201)]
253 [ProducesResponseType(typeof(Result), 400)]
254 [ProducesResponseType(typeof(Result), 404)]
255 public IActionResult ShellRepo_PutSubmodelElement(string aasId, string submodelIdShort, string seIdShortPath, [FromBody] ISubmodelElement submodelElement)
256 {
257 if (IsNullOrNotFound(aasId, out IActionResult result, out IAssetAdministrationShellServiceProvider provider))
258 return result;
259
260 var service = new AssetAdministrationShellController(provider);
261 return service.Shell_PutSubmodelElement(submodelIdShort, seIdShortPath, submodelElement);
262 }
263
264 /// <summary>
265 /// Retrieves a specific Submodel-Element from the Submodel
266 /// </summary>
267 /// <param name="aasId">The Asset Administration Shell's unique id</param>
268 /// <param name="submodelIdShort">The Submodel's short id</param>
269 /// <param name="seIdShortPath">The Submodel-Element's IdShort-Path</param>
270 /// <returns></returns>
271 /// <response code="200">Returns the requested Submodel-Element</response>
272 /// <response code="404">Submodel / Submodel-Element not found</response>
273 [HttpGet("shells/{aasId}/aas/submodels/{submodelIdShort}/submodel/submodelElements/{seIdShortPath}", Name = "ShellRepo_GetSubmodelElementByIdShort")]
274 [Produces("application/json")]
275 [ProducesResponseType(typeof(SubmodelElement), 200)]
276 [ProducesResponseType(typeof(Result), 404)]
277 public IActionResult ShellRepo_GetSubmodelElementByIdShort(string aasId, string submodelIdShort, string seIdShortPath)
278 {
279 if (IsNullOrNotFound(aasId, out IActionResult result, out IAssetAdministrationShellServiceProvider provider))
280 return result;
281
282 var service = new AssetAdministrationShellController(provider);
283 return service.Shell_GetSubmodelElementByIdShort(submodelIdShort, seIdShortPath);
284 }
285
286 /// <summary>
287 /// Retrieves the value of a specific Submodel-Element from the Submodel
288 /// </summary>
289 /// <param name="aasId">The Asset Administration Shell's unique id</param>
290 /// <param name="submodelIdShort">The Submodel's short id</param>
291 /// <param name="seIdShortPath">The Submodel-Element's IdShort-Path</param>
292 /// <returns></returns>
293 /// <response code="200">Returns the value of a specific Submodel-Element</response>
294 /// <response code="404">Submodel / Submodel-Element not found</response>
295 /// <response code="405">Method not allowed</response>
296 [HttpGet("shells/{aasId}/aas/submodels/{submodelIdShort}/submodel/submodelElements/{seIdShortPath}/value", Name = "ShellRepo_GetSubmodelElementValueByIdShort")]
297 [Produces("application/json")]
298 [ProducesResponseType(typeof(object), 200)]
299 [ProducesResponseType(typeof(Result), 404)]
300 [ProducesResponseType(typeof(Result), 405)]
301 public IActionResult ShellRepo_GetSubmodelElementValueByIdShort(string aasId, string submodelIdShort, string seIdShortPath)
302 {
303 if (IsNullOrNotFound(aasId, out IActionResult result, out IAssetAdministrationShellServiceProvider provider))
304 return result;
305
306 var service = new AssetAdministrationShellController(provider);
307 return service.Shell_GetSubmodelElementValueByIdShort(submodelIdShort, seIdShortPath);
308 }
309
310 /// <summary>
311 /// Updates the Submodel-Element's value
312 /// </summary>
313 /// <param name="aasId">The Asset Administration Shell's unique id</param>
314 /// <param name="submodelIdShort">The Submodel's short id</param>
315 /// <param name="seIdShortPath">The Submodel-Element's IdShort-Path</param>
316 /// <param name="value">The new value</param>
317 /// <returns></returns>
318 /// <response code="200">Submodel-Element's value changed successfully</response>
319 /// <response code="404">Submodel / Submodel-Element not found</response>
320 /// <response code="405">Method not allowed</response>
321 [HttpPut("shells/{aasId}/aas/submodels/{submodelIdShort}/submodel/submodelElements/{seIdShortPath}/value", Name = "ShellRepo_PutSubmodelElementValueByIdShort")]
322 [Produces("application/json")]
323 [Consumes("application/json")]
324 [ProducesResponseType(typeof(ElementValue), 200)]
325 [ProducesResponseType(typeof(Result), 404)]
326 public IActionResult ShellRepo_PutSubmodelElementValueByIdShort(string aasId, string submodelIdShort, string seIdShortPath, [FromBody] object value)
327 {
328 if (IsNullOrNotFound(aasId, out IActionResult result, out IAssetAdministrationShellServiceProvider provider))
329 return result;
330
331 var service = new AssetAdministrationShellController(provider);
332 return service.Shell_PutSubmodelElementValueByIdShort(submodelIdShort, seIdShortPath, value);
333 }
334
335 /// <summary>
336 /// Deletes a specific Submodel-Element from the Submodel
337 /// </summary>
338 /// <param name="aasId">The Asset Administration Shell's unique id</param>
339 /// <param name="submodelIdShort">The Submodel's short id</param>
340 /// <param name="seIdShortPath">The Submodel-Element's IdShort-Path</param>
341 /// <returns></returns>
342 /// <response code="204">Submodel-Element deleted successfully</response>
343 /// <response code="404">Submodel / Submodel-Element not found</response>
344 [HttpDelete("shells/{aasId}/aas/submodels/{submodelIdShort}/submodel/submodelElements/{seIdShortPath}", Name = "ShellRepo_DeleteSubmodelElementByIdShort")]
345 [Produces("application/json")]
346 [ProducesResponseType(typeof(Result), 200)]
347 public IActionResult ShellRepo_DeleteSubmodelElementByIdShort(string aasId, string submodelIdShort, string seIdShortPath)
348 {
349 if (IsNullOrNotFound(aasId, out IActionResult result, out IAssetAdministrationShellServiceProvider provider))
350 return result;
351
352 var service = new AssetAdministrationShellController(provider);
353 return service.Shell_DeleteSubmodelElementByIdShort(submodelIdShort, seIdShortPath);
354 }
355
356 /// <summary>
357 /// Invokes a specific operation from the Submodel synchronously or asynchronously
358 /// </summary>
359 /// <param name="aasId">The Asset Administration Shell's unique id</param>
360 /// <param name="submodelIdShort">Submodel's short id</param>
361 /// <param name="idShortPathToOperation">The IdShort path to the Operation</param>
362 /// <param name="invocationRequest">The parameterized request object for the invocation</param>
363 /// <param name="async">Determines whether the execution of the operation is asynchronous (true) or not (false)</param>
364 /// <returns></returns>
365 /// <response code="200">Operation invoked successfully</response>
366 /// <response code="400">Bad Request</response>
367 /// <response code="404">Submodel / Method handler not found</response>
368 [HttpPost("shells/{aasId}/aas/submodels/{submodelIdShort}/submodel/submodelElements/{idShortPathToOperation}/invoke", Name = "ShellRepo_InvokeOperationByIdShort")]
369 [Produces("application/json")]
370 [Consumes("application/json")]
371 [ProducesResponseType(typeof(Result), 400)]
372 [ProducesResponseType(typeof(Result), 404)]
373 public IActionResult ShellRepo_InvokeOperationByIdShort(string aasId, string submodelIdShort, string idShortPathToOperation, [FromBody] InvocationRequest invocationRequest, [FromQuery] bool async)
374 {
375 if (IsNullOrNotFound(aasId, out IActionResult result, out IAssetAdministrationShellServiceProvider provider))
376 return result;
377
378 var service = new AssetAdministrationShellController(provider);
379 return service.Shell_InvokeOperationByIdShort(submodelIdShort, idShortPathToOperation, invocationRequest, async);
380 }
381
382 /// <summary>
383 /// Retrieves the result of an asynchronously started operation
384 /// </summary>
385 /// <param name="aasId">The Asset Administration Shell's unique id</param>
386 /// <param name="submodelIdShort">Submodel's short id</param>
387 /// <param name="idShortPathToOperation">The IdShort path to the Operation</param>
388 /// <param name="requestId">The request id</param>
389 /// <returns></returns>
390 /// <response code="200">Result found</response>
391 /// <response code="400">Bad Request</response>
392 /// <response code="404">Submodel / Operation / Request not found</response>
393 [HttpGet("shells/{aasId}/aas/submodels/{submodelIdShort}/submodel/submodelElements/{idShortPathToOperation}/invocationList/{requestId}", Name = "ShellRepo_GetInvocationResultByIdShort")]
394 [Produces("application/json")]
395 [ProducesResponseType(typeof(InvocationResponse), 200)]
396 [ProducesResponseType(typeof(Result), 400)]
397 [ProducesResponseType(typeof(Result), 404)]
398 public IActionResult ShellRepo_GetInvocationResultByIdShort(string aasId, string submodelIdShort, string idShortPathToOperation, string requestId)
399 {
400 if (IsNullOrNotFound(aasId, out IActionResult result, out IAssetAdministrationShellServiceProvider provider))
401 return result;
402
403 var service = new AssetAdministrationShellController(provider);
404 return service.Shell_GetInvocationResultByIdShort(submodelIdShort, idShortPathToOperation, requestId);
405 }
406
407 #endregion
408
409 #region Helper
410 /// <summary>
411 /// Checks whether aasId is null or Asset Administration Shell Service Provider cannot be found
412 /// </summary>
413 /// <param name="aasId">The Asset Administration Shell's unique id</param>
414 /// <param name="result">The IActionResult in case aasId is null or the provider cannot be found</param>
415 /// <param name="provider">The Asset Administration Shell Service Provider</param>
416 /// <returns></returns>
417 public bool IsNullOrNotFound(string aasId, out IActionResult result, out IAssetAdministrationShellServiceProvider provider)
418 {
419 if (string.IsNullOrEmpty(aasId))
420 {
421 result = ResultHandling.NullResult(nameof(aasId));
422 provider = null;
423 return true;
424 }
425 provider = serviceProvider.GetAssetAdministrationShellServiceProvider(aasId);
426 if (provider == null)
427 {
428 result = NotFound(new Result(false, new NotFoundMessage("Asset Administration Shell Provider")));
429 return true;
430 }
431 result = null;
432 return false;
433 }
434
435 #endregion
436
437 }
438}