Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
- event pushing
- metadata request by type
  • Loading branch information
tjamakeev committed Dec 29, 2018
1 parent 466ad0b commit 1a19de0
Show file tree
Hide file tree
Showing 13 changed files with 74 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import org.apache.commons.lang3.StringUtils;

import io.subutai.bazaar.share.common.BazaaarAdapter;
import io.subutai.bazaar.share.event.payload.Payload;
import io.subutai.common.dao.DaoManager;
import io.subutai.common.environment.Environment;
import io.subutai.common.host.ContainerHostInfo;
Expand All @@ -17,17 +19,16 @@
import io.subutai.common.peer.HostNotFoundException;
import io.subutai.common.peer.LocalPeer;
import io.subutai.common.util.ServiceLocator;
import io.subutai.core.environment.api.EnvironmentEventListener;
import io.subutai.core.hostregistry.api.HostListener;
import io.subutai.core.bazaarmanager.api.BazaarManager;
import io.subutai.core.bazaarmanager.api.RestClient;
import io.subutai.core.bazaarmanager.api.RestResult;
import io.subutai.core.bazaarmanager.api.dao.ConfigDataService;
import io.subutai.core.bazaarmanager.impl.dao.ConfigDataServiceImpl;
import io.subutai.core.environment.api.EnvironmentEventListener;
import io.subutai.core.hostregistry.api.HostListener;
import io.subutai.core.identity.api.IdentityManager;
import io.subutai.core.identity.api.model.User;
import io.subutai.core.peer.api.PeerManager;
import io.subutai.bazaar.share.common.BazaaarAdapter;

import static java.lang.String.format;

Expand Down Expand Up @@ -258,6 +259,35 @@ public void notifyContainerDiskUsageExcess( String peerId, String envId, String
}


@Override
public void pushEvent( final Payload message )
{
RestResult result =
getRestClient().post( String.format( "/rest/v1/peers/%s/event", peerId ), message, message.getClass() );

if ( !result.isSuccess() )
{
log.error( "Error pushing event to Bazaar: HTTP {} - {}", result.getStatus(), result.getReasonPhrase() );
}
}


@Override
public Payload getMetaData( final String environmentId, final String type )
{
RestResult result = getRestClient()
.get( String.format( "/rest/v1/peers/%s/meta/%s/%s", peerId, environmentId, type ), Payload.class );

if ( !result.isSuccess() )
{
log.error( "Error retrieving meta data from Bazaar: HTTP {} - {}", result.getStatus(),
result.getReasonPhrase() );
}

return ( Payload ) result.getEntity();
}


private void onContainerStateChange( String envId, String contId, String state )
{
if ( !isRegistered() )
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package io.subutai.core.environment.metadata.api;


import io.subutai.bazaar.share.dto.environment.EnvironmentInfoDto;
import io.subutai.bazaar.share.events.Event;
import io.subutai.bazaar.share.event.payload.Payload;
import io.subutai.core.identity.api.exception.TokenCreateException;


Expand All @@ -18,7 +17,7 @@ public interface EnvironmentMetadataManager

void issueToken( String containerIp ) throws TokenCreateException;

EnvironmentInfoDto getEnvironmentInfoDto( String environmentId );
Payload getEnvironmentInfoDto( String environmentId, String type );

void pushEvent( Event event );
void pushEvent( Payload eventMessage );
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.subutai.bazaar.share.dto.environment.EnvironmentInfoDto;
import io.subutai.bazaar.share.events.Event;
import io.subutai.bazaar.share.common.BazaaarAdapter;
import io.subutai.bazaar.share.event.payload.Payload;
import io.subutai.common.command.CommandException;
import io.subutai.common.command.RequestBuilder;
import io.subutai.common.environment.Environment;
import io.subutai.common.environment.EnvironmentNotFoundException;
import io.subutai.common.host.SubutaiOrigin;
import io.subutai.common.peer.ContainerHost;
import io.subutai.common.peer.HostNotFoundException;
Expand All @@ -29,14 +27,16 @@ public class EnvironmentMetadataManagerImpl implements EnvironmentMetadataManage
private final IdentityManager identityManager;
private PeerManager peerManager;
private EnvironmentManager environmentManager;
private BazaaarAdapter bazaarAdapter;


public EnvironmentMetadataManagerImpl( PeerManager peerManager, EnvironmentManager environmentManager,
IdentityManager identityManager )
IdentityManager identityManager, BazaaarAdapter bazaaarAdapter )
{
this.peerManager = peerManager;
this.environmentManager = environmentManager;
this.identityManager = identityManager;
this.bazaarAdapter = bazaaarAdapter;
}


Expand Down Expand Up @@ -74,27 +74,17 @@ public void issueToken( String containerIp ) throws TokenCreateException


@Override
public EnvironmentInfoDto getEnvironmentInfoDto( final String environmentId )
public Payload getEnvironmentInfoDto( final String environmentId, final String type )
{
final EnvironmentInfoDto result = new EnvironmentInfoDto();
try
{
Environment environment = environmentManager.loadEnvironment( environmentId );
result.setName( environment.getName() );
result.setSubnetCidr( environment.getSubnetCidr() );
}
catch ( EnvironmentNotFoundException e )
{
// ignore
}
return result;
return bazaarAdapter.getMetaData( environmentId, type );
}


@Override
public void pushEvent( final Event event )
public void pushEvent( final Payload eventMessage )
{
LOG.debug( "Event received: {}", event );
LOG.debug( "Event received: {}", eventMessage );
bazaarAdapter.pushEvent( eventMessage );
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@
<reference id="identityManager"
interface="io.subutai.core.identity.api.IdentityManager" />

<reference id="bazaarAdapter"
interface="io.subutai.bazaar.share.common.BazaaarAdapter" />

<bean id="environmentMetadataManager" scope="singleton" init-method="init" destroy-method="dispose"
class="io.subutai.core.environment.metadata.impl.EnvironmentMetadataManagerImpl">
<argument ref="peerManager" />
<argument ref="environmentManager" />
<argument ref="identityManager" />
<argument ref="bazaarAdapter" />
</bean>

<service ref="environmentMetadataManager"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import io.subutai.bazaar.share.events.Event;
import io.subutai.bazaar.share.event.payload.Payload;
import io.subutai.common.host.SubutaiOrigin;
import io.subutai.core.identity.api.IdentityManager;

Expand All @@ -30,13 +30,14 @@ public interface RestService
@GET
Response echo( @HeaderParam( "containerId" ) String containerId, @PathParam( "message" ) String message );

@Path( "/info" )
@Path( "/info/{type}" )
@Produces( MediaType.APPLICATION_JSON )
@GET
Response getEnvironmentDto( @HeaderParam( IdentityManager.SUBUTAI_ORIGIN_HEADER_KEY ) SubutaiOrigin origin );
Response getEnvironmentDto( @HeaderParam( IdentityManager.SUBUTAI_ORIGIN_HEADER_KEY ) SubutaiOrigin origin, @PathParam( "type" ) String type );

@Path( "/event" )
@Consumes( MediaType.APPLICATION_JSON )
@POST
Response pushEvent( @HeaderParam( IdentityManager.SUBUTAI_ORIGIN_HEADER_KEY ) SubutaiOrigin origin, Event event );
Response pushEvent( @HeaderParam( IdentityManager.SUBUTAI_ORIGIN_HEADER_KEY ) SubutaiOrigin origin,
Payload payload );
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.subutai.bazaar.share.dto.environment.EnvironmentInfoDto;
import io.subutai.bazaar.share.events.Event;
import io.subutai.bazaar.share.event.payload.Payload;
import io.subutai.common.host.SubutaiOrigin;
import io.subutai.core.environment.metadata.api.EnvironmentMetadataManager;
import io.subutai.core.identity.api.exception.TokenCreateException;
Expand Down Expand Up @@ -50,18 +49,17 @@ public Response echo( final String containerId, String message )


@Override
public Response getEnvironmentDto( final SubutaiOrigin origin )
public Response getEnvironmentDto( final SubutaiOrigin origin, final String type )
{
EnvironmentInfoDto environmentInfoDto =
environmentMetadataManager.getEnvironmentInfoDto( origin.getEnvironmentId() );
Payload environmentInfoDto = environmentMetadataManager.getEnvironmentInfoDto( origin.getEnvironmentId(), type );
return Response.ok( environmentInfoDto ).build();
}


@Override
public Response pushEvent( final SubutaiOrigin origin, final Event event )
public Response pushEvent( final SubutaiOrigin origin, final Payload payload )
{
environmentMetadataManager.pushEvent( event );
environmentMetadataManager.pushEvent( payload );
return Response.noContent().build();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package io.subutai.bazaar.share.common;


import io.subutai.bazaar.share.event.EventMessage;
import io.subutai.bazaar.share.event.payload.Payload;


public interface BazaaarAdapter
{
//
Expand All @@ -27,4 +31,8 @@ public interface BazaaarAdapter

void notifyContainerDiskUsageExcess( String peerId, String envId, String contId, long diskUsage,
boolean containerWasStopped );

Payload getMetaData( final String environmentId, final String type );

void pushEvent( Payload message );
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Preconditions;
Expand All @@ -23,11 +24,11 @@ public class OriginMeta implements Meta
@JsonProperty
private String environmentId;


@JsonCreator
public OriginMeta( final String subutaiOrigin )
{
Preconditions.checkNotNull( subutaiOrigin );
final String[] parts = subutaiOrigin.split( "\\." );
final String[] parts = subutaiOrigin.split( ":" );
if ( parts.length != 3 )
{
throw new IllegalArgumentException( "Invalid origin argument." );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ public String getMessage()
@Override
public String toString()
{
return "StringPayload{" + "message='" + message + '\'' + '}';
return "CustomPayload{" + "message='" + message + '\'' + '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import com.fasterxml.jackson.annotation.JsonTypeInfo;


@JsonTypeInfo( use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type" )
@JsonTypeInfo( use = JsonTypeInfo.Id.NAME, property = "type" )
@JsonSubTypes( {
@JsonSubTypes.Type( value = CustomPayload.class, name = "custom" ),
@JsonSubTypes.Type( value = LogPayload.class, name = "log" ),
Expand All @@ -18,8 +18,4 @@
@JsonIgnoreProperties( ignoreUnknown = true )
abstract public class Payload
{
public enum Nature
{
CUSTOM, LOG, PROGRESS
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void setup()

logPayload = new LogPayload("source", "initiated", LogPayload.Level.INFO);

origin = new OriginMeta( "subutaiPeer.subutaiContainer.subutaiEnvironment" );
origin = new OriginMeta( "subutaiPeer:subutaiContainer:subutaiEnvironment" );

source = new SourceMeta( "cassandra-blueprint", SourceMeta.Type.BLUEPRINT );
}
Expand Down

0 comments on commit 1a19de0

Please sign in to comment.