blob: 8e557c9991614d7da1e5c73da8f9e103477f50c9 [file] [log] [blame]
Constantin Ziesche8b4a64d2020-06-25 11:52:09 +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.Connectivity.Descriptors;
17using BaSyx.Models.Core.Common;
18
19namespace BaSyx.API.Http.Controllers
20{
21 /// <summary>
22 /// The Submodel Repository Controller
23 /// </summary>
24 public class SubmodelRepositoryServices : Controller, ISubmodelRepositoryServiceProvider
25 {
26
27 private readonly ISubmodelRepositoryServiceProvider repository;
28
29 public IEnumerable<ISubmodel> Submodels => repository.Submodels;
30 public ISubmodelRepositoryDescriptor ServiceDescriptor { get; }
31
32
33 public SubmodelRepositoryServices(ISubmodelRepositoryServiceProvider submodelRepositoryServiceProvider)
34 {
35 repository = submodelRepositoryServiceProvider;
36 ServiceDescriptor = submodelRepositoryServiceProvider.ServiceDescriptor;
37 }
38
39 #region REST-Interface SubmodelRepository
40
41 /// <summary>
42 /// Retrieves all Submodels from the repository service endpoint
43 /// </summary>
44 /// <returns></returns>
45 /// <response code="200">Returns a list of found Submodels</response>
46 /// <response code="404">No Submodels found</response>
47 /// <response code="400">Bad Request</response>
48 /// <response code="502">Bad Gateway</response>
Constantin Ziescheb4a60112020-08-05 12:08:10 +020049 [HttpGet("submodels", Name = "GetAllSubmodelsFromRepo")]
Constantin Ziesche02817f12020-08-04 21:40:43 +020050 [ProducesResponseType(typeof(List<BaSyx.Models.Core.AssetAdministrationShell.Implementations.Submodel>), 200)]
Constantin Ziescheb4a60112020-08-05 12:08:10 +020051 public IActionResult GetAllSubmodelsFromRepo()
Constantin Ziesche8b4a64d2020-06-25 11:52:09 +020052 {
53 var result = RetrieveSubmodels();
54 return result.CreateActionResult(CrudOperation.Retrieve);
55 }
56 /// <summary>
Constantin Ziesche02817f12020-08-04 21:40:43 +020057 /// Retrieves a specific Submodel from the repository service endpoint
Constantin Ziesche8b4a64d2020-06-25 11:52:09 +020058 /// </summary>
Constantin Ziesche02817f12020-08-04 21:40:43 +020059 /// <param name="submodelId">The Submodel's unique id</param>
Constantin Ziesche8b4a64d2020-06-25 11:52:09 +020060 /// <returns></returns>
61 /// <response code="200">Returns the requested Submodel</response>
62 /// <response code="404">No Submodel found</response>
63 /// <response code="400">Bad Request</response>
64 /// <response code="502">Bad Gateway</response>
Constantin Ziescheb4a60112020-08-05 12:08:10 +020065 [HttpGet("submodels/{submodelId}", Name = "RetrieveSubmodelFromRepoById")]
Constantin Ziesche02817f12020-08-04 21:40:43 +020066 [ProducesResponseType(typeof(BaSyx.Models.Core.AssetAdministrationShell.Implementations.Submodel), 200)]
Constantin Ziescheb4a60112020-08-05 12:08:10 +020067 public IActionResult RetrieveSubmodelFromRepoById(string submodelId)
Constantin Ziesche8b4a64d2020-06-25 11:52:09 +020068 {
Constantin Ziesche02817f12020-08-04 21:40:43 +020069 if (string.IsNullOrEmpty(submodelId))
70 return ResultHandling.NullResult(nameof(submodelId));
71
Constantin Ziesche8b4a64d2020-06-25 11:52:09 +020072 var result = RetrieveSubmodel(submodelId);
73 return result.CreateActionResult(CrudOperation.Retrieve);
74 }
Constantin Ziesche02817f12020-08-04 21:40:43 +020075
Constantin Ziesche8b4a64d2020-06-25 11:52:09 +020076 /// <summary>
Constantin Ziesche02817f12020-08-04 21:40:43 +020077 /// Creates or updates a Submodel at the repository service endpoint
Constantin Ziesche8b4a64d2020-06-25 11:52:09 +020078 /// </summary>
79 /// <param name="submodel">The serialized Submodel object</param>
80 /// <returns></returns>
Constantin Ziesche02817f12020-08-04 21:40:43 +020081 /// <response code="201">Submodel created/updated successfully</response>
Constantin Ziesche8b4a64d2020-06-25 11:52:09 +020082 /// <response code="400">Bad Request</response>
83 /// <response code="502">Bad Gateway</response>
Constantin Ziescheb4a60112020-08-05 12:08:10 +020084 [HttpPost("submodels", Name = "PutSubmodelToRepo")]
Constantin Ziesche02817f12020-08-04 21:40:43 +020085 [ProducesResponseType(typeof(BaSyx.Models.Core.AssetAdministrationShell.Implementations.Submodel), 201)]
Constantin Ziescheb4a60112020-08-05 12:08:10 +020086 public IActionResult PutSubmodelToRepo([FromBody] ISubmodel submodel)
Constantin Ziesche8b4a64d2020-06-25 11:52:09 +020087 {
Constantin Ziesche02817f12020-08-04 21:40:43 +020088 if (submodel == null)
89 return ResultHandling.NullResult(nameof(submodel));
90
Constantin Ziesche8b4a64d2020-06-25 11:52:09 +020091 var result = CreateSubmodel(submodel);
92 return result.CreateActionResult(CrudOperation.Create);
93 }
94 /// <summary>
95 /// Deletes a specific Submodel at the repository service endpoint
96 /// </summary>
97 /// <param name="submodelId">The Submodel's unique id</param>
98 /// <returns></returns>
99 /// <response code="200">Submodel deleted successfully</response>
100 /// <response code="400">Bad Request</response>
101 /// <response code="502">Bad Gateway</response>
Constantin Ziescheb4a60112020-08-05 12:08:10 +0200102 [HttpDelete("submodels/{submodelId}", Name = "DeleteSubmodelFromRepoById")]
Constantin Ziesche02817f12020-08-04 21:40:43 +0200103 [ProducesResponseType(typeof(Result), 200)]
Constantin Ziescheb4a60112020-08-05 12:08:10 +0200104 public IActionResult DeleteSubmodelFromRepoById(string submodelId)
Constantin Ziesche8b4a64d2020-06-25 11:52:09 +0200105 {
Constantin Ziesche02817f12020-08-04 21:40:43 +0200106 if (string.IsNullOrEmpty(submodelId))
107 return ResultHandling.NullResult(nameof(submodelId));
108
Constantin Ziesche8b4a64d2020-06-25 11:52:09 +0200109 var result = DeleteSubmodel(submodelId);
110 return result.CreateActionResult(CrudOperation.Delete);
111 }
112
113 #endregion
114
115 #region Helper Methods
116
Constantin Ziesche8b4a64d2020-06-25 11:52:09 +0200117 #endregion
118
119 #region Interface Implementation SubmodelRepository
120 public void RegisterSubmodelServiceProvider(string id, ISubmodelServiceProvider submodelServiceProvider)
121 {
122 repository.RegisterSubmodelServiceProvider(id, submodelServiceProvider);
123 }
124
125 public ISubmodelServiceProvider GetSubmodelServiceProvider(string id)
126 {
127 return repository.GetSubmodelServiceProvider(id);
128 }
129
130 public IEnumerable<ISubmodelServiceProvider> GetSubmodelServiceProviders()
131 {
132 return repository.GetSubmodelServiceProviders();
133 }
134
135 public void BindTo(IEnumerable<ISubmodel> element)
136 {
137 repository.BindTo(element);
138 }
139
140 public IEnumerable<ISubmodel> GetBinding()
141 {
142 return repository.GetBinding();
143 }
144
145 public IResult<ISubmodel> CreateSubmodel(ISubmodel submodel)
146 {
147 return repository.CreateSubmodel(submodel);
148 }
149
150 public IResult<ISubmodel> RetrieveSubmodel(string submodelId)
151 {
152 return repository.RetrieveSubmodel(submodelId);
153 }
154
155 public IResult<IElementContainer<ISubmodel>> RetrieveSubmodels()
156 {
157 return repository.RetrieveSubmodels();
158 }
159
160 public IResult UpdateSubmodel(string submodelId, ISubmodel submodel)
161 {
162 return repository.UpdateSubmodel(submodelId, submodel);
163 }
164
165 public IResult DeleteSubmodel(string submodelId)
166 {
167 return repository.DeleteSubmodel(submodelId);
168 }
169
170
171
172
173 #endregion
174 }
175}