Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 1 | /******************************************************************************* |
| 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 | *******************************************************************************/ |
| 11 | using System.Collections.Generic; |
| 12 | using Microsoft.AspNetCore.Mvc; |
| 13 | using BaSyx.API.Components; |
| 14 | using BaSyx.Utils.ResultHandling; |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 15 | using System.Web; |
| 16 | using BaSyx.Models.Connectivity.Descriptors; |
| 17 | using BaSyx.Models.Core.Common; |
| 18 | |
| 19 | namespace BaSyx.API.Http.Controllers |
| 20 | { |
| 21 | public class AssetAdministrationShellRegistry : Controller, IAssetAdministrationShellRegistry |
| 22 | { |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 23 | private readonly IAssetAdministrationShellRegistry aasRegistryImpl; |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 24 | public AssetAdministrationShellRegistry(IAssetAdministrationShellRegistry aasRegistry) |
| 25 | { |
| 26 | aasRegistryImpl = aasRegistry; |
| 27 | } |
| 28 | |
| 29 | #region REST-Interface |
| 30 | /// <summary> |
| 31 | /// Retrieves all registered Asset Administration Shells within a defined system (e.g. site, area, production line, station) |
| 32 | /// </summary> |
| 33 | /// <returns></returns> |
Constantin Ziesche | 02817f1 | 2020-08-04 21:40:43 +0200 | [diff] [blame^] | 34 | /// <response code="200">Returns a list of found Asset Administration Shell Descriptors</response> |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 35 | /// <response code="400">Bad Request</response> |
| 36 | /// <response code="502">Bad Gateway</response> |
Constantin Ziesche | 02817f1 | 2020-08-04 21:40:43 +0200 | [diff] [blame^] | 37 | [HttpGet("api/v1/registry", Name = "GetAssetAdministrationShellDescriptors")] |
| 38 | [ProducesResponseType(typeof(List<AssetAdministrationShellDescriptor>), 200)] |
| 39 | public IActionResult GetAssetAdministrationShellDescriptors() |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 40 | { |
| 41 | var result = RetrieveAssetAdministrationShells(); |
Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 42 | return result.CreateActionResult(CrudOperation.Retrieve); |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 43 | } |
| 44 | /// <summary> |
| 45 | /// Retrieves a specific Asset Administration Shell |
| 46 | /// </summary> |
| 47 | /// <param name="aasId">The Asset Administration Shell's unique id</param> |
| 48 | /// <returns></returns> |
| 49 | /// <response code="200">Returns the requested Asset Administration Shell</response> |
| 50 | /// <response code="400">Bad Request</response> |
| 51 | /// <response code="404">No Asset Administration Shell with passed id found</response> |
| 52 | /// <response code="502">Bad Gateway</response> |
Constantin Ziesche | 02817f1 | 2020-08-04 21:40:43 +0200 | [diff] [blame^] | 53 | [HttpGet("api/v1/registry/{*aasId}", Name = "GetAssetAdministrationShellDescriptor")] |
| 54 | [ProducesResponseType(typeof(AssetAdministrationShellDescriptor), 200)] |
| 55 | public IActionResult GetAssetAdministrationShellDescriptor(string aasId) |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 56 | { |
Constantin Ziesche | 02817f1 | 2020-08-04 21:40:43 +0200 | [diff] [blame^] | 57 | if (string.IsNullOrEmpty(aasId)) |
| 58 | return ResultHandling.NullResult(nameof(aasId)); |
| 59 | |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 60 | aasId = HttpUtility.UrlDecode(aasId); |
| 61 | var result = RetrieveAssetAdministrationShell(aasId); |
Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 62 | return result.CreateActionResult(CrudOperation.Retrieve); |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 63 | } |
Constantin Ziesche | 02817f1 | 2020-08-04 21:40:43 +0200 | [diff] [blame^] | 64 | |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 65 | /// <summary> |
| 66 | /// Renews a specific Asset Administration Shell's registration |
| 67 | /// </summary> |
| 68 | /// <param name="aasId">The Asset Administration Shell's unique id</param> |
| 69 | /// <returns></returns> |
| 70 | /// <response code="200">The Asset Administration Shell's registration was successfully renewed</response> |
| 71 | /// <response code="400">The syntax of the passed Asset Administration Shell is not valid or malformed request</response> |
| 72 | /// <response code="404">No Asset Administration Shell with passed id found</response> |
| 73 | /// <response code="502">Bad Gateway</response> |
Constantin Ziesche | 02817f1 | 2020-08-04 21:40:43 +0200 | [diff] [blame^] | 74 | [HttpPut("api/v1/registry/{*aasId}", Name = "RenewAssetAdministrationShellRegistration")] |
| 75 | [ProducesResponseType(typeof(Result), 204)] |
| 76 | public IActionResult RenewAssetAdministrationShellRegistration(string aasId) |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 77 | { |
Constantin Ziesche | 02817f1 | 2020-08-04 21:40:43 +0200 | [diff] [blame^] | 78 | if (string.IsNullOrEmpty(aasId)) |
| 79 | return ResultHandling.NullResult(nameof(aasId)); |
| 80 | |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 81 | aasId = HttpUtility.UrlDecode(aasId); |
| 82 | Dictionary<string, string> keyValues = null; |
| 83 | if (Request.Query?.Count > 0) |
| 84 | { |
| 85 | keyValues = new Dictionary<string, string>(); |
| 86 | foreach (string key in Request.Query.Keys) |
| 87 | { |
| 88 | keyValues.Add(key, Request.Query[key].ToString()); |
| 89 | } |
| 90 | } |
| 91 | var result = UpdateAssetAdministrationShell(aasId, keyValues); |
Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 92 | return result.CreateActionResult(CrudOperation.Update); |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 93 | } |
| 94 | /// <summary> |
Constantin Ziesche | 02817f1 | 2020-08-04 21:40:43 +0200 | [diff] [blame^] | 95 | /// Registers a new Asset Administration Shell at the registry |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 96 | /// </summary> |
Constantin Ziesche | 02817f1 | 2020-08-04 21:40:43 +0200 | [diff] [blame^] | 97 | /// <param name="aasDescriptor">The Asset Administration Shell descriptor object</param> |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 98 | /// <returns></returns> |
| 99 | /// <response code="201">The Asset Administration Shell was created successfully</response> |
| 100 | /// <response code="400">The syntax of the passed Asset Administration Shell is not valid or malformed request</response> |
| 101 | /// <response code="422">The passed Asset Administration Shell conflicts with already registered Asset Administration Shells</response> |
| 102 | /// <response code="502">Bad Gateway</response> |
Constantin Ziesche | 02817f1 | 2020-08-04 21:40:43 +0200 | [diff] [blame^] | 103 | [HttpPost("api/v1/registry", Name = "RegisterAssetAdministrationShell")] |
| 104 | [ProducesResponseType(typeof(AssetAdministrationShellDescriptor), 201)] |
| 105 | public IActionResult RegisterAssetAdministrationShell([FromBody] IAssetAdministrationShellDescriptor aasDescriptor) |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 106 | { |
Constantin Ziesche | 02817f1 | 2020-08-04 21:40:43 +0200 | [diff] [blame^] | 107 | if (aasDescriptor == null) |
| 108 | return ResultHandling.NullResult(nameof(aasDescriptor)); |
| 109 | |
| 110 | var result = CreateAssetAdministrationShell(aasDescriptor); |
| 111 | return result.CreateActionResult(CrudOperation.Create, "api/v1/registry/"+ HttpUtility.UrlEncode(aasDescriptor.Identification.Id)); |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 112 | } |
| 113 | /// <summary> |
| 114 | /// Deletes a specific Asset Administration Shell |
| 115 | /// </summary> |
| 116 | /// <param name="aasId">The Asset Administration Shell's unique id</param> |
| 117 | /// <returns></returns> |
| 118 | /// <response code="200">The Asset Administration Shell was deleted successfully</response> |
| 119 | /// <response code="400">Bad Request</response> |
| 120 | /// <response code="404">No Asset Administration Shell with passed id found</response> |
| 121 | /// <response code="502">Bad Gateway</response> |
Constantin Ziesche | 02817f1 | 2020-08-04 21:40:43 +0200 | [diff] [blame^] | 122 | [HttpDelete("api/v1/registry/{*aasId}", Name = "DeleteAssetAdministrationShellRegistration")] |
| 123 | [ProducesResponseType(typeof(Result), 204)] |
| 124 | public IActionResult DeleteAssetAdministrationShellRegistration(string aasId) |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 125 | { |
Constantin Ziesche | 02817f1 | 2020-08-04 21:40:43 +0200 | [diff] [blame^] | 126 | if (string.IsNullOrEmpty(aasId)) |
| 127 | return ResultHandling.NullResult(nameof(aasId)); |
| 128 | |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 129 | aasId = HttpUtility.UrlDecode(aasId); |
| 130 | var result = DeleteAssetAdministrationShell(aasId); |
Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 131 | return result.CreateActionResult(CrudOperation.Delete); |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | |
| 135 | /// <summary> |
| 136 | /// Adds a new Submodel to an existing resp. registered Asset Administration Shell |
| 137 | /// </summary> |
| 138 | /// <param name="aasId">The Asset Administration Shell's unique id</param> |
Constantin Ziesche | 02817f1 | 2020-08-04 21:40:43 +0200 | [diff] [blame^] | 139 | /// <param name="submodelDescriptor">The Submodel descriptor object</param> |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 140 | /// <returns></returns> |
| 141 | /// <response code="201">The Submodel was created successfully</response> |
| 142 | /// <response code="400">The syntax of the passed Submodel is not valid or malformed request</response> |
| 143 | /// <response code="404">No Asset Administration Shell with passed id found</response> |
| 144 | /// <response code="422">The passed Submodel conflicts with already registered Submodels</response> |
| 145 | /// <response code="502">Bad Gateway</response> |
Constantin Ziesche | 02817f1 | 2020-08-04 21:40:43 +0200 | [diff] [blame^] | 146 | [HttpPost("api/v1/registry/{aasId}/submodels", Name = "RegisterSubmodelAtAssetAdministrationShell")] |
| 147 | [ProducesResponseType(typeof(SubmodelDescriptor), 201)] |
| 148 | public IActionResult RegisterSubmodelAtAssetAdministrationShell(string aasId, [FromBody] ISubmodelDescriptor submodelDescriptor) |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 149 | { |
Constantin Ziesche | 02817f1 | 2020-08-04 21:40:43 +0200 | [diff] [blame^] | 150 | if (string.IsNullOrEmpty(aasId)) |
| 151 | return ResultHandling.NullResult(nameof(aasId)); |
| 152 | if (submodelDescriptor == null) |
| 153 | return ResultHandling.NullResult(nameof(submodelDescriptor)); |
| 154 | |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 155 | aasId = HttpUtility.UrlDecode(aasId); |
Constantin Ziesche | 02817f1 | 2020-08-04 21:40:43 +0200 | [diff] [blame^] | 156 | var result = CreateSubmodel(aasId, submodelDescriptor); |
| 157 | return result.CreateActionResult(CrudOperation.Create, "api/v1/registry/" + aasId + "/submodels/" + submodelDescriptor.IdShort); |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | /// <summary> |
| 161 | /// Retrieves a specific Submodel from a specific Asset Administration Shell |
| 162 | /// </summary> |
| 163 | /// <param name="aasId">The Asset Administration Shell's unique id</param> |
Constantin Ziesche | 02817f1 | 2020-08-04 21:40:43 +0200 | [diff] [blame^] | 164 | /// <param name="submodelIdShort">The Submodel's short id</param> |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 165 | /// <returns></returns> |
| 166 | /// <response code="200">Returns the requested Submodels</response> |
| 167 | /// <response code="400">Bad Request</response> |
| 168 | /// <response code="404">No Asset Administration Shell / Submodel with passed id found</response> |
| 169 | /// <response code="502">Bad Gateway</response> |
Constantin Ziesche | 02817f1 | 2020-08-04 21:40:43 +0200 | [diff] [blame^] | 170 | [HttpGet("api/v1/registry/{aasId}/submodels/{submodelIdShort}", Name = "GetSubmodelDescriptorFromAssetAdministrationShell")] |
| 171 | [ProducesResponseType(typeof(SubmodelDescriptor), 200)] |
| 172 | public IActionResult GetSubmodelDescriptorFromAssetAdministrationShell(string aasId, string submodelIdShort) |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 173 | { |
Constantin Ziesche | 02817f1 | 2020-08-04 21:40:43 +0200 | [diff] [blame^] | 174 | if (string.IsNullOrEmpty(aasId)) |
| 175 | return ResultHandling.NullResult(nameof(aasId)); |
| 176 | if (string.IsNullOrEmpty(submodelIdShort)) |
| 177 | return ResultHandling.NullResult(nameof(submodelIdShort)); |
| 178 | |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 179 | aasId = HttpUtility.UrlDecode(aasId); |
| 180 | var result = RetrieveSubmodel(aasId, submodelIdShort); |
Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 181 | return result.CreateActionResult(CrudOperation.Retrieve); |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 182 | } |
| 183 | /// <summary> |
| 184 | /// Deletes a specific Submodel from a specific Asset Administration Shell |
| 185 | /// </summary> |
| 186 | /// <param name="aasId">The Asset Administration Shell's unique id</param> |
| 187 | /// <param name="submodelIdShort">The Submodel's short id (idShort)</param> |
| 188 | /// <returns></returns> |
| 189 | /// <response code="200">The Submodel was deleted successfully</response> |
| 190 | /// <response code="400">Bad Request</response> |
| 191 | /// <response code="404">No Asset Administration Shell / Submodel with passed id found</response> |
| 192 | /// <response code="502">Bad Gateway</response> |
Constantin Ziesche | 02817f1 | 2020-08-04 21:40:43 +0200 | [diff] [blame^] | 193 | [HttpDelete("api/v1/registry/{aasId}/submodels/{submodelIdShort}", Name = "DeleteSubmodelFromAssetAdministrationShell")] |
| 194 | [ProducesResponseType(typeof(Result), 204)] |
| 195 | public IActionResult DeleteSubmodelFromAssetAdministrationShell(string aasId, string submodelIdShort) |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 196 | { |
Constantin Ziesche | 02817f1 | 2020-08-04 21:40:43 +0200 | [diff] [blame^] | 197 | if (string.IsNullOrEmpty(aasId)) |
| 198 | return ResultHandling.NullResult(nameof(aasId)); |
| 199 | if (string.IsNullOrEmpty(submodelIdShort)) |
| 200 | return ResultHandling.NullResult(nameof(submodelIdShort)); |
| 201 | |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 202 | aasId = HttpUtility.UrlDecode(aasId); |
| 203 | var result = DeleteSubmodel(aasId, submodelIdShort); |
Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 204 | return result.CreateActionResult(CrudOperation.Delete); |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 205 | } |
| 206 | /// <summary> |
| 207 | /// Retrieves all Submodels from a specific Asset Administration Shell |
| 208 | /// </summary> |
| 209 | /// <param name="aasId">The Asset Administration Shell's unique id</param> |
| 210 | /// <returns></returns> |
| 211 | /// <response code="200">Returns a list of found Submodels</response> |
| 212 | /// <response code="400">Bad Request</response> |
| 213 | /// <response code="404">No Asset Administration Shell with passed id found</response> |
| 214 | /// <response code="502">Bad Gateway</response> |
Constantin Ziesche | 02817f1 | 2020-08-04 21:40:43 +0200 | [diff] [blame^] | 215 | [HttpGet("api/v1/registry/{aasId}/submodels", Name = "GetAllSubmodelDescriptorsFromAssetAdministrationShell")] |
| 216 | [ProducesResponseType(typeof(List<SubmodelDescriptor>), 200)] |
| 217 | public IActionResult GetAllSubmodelDescriptorsFromAssetAdministrationShell(string aasId) |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 218 | { |
Constantin Ziesche | 02817f1 | 2020-08-04 21:40:43 +0200 | [diff] [blame^] | 219 | if (string.IsNullOrEmpty(aasId)) |
| 220 | return ResultHandling.NullResult(nameof(aasId)); |
| 221 | |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 222 | aasId = HttpUtility.UrlDecode(aasId); |
| 223 | var result = RetrieveSubmodels(aasId); |
Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 224 | return result.CreateActionResult(CrudOperation.Retrieve); |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 225 | } |
| 226 | #endregion |
| 227 | |
| 228 | #region InterfaceImplementation |
| 229 | public IResult<IAssetAdministrationShellDescriptor> CreateAssetAdministrationShell(IAssetAdministrationShellDescriptor aas) |
| 230 | { |
| 231 | return aasRegistryImpl.CreateAssetAdministrationShell(aas); |
| 232 | } |
| 233 | |
| 234 | public IResult DeleteAssetAdministrationShell(string aasId) |
| 235 | { |
| 236 | return aasRegistryImpl.DeleteAssetAdministrationShell(aasId); |
| 237 | } |
| 238 | |
| 239 | public IResult<IAssetAdministrationShellDescriptor> RetrieveAssetAdministrationShell(string aasId) |
| 240 | { |
| 241 | return aasRegistryImpl.RetrieveAssetAdministrationShell(aasId); |
| 242 | } |
| 243 | |
| 244 | public IResult<IElementContainer<IAssetAdministrationShellDescriptor>> RetrieveAssetAdministrationShells() |
| 245 | { |
| 246 | return aasRegistryImpl.RetrieveAssetAdministrationShells(); |
| 247 | } |
| 248 | |
| 249 | public IResult UpdateAssetAdministrationShell(string aasId, Dictionary<string, string> metaData) |
| 250 | { |
| 251 | return aasRegistryImpl.UpdateAssetAdministrationShell(aasId, metaData); |
| 252 | } |
| 253 | |
| 254 | public IResult<ISubmodelDescriptor> CreateSubmodel(string aasId, ISubmodelDescriptor submodel) |
| 255 | { |
| 256 | return aasRegistryImpl.CreateSubmodel(aasId, submodel); |
| 257 | } |
| 258 | |
| 259 | public IResult<IElementContainer<ISubmodelDescriptor>> RetrieveSubmodels(string aasId) |
| 260 | { |
| 261 | return aasRegistryImpl.RetrieveSubmodels(aasId); |
| 262 | } |
| 263 | |
| 264 | public IResult<ISubmodelDescriptor> RetrieveSubmodel(string aasId, string submodelIdShort) |
| 265 | { |
| 266 | return aasRegistryImpl.RetrieveSubmodel(aasId, submodelIdShort); |
| 267 | } |
| 268 | |
| 269 | public IResult DeleteSubmodel(string aasId, string submodelIdShort) |
| 270 | { |
| 271 | return aasRegistryImpl.DeleteSubmodel(aasId, submodelIdShort); |
| 272 | } |
| 273 | |
| 274 | |
| 275 | #endregion |
| 276 | |
| 277 | #region Helper Methods |
| 278 | |
| 279 | |
| 280 | #endregion |
| 281 | } |
| 282 | } |