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