softwarecontainer  0.18.0-739e8d7 2017-05-04
gateway.h
1 /*
2  * Copyright (C) 2016-2017 Pelagicore AB
3  *
4  * Permission to use, copy, modify, and/or distribute this software for
5  * any purpose with or without fee is hereby granted, provided that the
6  * above copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
9  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
10  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
11  * BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
12  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
13  * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
14  * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
15  * SOFTWARE.
16  *
17  * For further information see LICENSE
18  */
19 
20 #pragma once
21 
22 #include <string>
23 #include <exception>
24 
25 #include "containerabstractinterface.h"
26 #include "jsonparser.h"
27 #include "softwarecontainererror.h"
28 
29 namespace softwarecontainer {
30 
32 {
33 public:
34  GatewayError():
35  SoftwareContainerError("Gateway error.")
36  {
37  }
38 
39  GatewayError(const std::string &message):
40  SoftwareContainerError(message)
41  {
42  }
43 };
44 
61 class Gateway
62 {
63  LOG_DECLARE_CLASS_CONTEXT("GATE", "Gateway");
64 
65 public:
66  enum class GatewayState : unsigned int {
67  CREATED = 0,
68  CONFIGURED = 1,
69  ACTIVATED = 2,
70  };
71 
86  Gateway(const std::string &id,
87  std::shared_ptr<ContainerAbstractInterface> container,
88  bool isDynamic = false);
89 
90  virtual ~Gateway() {}
91 
97  virtual std::string id() const;
98 
110  virtual bool setConfig(const json_t *config);
111 
122  virtual bool activate();
123 
133  virtual bool teardown();
134 
138  virtual bool isConfigured();
139 
146  virtual bool isActivated();
147 
148 protected:
158  virtual bool readConfigElement(const json_t *element) = 0;
159 
165  std::shared_ptr<ContainerAbstractInterface> getContainer();
166 
170  bool setEnvironmentVariable(const std::string &variable, const std::string &value);
171 
172  virtual bool activateGateway() = 0;
173  virtual bool teardownGateway() = 0;
174 
175  /*
176  * Dynamic gateways must set this to true the first time they are activated.
177  * This is used by this class to keep track of this, but it should also be expected
178  * that inheriting gateway implementations to use this member to keep
179  * track of this state as well.
180  */
181  bool m_activatedOnce;
182 
183 private:
184  std::string m_id;
185  std::shared_ptr<ContainerAbstractInterface> m_container;
186 
187  // Dynamic gateways must set this on initialization to enable dynamic behavior
188  bool m_isDynamic;
189 
190  GatewayState m_state;
191 
192 };
193 
194 } // namespace softwarecontainer
Gateway base class for SoftwareContainer.
Definition: gateway.h:61
Developers guide to adding a config item: