aboutsummaryrefslogtreecommitdiffstats
path: root/winery/org.eclipse.winery.repository/src/main/java/org/eclipse/winery/repository/resources/servicetemplates/boundarydefinitions/BoundaryDefinitionsResource.java
blob: 58dfbe93c17de9f4b3b61ca985437162044475dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*******************************************************************************
 * Copyright (c) 2013-2014 University of Stuttgart.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and the Apache License 2.0 which both accompany this distribution,
 * and are available at http://www.eclipse.org/legal/epl-v10.html
 * and http://www.apache.org/licenses/LICENSE-2.0
 *
 * Contributors:
 *     Oliver Kopp - initial API and implementation
 *******************************************************************************/
package org.eclipse.winery.repository.resources.servicetemplates.boundarydefinitions;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

import org.eclipse.winery.common.ModelUtilities;
import org.eclipse.winery.model.tosca.TBoundaryDefinitions;
import org.eclipse.winery.model.tosca.TBoundaryDefinitions.Capabilities;
import org.eclipse.winery.model.tosca.TBoundaryDefinitions.Interfaces;
import org.eclipse.winery.model.tosca.TBoundaryDefinitions.Policies;
import org.eclipse.winery.model.tosca.TBoundaryDefinitions.Properties;
import org.eclipse.winery.model.tosca.TBoundaryDefinitions.Properties.PropertyMappings;
import org.eclipse.winery.model.tosca.TBoundaryDefinitions.Requirements;
import org.eclipse.winery.model.tosca.TCapabilityRef;
import org.eclipse.winery.model.tosca.TRequirementRef;
import org.eclipse.winery.repository.backend.BackendUtils;
import org.eclipse.winery.repository.resources.servicetemplates.ServiceTemplateResource;
import org.eclipse.winery.repository.resources.servicetemplates.boundarydefinitions.interfaces.InterfacesResource;
import org.eclipse.winery.repository.resources.servicetemplates.boundarydefinitions.policies.PoliciesResource;
import org.eclipse.winery.repository.resources.servicetemplates.boundarydefinitions.reqscaps.CapabilitiesResource;
import org.eclipse.winery.repository.resources.servicetemplates.boundarydefinitions.reqscaps.RequirementsResource;
import org.restdoc.annotations.RestDoc;
import org.restdoc.annotations.RestDocParam;
import org.w3c.dom.Document;

import com.sun.jersey.api.view.Viewable;

public class BoundaryDefinitionsResource {
	
	private final ServiceTemplateResource serviceTemplateResource;
	private final TBoundaryDefinitions boundaryDefinitions;
	
	
	public BoundaryDefinitionsResource(ServiceTemplateResource serviceTemplateResource, TBoundaryDefinitions boundaryDefinitions) {
		this.serviceTemplateResource = serviceTemplateResource;
		this.boundaryDefinitions = boundaryDefinitions;
	}
	
	@GET
	@Produces(MediaType.TEXT_HTML)
	public Viewable getHTML(@Context UriInfo uriInfo) {
		return new Viewable("/jsp/servicetemplates/boundarydefinitions/boundarydefinitions.jsp", new BoundaryDefinitionsJSPData(this.serviceTemplateResource.getServiceTemplate(), uriInfo.getBaseUri()));
	}
	
	@PUT
	@RestDoc(methodDescription = "Replaces the boundary definitions by the information given in the XML")
	@Consumes(MediaType.TEXT_XML)
	public Response setModel(TBoundaryDefinitions boundaryDefinitions) {
		this.serviceTemplateResource.getServiceTemplate().setBoundaryDefinitions(boundaryDefinitions);
		return BackendUtils.persist(this.serviceTemplateResource);
	}
	
	@Path("properties/")
	@PUT
	@Consumes(MediaType.TEXT_XML)
	@RestDoc(resourceDescription = "Models the user-defined properties. The property mappings go into a separate resource propertymappings.")
	public Response putProperties(@RestDocParam(description = "Stored properties. The XSD allows a single element only. Therefore, we go for the contained element") Document doc) {
		org.eclipse.winery.model.tosca.TBoundaryDefinitions.Properties properties = ModelUtilities.getProperties(this.boundaryDefinitions);
		properties.setAny(doc.getDocumentElement());
		return BackendUtils.persist(this.serviceTemplateResource);
	}
	
	@Path("requirements/")
	public RequirementsResource getRequiremensResource() {
		Requirements requirements = this.boundaryDefinitions.getRequirements();
		if (requirements == null) {
			requirements = new Requirements();
			this.boundaryDefinitions.setRequirements(requirements);
		}
		List<TRequirementRef> refs = requirements.getRequirement();
		return new RequirementsResource(this.serviceTemplateResource, refs);
	}
	
	@Path("capabilities/")
	public CapabilitiesResource getCapabilitiesResource() {
		Capabilities caps = this.boundaryDefinitions.getCapabilities();
		if (caps == null) {
			caps = new Capabilities();
			this.boundaryDefinitions.setCapabilities(caps);
		}
		List<TCapabilityRef> refs = caps.getCapability();
		return new CapabilitiesResource(this.serviceTemplateResource, refs);
	}
	
	@Path("policies/")
	public PoliciesResource getPoliciesResource() {
		Policies policies = this.boundaryDefinitions.getPolicies();
		if (policies == null) {
			policies = new Policies();
			this.boundaryDefinitions.setPolicies(policies);
		}
		return new PoliciesResource(policies.getPolicy(), this.serviceTemplateResource);
	}
	
	/**
	 * This path is below "boundary definitions" to ease implementation If it
	 * was modeled following the XSD, it would have been nested below
	 * "properties". We did not do that
	 */
	@Path("propertymappings/")
	public PropertyMappingsResource getPropertyMappings() {
		Properties properties = this.boundaryDefinitions.getProperties();
		if (properties == null) {
			properties = new Properties();
			this.boundaryDefinitions.setProperties(properties);
		}
		PropertyMappings propertyMappings = properties.getPropertyMappings();
		if (propertyMappings == null) {
			propertyMappings = new PropertyMappings();
			properties.setPropertyMappings(propertyMappings);
		}
		return new PropertyMappingsResource(propertyMappings, this.serviceTemplateResource);
	}
	
	@Path("interfaces/")
	public InterfacesResource getInterfacesResource() {
		Interfaces interfaces = this.boundaryDefinitions.getInterfaces();
		if (interfaces == null) {
			interfaces = new Interfaces();
			this.boundaryDefinitions.setInterfaces(interfaces);
		}
		return new InterfacesResource(interfaces.getInterface(), this.serviceTemplateResource);
	}
}