def GenerateServer(self, file, MetaDict):
        cdecl = """
#include <pugixml/pugixml.hpp>
using namespace pugi;
namespace autogen {
"""
        file.write(cdecl)

        n_links = 0
        for meta in MetaDict.itervalues():
            genr = None
            if type(meta) is IFMapProperty:
                genr = IFMapGenProperty(meta)
            elif type(meta) is IFMapLink:
                genr = IFMapGenLink(meta)
                n_links += 1
            elif type(meta) is IFMapLinkAttr:
                genr = IFMapGenLinkAttr(meta)
            if genr:
                genr.GenerateParser(self._DecoderDict, file)
                genr.GenerateJsonParser(self._JsonDecoderDict, file)

        parse_link_decl = """static bool ParseLinkMetadata(const xml_node &parent,
    std::auto_ptr<AutogenProperty> *resultp) {
    return true;
}

static bool ParseJsonLinkMetadata(const contrail_rapidjson::Value &parent,
    std::auto_ptr<AutogenProperty> *resultp) {
    return true;
}

"""
        if n_links > 0:
            file.write(parse_link_decl)

        file.write('}  // namespace autogen\n\n')

        module_name = GetModuleName(file, '_server.cc')
        file.write('void %s_ParserInit(IFMapServerParser *xparser) {\n' %
                   module_name)
        indent = ' ' * 4
        for kvp in self._DecoderDict.iteritems():
            fmt = 'xparser->MetadataRegister("%s",\n'
            file.write(indent + fmt % kvp[0])
            indent1 = ' ' * 8
            fmt = '&autogen::%s);\n'
            file.write(indent1 + fmt % kvp[1])
        file.write('}\n')

        file.write('\n')
        module_name = GetModuleName(file, '_server.cc')
        file.write('void %s_JsonParserInit(ConfigJsonParser *jparser) {\n' %
                   module_name)
        indent = ' ' * 4
        for kvp in self._JsonDecoderDict.iteritems():
            fmt = 'jparser->MetadataRegister("%s",\n'
            file.write(indent + fmt % kvp[0])
            indent1 = ' ' * 8
            fmt = '&autogen::%s);\n'
            file.write(indent1 + fmt % kvp[1])
        file.write('}\n')
    def GenerateAgent(self, file, IdDict, MetaDict):
        cdecl = """
#include <pugixml/pugixml.hpp>
using namespace autogen;
using namespace pugi;
"""
        file.write(cdecl)
        module_name = GetModuleName(file, '_agent.cc')
        for ident in IdDict.itervalues():
            if not ident._xelement:
                # cross-ref'd id from another file
                continue
            cdecl = """
IFMapObject* %(class)sAgentParse(xml_node &node, DB *db, std::string *id_name) {
    
    IFMapAgentTable *table = static_cast<IFMapAgentTable *>
                                            (IFMapTable::FindTable(db, "%(nodename)s"));
    if (!table) {
        return NULL;
    }

    %(class)s *data = static_cast<%(class)s *>(table->AllocObject());
    if (%(class)s::Decode(node, id_name, data) == false) {
        delete data;
        return NULL;
    };

    return data;
}

""" % {
                'class': ident.getCppName(),
                'nodename': ident.getName()
            }
            file.write(cdecl)

        for ident in MetaDict.itervalues():
            if not type(ident) is IFMapLinkAttr:
                continue
            cdecl = """
IFMapObject* %(class)sAgentParse(xml_node &node, DB *db, std::string *id_name) {
    
    IFMapAgentTable *table = static_cast<IFMapAgentTable *>
                                            (IFMapTable::FindTable(db, "%(nodename)s"));
    if (!table) {
        return NULL;
    }

    %(class)s *data = static_cast<%(class)s *>(table->AllocObject());
    if (%(class)s::Decode(node, id_name, data) == false) {
        return NULL;
    };

    return data;
}

""" % {
                'class': ident.getCppName(),
                'nodename': ident.getName()
            }
            file.write(cdecl)

        file.write(
            'void %s_Agent_ParserInit(DB *db, IFMapAgentParser *xparser) {\n' %
            module_name)
        indent = ' ' * 4
        for ident in IdDict.itervalues():
            if not ident._xelement:
                # cross-ref'd id from another file
                continue
            file.write(indent)
            fmt = 'xparser->NodeRegister("%s", &%sAgentParse);\n'
            file.write(fmt % (ident.getName(), ident.getCppName()))

        for ident in MetaDict.itervalues():
            if not type(ident) is IFMapLinkAttr:
                continue
            indent = ' ' * 4
            file.write(indent)

            fmt = 'xparser->NodeRegister("%s", &%sAgentParse);\n'
            file.write(fmt % (ident.getName(), ident.getCppName()))
        file.write('}\n')
Beispiel #3
0
    def Generate(self, file, IdentifierDict, MetaDict):
        module_name = GetModuleName(file, '_types.h')

#include <boost/cstdint.hpp>  // for boost::uint16_t
        header = """
// autogenerated file --- DO NOT EDIT ---
#ifndef __SCHEMA__%(modname)s_TYPES_H__
#define __SCHEMA__%(modname)s_TYPES_H__
#include <vector>

#include <boost/dynamic_bitset.hpp>
#include <boost/crc.hpp>      // for boost::crc_32_type
namespace pugi {
class xml_node;
}  // namespace pugi

#include "ifmap/autogen.h"
#include "ifmap/ifmap_object.h"

class DB;
class DBGraph;
class IFMapServerParser;
class IFMapAgentParser;

namespace autogen {

""" % {'modname': module_name.upper()}
        file.write(header)
        for idn in IdentifierDict.values():
            # generate all dependent types
            properties = idn.getProperties()
            for prop in properties:
                if prop._xelement.isComplex():
                    self._GenerateProperty(file, prop)
                elif prop.getParent() == 'all':
                    self._GenerateSimpleProperty(file, prop)

        for meta in MetaDict.values():
            if type(meta) is IFMapLinkAttr:
                ctype = meta.getCType()
                if ctype:
                    self._TypeGenerator.GenerateType(file, ctype)

        for idn in IdentifierDict.values():
            if not idn._xelement:
                # cross-ref'd id from another file
                continue
            generator = IFMapGenIdentifier(self._cTypeDict, idn)
            generator.ServerClassDefn(file)

        for meta in MetaDict.values():
            if type(meta) is IFMapLinkAttr:
                generator = IFMapGenLinkAttr(self, meta)
                generator.ServerClassDefn(file)

        file.write('}  // namespace autogen\n')

        file.write('\nstruct %s_GraphFilterInfo {\n' % module_name)
        file.write('    %s_GraphFilterInfo(std::string left, std::string right,\n' % module_name) 
        file.write('                            std::string meta, bool linkattr) :\n')
        file.write('        left_(left), right_(right), metadata_(meta), linkattr_(linkattr) { }\n')
        file.write('    std::string left_;\n')
        file.write('    std::string right_;\n')
        file.write('    std::string metadata_;\n')
        file.write('    bool linkattr_;\n')
        file.write('};\n')
        file.write('typedef std::vector<%s_GraphFilterInfo> %s_FilterInfo;\n\n' % (module_name, module_name))

        file.write('extern void %s_Server_ModuleInit(DB *, DBGraph *);\n'
                   % module_name)
        file.write('extern void %s_Server_GenerateGraphFilter(%s_FilterInfo *);\n'
                   % (module_name, module_name))
        file.write('extern void %s_Agent_ModuleInit(DB *, DBGraph *);\n'
                   % module_name)
        file.write('extern void %s_Agent_ParserInit(DB *, IFMapAgentParser *);\n' % module_name)        
        file.write('extern void %s_ParserInit(IFMapServerParser *);\n' % module_name)
        file.write('#endif  // __SCHEMA__%s_TYPES_H__\n' %
                   module_name.upper())