summaryrefslogtreecommitdiffstats
path: root/winery/org.eclipse.winery.repository/src/main/java/org/eclipse/winery/repository/resources/servicetemplates/ServiceTemplateResource.java
blob: 6a249df0e8e1b27732a861fadd135258537652a1 (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*******************************************************************************
 * Copyright (c) 2012-2013 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;

import java.io.IOException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.SortedSet;

import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.xml.namespace.QName;

import org.eclipse.winery.common.RepositoryFileReference;
import org.eclipse.winery.common.ids.XMLId;
import org.eclipse.winery.common.ids.definitions.ServiceTemplateId;
import org.eclipse.winery.common.ids.elements.PlanId;
import org.eclipse.winery.common.ids.elements.PlansId;
import org.eclipse.winery.model.tosca.TBoundaryDefinitions;
import org.eclipse.winery.model.tosca.TExtensibleElements;
import org.eclipse.winery.model.tosca.TPlan;
import org.eclipse.winery.model.tosca.TPlan.PlanModelReference;
import org.eclipse.winery.model.tosca.TPlans;
import org.eclipse.winery.model.tosca.TServiceTemplate;
import org.eclipse.winery.model.tosca.TTopologyTemplate;
import org.eclipse.winery.repository.Utils;
import org.eclipse.winery.repository.backend.BackendUtils;
import org.eclipse.winery.repository.backend.Repository;
import org.eclipse.winery.repository.resources.AbstractComponentInstanceWithReferencesResource;
import org.eclipse.winery.repository.resources.IHasName;
import org.eclipse.winery.repository.resources.servicetemplates.boundarydefinitions.BoundaryDefinitionsResource;
import org.eclipse.winery.repository.resources.servicetemplates.plans.PlansResource;
import org.eclipse.winery.repository.resources.servicetemplates.selfserviceportal.SelfServicePortalResource;
import org.eclipse.winery.repository.resources.servicetemplates.topologytemplates.TopologyTemplateResource;
import org.restdoc.annotations.RestDoc;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ServiceTemplateResource extends AbstractComponentInstanceWithReferencesResource implements IHasName {
	
	private static final Logger logger = LoggerFactory.getLogger(ServiceTemplateResource.class);
	
	
	public ServiceTemplateResource(ServiceTemplateId id) {
		super(id);
	}
	
	/** sub-resources **/
	
	@Path("topologytemplate/")
	public TopologyTemplateResource getTopologyTemplateResource() {
		if (this.getServiceTemplate().getTopologyTemplate() == null) {
			// the main service template resource exists
			// default topology template: empty template
			// This eases the JSPs etc. and is valid as a non-existant topology template is equal to an empty one
			this.getServiceTemplate().setTopologyTemplate(new TTopologyTemplate());
		}
		return new TopologyTemplateResource(this);
	}
	
	@Path("plans/")
	public PlansResource getPlansResource() {
		TPlans plans = this.getServiceTemplate().getPlans();
		if (plans == null) {
			plans = new TPlans();
			this.getServiceTemplate().setPlans(plans);
		}
		return new PlansResource(plans.getPlan(), this);
	}
	
	@Path("selfserviceportal/")
	public SelfServicePortalResource getSelfServicePortalResource() {
		return new SelfServicePortalResource(this);
	}
	
	@Path("boundarydefinitions/")
	public BoundaryDefinitionsResource getBoundaryDefinitionsResource() {
		TBoundaryDefinitions boundaryDefinitions = this.getServiceTemplate().getBoundaryDefinitions();
		if (boundaryDefinitions == null) {
			boundaryDefinitions = new TBoundaryDefinitions();
			this.getServiceTemplate().setBoundaryDefinitions(boundaryDefinitions);
		}
		return new BoundaryDefinitionsResource(this, boundaryDefinitions);
	}
	
	@Override
	public String getName() {
		String name = this.getServiceTemplate().getName();
		if (name == null) {
			// place default
			name = this.getId().getXmlId().getDecoded();
		}
		return name;
	}
	
	@Override
	public Response setName(String name) {
		this.getServiceTemplate().setName(name);
		return BackendUtils.persist(this);
	}
	
	// @formatter:off
	@GET
	@RestDoc(methodDescription="Returns the associated node type, which can be substituted by this service template.<br />" +
	"@return a QName of the form {namespace}localName is returned.")
	@Path("substitutableNodeType")
	@Produces(MediaType.TEXT_PLAIN)
	// @formatter:on
	public Response getSubstitutableNodeTypeAsResponse() {
		QName qname = this.getServiceTemplate().getSubstitutableNodeType();
		if (qname == null) {
			return Response.status(Status.NOT_FOUND).build();
		} else {
			return Response.ok(qname.toString()).build();
		}
	}
	
	/**
	 * 
	 * @return null if there is no substitutable node type
	 */
	public QName getSubstitutableNodeType() {
		return this.getServiceTemplate().getSubstitutableNodeType();
	}
	
	@DELETE
	@RestDoc(methodDescription = "Removes the association to substitutable node type")
	@Path("substitutableNodeType")
	public Response deleteSubstitutableNodeType() {
		this.getServiceTemplate().setSubstitutableNodeType(null);
		BackendUtils.persist(this);
		return Response.noContent().build();
	}
	
	public TServiceTemplate getServiceTemplate() {
		return (TServiceTemplate) this.getElement();
	}
	
	@Override
	protected TExtensibleElements createNewElement() {
		return new TServiceTemplate();
	}
	
	@Override
	protected void copyIdToFields() {
		this.getServiceTemplate().setId(this.getId().getXmlId().getDecoded());
		this.getServiceTemplate().setTargetNamespace(this.getId().getNamespace().getDecoded());
	}
	
	/**
	 * Synchronizes the known plans with the data in the XML. When there is a
	 * stored file, but no known entry in the XML, we guess "BPEL" as language
	 * and "build plan" as type.
	 * 
	 * @throws IOException
	 */
	@Override
	public void synchronizeReferences() {
		// locally stored plans
		TPlans plans = this.getServiceTemplate().getPlans();
		
		// plans stored in the repository
		PlansId plansContainerId = new PlansId((ServiceTemplateId) this.getId());
		SortedSet<PlanId> nestedPlans = Repository.INSTANCE.getNestedIds(plansContainerId, PlanId.class);
		
		Set<PlanId> plansToAdd = new HashSet<PlanId>();
		plansToAdd.addAll(nestedPlans);
		
		if (nestedPlans.isEmpty()) {
			if (plans == null) {
				// data on the file system equals the data -> no plans
				return;
			} else {
				// we have to check for equality later
			}
		}
		
		if (plans == null) {
			plans = new TPlans();
			this.getServiceTemplate().setPlans(plans);
		}
		
		for (Iterator<TPlan> iterator = plans.getPlan().iterator(); iterator.hasNext();) {
			TPlan plan = iterator.next();
			if (plan.getPlanModel() != null) {
				// in case, a plan is directly contained in a Model element, we do not need to do anything
				continue;
			}
			PlanModelReference planModelReference = plan.getPlanModelReference();
			if ((planModelReference = plan.getPlanModelReference()) != null) {
				String ref = planModelReference.getReference();
				if ((ref == null) || ref.startsWith("../")) {
					// references to local plans start with "../"
					// special case (due to errors in the importer): empty PlanModelReference field
					if (plan.getId() == null) {
						// invalid plan entry: no id.
						// we remove the entry
						iterator.remove();
						continue;
					}
					PlanId planId = new PlanId(plansContainerId, new XMLId(plan.getId(), false));
					if (nestedPlans.contains(planId)) {
						// everything allright
						// we do NOT need to add the plan on the HDD to the XML
						plansToAdd.remove(planId);
					} else {
						// no local storage for the plan, we remove it from the XML
						iterator.remove();
					}
				}
			}
		}
		
		// add all plans locally stored, but not contained in the XML, as plan element to the plans of the service template.
		List<TPlan> thePlans = plans.getPlan();
		for (PlanId planId : plansToAdd) {
			SortedSet<RepositoryFileReference> files = Repository.INSTANCE.getContainedFiles(planId);
			if (files.size() != 1) {
				throw new IllegalStateException("Currently, only one file per plan is supported.");
			}
			RepositoryFileReference ref = files.iterator().next();
			
			TPlan plan = new TPlan();
			plan.setId(planId.getXmlId().getDecoded());
			plan.setName(planId.getXmlId().getDecoded());
			plan.setPlanType(org.eclipse.winery.repository.Constants.TOSCA_PLANTYPE_BUILD_PLAN);
			plan.setPlanLanguage(org.eclipse.winery.common.constants.Namespaces.URI_BPEL20_EXECUTABLE);
			
			// create a PlanModelReferenceElement pointing to that file
			String path = Utils.getURLforPathInsideRepo(BackendUtils.getPathInsideRepo(ref));
			// path is relative from the definitions element
			path = "../" + path;
			PlanModelReference pref = new PlanModelReference();
			pref.setReference(path);
			
			plan.setPlanModelReference(pref);
			thePlans.add(plan);
		}
		
		try {
			this.persist();
		} catch (IOException e) {
			throw new IllegalStateException("Could not persist resource", e);
		}
		return;
	}
}