1 /*******************************************************************************
2 * Copyright (c) 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
10 * Oliver Kopp - initial API and implementation
11 *******************************************************************************/
12 package org.eclipse.winery.repository.resources.servicetemplates.boundarydefinitions.interfaces;
14 import java.io.StringWriter;
15 import java.util.List;
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.WebApplicationException;
22 import javax.ws.rs.core.MediaType;
23 import javax.ws.rs.core.Response;
24 import javax.ws.rs.core.Response.Status;
26 import org.eclipse.winery.common.ModelUtilities;
27 import org.eclipse.winery.model.tosca.TExportedOperation;
28 import org.eclipse.winery.model.tosca.TExportedOperation.NodeOperation;
29 import org.eclipse.winery.model.tosca.TExportedOperation.Plan;
30 import org.eclipse.winery.model.tosca.TExportedOperation.RelationshipOperation;
31 import org.eclipse.winery.model.tosca.TNodeTemplate;
32 import org.eclipse.winery.model.tosca.TPlan;
33 import org.eclipse.winery.model.tosca.TRelationshipTemplate;
34 import org.eclipse.winery.model.tosca.TServiceTemplate;
35 import org.eclipse.winery.repository.backend.BackendUtils;
36 import org.eclipse.winery.repository.resources._support.IPersistable;
37 import org.eclipse.winery.repository.resources._support.collections.IIdDetermination;
38 import org.eclipse.winery.repository.resources._support.collections.withid.EntityWithIdResource;
39 import org.eclipse.winery.repository.resources.servicetemplates.ServiceTemplateResource;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 import com.fasterxml.jackson.core.JsonFactory;
44 import com.fasterxml.jackson.core.JsonGenerator;
46 public class ExportedOperationResource extends EntityWithIdResource<TExportedOperation> {
48 private static final Logger logger = LoggerFactory.getLogger(ExportedOperationResource.class);
51 public ExportedOperationResource(IIdDetermination<TExportedOperation> idDetermination, TExportedOperation o, int idx, List<TExportedOperation> list, IPersistable res) {
52 super(idDetermination, o, idx, list, res);
56 @Produces(MediaType.APPLICATION_JSON)
57 public String getJSONRepresentation() {
58 JsonFactory jsonFactory = new JsonFactory();
59 StringWriter sw = new StringWriter();
61 JsonGenerator jg = jsonFactory.createGenerator(sw);
62 jg.writeStartObject();
63 String type = this.getType();
64 jg.writeStringField("type", type);
65 jg.writeStringField("ref", this.getReference());
66 if ((type != null) && (!type.equals("Plan"))) {
67 jg.writeStringField("interfacename", this.getInterfaceName());
68 jg.writeStringField("operationname", this.getOperationName());
72 } catch (Exception e) {
73 ExportedOperationResource.logger.error(e.getMessage(), e);
74 throw new WebApplicationException(Response.status(Status.INTERNAL_SERVER_ERROR).entity(e).build());
76 String res = sw.toString();
82 * @return "NodeOperation" | "RelationshipOperation" | "Plan" | null. null
83 * is returned if no type is set
87 public String getType() {
88 if (this.o.getNodeOperation() != null) {
89 return "NodeOperation";
90 } else if (this.o.getRelationshipOperation() != null) {
91 return "RelationshipOperation";
92 } else if (this.o.getPlan() != null) {
101 public Response setType(String type) {
103 case "NodeOperation":
104 if (this.o.getNodeOperation() == null) {
105 // only do something, if the type is really changed
106 this.o.setRelationshipOperation(null);
107 this.o.setPlan(null);
108 NodeOperation no = new NodeOperation();
109 this.o.setNodeOperation(no);
112 case "RelationshipOperation":
113 if (this.o.getRelationshipOperation() == null) {
114 // only do something, if the type is really changed
115 this.o.setNodeOperation(null);
116 this.o.setPlan(null);
117 RelationshipOperation ro = new RelationshipOperation();
118 this.o.setRelationshipOperation(ro);
122 if (this.o.getPlan() == null) {
123 // only do something, if the type is really changed
124 this.o.setNodeOperation(null);
125 this.o.setRelationshipOperation(null);
126 Plan plan = new Plan();
127 this.o.setPlan(plan);
131 return Response.status(Status.BAD_REQUEST).entity("Unknown type " + type).build();
133 return BackendUtils.persist(this.res);
137 * @return null if no reference is set
141 public String getReference() {
142 if (this.o.getNodeOperation() != null) {
143 TNodeTemplate nt = (TNodeTemplate) this.o.getNodeOperation().getNodeRef();
148 } else if (this.o.getRelationshipOperation() != null) {
149 TRelationshipTemplate rt = (TRelationshipTemplate) this.o.getRelationshipOperation().getRelationshipRef();
154 } else if (this.o.getPlan() != null) {
155 TPlan plan = (TPlan) this.o.getPlan().getPlanRef();
161 // no type set -> no reference can be returned
168 public Response setReference(String ref) {
169 TServiceTemplate ste = ((ServiceTemplateResource) this.res).getServiceTemplate();
171 // we assume that a correctly set type also means that getX (getNodeOperation, ...) returns non null
172 switch (this.getType()) {
173 case "NodeOperation":
174 TNodeTemplate nodeTemplate = ModelUtilities.resolveNodeTemplate(ste, ref);
175 this.o.getNodeOperation().setNodeRef(nodeTemplate);
177 case "RelationshipOperation":
178 TRelationshipTemplate relationshipTemplate = ModelUtilities.resolveRelationshipTemplate(ste, ref);
179 this.o.getRelationshipOperation().setRelationshipRef(relationshipTemplate);
182 TPlan plan = ModelUtilities.resolvePlan(ste, ref);
183 this.o.getPlan().setPlanRef(plan);
186 return Response.status(Status.INTERNAL_SERVER_ERROR).entity("Unknown type " + this.getType()).build();
188 return BackendUtils.persist(this.res);
191 @Path("interfacename")
193 public String getInterfaceName() {
194 if (this.o.getNodeOperation() != null) {
195 return this.o.getNodeOperation().getInterfaceName();
196 } else if (this.o.getRelationshipOperation() != null) {
197 return this.o.getRelationshipOperation().getInterfaceName();
198 } else if (this.o.getPlan() != null) {
199 throw new WebApplicationException(Response.status(Status.BAD_REQUEST).entity("A plan does not carry an interface").build());
201 throw new WebApplicationException(Response.status(Status.INTERNAL_SERVER_ERROR).entity("Unsupported state of ExportedOperation").build());
205 @Path("interfacename")
207 public Response setInterfaceName(String interfacename) {
208 if (this.o.getNodeOperation() != null) {
209 this.o.getNodeOperation().setInterfaceName(interfacename);
210 } else if (this.o.getRelationshipOperation() != null) {
211 this.o.getRelationshipOperation().setInterfaceName(interfacename);
212 } else if (this.o.getPlan() != null) {
213 throw new WebApplicationException(Response.status(Status.BAD_REQUEST).entity("A plan does not carry an interface").build());
215 throw new WebApplicationException(Response.status(Status.BAD_REQUEST).entity("No type set").build());
217 return BackendUtils.persist(this.res);
220 @Path("operationname")
222 public String getOperationName() {
223 if (this.o.getNodeOperation() != null) {
224 return this.o.getNodeOperation().getOperationName();
225 } else if (this.o.getRelationshipOperation() != null) {
226 return this.o.getRelationshipOperation().getOperationName();
227 } else if (this.o.getPlan() != null) {
228 throw new WebApplicationException(Response.status(Status.BAD_REQUEST).entity("A plan does not carry an operation").build());
230 throw new WebApplicationException(Response.status(Status.INTERNAL_SERVER_ERROR).entity("Unsupported state of ExportedOperation").build());
234 @Path("operationname")
236 public Response setOperationName(String name) {
237 if (this.o.getNodeOperation() != null) {
238 this.o.getNodeOperation().setOperationName(name);
239 } else if (this.o.getRelationshipOperation() != null) {
240 this.o.getRelationshipOperation().setOperationName(name);
241 } else if (this.o.getPlan() != null) {
242 throw new WebApplicationException(Response.status(Status.BAD_REQUEST).entity("A plan does not carry an operation").build());
244 throw new WebApplicationException(Response.status(Status.BAD_REQUEST).entity("No type set").build());
246 return BackendUtils.persist(this.res);