aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/sc_plugins.c
blob: 66790a1832640f7ab674c7857e6327bcbaabdc5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
 * Copyright (c) 2018 HUACHENTEL and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <vapi/vapi.h>
#include <vapi/vpe.api.vapi.h>
#include <vapi/interface.api.vapi.h>
#include <vapi/l2.api.vapi.h>
#include <vapi/ip.api.vapi.h>
#include <vapi/tapv2.api.vapi.h>
#include <vapi/ipsec.api.vapi.h>
#include <vapi/vxlan.api.vapi.h>
#include <vnet/interface.h>
#include <vnet/mpls/mpls_types.h>
DEFINE_VAPI_MSG_IDS_VPE_API_JSON;
DEFINE_VAPI_MSG_IDS_INTERFACE_API_JSON;
DEFINE_VAPI_MSG_IDS_L2_API_JSON;
DEFINE_VAPI_MSG_IDS_IP_API_JSON;
DEFINE_VAPI_MSG_IDS_TAPV2_API_JSON;
DEFINE_VAPI_MSG_IDS_IPSEC_API_JSON;
DEFINE_VAPI_MSG_IDS_VXLAN_API_JSON;


#include "sc_plugins.h"
#include "ietf/ietf_interface.h"
#include "openconfig/openconfig_plugin.h"

int sr_plugin_init_cb(sr_session_ctx_t *session, void **private_ctx)
{
  SC_INVOKE_BEGIN;
  sr_subscription_ctx_t *subscription = NULL;
  int rc = SR_ERR_OK;
  rc = sc_connect_vpp();
  if (0 != rc)
    {
      SC_LOG_ERR("vpp connect error , with return %d.", rc);
      return SR_ERR_INTERNAL;
    }

  //SC_REGISTER_RPC_EVT_HANDLER(sc_ip_subscribe_route_events);
  //SC_REGISTER_RPC_EVT_HANDLER(sc_vxlan_subscribe_tunnel_events);
  //SC_REGISTER_RPC_EVT_HANDLER(sc_l2_bridge_domain_add_del_subscribe_events);
  //SC_REGISTER_RPC_EVT_HANDLER(sc_l2_interface_set_l2_bridge_subscribe_events);


  //INTERFACE
  ietf_interface_subscribe_events(session, &subscription);

  //Openconfig modules
  openconfig_plugin_init(session);

  /* set subscription as our private context */
  *private_ctx = subscription;
  SC_INVOKE_END;
  return SR_ERR_OK;
}

void sr_plugin_cleanup_cb(sr_session_ctx_t *session, void *private_ctx)
{
  SC_INVOKE_BEGIN;

  openconfig_plugin_cleanup();

  /* subscription was set as our private context */
  sr_unsubscribe(session, private_ctx);
  SC_LOG_DBG_MSG("unload plugin ok.");
  sc_disconnect_vpp();
  SC_LOG_DBG_MSG("plugin disconnect vpp ok.");
  SC_INVOKE_END;
}


///#############################
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <inttypes.h>
sr_subscription_ctx_t *subscription = NULL;
volatile int exit_application = 0;

static void
sigint_handler(int signum)
{
  exit_application = 1;
}
int 
subscribe_all_module_events(sr_session_ctx_t *session)
{
  sr_plugin_init_cb(session, (void**)&subscription);
  return 0;
}
int
main(int argc, char **argv)
{
  sr_conn_ctx_t *connection = NULL;
  sr_session_ctx_t *session = NULL;
  int rc = SR_ERR_OK;

  /* connect to vpp */
  rc = sc_connect_vpp();
  if (-1 == rc){
    fprintf(stderr, "vpp connect error");
    return -1;
  }
    
  /* connect to sysrepo */
  rc = sr_connect("cpe_application", SR_CONN_DEFAULT, &connection);
  if (SR_ERR_OK != rc) {
    fprintf(stderr, "Error by sr_connect: %s\n", sr_strerror(rc));
    goto cleanup;
  }

  /* start session */
  rc = sr_session_start(connection, SR_DS_STARTUP, SR_SESS_DEFAULT, &session);
  if (SR_ERR_OK != rc) {
    fprintf(stderr, "Error by sr_session_start: %s\n", sr_strerror(rc));
    goto cleanup;
  }

  /* subscribe all module events */
  rc = subscribe_all_module_events(session);
  if (SR_ERR_OK != rc) {
    fprintf(stderr, "Error by subscribe module events: %s\n", sr_strerror(rc));
    goto cleanup;
  }
    
  /* loop until ctrl-c is pressed / SIGINT is received */
  signal(SIGINT, sigint_handler);
  signal(SIGPIPE, SIG_IGN);

  while (!exit_application) {
    sleep(2);
  }

  printf("Application exit requested, exiting.\n");

 cleanup:
  if (NULL != subscription) {
    sr_unsubscribe(session, subscription);
  }
  if (NULL != session) {
    sr_session_stop(session);
  }
  if (NULL != connection) {
    sr_disconnect(connection);
  }
  sc_disconnect_vpp();
  return rc;
}