Skip to content

Commit 51b46f0

Browse files
authored
Merge pull request #107 from polycube-network/pr/port_simple_services_to_base_class
services: port simple services to new code generation schema
2 parents 33feac8 + 95e9129 commit 51b46f0

83 files changed

Lines changed: 1170 additions & 1260 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/services/pcn-helloworld/src/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ include(${PROJECT_SOURCE_DIR}/cmake/LoadFileAsVariable.cmake)
22

33
aux_source_directory(serializer SERIALIZER_SOURCES)
44
aux_source_directory(api API_SOURCES)
5-
aux_source_directory(default-src SRC_SOURCES)
5+
aux_source_directory(base BASE_SOURCES)
66

77
include_directories(serializer)
88
include_directories(interface)
@@ -14,7 +14,7 @@ include_directories(${CMAKE_CURRENT_BINARY_DIR})
1414
add_library(pcn-helloworld SHARED
1515
${SERIALIZER_SOURCES}
1616
${API_SOURCES}
17-
${SRC_SOURCES}
17+
${BASE_SOURCES}
1818
Helloworld.cpp
1919
Ports.cpp
2020
Helloworld-lib.cpp)

src/services/pcn-helloworld/src/Helloworld.cpp

Lines changed: 5 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,10 @@
2525
using namespace polycube::service;
2626

2727
Helloworld::Helloworld(const std::string name, const HelloworldJsonObject &conf)
28-
: Cube(conf.getBase(), {helloworld_code_ingress}, {}) {
29-
logger()->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [helloworld] [%n] [%l] %v");
30-
logger()->info("Creating helloworld instance");
31-
32-
// TODO: action should have a default value, so the actionIsSet control could
33-
// be ommitted.
34-
if (conf.actionIsSet()) {
35-
setAction(conf.getAction());
36-
} else {
37-
setAction(HelloworldActionEnum::DROP);
38-
}
28+
: Cube(conf.getBase(), {helloworld_code_ingress}, {}),
29+
HelloworldBase(name) {
30+
logger()->info("Creating Helloworld instance");
31+
setAction(conf.getAction());
3932

4033
// set an initial state before doing any change to the configuration
4134
// UINT16_MAX means that the port is not connected
@@ -47,41 +40,7 @@ Helloworld::Helloworld(const std::string name, const HelloworldJsonObject &conf)
4740
}
4841

4942
Helloworld::~Helloworld() {
50-
logger()->info("destroying helloworld instance");
51-
}
52-
53-
void Helloworld::update(const HelloworldJsonObject &conf) {
54-
// This method updates all the object/parameter in Helloworld object specified
55-
// in the conf JsonObject.
56-
// You can modify this implementation.
57-
58-
// update base cube implementation
59-
Cube::set_conf(conf.getBase());
60-
61-
if (conf.actionIsSet()) {
62-
setAction(conf.getAction());
63-
}
64-
65-
if (conf.portsIsSet()) {
66-
for (auto &i : conf.getPorts()) {
67-
auto name = i.getName();
68-
auto m = getPorts(name);
69-
m->update(i);
70-
}
71-
}
72-
}
73-
74-
HelloworldJsonObject Helloworld::toJsonObject() {
75-
HelloworldJsonObject conf;
76-
conf.setBase(Cube::to_json());
77-
78-
conf.setAction(getAction());
79-
80-
for (auto &i : getPortsList()) {
81-
conf.addPorts(i->toJsonObject());
82-
}
83-
84-
return conf;
43+
logger()->info("Destroying Helloworld instance");
8544
}
8645

8746
void Helloworld::packet_in(Ports &port, polycube::service::PacketInMetadata &md,
@@ -99,14 +58,6 @@ void Helloworld::setAction(const HelloworldActionEnum &value) {
9958
get_array_table<uint8_t>("action_map").set(0x0, action);
10059
}
10160

102-
std::shared_ptr<Ports> Helloworld::getPorts(const std::string &name) {
103-
return get_port(name);
104-
}
105-
106-
std::vector<std::shared_ptr<Ports>> Helloworld::getPortsList() {
107-
return get_ports();
108-
}
109-
11061
void Helloworld::addPorts(const std::string &name,
11162
const PortsJsonObject &conf) {
11263
if (get_ports().size() == 2) {
@@ -135,20 +86,6 @@ void Helloworld::addPorts(const std::string &name,
13586
ports_table.set(port_map_index, p->index());
13687
}
13788

138-
void Helloworld::addPortsList(const std::vector<PortsJsonObject> &conf) {
139-
for (auto &i : conf) {
140-
std::string name_ = i.getName();
141-
addPorts(name_, i);
142-
}
143-
}
144-
145-
void Helloworld::replacePorts(const std::string &name,
146-
const PortsJsonObject &conf) {
147-
delPorts(name);
148-
std::string name_ = conf.getName();
149-
addPorts(name_, conf);
150-
}
151-
15289
void Helloworld::delPorts(const std::string &name) {
15390
int index = get_port(name)->index();
15491

@@ -165,10 +102,3 @@ void Helloworld::delPorts(const std::string &name) {
165102
remove_port(name);
166103
logger()->info("port {0} was removed", name);
167104
}
168-
169-
void Helloworld::delPortsList() {
170-
auto ports = get_ports();
171-
for (auto it : ports) {
172-
delPorts(it->name());
173-
}
174-
}

src/services/pcn-helloworld/src/Helloworld.h

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,47 +16,29 @@
1616

1717
#pragma once
1818

19-
#include "../interface/HelloworldInterface.h"
20-
21-
#include "polycube/services/cube.h"
22-
#include "polycube/services/port.h"
23-
#include "polycube/services/utils.h"
24-
25-
#include <spdlog/spdlog.h>
19+
#include "../base/HelloworldBase.h"
2620

2721
#include "Ports.h"
2822

29-
using namespace io::swagger::server::model;
30-
31-
class Helloworld : public polycube::service::Cube<Ports>,
32-
public HelloworldInterface {
33-
friend class Ports;
23+
using namespace polycube::service::model;
3424

25+
class Helloworld : public HelloworldBase {
3526
public:
3627
Helloworld(const std::string name, const HelloworldJsonObject &conf);
3728
virtual ~Helloworld();
29+
3830
void packet_in(Ports &port, polycube::service::PacketInMetadata &md,
3931
const std::vector<uint8_t> &packet) override;
4032

41-
void update(const HelloworldJsonObject &conf) override;
42-
HelloworldJsonObject toJsonObject() override;
43-
44-
/// <summary>
45-
/// Action performed on the received packet (i.e., DROP, SLOWPATH, or FORWARD;
46-
/// default: DROP)
47-
/// </summary>
48-
HelloworldActionEnum getAction() override;
49-
void setAction(const HelloworldActionEnum &value) override;
50-
5133
/// <summary>
5234
/// Entry of the ports table
5335
/// </summary>
54-
std::shared_ptr<Ports> getPorts(const std::string &name) override;
55-
std::vector<std::shared_ptr<Ports>> getPortsList() override;
5636
void addPorts(const std::string &name, const PortsJsonObject &conf) override;
57-
void addPortsList(const std::vector<PortsJsonObject> &conf) override;
58-
void replacePorts(const std::string &name,
59-
const PortsJsonObject &conf) override;
6037
void delPorts(const std::string &name) override;
61-
void delPortsList() override;
38+
39+
/// <summary>
40+
/// Action performed on the received packet (i.e., DROP, SLOWPATH, or FORWARD; default: DROP)
41+
/// </summary>
42+
HelloworldActionEnum getAction() override;
43+
void setAction(const HelloworldActionEnum &value) override;
6244
};

src/services/pcn-helloworld/src/Ports.cpp

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
* limitations under the License.
1515
*/
1616

17-
// Modify these methods with your own implementation
18-
1917
#include "Ports.h"
2018
#include "Helloworld.h"
2119

@@ -24,26 +22,6 @@ using namespace polycube::service;
2422
Ports::Ports(polycube::service::Cube<Ports> &parent,
2523
std::shared_ptr<polycube::service::PortIface> port,
2624
const PortsJsonObject &conf)
27-
: Port(port), parent_(static_cast<Helloworld &>(parent)) {
28-
logger()->info("Creating Ports instance");
29-
}
25+
: PortsBase(parent, port) {}
3026

3127
Ports::~Ports() {}
32-
33-
void Ports::update(const PortsJsonObject &conf) {
34-
Port::set_conf(conf.getBase());
35-
// This method updates all the object/parameter in Ports object specified in
36-
// the conf JsonObject.
37-
// You can modify this implementation.
38-
}
39-
40-
PortsJsonObject Ports::toJsonObject() {
41-
PortsJsonObject conf;
42-
conf.setBase(Port::to_json());
43-
44-
return conf;
45-
}
46-
47-
std::shared_ptr<spdlog::logger> Ports::logger() {
48-
return parent_.logger();
49-
}

src/services/pcn-helloworld/src/Ports.h

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,19 @@
1414
* limitations under the License.
1515
*/
1616

17-
#pragma once
18-
19-
#include "../interface/PortsInterface.h"
20-
21-
#include "polycube/services/cube.h"
22-
#include "polycube/services/port.h"
23-
#include "polycube/services/utils.h"
2417

25-
#include <spdlog/spdlog.h>
18+
#pragma once
2619

27-
#include <memory>
20+
#include "../base/PortsBase.h"
2821

2922
class Helloworld;
30-
// class PortIface;
3123

32-
using namespace io::swagger::server::model;
24+
using namespace polycube::service::model;
3325

34-
class Ports : public polycube::service::Port, public PortsInterface {
26+
class Ports : public PortsBase {
3527
public:
3628
Ports(polycube::service::Cube<Ports> &parent,
37-
std::shared_ptr<polycube::service::PortIface> port,
38-
const PortsJsonObject &conf);
29+
std::shared_ptr<polycube::service::PortIface> port,
30+
const PortsJsonObject &conf);
3931
virtual ~Ports();
40-
41-
std::shared_ptr<spdlog::logger> logger();
42-
void update(const PortsJsonObject &conf) override;
43-
PortsJsonObject toJsonObject() override;
44-
45-
private:
46-
Helloworld &parent_;
4732
};

src/services/pcn-helloworld/src/api/HelloworldApi.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
/**
2-
* helloworld API
32
* helloworld API generated from helloworld.yang
43
*
5-
* OpenAPI spec version: 1.0.0
6-
*
7-
* NOTE: This class is auto generated by the swagger code generator program.
8-
* https://github.com/polycube-network/swagger-codegen.git
9-
* branch polycube
4+
* NOTE: This file is auto generated by polycube-codegen
5+
* https://github.com/polycube-network/polycube-codegen
106
*/
117

128

@@ -16,8 +12,8 @@
1612
#include "HelloworldApi.h"
1713
#include "HelloworldApiImpl.h"
1814

19-
using namespace io::swagger::server::model;
20-
using namespace io::swagger::server::api::HelloworldApiImpl;
15+
using namespace polycube::service::model;
16+
using namespace polycube::service::api::HelloworldApiImpl;
2117

2218
#ifdef __cplusplus
2319
extern "C" {

src/services/pcn-helloworld/src/api/HelloworldApi.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
/**
2-
* helloworld API
32
* helloworld API generated from helloworld.yang
43
*
5-
* OpenAPI spec version: 1.0.0
6-
*
7-
* NOTE: This class is auto generated by the swagger code generator program.
8-
* https://github.com/polycube-network/swagger-codegen.git
9-
* branch polycube
4+
* NOTE: This file is auto generated by polycube-codegen
5+
* https://github.com/polycube-network/polycube-codegen
106
*/
117

128

src/services/pcn-helloworld/src/api/HelloworldApiImpl.cpp

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
/**
2-
* helloworld API
32
* helloworld API generated from helloworld.yang
43
*
5-
* OpenAPI spec version: 1.0.0
6-
*
7-
* NOTE: This class is auto generated by the swagger code generator program.
8-
* https://github.com/polycube-network/swagger-codegen.git
9-
* branch polycube
4+
* NOTE: This file is auto generated by polycube-codegen
5+
* https://github.com/polycube-network/polycube-codegen
106
*/
117

128

@@ -15,12 +11,11 @@
1511

1612
#include "HelloworldApiImpl.h"
1713

18-
namespace io {
19-
namespace swagger {
20-
namespace server {
14+
namespace polycube {
15+
namespace service {
2116
namespace api {
2217

23-
using namespace io::swagger::server::model;
18+
using namespace polycube::service::model;
2419

2520
namespace HelloworldApiImpl {
2621
namespace {
@@ -380,5 +375,4 @@ std::vector<nlohmann::fifo_map<std::string, std::string>> read_helloworld_ports_
380375
}
381376
}
382377
}
383-
}
384378

src/services/pcn-helloworld/src/api/HelloworldApiImpl.h

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
/**
2-
* helloworld API
32
* helloworld API generated from helloworld.yang
43
*
5-
* OpenAPI spec version: 1.0.0
6-
*
7-
* NOTE: This class is auto generated by the swagger code generator program.
8-
* https://github.com/polycube-network/swagger-codegen.git
9-
* branch polycube
4+
* NOTE: This file is auto generated by polycube-codegen
5+
* https://github.com/polycube-network/polycube-codegen
106
*/
117

128

@@ -30,12 +26,11 @@
3026
#include "PortsJsonObject.h"
3127
#include <vector>
3228

33-
namespace io {
34-
namespace swagger {
35-
namespace server {
29+
namespace polycube {
30+
namespace service {
3631
namespace api {
3732

38-
using namespace io::swagger::server::model;
33+
using namespace polycube::service::model;
3934

4035
namespace HelloworldApiImpl {
4136
void create_helloworld_by_id(const std::string &name, const HelloworldJsonObject &value);
@@ -66,5 +61,4 @@ namespace HelloworldApiImpl {
6661
}
6762
}
6863
}
69-
}
7064

0 commit comments

Comments
 (0)