Add winery source code
[vfc/nfvo/wfengine.git] / winery / org.eclipse.winery.repository / src / main / java / org / eclipse / winery / repository / resources / servicetemplates / boundarydefinitions / PropertyMappingsResource.java
1 /*******************************************************************************
2  * Copyright (c) 2013-2014 University of Stuttgart.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * and the Apache License 2.0 which both accompany this distribution,
6  * and are available at http://www.eclipse.org/legal/epl-v10.html
7  * and http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Contributors:
10  *     Oliver Kopp - initial API and implementation
11  *******************************************************************************/
12 package org.eclipse.winery.repository.resources.servicetemplates.boundarydefinitions;
13
14 import java.util.Iterator;
15
16 import javax.ws.rs.Consumes;
17 import javax.ws.rs.DELETE;
18 import javax.ws.rs.FormParam;
19 import javax.ws.rs.POST;
20 import javax.ws.rs.Path;
21 import javax.ws.rs.PathParam;
22 import javax.ws.rs.core.MediaType;
23 import javax.ws.rs.core.Response;
24 import javax.ws.rs.core.Response.Status;
25
26 import org.apache.commons.lang3.StringUtils;
27 import org.eclipse.winery.common.ModelUtilities;
28 import org.eclipse.winery.common.Util;
29 import org.eclipse.winery.model.tosca.TBoundaryDefinitions.Properties.PropertyMappings;
30 import org.eclipse.winery.model.tosca.TEntityTemplate;
31 import org.eclipse.winery.model.tosca.TPropertyMapping;
32 import org.eclipse.winery.repository.backend.BackendUtils;
33 import org.eclipse.winery.repository.resources.servicetemplates.ServiceTemplateResource;
34 import org.restdoc.annotations.RestDoc;
35
36 public class PropertyMappingsResource {
37         
38         private final PropertyMappings propertyMappings;
39         private final ServiceTemplateResource res;
40         
41         
42         public PropertyMappingsResource(PropertyMappings propertyMappings, ServiceTemplateResource res) {
43                 this.propertyMappings = propertyMappings;
44                 this.res = res;
45         }
46         
47         @Path("{serviceTemplatePropertyRef}")
48         @DELETE
49         public Response onDelete(@PathParam("serviceTemplatePropertyRef") String serviceTemplatePropertyRef) {
50                 serviceTemplatePropertyRef = Util.URLdecode(serviceTemplatePropertyRef);
51                 Iterator<TPropertyMapping> iterator = this.propertyMappings.getPropertyMapping().iterator();
52                 while (iterator.hasNext()) {
53                         TPropertyMapping propertyMapping = iterator.next();
54                         if (propertyMapping.getServiceTemplatePropertyRef().equals(serviceTemplatePropertyRef)) {
55                                 iterator.remove();
56                                 return BackendUtils.persist(this.res);
57                         }
58                 }
59                 // if the property mapping was not found, we reach this point
60                 // otherwise "iterator.remove()" has called and the resource persisted
61                 return Response.status(Status.NOT_FOUND).build();
62         }
63         
64         private void updatePropertyMapping(TPropertyMapping propertyMapping, String serviceTemplatePropertyRef, TEntityTemplate template, String targetPropertyRef) {
65                 propertyMapping.setServiceTemplatePropertyRef(serviceTemplatePropertyRef);
66                 propertyMapping.setTargetObjectRef(template);
67                 propertyMapping.setTargetPropertyRef(targetPropertyRef);
68         }
69         
70         @RestDoc(methodDescription = "Creates or updates a property mapping with the given fields")
71         @POST
72         @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
73         // @formatter:off
74         public Response onPost(
75                 @FormParam("serviceTemplatePropertyRef") String serviceTemplatePropertyRef,
76                 @FormParam("targetObjectRef") String targetObjectRef,
77                 @FormParam("targetPropertyRef") String targetPropertyRef
78         ) {
79         // @formatter:on
80                 if (StringUtils.isEmpty(serviceTemplatePropertyRef)) {
81                         return Response.status(Status.BAD_REQUEST).entity("serviceTemplatePropertyRef must not be empty").build();
82                 }
83                 if (StringUtils.isEmpty(targetObjectRef)) {
84                         return Response.status(Status.BAD_REQUEST).entity("targetObjectRef must not be empty").build();
85                 }
86                 if (StringUtils.isEmpty(targetPropertyRef)) {
87                         return Response.status(Status.BAD_REQUEST).entity("targetPropertyRef must not be empty").build();
88                 }
89                 
90                 TEntityTemplate template = ModelUtilities.findNodeTemplateOrRequirementOfNodeTemplateOrCapabilityOfNodeTemplateOrRelationshipTemplate(this.res.getServiceTemplate().getTopologyTemplate(), targetObjectRef);
91                 if (template == null) {
92                         return Response.status(Status.BAD_REQUEST).entity("targetObjectRef " + targetObjectRef + " could not be resolved.").build();
93                 }
94                 
95                 // replace propertyMapping if it exists
96                 Iterator<TPropertyMapping> iterator = this.propertyMappings.getPropertyMapping().iterator();
97                 while (iterator.hasNext()) {
98                         TPropertyMapping propertyMapping = iterator.next();
99                         if (propertyMapping.getServiceTemplatePropertyRef().equals(serviceTemplatePropertyRef)) {
100                                 // we found a property with the same mapping
101                                 // just update it ...
102                                 this.updatePropertyMapping(propertyMapping, serviceTemplatePropertyRef, template, targetPropertyRef);
103                                 // ... and finish processing
104                                 return BackendUtils.persist(this.res);
105                         }
106                 }
107                 
108                 // the property mapping didn't exist,
109                 // we create a new one
110                 TPropertyMapping newPropertyMapping = new TPropertyMapping();
111                 this.updatePropertyMapping(newPropertyMapping, serviceTemplatePropertyRef, template, targetPropertyRef);
112                 return BackendUtils.persist(this.res);
113         }
114         
115 }