#!/usr/bin/env python # # Copyright (c) 2016 Cisco and/or its affiliates. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at: # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import os from string import Template import dto_gen import util jvpp_facade_callback_template = Template(""" package $base_package.$future_package; /** *

Async facade callback setting values to future objects *
It was generated by jvpp_future_facade_gen.py based on $inputfile *
(python representation of vpe.api generated by vppapigen). */ public final class FutureJVppFacadeCallback implements $base_package.$callback_package.JVppGlobalCallback { private final java.util.Map>> requests; private final $base_package.$notification_package.GlobalNotificationCallback notificationCallback; public FutureJVppFacadeCallback(final java.util.Map>> requestMap, final $base_package.$notification_package.GlobalNotificationCallback notificationCallback) { this.requests = requestMap; this.notificationCallback = notificationCallback; } @Override @SuppressWarnings("unchecked") public void onError(org.openvpp.jvpp.VppCallbackException reply) { final java.util.concurrent.CompletableFuture<$base_package.$dto_package.JVppReply> completableFuture; synchronized(requests) { completableFuture = (java.util.concurrent.CompletableFuture<$base_package.$dto_package.JVppReply>) requests.get(reply.getCtxId()); } if(completableFuture != null) { completableFuture.completeExceptionally(reply); synchronized(requests) { requests.remove(reply.getCtxId()); } } } $methods } """) jvpp_facade_callback_method_template = Template(""" @Override @SuppressWarnings("unchecked") public void on$callback_dto($base_package.$dto_package.$callback_dto reply) { final java.util.concurrent.CompletableFuture<$base_package.$dto_package.JVppReply> completableFuture; synchronized(requests) { completableFuture = (java.util.concurrent.CompletableFuture<$base_package.$dto_package.JVppReply>) requests.get(reply.context); } if(completableFuture != null) { completableFuture.complete(reply); synchronized(requests) { requests.remove(reply.context); } } } """) jvpp_facade_callback_notification_method_template = Template(""" @Override public void on$callback_dto($base_package.$dto_package.$callback_dto notification) { notificationCallback.on$callback_dto(notification); } """) # TODO reuse common parts with generic method callback jvpp_facade_control_ping_method_template = Template(""" @Override @SuppressWarnings("unchecked") public void on$callback_dto($base_package.$dto_package.$callback_dto reply) { final java.util.concurrent.CompletableFuture<$base_package.$dto_package.JVppReply> completableFuture; synchronized(requests) { completableFuture = (java.util.concurrent.CompletableFuture<$base_package.$dto_package.JVppReply>) requests.get(reply.context); } if(completableFuture != null) { // Finish dump call if (completableFuture instanceof $base_package.$future_package.FutureJVppFacade.CompletableDumpFuture) { completableFuture.complete((($base_package.$future_package.FutureJVppFacade.CompletableDumpFuture) completableFuture).getReplyDump()); // Remove future mapped to dump call context id synchronized(requests) { requests.remove((($base_package.$future_package.FutureJVppFacade.CompletableDumpFuture) completableFuture).getContextId()); } } else { completableFuture.comp

import zlib
import struct

class ZippedMsg:

    MSG_COMPRESS_THRESHOLD    = 256
    MSG_COMPRESS_HEADER_MAGIC = 0xABE85CEA

    def check_threshold (self, msg):
        return len(msg) >= self.MSG_COMPRESS_THRESHOLD

    def compress (self, msg):
        # compress
        compressed = zlib.compress(msg)
        new_msg = struct.pack(">II", self.MSG_COMPRESS_HEADER_MAGIC, len(msg)) + compressed
        return new_msg


    def decompress (self, msg):
        if len(msg) < 8:
            return None

        t = struct.unpack(">II", msg[:8])
        if (t[0] != self.MSG_COMPRESS_HEADER_MAGIC):
            return None

        x = zlib.decompress(msg[8:])
        if len(x) != t[1]:
            return None

        return x
late(''' package $base_package.$future_package; /** *

Async facade extension adding specific methods for each request invocation *
It was generated by jvpp_future_facade_gen.py based on $inputfile *
(python representation of vpe.api generated by vppapigen). */ public interface FutureJVpp extends FutureJVppInvoker { $methods } ''') future_jvpp_method_template = Template(''' java.util.concurrent.CompletionStage<$base_package.$dto_package.$reply_name> $method_name($base_package.$dto_package.$request_name request); ''') future_jvpp_facade_template = Template(''' package $base_package.$future_package; /** *

Implementation of FutureJVpp based on FutureJVppInvokerFacade *
It was generated by jvpp_future_facade_gen.py based on $inputfile *
(python representation of vpe.api generated by vppapigen). */ public class FutureJVppFacade extends FutureJVppInvokerFacade implements FutureJVpp { /** *

Create FutureJVppFacade object for provided JVpp instance. * Constructor internally creates FutureJVppFacadeCallback class for processing callbacks * and then connects to provided JVpp instance * * @param jvpp provided $base_package.JVpp instance * * @throws java.io.IOException in case instance cannot connect to JVPP */ public FutureJVppFacade(final $base_package.JVpp jvpp) throws java.io.IOException { super(jvpp, new java.util.HashMap<>()); jvpp.connect(new FutureJVppFacadeCallback(getRequests(), getNotificationCallback())); } $methods } ''') future_jvpp_method_impl_template = Template(''' @Override public java.util.concurrent.CompletionStage<$base_package.$dto_package.$reply_name> $method_name($base_package.$dto_package.$request_name request) { return send(request); } ''') # Returns request name or special one from unconventional_naming_rep_req map def get_standard_dump_reply_name(camel_case_dto_name, func_name): # FIXME this is a hotfix for sub-details callbacks # FIXME also for L2FibTableEntry # It's all because unclear mapping between # request -> reply, # dump -> reply, details, # notification_start -> reply, notifications # vpe.api needs to be "standardized" so we can parse the information and create maps before generating java code suffix = func_name.split("_")[-1] return util.underscore_to_camelcase_upper( util.unconventional_naming_rep_req[func_name]) + util.underscore_to_camelcase_upper(suffix) if func_name in util.unconventional_naming_rep_req \ else camel_case_dto_name