aboutsummaryrefslogtreecommitdiffstats
path: root/ctrl/sysrepo-plugins/hicn-light/plugin/model/hicn_model.c
blob: 7cfb8363aa3241a2389c690f1a15d9ec1edbedcb (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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
 * Copyright (c) 2019 Cisco 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 <stdio.h>
#include <malloc.h>
#include <sysrepo/xpath.h>

/* Hicn headers */

#include "hicn_model.h"
#include "tlock.h"
#include "../hicn_light.h"
#include "../hicn_light_comm.h"



/**
 * @brief API to add hicn face ip in hicn-light.
 */
static int hicn_face_ip_add_cb(const char *xpath, const sr_val_t *input,
                               const size_t input_cnt, sr_val_t **output,
                               size_t *output_cnt, void *private_ctx) {

  SRP_LOG_DBG_MSG("hicn face ip add received successfully");

  hc_face_t face;

  if(strcmp(input[0].data.string_val,"-1")){

  struct sockaddr_in sa;
  // store this IP address in sa:
  inet_pton(AF_INET,  input[0].data.string_val, &(sa.sin_addr));
  face.face.hicn.family=AF_INET;
  face.face.hicn.local_addr.v4.as_inaddr=sa.sin_addr;


  }else if(strcmp(input[1].data.string_val,"-1")){

    struct in6_addr *dst = malloc(sizeof(struct in6_addr));
    inet_pton(AF_INET6, input[1].data.string_val, dst);
    face.face.hicn.family=AF_INET6;
    face.face.hicn.local_addr.v6.as_in6addr = *dst;

  }else{
      SRP_LOG_DBG_MSG("Invalid local IP address");
      return SR_ERR_OPERATION_FAILED;
  }

  if(strcmp(input[2].data.string_val,"-1")){

  struct sockaddr_in sa;
  // store this IP address in sa:
  inet_pton(AF_INET,  input[2].data.string_val, &(sa.sin_addr));
  face.face.hicn.family=AF_INET;
  face.face.hicn.remote_addr.v4.as_inaddr=sa.sin_addr;


  }else if(strcmp(input[3].data.string_val,"-1")){

    struct in6_addr *dst = malloc(sizeof(struct in6_addr));
    inet_pton(AF_INET6, input[3].data.string_val, dst);
    face.face.hicn.family=AF_INET6;
    face.face.hicn.remote_addr.v6.as_in6addr = *dst;

  }else{
      SRP_LOG_DBG_MSG("Invalid local IP address");
      return SR_ERR_OPERATION_FAILED;
  }


  // strncpy(face.face.hicn.netdevice.name,"ens39"); // Can we work only with Idx number ?
  face.face.hicn.netdevice.index = input[4].data.uint32_val;  // This is the idx number of interface


  face.id=0;//can be empty
  face.face.tags=0;//can be empty
  strcpy(face.name,"hicn_face");
  face.face.type=1;

  int rc;
  rc = hc_face_create(hsocket, &face);
  if (rc > 0) {
       SRP_LOG_DBG_MSG("Face added successfully");
       return SR_ERR_OK;
  }

  SRP_LOG_DBG_MSG("Operation Failed");
  return SR_ERR_OPERATION_FAILED;
}

/**
 * @brief API to del hicn face ip in vpp.
 */
static int hicn_face_ip_del_cb(const char *xpath, const sr_val_t *input,
                               const size_t input_cnt, sr_val_t **output,
                               size_t *output_cnt, void *private_ctx) {

  SRP_LOG_DBG_MSG("hicn face ip del received successfully");
  face_t * face=NULL;

  // msg->payload.faceid = input[0].data.uint16_val;
  //lookup(face);
  face_free(face);

  // if(!resp->payload.retval){
  //     SRP_LOG_DBG_MSG("Successfully Done");
  //     return SR_ERR_OK;
  // }

  SRP_LOG_DBG_MSG("Operation Failed");
  return SR_ERR_OPERATION_FAILED;

}


/**
 * @brief API to del hicn face ip in vpp.
 */
static int hicn_route_add_cb(const char *xpath, const sr_val_t *input,
                               const size_t input_cnt, sr_val_t **output,
                               size_t *output_cnt, void *private_ctx) {

/*

  SRP_LOG_DBG_MSG("hicn route add received successfully");

  hc_route_t * route;

  if(strcmp(input[0].data.string_val,"-1")){

  struct sockaddr_in sa;
  // store this IP address in sa:
  inet_pton(AF_INET,  input[0].data.string_val, &(sa.sin_addr));
  route.family=AF_INET;
  route.face.hicn.local_addr.v4.as_inaddr=sa.sin_addr;


  }else if(strcmp(input[1].data.string_val,"-1")){

    struct in6_addr *dst = malloc(sizeof(struct in6_addr));
    inet_pton(AF_INET6, input[1].data.string_val, dst);
    face.face.hicn.family=AF_INET6;
    face.face.hicn.local_addr.v6.as_in6addr = *dst;

  }else{
      SRP_LOG_DBG_MSG("Invalid local IP address");
      return SR_ERR_OPERATION_FAILED;
  }


hc_route_create(hsocket, route);
*/
 return SR_ERR_OK;
}



int hicn_subscribe_events(sr_session_ctx_t *session,
                          sr_subscription_ctx_t **subscription) {


  int rc;
    rc = sr_rpc_subscribe(session, "/hicn:face-ip-add", hicn_face_ip_add_cb,
    session, SR_SUBSCR_CTX_REUSE, subscription);
     if (rc != SR_ERR_OK) {
      SRP_LOG_DBG_MSG("Problem in subscription stat-get\n");
      goto error;
    }


    rc = sr_rpc_subscribe(session, "/hicn:face-ip-del", hicn_face_ip_del_cb,
    session, SR_SUBSCR_CTX_REUSE, subscription);
    if (rc != SR_ERR_OK) {
      SRP_LOG_DBG_MSG("Problem in subscription face-ip-del\n");
      goto error;
    }


    rc = sr_rpc_subscribe(session, "/hicn:route-nhops-add",
    hicn_route_add_cb, session, SR_SUBSCR_CTX_REUSE, subscription);
     if (rc!= SR_ERR_OK) {
      SRP_LOG_DBG_MSG("Problem in subscription route-nhops-add\n");
      goto error;
    }


  SRP_LOG_INF_MSG("hicn light initialized successfully.");
  return SR_ERR_OK;

error:
  SRP_LOG_ERR_MSG("Error by initialization of the hicn plugin.");
  sr_plugin_cleanup_cb(session, hsocket);
  return rc;

}