Fledge
An open source edge computing platform for industrial users
north_service.h
1 #ifndef _NORTH_SERVICE_H
2 #define _NORTH_SERVICE_H
3 /*
4  * Fledge north service.
5  *
6  * Copyright (c) 2020 Dianomic Systems
7  *
8  * Released under the Apache 2.0 Licence
9  *
10  * Author: Mark Riddoch
11  */
12 
13 #include <logger.h>
14 #include <north_plugin.h>
15 #include <service_handler.h>
16 #include <storage_client.h>
17 #include <config_category.h>
18 #include <filter_plugin.h>
19 #include <mutex>
20 #include <condition_variable>
21 #include <audit_logger.h>
22 #include <perfmonitors.h>
23 #include <data_load.h>
24 #include <data_sender.h>
25 
26 #define SERVICE_NAME "Fledge North"
27 
31 #define DEBUG_ATTACHED 0x01
32 #define DEBUG_SUSPENDED 0x02
33 #define DEBUG_ISOLATED 0x04
34 
36 
43  public:
44  NorthService(const std::string& name,
45  const std::string& token = "");
46  virtual ~NorthService();
47  void start(std::string& coreAddress,
48  unsigned short corePort);
49  void stop();
50  void shutdown();
51  void restart();
52  void configChange(const std::string&, const std::string&);
53  void configChildCreate(const std::string& , const std::string&, const std::string&){};
54  void configChildDelete(const std::string& , const std::string&){};
55  bool isRunning() { return !m_shutdown; };
56  const std::string& getName() { return m_name; };
57  const std::string& getPluginName() { return m_pluginName; };
58  void pause();
59  void release();
60  bool write(const std::string& name, const std::string& value, const ControlDestination);
61  bool write(const std::string& name, const std::string& value, const ControlDestination, const std::string& arg);
62  int operation(const std::string& name, int paramCount, char *names[], char *parameters[], const ControlDestination);
63  int operation(const std::string& name, int paramCount, char *names[], char *parameters[], const ControlDestination, const std::string& arg);
64  void setDryRun() { m_dryRun = true; };
65  bool getDryRun() { return m_dryRun; };
66  void alertFailures();
67  void clearFailures();
68  // Debugger Entry point
69  bool attachDebugger()
70  {
71  if (m_dataLoad)
72  {
73  m_debugState = DEBUG_ATTACHED;
74  return m_dataLoad->attachDebugger();
75  }
76  return false;
77  };
78  void detachDebugger()
79  {
80  if (m_dataLoad)
81  m_dataLoad->detachDebugger();
82  suspendDebugger(false);
83  isolateDebugger(false);
84  m_debugState = 0;
85  };
86  void setDebuggerBuffer(unsigned int size)
87  {
88  if (m_dataLoad)
89  m_dataLoad->setDebuggerBuffer(size);
90  };
91  std::string getDebuggerBuffer()
92  {
93  if (m_dataLoad)
94  return m_dataLoad->getDebuggerBuffer();
95  return "";
96  };
97  void suspendDebugger(bool suspend)
98  {
99  if (m_dataLoad)
100  {
101  m_dataLoad->suspendIngest(suspend);
102  if (suspend)
103  m_debugState |= DEBUG_SUSPENDED;
104  else
105  m_debugState &= ~(unsigned int)DEBUG_SUSPENDED;
106  }
107  };
108  void isolateDebugger(bool isolate)
109  {
110  if (m_dataLoad)
111  {
112  m_dataLoad->isolate(isolate);
113  if (isolate)
114  m_debugState |= DEBUG_ISOLATED;
115  else
116  m_debugState &= ~(unsigned int)DEBUG_ISOLATED;
117  }
118  };
119  void stepDebugger(unsigned int steps)
120  {
121  if (m_dataLoad)
122  m_dataLoad->stepDebugger(steps);
123  }
124  void replayDebugger()
125  {
126  if (m_dataLoad)
127  m_dataLoad->replayDebugger();
128  };
129  std::string debugState();
130  bool debuggerAttached()
131  {
132  return m_debugState & DEBUG_ATTACHED;
133  }
134  bool allowDebugger()
135  {
136  return m_allowDebugger;
137  }
138 
139  private:
140  void addConfigDefaults(DefaultConfigCategory& defaults);
141  bool loadPlugin();
142  void createConfigCategories(DefaultConfigCategory configCategory, std::string parent_name,std::string current_name);
143  void restartPlugin();
144  void updateFeatures(const ConfigCategory& category);
145  private:
146  std::string controlSource();
147  bool sendToService(const std::string& southService, const std::string& name, const std::string& value);
148  bool sendToDispatcher(const std::string& path, const std::string& payload);
149  DataLoad *m_dataLoad;
150  DataSender *m_dataSender;
151  NorthPlugin *northPlugin;
152  std::string m_pluginName;
153  Logger *logger;
154  AssetTracker *m_assetTracker;
155  volatile bool m_shutdown;
156  ConfigCategory m_config;
157  ConfigCategory m_configAdvanced;
158  StorageClient *m_storage;
159  std::mutex m_mutex;
160  std::condition_variable m_cv;
161  PluginData *m_pluginData;
162  bool m_restartPlugin;
163  const std::string m_token;
164  bool m_allowControl;
165  bool m_dryRun;
166  bool m_requestRestart;
167  AuditLogger *m_auditLogger;
168  PerformanceMonitor *m_perfMonitor;
169  unsigned int m_debugState;
170  NorthServiceProvider *m_provider;
171  bool m_allowDebugger;
172 };
173 
174 
180  public:
181  NorthServiceProvider(NorthService *north) : m_north(north) {};
182  virtual ~NorthServiceProvider() {};
183  void asJSON(std::string &json) const
184  {
185  if (m_north)
186  {
187  json = "\"debug\" : " + m_north->debugState();
188  }
189  };
190  private:
191  NorthService *m_north;
192 };
193 #endif
DefaultConfigCategory
DefaultConfigCategory.
Definition: config_category.h:241
NorthService::clearFailures
void clearFailures()
Clear the failure alert for sending data.
Definition: north.cpp:1370
DataSender
Definition: data_sender.h:25
JSONProvider
Definition: json_provider.h:14
NorthService::stop
void stop()
Stop the storage service/.
Definition: north.cpp:625
NorthService::start
void start(std::string &coreAddress, unsigned short corePort)
Start the north service.
Definition: north.cpp:352
NorthPlugin
Class that represents a north plugin.
Definition: north_plugin.h:33
NorthService::debugState
std::string debugState()
Return the state of the pipeline debugger.
Definition: north.cpp:1382
NorthService::restart
void restart()
Restart request.
Definition: north.cpp:794
NorthService::shutdown
void shutdown()
Shutdown request.
Definition: north.cpp:779
NorthService::alertFailures
void alertFailures()
Raise an alert that we are having issues sending data.
Definition: north.cpp:1356
DataLoad
A class used in the North service to load data from the buffer.
Definition: data_load.h:24
ConfigCategory
Definition: config_category.h:56
NorthService::configChange
void configChange(const std::string &, const std::string &)
Configuration change notification.
Definition: north.cpp:811
NorthService::~NorthService
virtual ~NorthService()
Destructor for the north service.
Definition: north.cpp:324
StorageClient
Client for accessing the storage service.
Definition: storage_client.h:43
NorthService::NorthService
NorthService(const std::string &name, const std::string &token="")
Constructor for the north service.
Definition: north.cpp:297
NorthServiceProvider
A data provider class to return data in the north service ping response.
Definition: north_service.h:179
PluginData
Definition: plugin_data.h:15
Logger
Fledge Logger class used to log to syslog.
Definition: logger.h:42
AssetTracker
The AssetTracker class provides the asset tracking functionality.
Definition: asset_tracking.h:239
NorthService
The NorthService class.
Definition: north_service.h:42
ServiceAuthHandler
ServiceAuthHandler adds security to the base class ServiceHandler.
Definition: service_handler.h:35
PerformanceMonitor
Class to handle the performance monitors.
Definition: perfmonitors.h:35
AuditLogger
A singleton class for access to the audit logger within services.
Definition: audit_logger.h:21