Line data Source code
1 : /* 2 : * Copyright (c) 2014 Juniper Networks, Inc. All rights reserved. 3 : * Copyright Contributors to the OpenSDN project 4 : */ 5 : 6 : #ifndef VNSW_AGENT_OPER_SERVICE_INSTANCE_H__ 7 : #define VNSW_AGENT_OPER_SERVICE_INSTANCE_H__ 8 : 9 : #include <string> 10 : #include <map> 11 : #include <boost/uuid/uuid.hpp> 12 : #include "cmn/agent_db.h" 13 : #include "oper/ifmap_dependency_manager.h" 14 : #include "schema/vnc_cfg_types.h" 15 : 16 : class DBGraph; 17 : class IFMapDependencyManager; 18 : class InstanceManager; 19 : 20 : class ServiceInstanceKey : public AgentKey { 21 : public: 22 0 : explicit ServiceInstanceKey(boost::uuids::uuid uuid) { 23 0 : uuid_ = uuid; 24 0 : } 25 0 : const boost::uuids::uuid &instance_id() const { 26 0 : return uuid_; 27 : } 28 : 29 : private: 30 : boost::uuids::uuid uuid_; 31 : }; 32 : 33 : class ServiceInstance : public AgentRefCount<ServiceInstance>, 34 : public AgentDBEntry { 35 : public: 36 : enum ServiceType { 37 : Other = 1, 38 : SourceNAT, 39 : LoadBalancer, 40 : }; 41 : enum VirtualizationType { 42 : VirtualMachine = 1, 43 : NetworkNamespace, 44 : VRouterInstance 45 : }; 46 : enum VRouterInstanceType { 47 : KVM = 1, 48 : Docker 49 : }; 50 : 51 : /* 52 : * Properties computed from walking the ifmap graph. 53 : * POD type. 54 : */ 55 : struct Properties { 56 : void Clear(); 57 : int CompareTo(const Properties &rhs) const; 58 : const std::string &ServiceTypeString() const; 59 : 60 : bool Usable() const; 61 : std::string DiffString(const Properties &rhs) const; 62 : std::string IdToCmdLineStr() const; 63 : boost::uuids::uuid ToId() const; 64 : 65 : /* template parameters */ 66 : int service_type; 67 : int virtualization_type; 68 : int vrouter_instance_type; 69 : 70 : /* virtual machine */ 71 : boost::uuids::uuid instance_id; 72 : 73 : /* interfaces */ 74 : boost::uuids::uuid vmi_inside; 75 : boost::uuids::uuid vmi_outside; 76 : boost::uuids::uuid vmi_management; 77 : 78 : std::string mac_addr_inside; 79 : std::string mac_addr_outside; 80 : std::string mac_addr_management; 81 : 82 : std::string ip_addr_inside; 83 : std::string ip_addr_outside; 84 : std::string ip_addr_management; 85 : 86 : std::string gw_ip; 87 : std::string image_name; 88 : 89 : int ip_prefix_len_inside; 90 : int ip_prefix_len_outside; 91 : int ip_prefix_len_management; 92 : 93 : int interface_count; 94 : 95 : std::string instance_data; 96 : std::vector<autogen::KeyValuePair> instance_kvps; 97 : 98 : // loadbalancer id 99 : std::string loadbalancer_id; 100 : }; 101 : 102 : ServiceInstance(); 103 : virtual bool IsLess(const DBEntry &rhs) const; 104 : virtual std::string ToString() const; 105 : virtual void SetKey(const DBRequestKey *key); 106 : virtual KeyPtr GetDBRequestKey() const; 107 : bool DBEntrySandesh(Sandesh *sresp, std::string &name) const; 108 : 109 0 : virtual uint32_t GetRefCount() const { 110 0 : return AgentRefCount<ServiceInstance>::GetRefCount(); 111 : } 112 : 113 0 : void set_properties(const Properties &properties) { 114 0 : properties_ = properties; 115 0 : } 116 : 117 0 : const Properties &properties() const { return properties_; } 118 : 119 0 : const boost::uuids::uuid &uuid() const { return uuid_; } 120 : 121 : bool IsUsable() const; 122 : 123 0 : void SetIFMapNodeState(IFMapDependencyManager::IFMapNodePtr ref) { 124 0 : ifmap_node_state_ref_ = ref; 125 0 : } 126 : 127 0 : IFMapNode *ifmap_node() { 128 0 : if (!ifmap_node_state_ref_) 129 0 : return NULL; 130 0 : IFMapNodeState *state = ifmap_node_state_ref_.get(); 131 0 : return state->node(); 132 : } 133 : private: 134 : boost::uuids::uuid uuid_; 135 : Properties properties_; 136 : IFMapDependencyManager::IFMapNodePtr ifmap_node_state_ref_; 137 : 138 : DISALLOW_COPY_AND_ASSIGN(ServiceInstance); 139 : }; 140 : 141 : class ServiceInstanceTable : public AgentDBTable { 142 : public: 143 : ServiceInstanceTable(DB *db, const std::string &name); 144 : 145 : virtual std::unique_ptr<DBEntry> AllocEntry(const DBRequestKey *key) const; 146 : 147 : /* 148 : * Register with the dependency manager. 149 : */ 150 : void Initialize(DBGraph *graph, IFMapDependencyManager *dependency_manager); 151 : 152 : /* 153 : * Add/Delete methods establish the mapping between the IFMapNode 154 : * and the ServiceInstance DBEntry with the dependency manager. 155 : */ 156 : virtual DBEntry *Add(const DBRequest *request); 157 : virtual bool Delete(DBEntry *entry, const DBRequest *request); 158 : virtual bool OnChange(DBEntry *entry, const DBRequest *request); 159 : 160 : /* 161 : * Walk the IFMap graph and calculate the properties for this node. 162 : */ 163 : void CalculateProperties(DBGraph *graph, IFMapNode *node, 164 : ServiceInstance::Properties *properties); 165 : 166 : /* 167 : * IFNodeToReq 168 : * 169 : * Convert the ifmap node to a (key,data) pair stored in the database. 170 : */ 171 : virtual bool IFNodeToReq(IFMapNode *node, DBRequest &req, 172 : const boost::uuids::uuid &u); 173 : virtual bool IFNodeToUuid(IFMapNode *node, boost::uuids::uuid &id); 174 : 175 : virtual AgentSandeshPtr GetAgentSandesh(const AgentSandeshArguments *args, 176 : const std::string &context); 177 : static DBTableBase *CreateTable(DB *db, const std::string &name); 178 : 179 : private: 180 : /* 181 : * Invoked by dependency manager whenever a node in the graph 182 : * changes in a way that it affects the service instance 183 : * configuration. The dependency tracking policy is configured in 184 : * the dependency manager directly. 185 : */ 186 : void ChangeEventHandler(IFMapNode *node, DBEntry *entry); 187 : bool HandleAddChange(ServiceInstance **svc_instance, const DBRequest *key); 188 : 189 : DBGraph *graph_; 190 : IFMapDependencyManager *dependency_manager_; 191 : 192 : DISALLOW_COPY_AND_ASSIGN(ServiceInstanceTable); 193 : }; 194 : 195 : #endif // VNSW_AGENT_OPER_SERVICE_INSTANCE_H__