Add winery source code
[vfc/nfvo/wfengine.git] / winery / org.eclipse.winery.repository / src / main / java / org / eclipse / winery / repository / resources / servicetemplates / boundarydefinitions / BoundaryDefinitionsResource.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.List;
15
16 import javax.ws.rs.Consumes;
17 import javax.ws.rs.GET;
18 import javax.ws.rs.PUT;
19 import javax.ws.rs.Path;
20 import javax.ws.rs.Produces;
21 import javax.ws.rs.core.Context;
22 import javax.ws.rs.core.MediaType;
23 import javax.ws.rs.core.Response;
24 import javax.ws.rs.core.UriInfo;
25
26 import org.eclipse.winery.common.ModelUtilities;
27 import org.eclipse.winery.model.tosca.TBoundaryDefinitions;
28 import org.eclipse.winery.model.tosca.TBoundaryDefinitions.Capabilities;
29 import org.eclipse.winery.model.tosca.TBoundaryDefinitions.Interfaces;
30 import org.eclipse.winery.model.tosca.TBoundaryDefinitions.Policies;
31 import org.eclipse.winery.model.tosca.TBoundaryDefinitions.Properties;
32 import org.eclipse.winery.model.tosca.TBoundaryDefinitions.Properties.PropertyMappings;
33 import org.eclipse.winery.model.tosca.TBoundaryDefinitions.Requirements;
34 import org.eclipse.winery.model.tosca.TCapabilityRef;
35 import org.eclipse.winery.model.tosca.TRequirementRef;
36 import org.eclipse.winery.repository.backend.BackendUtils;
37 import org.eclipse.winery.repository.resources.servicetemplates.ServiceTemplateResource;
38 import org.eclipse.winery.repository.resources.servicetemplates.boundarydefinitions.interfaces.InterfacesResource;
39 import org.eclipse.winery.repository.resources.servicetemplates.boundarydefinitions.policies.PoliciesResource;
40 import org.eclipse.winery.repository.resources.servicetemplates.boundarydefinitions.reqscaps.CapabilitiesResource;
41 import org.eclipse.winery.repository.resources.servicetemplates.boundarydefinitions.reqscaps.RequirementsResource;
42 import org.restdoc.annotations.RestDoc;
43 import org.restdoc.annotations.RestDocParam;
44 import org.w3c.dom.Document;
45
46 import com.sun.jersey.api.view.Viewable;
47
48 public class BoundaryDefinitionsResource {
49         
50         private final ServiceTemplateResource serviceTemplateResource;
51         private final TBoundaryDefinitions boundaryDefinitions;
52         
53         
54         public BoundaryDefinitionsResource(ServiceTemplateResource serviceTemplateResource, TBoundaryDefinitions boundaryDefinitions) {
55                 this.serviceTemplateResource = serviceTemplateResource;
56                 this.boundaryDefinitions = boundaryDefinitions;
57         }
58         
59         @GET
60         @Produces(MediaType.TEXT_HTML)
61         public Viewable getHTML(@Context UriInfo uriInfo) {
62                 return new Viewable("/jsp/servicetemplates/boundarydefinitions/boundarydefinitions.jsp", new BoundaryDefinitionsJSPData(this.serviceTemplateResource.getServiceTemplate(), uriInfo.getBaseUri()));
63         }
64         
65         @PUT
66         @RestDoc(methodDescription = "Replaces the boundary definitions by the information given in the XML")
67         @Consumes(MediaType.TEXT_XML)
68         public Response setModel(TBoundaryDefinitions boundaryDefinitions) {
69                 this.serviceTemplateResource.getServiceTemplate().setBoundaryDefinitions(boundaryDefinitions);
70                 return BackendUtils.persist(this.serviceTemplateResource);
71         }
72         
73         @Path("properties/")
74         @PUT
75         @Consumes(MediaType.TEXT_XML)
76         @RestDoc(resourceDescription = "Models the user-defined properties. The property mappings go into a separate resource propertymappings.")
77         public Response putProperties(@RestDocParam(description = "Stored properties. The XSD allows a single element only. Therefore, we go for the contained element") Document doc) {
78                 org.eclipse.winery.model.tosca.TBoundaryDefinitions.Properties properties = ModelUtilities.getProperties(this.boundaryDefinitions);
79                 properties.setAny(doc.getDocumentElement());
80                 return BackendUtils.persist(this.serviceTemplateResource);
81         }
82         
83         @Path("requirements/")
84         public RequirementsResource getRequiremensResource() {
85                 Requirements requirements = this.boundaryDefinitions.getRequirements();
86                 if (requirements == null) {
87                         requirements = new Requirements();
88                         this.boundaryDefinitions.setRequirements(requirements);
89                 }
90                 List<TRequirementRef> refs = requirements.getRequirement();
91                 return new RequirementsResource(this.serviceTemplateResource, refs);
92         }
93         
94         @Path("capabilities/")
95         public CapabilitiesResource getCapabilitiesResource() {
96                 Capabilities caps = this.boundaryDefinitions.getCapabilities();
97                 if (caps == null) {
98                         caps = new Capabilities();
99                         this.boundaryDefinitions.setCapabilities(caps);
100                 }
101                 List<TCapabilityRef> refs = caps.getCapability();
102                 return new CapabilitiesResource(this.serviceTemplateResource, refs);
103         }
104         
105         @Path("policies/")
106         public PoliciesResource getPoliciesResource() {
107                 Policies policies = this.boundaryDefinitions.getPolicies();
108                 if (policies == null) {
109                         policies = new Policies();
110                         this.boundaryDefinitions.setPolicies(policies);
111                 }
112                 return new PoliciesResource(policies.getPolicy(), this.serviceTemplateResource);
113         }
114         
115         /**
116          * This path is below "boundary definitions" to ease implementation If it
117          * was modeled following the XSD, it would have been nested below
118          * "properties". We did not do that
119          */
120         @Path("propertymappings/")
121         public PropertyMappingsResource getPropertyMappings() {
122                 Properties properties = this.boundaryDefinitions.getProperties();
123                 if (properties == null) {
124                         properties = new Properties();
125                         this.boundaryDefinitions.setProperties(properties);
126                 }
127                 PropertyMappings propertyMappings = properties.getPropertyMappings();
128                 if (propertyMappings == null) {
129                         propertyMappings = new PropertyMappings();
130                         properties.setPropertyMappings(propertyMappings);
131                 }
132                 return new PropertyMappingsResource(propertyMappings, this.serviceTemplateResource);
133         }
134         
135         @Path("interfaces/")
136         public InterfacesResource getInterfacesResource() {
137                 Interfaces interfaces = this.boundaryDefinitions.getInterfaces();
138                 if (interfaces == null) {
139                         interfaces = new Interfaces();
140                         this.boundaryDefinitions.setInterfaces(interfaces);
141                 }
142                 return new InterfacesResource(interfaces.getInterface(), this.serviceTemplateResource);
143         }
144 }