2a59aa5eaa7d4abe625c4e0dd40594daac61414e
[vfc/nfvo/wfengine.git] / winery / org.eclipse.winery.repository / src / main / java / org / eclipse / winery / repository / resources / servicetemplates / topologytemplates / NodeTemplateResource.java
1 /*******************************************************************************
2  * Copyright (c) 2012-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.topologytemplates;
13
14 import java.util.List;
15 import java.util.Map;
16
17 import javax.ws.rs.FormParam;
18 import javax.ws.rs.GET;
19 import javax.ws.rs.HEAD;
20 import javax.ws.rs.PUT;
21 import javax.ws.rs.Path;
22 import javax.ws.rs.core.Response;
23 import javax.xml.namespace.QName;
24
25 import org.eclipse.winery.common.constants.Namespaces;
26 import org.eclipse.winery.common.ids.Namespace;
27 import org.eclipse.winery.model.tosca.TNodeTemplate;
28 import org.eclipse.winery.repository.backend.BackendUtils;
29 import org.eclipse.winery.repository.resources.INodeTemplateResourceOrNodeTypeImplementationResource;
30 import org.eclipse.winery.repository.resources._support.IPersistable;
31 import org.eclipse.winery.repository.resources._support.collections.IIdDetermination;
32 import org.eclipse.winery.repository.resources.artifacts.DeploymentArtifactsResource;
33 import org.eclipse.winery.repository.resources.entitytemplates.TEntityTemplateResource;
34 import org.eclipse.winery.repository.resources.servicetemplates.ServiceTemplateResource;
35 import org.restdoc.annotations.RestDoc;
36
37 public class NodeTemplateResource extends TEntityTemplateResource<TNodeTemplate> implements INodeTemplateResourceOrNodeTypeImplementationResource {
38         
39         public NodeTemplateResource(IIdDetermination<TNodeTemplate> idDetermination, TNodeTemplate o, int idx, List<TNodeTemplate> list, IPersistable res) {
40                 super(idDetermination, o, idx, list, res);
41         }
42         
43         @Path("deploymentartifacts/")
44         public DeploymentArtifactsResource getDeploymentArtifacts() {
45                 return new DeploymentArtifactsResource(this.o, this);
46         }
47         
48         @GET
49         @RestDoc(methodDescription = "* The following methods are currently *not* used by the topology modeler.<br />" + "The modeler is using the repository client to interact with the repository")
50         @Path("minInstances")
51         public String getMinInstances() {
52                 return Integer.toString(this.o.getMinInstances());
53         }
54         
55         @PUT
56         @Path("minInstances")
57         public Response setMinInstances(@FormParam(value = "minInstances") String minInstances) {
58                 int min = Integer.parseInt(minInstances);
59                 this.o.setMinInstances(min);
60                 return BackendUtils.persist(this.res);
61         }
62         
63         @GET
64         @Path("maxInstances")
65         public String getMaxInstances() {
66                 return this.o.getMaxInstances();
67         }
68         
69         @PUT
70         @Path("maxInstances")
71         public Response setMaxInstances(@FormParam(value = "maxInstances") String maxInstances) {
72                 // TODO: check for valid integer | "unbound"
73                 this.o.setMaxInstances(maxInstances);
74                 return BackendUtils.persist(this.res);
75         }
76         
77         
78         /* * *
79          * The visual appearance
80          *
81          * We do not use a subresource "visualappearance" here to avoid generation of more objects
82          * * */
83         
84         private final QName qnameX = new QName(Namespaces.TOSCA_WINERY_EXTENSIONS_NAMESPACE, "x");
85         private final QName qnameY = new QName(Namespaces.TOSCA_WINERY_EXTENSIONS_NAMESPACE, "y");
86         
87         
88         @Path("x")
89         @GET
90         @RestDoc(methodDescription = "@return the x coordinate of the node template")
91         public String getX() {
92                 Map<QName, String> otherAttributes = this.o.getOtherAttributes();
93                 return otherAttributes.get(this.qnameX);
94         }
95         
96         @Path("x")
97         @PUT
98         public Response setX(String x) {
99                 this.o.getOtherAttributes().put(this.qnameX, x);
100                 return BackendUtils.persist(this.res);
101         }
102         
103         @Path("y")
104         @GET
105         @RestDoc(methodDescription = "@return the y coordinate of the node template")
106         public String getY() {
107                 Map<QName, String> otherAttributes = this.o.getOtherAttributes();
108                 return otherAttributes.get(this.qnameY);
109         }
110         
111         @Path("y")
112         @PUT
113         public Response setY(String y) {
114                 this.o.getOtherAttributes().put(this.qnameY, y);
115                 return BackendUtils.persist(this.res);
116         }
117         
118         @Override
119         public Namespace getNamespace() {
120                 // TODO Auto-generated method stub
121                 throw new IllegalStateException("Not yet implemented.");
122         }
123         
124         /**
125          * Required for persistence after a change of the deployment artifact.
126          * Required by DeploymentArtifactResource to be able to persist
127          * 
128          * @return the service template this node template belongs to
129          */
130         public ServiceTemplateResource getServiceTemplateResource() {
131                 return (ServiceTemplateResource) this.res;
132         }
133         
134         /**
135          * required for topology modeler to check for existence of a node template
136          * at the server
137          * 
138          * @return empty response
139          */
140         @HEAD
141         public Response getHEAD() {
142                 return Response.noContent().build();
143         }
144         
145 }