c874bd50188ca6551024f714f1c16d41096beecf
[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.relationshiptypes;
13
14 import java.util.Collection;
15 import java.util.SortedSet;
16
17 import javax.ws.rs.Consumes;
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.TExtensibleElements;
27 import org.eclipse.winery.model.tosca.TRelationshipType;
28 import org.eclipse.winery.model.tosca.TRelationshipType.SourceInterfaces;
29 import org.eclipse.winery.model.tosca.TRelationshipType.TargetInterfaces;
30 import org.eclipse.winery.model.tosca.TRelationshipType.ValidSource;
31 import org.eclipse.winery.model.tosca.TRelationshipType.ValidTarget;
32 import org.eclipse.winery.model.tosca.TTopologyElementInstanceStates;
33 import org.eclipse.winery.common.ids.definitions.NodeTypeId;
34 import org.eclipse.winery.common.ids.definitions.RelationshipTypeId;
35 import org.eclipse.winery.repository.backend.BackendUtils;
36 import org.eclipse.winery.repository.backend.Repository;
37 import org.eclipse.winery.repository.resources.entitytypes.InstanceStatesResource;
38 import org.eclipse.winery.repository.resources.entitytypes.TopologyGraphElementEntityTypeResource;
39 import org.eclipse.winery.repository.resources.interfaces.InterfacesResource;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 import com.sun.jersey.api.view.Viewable;
44
45 public class RelationshipTypeResource extends TopologyGraphElementEntityTypeResource {
46         
47         private static final Logger logger = LoggerFactory.getLogger(RelationshipTypeResource.class);
48         
49         
50         public RelationshipTypeResource(RelationshipTypeId id) {
51                 super(id);
52         }
53         
54         @Path("implementations/")
55         public ImplementationsOfOneRelationshipTypeResource getImplementations() {
56                 return new ImplementationsOfOneRelationshipTypeResource((RelationshipTypeId) this.id);
57         }
58         
59         @Path("visualappearance/")
60         public VisualAppearanceResource getVisualAppearanceResource() {
61                 return new VisualAppearanceResource(this, this.getElement().getOtherAttributes(), (RelationshipTypeId) this.id);
62         }
63         
64         @Path("instancestates/")
65         public InstanceStatesResource getInstanceStatesResource() {
66                 TTopologyElementInstanceStates instanceStates = this.getRelationshipType().getInstanceStates();
67                 if (instanceStates == null) {
68                         // if an explicit (empty) list does not exist, create it
69                         instanceStates = new TTopologyElementInstanceStates();
70                         this.getRelationshipType().setInstanceStates(instanceStates);
71                 }
72                 return new InstanceStatesResource(this.getRelationshipType().getInstanceStates(), this);
73         }
74         
75         @Path("sourceinterfaces/")
76         public InterfacesResource getSourceInterfaces() {
77                 SourceInterfaces interfaces = this.getRelationshipType().getSourceInterfaces();
78                 if (interfaces == null) {
79                         interfaces = new SourceInterfaces();
80                         this.getRelationshipType().setSourceInterfaces(interfaces);
81                 }
82                 return new InterfacesResource("source", interfaces.getInterface(), this);
83         }
84         
85         @Path("targetinterfaces/")
86         public InterfacesResource getTargetInterfaces() {
87                 TargetInterfaces interfaces = this.getRelationshipType().getTargetInterfaces();
88                 if (interfaces == null) {
89                         interfaces = new TargetInterfaces();
90                         this.getRelationshipType().setTargetInterfaces(interfaces);
91                 }
92                 return new InterfacesResource("target", interfaces.getInterface(), this);
93         }
94         
95         @Path("validendings/")
96         @GET
97         @Produces(MediaType.TEXT_HTML)
98         public Response getHTML() {
99                 Viewable viewable = new Viewable("/jsp/entitytypes/relationshiptypes/validendings.jsp", this);
100                 return Response.ok().entity(viewable).build();
101         }
102         
103         @Path("validsource")
104         @GET
105         public String getValidSource() {
106                 ValidSource validSource;
107                 if (((validSource = this.getRelationshipType().getValidSource()) == null) || (validSource.getTypeRef() == null)) {
108                         return null;
109                 }
110                 return this.getRelationshipType().getValidSource().getTypeRef().toString();
111         }
112         
113         @Path("validsource")
114         @PUT
115         @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
116         public Response setValidSource(String typeRef) {
117                 ValidSource vs = new ValidSource();
118                 QName qname = QName.valueOf(typeRef);
119                 vs.setTypeRef(qname);
120                 this.getRelationshipType().setValidSource(vs);
121                 return BackendUtils.persist(this);
122         }
123         
124         @Path("validtarget")
125         @GET
126         public String getValidTarget() {
127                 ValidTarget validTarget;
128                 if (((validTarget = this.getRelationshipType().getValidTarget()) == null) || (validTarget.getTypeRef() == null)) {
129                         return null;
130                 }
131                 return this.getRelationshipType().getValidTarget().getTypeRef().toString();
132         }
133         
134         @Path("validtarget")
135         @PUT
136         @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
137         public Response setValidTarget(String typeRef) {
138                 ValidTarget vt = new ValidTarget();
139                 QName qname = QName.valueOf(typeRef);
140                 vt.setTypeRef(qname);
141                 this.getRelationshipType().setValidTarget(vt);
142                 return BackendUtils.persist(this);
143         }
144         
145         /**
146          * Required for validendings.jsp
147          */
148         public Collection<NodeTypeId> getPossibleValidEndings() {
149                 SortedSet<NodeTypeId> allNodeTypeIds = Repository.INSTANCE.getAllTOSCAComponentIds(NodeTypeId.class);
150                 return allNodeTypeIds;
151         }
152         
153         /**
154          * Convenience method to avoid casting at the caller's side.
155          */
156         public TRelationshipType getRelationshipType() {
157                 return (TRelationshipType) this.getElement();
158         }
159         
160         @Override
161         protected TExtensibleElements createNewElement() {
162                 return new TRelationshipType();
163         }
164         
165 }