b7eea9258a94bd1c67a595216dd3b56ee9c54a7c
[vfc/nfvo/wfengine.git] /
1 /*******************************************************************************
2  * Copyright (c) 2012-2013 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.entitytypes.nodetypes.reqandcapdefs;
13
14 import java.lang.reflect.Method;
15 import java.util.List;
16
17 import javax.ws.rs.FormParam;
18 import javax.ws.rs.GET;
19 import javax.ws.rs.PUT;
20 import javax.ws.rs.Path;
21 import javax.ws.rs.Produces;
22 import javax.ws.rs.core.MediaType;
23 import javax.ws.rs.core.Response;
24 import javax.xml.namespace.QName;
25
26 import org.eclipse.winery.model.tosca.TCapabilityDefinition;
27 import org.eclipse.winery.model.tosca.TConstraint;
28 import org.eclipse.winery.model.tosca.TRequirementDefinition;
29 import org.eclipse.winery.repository.backend.BackendUtils;
30 import org.eclipse.winery.repository.resources.ConstraintsResource;
31 import org.eclipse.winery.repository.resources._support.collections.IIdDetermination;
32 import org.eclipse.winery.repository.resources._support.collections.withid.EntityWithIdResource;
33 import org.eclipse.winery.repository.resources.entitytypes.nodetypes.NodeTypeResource;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 /**
38  * Bundles common properties of TRequirementDefinition and TCapabilityDefinition
39  * 
40  * We agreed in the project not to modify org.eclipse.winery.model.tosca.
41  * Therefore, this resource models the common properties of a
42  * TRequirementDefinition and a TCapabilityDefinition
43  */
44 public abstract class AbstractReqOrCapDefResource<ReqOrCapDef> extends EntityWithIdResource<ReqOrCapDef> implements IIdDetermination<ReqOrCapDef> {
45         
46         private static final Logger logger = LoggerFactory.getLogger(AbstractReqOrCapDefResource.class);
47         
48         protected NodeTypeResource parent;
49         
50         // the capability or the requirement
51         private Object reqOrCapDef;
52         
53         private List<TConstraint> constraints;
54         
55         
56         /**
57          * @param constraints additional parameter (in comparison to the constructor
58          *            of EntityWithIdResource) as we require that sublist for the
59          *            constrinats sub resource
60          */
61         public AbstractReqOrCapDefResource(IIdDetermination<ReqOrCapDef> idDetermination, ReqOrCapDef reqOrCapDef, int idx, List<ReqOrCapDef> list, NodeTypeResource res, List<TConstraint> constraints) {
62                 super(idDetermination, reqOrCapDef, idx, list, res);
63                 assert ((reqOrCapDef instanceof TRequirementDefinition) || (reqOrCapDef instanceof TCapabilityDefinition));
64                 this.parent = res;
65                 this.reqOrCapDef = reqOrCapDef;
66                 this.constraints = constraints;
67         }
68         
69         @GET
70         @Path("name")
71         public String getName() {
72                 return (String) this.invokeGetter("getName");
73         }
74         
75         static String getName(Object reqOrCapDef) {
76                 return (String) AbstractReqOrCapDefResource.invokeGetter(reqOrCapDef, "getName");
77         }
78         
79         @GET
80         @Path("lowerbound")
81         public int getLowerBound() {
82                 return (int) this.invokeGetter("getLowerBound");
83         }
84         
85         @GET
86         @Path("upperbound")
87         public String getUpperBound() {
88                 return (String) this.invokeGetter("getUpperBound");
89         }
90         
91         @PUT
92         @Path("name")
93         public Response setName(@FormParam(value = "name") String name) {
94                 // TODO: type check - see also min/max Instance of a node template
95                 this.invokeSetter("setName", name);
96                 return BackendUtils.persist(this.parent);
97         }
98         
99         @PUT
100         @Path("lowerbound")
101         public Response setLowerBound(@FormParam(value = "lowerbound") String value) {
102                 // TODO: type check
103                 this.invokeSetter("setLowerBound", value);
104                 return BackendUtils.persist(this.parent);
105         }
106         
107         @PUT
108         @Path("upperbound")
109         public Response setUpperBound(@FormParam(value = "upperbound") String value) {
110                 // TODO: type check
111                 this.invokeSetter("setUpperBound", value);
112                 return BackendUtils.persist(this.parent);
113         }
114         
115         @Path("constraints/")
116         public ConstraintsResource getConstraintsResource() {
117                 return new ConstraintsResource(this.constraints, this.parent);
118         }
119         
120         private static Object invokeGetter(Object reqOrCapDef, String getterName) {
121                 Method method;
122                 Object res;
123                 try {
124                         method = reqOrCapDef.getClass().getMethod(getterName);
125                         res = method.invoke(reqOrCapDef);
126                 } catch (Exception e) {
127                         AbstractReqOrCapDefResource.logger.error("Could not invoke getter {}", getterName, e);
128                         throw new IllegalStateException(e);
129                 }
130                 return res;
131         }
132         
133         private Object invokeGetter(String getterName) {
134                 return AbstractReqOrCapDefResource.invokeGetter(this.reqOrCapDef, getterName);
135         }
136         
137         /**
138          * Quick hack method for RequirementOrCapabilityDefinitionsResource
139          */
140         static void invokeSetter(Object reqOrCapDef, String setterName, Object value) {
141                 Method method;
142                 try {
143                         method = reqOrCapDef.getClass().getMethod(setterName, value.getClass());
144                         method.invoke(reqOrCapDef, value);
145                 } catch (Exception e) {
146                         AbstractReqOrCapDefResource.logger.error("Could not invoke setter {}", setterName, e);
147                         throw new IllegalStateException(e);
148                 }
149         }
150         
151         private void invokeSetter(String setterName, Object value) {
152                 AbstractReqOrCapDefResource.invokeSetter(this.reqOrCapDef, setterName, value);
153         }
154         
155         @GET
156         @Path("type")
157         @Produces(MediaType.TEXT_PLAIN)
158         public String getTypeAsString() {
159                 return this.getType().toString();
160         }
161         
162         /**
163          * required by the JSP.
164          * 
165          * Therefore, we have two getters for the type: QName for the JSP and String
166          * for REST clients
167          */
168         public abstract QName getType();
169         
170         /**
171          * Required by reqandcapdefs.jsp
172          */
173         public Object getDef() {
174                 return this.reqOrCapDef;
175         }
176         
177 }