Merge "Update policy docker v1.1.2" into amsterdam
[demo.git] / vnfs / vCPE / kea-sdnc-notify-mod / src / pkt4_send.cc
1 /*****************************************************************************
2 * @file
3 * main function to process ipv4 packets
4 * -------
5 *
6 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *        http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 ****************************************************************************/
20
21 #include <dhcp/pkt4.h>
22 #include <log/logger.h>
23 #include <log/macros.h>
24 #include <log/message_initializer.h>
25 #include "library_common.h"
26 #include <curl/curl.h>
27 #include <sstream>
28
29 using namespace isc::dhcp;
30 using namespace isc::hooks;
31 using namespace std;
32 using namespace isc::log;
33 namespace pt = boost::property_tree;
34
35
36 isc::log::Logger logger("sdnc-notify-logger");
37 const char* log_messages[] = {
38     "SNL_BASE", "message: %1",
39     "SNL_PKT_SEND", "Outgoing packet: \n%1",
40     "SNL_CURL_RECEIVED", "Received: \n%1",
41     "SNL_CURL_FAILED", "Unable to receive data from %1",
42     NULL
43 };
44
45 /// Initializer for log messages.
46 const MessageInitializer message_initializer(log_messages);
47
48 extern "C" {
49 int pkt4_send(CalloutHandle& handle) {
50
51     // Curl variables 
52     CURL *curl;
53     CURLcode res;
54     struct curl_slist *list=NULL;
55     bool perform_updates = 0;
56     int curl_opt_res = 0;
57
58     // "C" variables.
59     char *bp;
60     size_t size;
61     FILE *response_memfile;
62
63     // String variables
64     string hwaddr;
65     string final_url;
66     string msg_name;
67     string yiaddr;
68     string post_data;
69     
70
71     // Pkt4 variables.
72     Pkt4Ptr response4_ptr;
73     HWAddrPtr hwaddr_ptr;
74     
75     // Boost / json related variables.
76     pt::ptree root;
77     std::stringstream ss;
78     boost::optional<std::string> siaddr_json_field;
79
80     /* Begin Code */
81     handle.getArgument("response4", response4_ptr);
82     hwaddr_ptr = response4_ptr->getHWAddr();
83     isc::asiolink::IOAddress new_yiaddr(response4_ptr->getYiaddr());
84     hwaddr = hwaddr_ptr->toText(false);
85     yiaddr = new_yiaddr.toText();
86     msg_name = response4_ptr->getName();
87     /* POST string for DMaaP */
88     post_data = "{\n\"macaddr\":\"" + hwaddr + "\",\n\"yiaddr\":\"" + yiaddr + "\",\n\"msg_name\":\"" + msg_name + "\"\n}";
89     final_url = json_params[0] ;
90     LOG_DEBUG(logger, 0, "SNL_BASE").arg(final_url);
91     LOG_DEBUG(logger, 0, "SNL_BASE").arg(yiaddr);
92     LOG_DEBUG(logger, 0, "SNL_BASE").arg(post_data);
93
94
95     if ( msg_name == "DHCPACK")
96         perform_updates = 1;
97
98     if (!perform_updates) {
99         LOG_WARN(logger, "SNL_BASE").arg("Nothing to update.");
100         return(0);
101     }
102
103     curl_global_init(CURL_GLOBAL_DEFAULT);
104     curl = curl_easy_init();
105     if(!curl) {
106         curl_global_cleanup();
107         LOG_ERROR(logger, "SNL_BASE").arg("Could not initialize curl");
108         return(1);
109     }
110
111     list = curl_slist_append(list, "Content-type: application/json");
112     list = curl_slist_append(list, "Accept: application/json");
113     if (list == NULL) {
114         curl_easy_cleanup(curl);
115         curl_global_cleanup();
116         LOG_ERROR(logger, "SNL_BASE").arg("Could not create curl slist.");
117         return(1);
118     }
119
120     response_memfile = open_memstream (&bp, &size);
121     if (response_memfile == NULL) {
122         curl_easy_cleanup(curl);
123         curl_global_cleanup();
124         LOG_ERROR(logger, "SNL_BASE").arg("Could not create memfile.");
125         return(1);
126     }
127         
128     // DEBUGGING
129     curl_opt_res += curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
130     curl_opt_res += curl_easy_setopt(curl, CURLOPT_HEADER, 1L);
131
132     // Default curl timeout is 300 seconds 
133     curl_opt_res += curl_easy_setopt(curl, CURLOPT_TIMEOUT, 1L);
134     curl_opt_res += curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 1L);
135     curl_opt_res += curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)response_memfile);
136     curl_opt_res += curl_easy_setopt(curl, CURLOPT_URL, (final_url).c_str());
137     curl_opt_res += curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, -1L );
138     curl_opt_res += curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_data.c_str() );
139     curl_opt_res += curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
140
141     #ifdef SKIP_PEER_VERIFICATION
142     curl_opt_res += curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
143     #endif
144
145     #ifdef SKIP_HOSTNAME_VERIFICATION
146     curl_opt_res += curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
147     #endif
148     if (curl_opt_res > 0) {
149       fclose(response_memfile);
150       free(bp);
151       curl_easy_cleanup(curl);
152       curl_global_cleanup();
153       LOG_ERROR(logger, "SNL_BASE").arg("Error setting curl options.");
154       return(1);
155     }
156     res = curl_easy_perform(curl);
157     /* Check for errors */
158     if(res != CURLE_OK) {
159         fclose(response_memfile);
160         free(bp);
161         curl_easy_cleanup(curl);
162         curl_global_cleanup();
163         LOG_ERROR(logger, "SNL_CURL_FAILED").arg(ss.str());
164         return(1);
165     }
166     // make bp available for reading.
167     fclose(response_memfile);
168     curl_easy_cleanup(curl);
169     curl_global_cleanup();
170     ss << bp;
171     free(bp);
172     LOG_DEBUG(logger, 0, "SNL_CURL_RECEIVED").arg(ss.str());
173     // Load the json file in this ptree
174
175
176     LOG_DEBUG(logger, 0, "SNL_PKT_SEND").arg(response4_ptr->toText());
177
178     return(0);
179 }
180 }