Skip to content

Commit

Permalink
fix for service instance states
Browse files Browse the repository at this point in the history
Signed-off-by: Kálmán Képes <kalman.kepes@iaas.uni-stuttgart.de>
  • Loading branch information
nyuuyn committed Jul 2, 2017
1 parent cfdcc14 commit aefeae7
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public Object getProperties() {

@Path("/State")
public Object getState() {
return new ServiceTemplateInstancePropertiesResource(this.csarId, this.serviceTemplateID, this.serviceTemplateInstanceId);
return new ServiceTemplateInstanceStateResource(this.serviceTemplateInstanceId);
}

@Path("/PlanInstances")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package org.opentosca.container.api.legacy.resources.csar.servicetemplate.instances;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.UriInfo;
import javax.xml.namespace.QName;

import org.opentosca.container.api.legacy.instancedata.exception.GenericRestException;
import org.opentosca.container.api.legacy.osgi.servicegetter.InstanceDataServiceHandler;
import org.opentosca.container.core.common.ReferenceNotFoundException;
import org.opentosca.container.core.model.instance.IdConverter;
import org.opentosca.container.core.service.IInstanceDataService;

import com.google.gson.JsonObject;

/**
* @author Marcus Eisele - marcus.eisele@gmail.com
*
*/
public class ServiceTemplateInstanceStateResource {

private final int nodeInstanceID;


public ServiceTemplateInstanceStateResource(final int id) {
this.nodeInstanceID = id;
}

@GET
@Produces(MediaType.TEXT_PLAIN)
public Response doGetXML() {

final String idr = this.getState();

return Response.ok(idr).build();
}

@GET
@Produces(MediaType.APPLICATION_JSON)
public Response doGetJSON() {

final String idr = this.getState();

final JsonObject json = new JsonObject();
json.addProperty("state", idr);

return Response.ok(json.toString()).build();
}

public String getState() {
final IInstanceDataService service = InstanceDataServiceHandler.getInstanceDataService();

try {
final String state = service.getServiceInstanceState(IdConverter.serviceInstanceIDtoURI(this.nodeInstanceID));

if (state != null) {
return state;
} else {
return null;
}
} catch (final ReferenceNotFoundException e) {
throw new GenericRestException(Status.NOT_FOUND, "Specified nodeInstance with id: " + this.nodeInstanceID + " doesn't exist");
}
}

@PUT
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.APPLICATION_XML)
public Response setState(@Context final UriInfo uriInfo, final String state) {
final IInstanceDataService service = InstanceDataServiceHandler.getInstanceDataService();

QName stateQName = null;
try {
stateQName = QName.valueOf(state);

} catch (final Exception e1) {
throw new GenericRestException(Status.BAD_REQUEST, "Error converting parameter state: " + e1.getMessage());
}

try {
service.setServiceInstanceState(IdConverter.serviceInstanceIDtoURI(this.nodeInstanceID), state);

// SimpleXLink xLink = new
// SimpleXLink(LinkBuilder.linkToNodeInstanceState(uriInfo,
// nodeInstanceID), "NodeInstance: " + nodeInstanceID + " State");
return Response.ok().build();
} catch (final ReferenceNotFoundException e) {
throw new GenericRestException(Status.NOT_FOUND, "Specified nodeInstance with id: " + this.nodeInstanceID + " doesn't exist");
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,17 @@ public List<ServiceInstance> getServiceInstances(final CSARID csarId, final QNam
return queryResults;
}

/**
* this method wraps the setting/saving of the state
*
* @param nodeInstance
* @param state to be set
*/
public void setState(ServiceInstance serviceInstance, String state) {
this.init();
serviceInstance.setState(State.valueOf(State.ServiceTemplate.class, state, State.ServiceTemplate.STARTED));
ServiceInstanceDAO.LOG.debug("Invoke of saving serviceInstance: " + serviceInstance.getServiceInstanceID() + " to update state");
this.storeServiceInstance(serviceInstance);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -915,4 +915,33 @@ public void setServiceInstanceProperties(final URI serviceInstanceID, final Docu

this.updateServiceInstanceProperties(serviceInstance);
}

@Override
public String getServiceInstanceState(URI serviceInstanceID) throws ReferenceNotFoundException {


final List<ServiceInstance> serviceInstances = this.siDAO.getServiceInstances(serviceInstanceID, null, null);

if ((serviceInstances == null) || (serviceInstances.size() != 1)) {
final String msg = String.format("Failed to get State of ServiceInstance: '%s' - does it exist?", serviceInstances);
InstanceDataServiceImpl.LOG.warn(msg);
throw new ReferenceNotFoundException(msg);
}
return serviceInstances.get(0).getState().toString();
}

@Override
public void setServiceInstanceState(URI serviceInstanceIDtoURI, String state) throws ReferenceNotFoundException {
final List<ServiceInstance> serviceInstances = this.siDAO.getServiceInstances(serviceInstanceIDtoURI, null, null);


if ((serviceInstances == null) || (serviceInstances.size() != 1)) {
final String msg = String.format("Failed to set State of NodeInstance: '%s' - does it exist?", serviceInstanceIDtoURI);
InstanceDataServiceImpl.LOG.warn(msg);
throw new ReferenceNotFoundException(msg);
}

this.siDAO.setState(serviceInstances.get(0),state);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -222,5 +222,9 @@ public interface IInstanceDataService {
public void setRelationInstanceProperties(URI relationInstanceID, Document properties) throws ReferenceNotFoundException;

public Document getRelationInstanceProperties(URI relationInstanceID, List<QName> propertiesList) throws ReferenceNotFoundException;

public String getServiceInstanceState(URI serviceInstanceID) throws ReferenceNotFoundException;

public void setServiceInstanceState(URI serviceInstanceIDtoURI, String state) throws ReferenceNotFoundException;

}

0 comments on commit aefeae7

Please sign in to comment.