/** * Method for getting the list of componentType. * * @return res ResponseObject. {@literal Body should be HashMap<ComponentType, * list[ComponentManager.property]>}. */ private Response getComponentTypes() { ComponentTypesHash componentTypesHash = new ComponentTypesHash(); for (String compMgrId : componentMgrsSet) { Response resp = getComponentTypes(compMgrId); if (resp.isError("GET")) { log.warn("invalid GET:" + resp.statusCode); return resp; } try { ComponentTypesHash types = resp.getBody(ComponentTypesHash.class); for (String type : types.keySet()) { if (componentTypesHash.containsKey(type)) { ComponentType compType = componentTypesHash.get(type); compType.addCmId(compMgrId); } else { ComponentType compType = types.get(type).clone(); compType.addCmId(compMgrId); componentTypesHash.put(type, compType); } } } catch (ParseBodyException e) { return new Response(Response.INTERNAL_SERVER_ERROR, "Failed GET component_types."); } } return new Response(Response.OK, componentTypesHash); }
/** * Method for getting the CompoentManager's componentTypes. * * @return res ResponseObject. {@literal Body should be HashMap<String, ComponentType>}. */ private Response getComponentTypes(String compmgrId) { try { Response resp = request(compmgrId, Method.GET, "component_types", null); if (resp.isError("GET")) { log.warn("invalid GET:" + resp.statusCode); return resp; } ComponentTypesHash types = resp.getBody(ComponentTypesHash.class); return new Response(Response.OK, types); } catch (Exception ex) { log.error("Recieved Message Exception.", ex); return new Response(Response.INTERNAL_SERVER_ERROR, "Failed GET component_types."); } }
/** * Asynchronous event publication service for requestSync(). * * <p>The response eventually reaches its originating method (requestSync()) via * RemoteMessageTransport. */ public void publishResponseAsync( final int sno, final String channel, final Request request, final Response response) throws IOException { BufferPacker pk = msgpack.createBufferPacker(); // write delivery header. pk.write(TYPE_RESPONSE); pk.write(sno); pk.write(request.objectId); // write delivery body. pk.write(response); byte[] message = pk.toByteArray(); // PUBLISH driverImpl.publish(channel, message); if (log.isDebugEnabled()) { log.debug( "Response returned:\n" + " MessageDispatcher: {}\n" + " channel: {}\n" + " statusCode: {}\n" + " body: {}", sourceDispatcherId, channel, response.statusCode, response.getBodyValue()); } }
/** * Method for creating a component. * * @param compId ComponentID to be deleted. * @param body RequestObject. Body should be component's objectProperty. * @return res ResponseObject. Body should be component's objectProperty. */ private Response putComponent(final String compId, final ObjectProperty body) { String createdType = body.getObjectType(); if (!allComponentTypes.containsKey(createdType)) { log.warn("Not Creatable Component Type:{}", createdType); return new Response(Response.BAD_REQUEST, "Not Creatable Component Type"); } if (allComponentTypes.get(createdType).isEmpty()) { log.warn("Not Creatable Component Type:{}", createdType); return new Response(Response.BAD_REQUEST, "Not Creatable Component Type"); } String compMgrId = null; if (body.getProperty(ObjectProperty.PropertyNames.CM_ID) != null) { String reqCmId = body.getProperty(ObjectProperty.PropertyNames.CM_ID); for (String cmId : allComponentTypes.get(createdType)) { if (reqCmId.equals(cmId)) { compMgrId = reqCmId; break; } } } else { ArrayList<String> compMgrIds = new ArrayList<String>(allComponentTypes.get(createdType)); compMgrId = compMgrIds.get(0); } if (compMgrId == null) { log.warn("Not Creatable Component Type:{}", createdType); return new Response(Response.BAD_REQUEST, null); } ObjectProperty createdObjProp = null; if (compId == null) { return new Response(Response.INTERNAL_SERVER_ERROR, "Component Id is null"); } else if (componentStateList.containsKey(compId)) { return new Response(Response.CONFLICT, "ComponentId is already registerd"); } body.setProperty(ObjectProperty.PropertyNames.OBJECT_ID, compId); try { this.eventSubscription.addFilter(compId, ObjectPropertyChanged.TYPE); try { this.applyEventSubscription(); } catch (Exception e) { log.error("Recieved Message Exception.", e); } String path = String.format("components/%s", body.getObjectId()); Response resp = request(compMgrId, Method.PUT, path, body); if (!resp.statusCode.equals(Response.CREATED)) { log.warn("Failed to create Component Type:{} StatusCode:{}", createdType, resp.statusCode); return resp; } createdObjProp = resp.getBody(ObjectProperty.class); if (createdObjProp != null) { componentStateList.put(compId, ObjectProperty.State.RUNNING); mapCompAndCompMgr.put(compId, compMgrId); this.componetsObjectProperties.put(compId, createdObjProp); log.info("Created Component Type:{} ID:{}", createdType, compId); // wait components's subscription Thread.sleep(WAIT_SUBSCRIPTION_TIME); return resp; } } catch (Exception e) { log.error("Exception to create Component Type:{} ID:{}", createdType, compId, e); return new Response(Response.INTERNAL_SERVER_ERROR, null); } log.error("Unknwon Failed to create Component Type:{} ID:{}", createdType, compId); return new Response(Response.INTERNAL_SERVER_ERROR, null); }
public static void main(String[] args) { // ///////////////////////////////////// // Set MessageDispatcher. // ///////////////////////////////////// MessageDispatcher dispatcher = new MessageDispatcher(SYSTEM_MGR_ID, DISPATCHER_IP, Integer.parseInt(DISPATCHER_PORT)); dispatcher.start(); // ///////////////////////////////////// // // Set SystemManager Interface // ///////////////////////////////////// SystemManagerInterface systemMngInterface = new SystemManagerInterface(dispatcher); // ///////////////////////////////////// // Set NetworkIntece // ///////////////////////////////////// NetworkInterface originalNetworkIf = new NetworkInterface(dispatcher, ORIGINAL_NW_ID); NetworkInterface aggregatedNetworkIf = new NetworkInterface(dispatcher, AGGREGATED_NW_ID); outMsg("//////////////////////////////////////////////////"); outMsg("//// (1) Delete All Flows."); outMsg("//////////////////////////////////////////////////"); List<Response> rsps = originalNetworkIf.deleteAllFlow(); for (Response rsp : rsps) { outMsg(" -DELETE FLow. "); outMsg(" -Received: " + rsp.statusCode + " " + rsp.getBodyValue()); } wait(WAIT_TIME); rsps = aggregatedNetworkIf.deleteAllFlow(); for (Response rsp : rsps) { outMsg(" -DELETE FLow. "); outMsg(" -Received: " + rsp.statusCode + " " + rsp.getBodyValue()); } wait(WAIT_TIME); outMsg("//////////////////////////////////////////////////"); outMsg("//// (2) Delete Topology."); outMsg("//////////////////////////////////////////////////"); rsps = originalNetworkIf.deleteTopology(); for (Response rsp : rsps) { outMsg(" -PUT (empty)Topology (Delete Topology). "); outMsg(" -Received: " + rsp.statusCode + " " + rsp.getBodyValue()); } wait(WAIT_TIME * 2); rsps = aggregatedNetworkIf.deleteTopology(); for (Response rsp : rsps) { outMsg(" -PUT (empty)Topology (Delete Topology). "); outMsg(" -Received: " + rsp.statusCode + " " + rsp.getBodyValue()); } wait(WAIT_TIME * 2); outMsg("//////////////////////////////////////////////////"); outMsg("//// (3) Delete Connections."); outMsg("//////////////////////////////////////////////////"); Response rsp = systemMngInterface.delConnection("conn1"); outMsg(" -DELETE Connection. "); outMsg(" -Received: " + rsp.statusCode + " " + rsp.getBodyValue()); wait(WAIT_TIME * 2); rsp = systemMngInterface.delConnection("conn2"); outMsg(" -DELETE Connection. "); outMsg(" -Received: " + rsp.statusCode + " " + rsp.getBodyValue()); wait(WAIT_TIME * 2); rsp = systemMngInterface.delConnection("conn3"); outMsg(" -DELETE Connection. "); outMsg(" -Received: " + rsp.statusCode + " " + rsp.getBodyValue()); wait(WAIT_TIME * 2); rsp = systemMngInterface.delConnection("conn4"); outMsg(" -DELETE Connection. "); outMsg(" -Received: " + rsp.statusCode + " " + rsp.getBodyValue()); wait(WAIT_TIME * 2); outMsg("//////////////////////////////////////////////////"); outMsg("//// (4) Delete Components."); outMsg("//////////////////////////////////////////////////"); rsp = systemMngInterface.delComponent(DUMMY_DRIVER_ID); outMsg(" -DELETE Component. "); outMsg(" -Received: " + rsp.statusCode + " " + rsp.getBodyValue()); wait(WAIT_TIME * 2); rsp = systemMngInterface.delComponent(LEARNING_SWITCH_ID); outMsg(" -DELETE Component. "); outMsg(" -Received: " + rsp.statusCode + " " + rsp.getBodyValue()); wait(WAIT_TIME * 2); rsp = systemMngInterface.delComponent(ORIGINAL_NW_ID); outMsg(" -DELETE Component. "); outMsg(" -Received: " + rsp.statusCode + " " + rsp.getBodyValue()); wait(WAIT_TIME * 2); rsp = systemMngInterface.delComponent(AGGREGATED_NW_ID); outMsg(" -DELETE Component. "); outMsg(" -Received: " + rsp.statusCode + " " + rsp.getBodyValue()); wait(WAIT_TIME * 2); rsp = systemMngInterface.delComponent(AGGREGATOR_ID); outMsg(" -DELETE Component. "); outMsg(" -Received: " + rsp.statusCode + " " + rsp.getBodyValue()); wait(WAIT_TIME * 2); dispatcher.close(); System.exit(0); }
/** * onMessage implementation. * * <p>This method has two roles: * * <ul> * <li>Works like a mail transfer agent to transfer a received request or event to {@link * org.o3project.odenos.remoteobject.RemoteObject}. * <li>Supports both synchronous and asynchronous messaging. * </ul> */ @Override public void onMessage(final String channel, byte[] message) { serial++; // Serial number for incoming messages. try { BufferUnpacker upk = msgpack.createBufferUnpacker(message); // read delivery header. byte type = upk.readByte(); final int sno = upk.readInt(); // Sequence number for outgoing request messages. final String sourceObjectId = upk.readString(); // i.e., channel RemoteObject localObject = null; Queue<Mail> mailbox = null; Mail mail = null; switch (type) { case TYPE_REQUEST: // Transaction(request): synchronous operation /* * --- request --> dispatchRequest() -------> [RemoteObject] * | * <-- response --- publishResponseAsync() <-------+ */ final Request request = upk.read(Request.class); if (log.isDebugEnabled()) { log.debug( "Request received:\n" + " MessageDispatcher: {}\n" + " sno: {}\n" + " channel: {}\n" + " method: {}\n" + " objectId: {}\n" + " sourceObjectId: {}\n" + " path: /{}/\n" + " body: {}", sourceDispatcherId, sno, channel, request.method, request.objectId, sourceObjectId, request.path, request.getBodyValue()); } // Wraps the request with Mail and deliver it to a mailbox. String to = request.objectId; mail = new Mail(serial, sno, to, sourceObjectId, this, request, null); localObject = localObjectsMap.get(to); if (localObject != null) { mailbox = localObject.getMailbox(); synchronized (mailbox) { mailbox.add(mail); if (!localObject.isRunning()) { localObject.setRunning(true); // Assigns a thread to read a mail in the mailbox. actor.read(localObject); } } } break; case TYPE_RESPONSE: // Transaction(response): synchronous operation /* * publishRequestAsync() * requestSync() -> [RemoteTransaction] ---- request -----> [RemoteObject] * <-- ^ | * | publishResponseAsync() * | | * +- signalResponse() <-- response --+ */ Response response = upk.read(Response.class); if (log.isDebugEnabled()) { log.debug( "Response received:\n" + " MessageDispatcher: {}\n" + " channel: {}\n" + " statusCode: {}\n" + " body: {}", sourceDispatcherId, channel, response.statusCode, response.getBodyValue()); } remoteTransactions.signalResponse(sno, response); break; case TYPE_EVENT: // Asynchronous /* * publishEventAsync() -- event --> dispatchEvent() --> [RemoteObject] * : * [EventSubscriptionMap] */ final Event event = upk.read(Event.class); if (log.isDebugEnabled()) { log.debug( "Event received:\n" + " MessageDispatcher: {}\n" + " channel: {}\n" + " body: {}", sourceDispatcherId, channel, event.getBodyValue()); } // All the subscribers of the channel final Collection<String> subscribers = subscribersMap.getSubscribers(channel); if (subscribers == null) { // No subscribers found on the channel if (log.isDebugEnabled()) { log.debug("no subscribers subscribing the channel: {}", channel); } return; // Silently discards the event } // Wraps the event with Mail and deliver it to a mailbox. for (String subscriber : subscribers) { localObject = localObjectsMap.get(subscriber); if (localObject != null) { mail = new Mail(serial, sno, subscriber, channel, this, null, event); mailbox = localObject.getMailbox(); synchronized (mailbox) { mailbox.add(mail); if (!localObject.isRunning()) { localObject.setRunning(true); // Assigns a thread to read a mail in the mailbox. actor.read(localObject); } } } } break; default: break; } } catch (Exception e) { log.error("onMessage failed", e); } }