dd10751202a9e9b4f1f320987b4f46c5aa1d69b8
[vfc/nfvo/wfengine.git] /
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
8  *
9  * Contributors:
10  *     Oliver Kopp - initial API and implementation
11  *******************************************************************************/
12 package org.eclipse.winery.repository.resources.servicetemplates.boundarydefinitions.interfaces;
13
14 import java.io.StringWriter;
15 import java.util.List;
16
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;
25
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;
42
43 import com.fasterxml.jackson.core.JsonFactory;
44 import com.fasterxml.jackson.core.JsonGenerator;
45
46 public class ExportedOperationResource extends EntityWithIdResource<TExportedOperation> {
47         
48         private static final Logger logger = LoggerFactory.getLogger(ExportedOperationResource.class);
49         
50         
51         public ExportedOperationResource(IIdDetermination<TExportedOperation> idDetermination, TExportedOperation o, int idx, List<TExportedOperation> list, IPersistable res) {
52                 super(idDetermination, o, idx, list, res);
53         }
54         
55         @GET
56         @Produces(MediaType.APPLICATION_JSON)
57         public String getJSONRepresentation() {
58                 JsonFactory jsonFactory = new JsonFactory();
59                 StringWriter sw = new StringWriter();
60                 try {
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());
69                         }
70                         jg.writeEndObject();
71                         jg.close();
72                 } catch (Exception e) {
73                         ExportedOperationResource.logger.error(e.getMessage(), e);
74                         throw new WebApplicationException(Response.status(Status.INTERNAL_SERVER_ERROR).entity(e).build());
75                 }
76                 String res = sw.toString();
77                 return res;
78         }
79         
80         /**
81          * 
82          * @return "NodeOperation" | "RelationshipOperation" | "Plan" | null. null
83          *         is returned if no type is set
84          */
85         @Path("type")
86         @GET
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) {
93                         return "Plan";
94                 } else {
95                         return null;
96                 }
97         }
98         
99         @Path("type")
100         @PUT
101         public Response setType(String type) {
102                 switch (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);
110                         }
111                         break;
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);
119                         }
120                         break;
121                 case "Plan":
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);
128                         }
129                         break;
130                 default:
131                         return Response.status(Status.BAD_REQUEST).entity("Unknown type " + type).build();
132                 }
133                 return BackendUtils.persist(this.res);
134         }
135         
136         /**
137          * @return null if no reference is set
138          */
139         @Path("ref")
140         @GET
141         public String getReference() {
142                 if (this.o.getNodeOperation() != null) {
143                         TNodeTemplate nt = (TNodeTemplate) this.o.getNodeOperation().getNodeRef();
144                         if (nt == null) {
145                                 return null;
146                         }
147                         return nt.getId();
148                 } else if (this.o.getRelationshipOperation() != null) {
149                         TRelationshipTemplate rt = (TRelationshipTemplate) this.o.getRelationshipOperation().getRelationshipRef();
150                         if (rt == null) {
151                                 return null;
152                         }
153                         return rt.getId();
154                 } else if (this.o.getPlan() != null) {
155                         TPlan plan = (TPlan) this.o.getPlan().getPlanRef();
156                         if (plan == null) {
157                                 return null;
158                         }
159                         return plan.getId();
160                 } else {
161                         // no type set -> no reference can be returned
162                         return null;
163                 }
164         }
165         
166         @Path("ref")
167         @PUT
168         public Response setReference(String ref) {
169                 TServiceTemplate ste = ((ServiceTemplateResource) this.res).getServiceTemplate();
170                 
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);
176                         break;
177                 case "RelationshipOperation":
178                         TRelationshipTemplate relationshipTemplate = ModelUtilities.resolveRelationshipTemplate(ste, ref);
179                         this.o.getRelationshipOperation().setRelationshipRef(relationshipTemplate);
180                         break;
181                 case "Plan":
182                         TPlan plan = ModelUtilities.resolvePlan(ste, ref);
183                         this.o.getPlan().setPlanRef(plan);
184                         break;
185                 default:
186                         return Response.status(Status.INTERNAL_SERVER_ERROR).entity("Unknown type " + this.getType()).build();
187                 }
188                 return BackendUtils.persist(this.res);
189         }
190         
191         @Path("interfacename")
192         @GET
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());
200                 } else {
201                         throw new WebApplicationException(Response.status(Status.INTERNAL_SERVER_ERROR).entity("Unsupported state of ExportedOperation").build());
202                 }
203         }
204         
205         @Path("interfacename")
206         @PUT
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());
214                 } else {
215                         throw new WebApplicationException(Response.status(Status.BAD_REQUEST).entity("No type set").build());
216                 }
217                 return BackendUtils.persist(this.res);
218         }
219         
220         @Path("operationname")
221         @GET
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());
229                 } else {
230                         throw new WebApplicationException(Response.status(Status.INTERNAL_SERVER_ERROR).entity("Unsupported state of ExportedOperation").build());
231                 }
232         }
233         
234         @Path("operationname")
235         @PUT
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());
243                 } else {
244                         throw new WebApplicationException(Response.status(Status.BAD_REQUEST).entity("No type set").build());
245                 }
246                 return BackendUtils.persist(this.res);
247         }
248         
249 }