1 # Copyright (c) 2017-2018 ZTE, Inc.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 from onaplogging.mdcContext import MDC
16 from lcm.pub.config.config import FORWARDED_FOR_FIELDS, SERVICE_NAME
19 class LogContextMiddleware(object):
20 # the last IP behind multiple proxies, if no exist proxies
22 def __init__(self, get_response):
23 self.get_response = get_response
25 def _getLastIp(self, request):
29 for field in FORWARDED_FOR_FIELDS:
30 if field in request.META:
31 if ',' in request.META[field]:
32 parts = request.META[field].split(',')
33 ip = parts[-1].strip().split(":")[0]
35 ip = request.META[field].split(":")[0]
38 ip = request.META.get("HTTP_HOST").split(":")[0]
45 def process_request(self, request):
46 # Fetch TRANSACTIONID Id and pass to plugin server
47 ReqeustID = request.META.get("HTTP_X_ONAP-RequestID", None)
49 ReqeustID = uuid.uuid3(uuid.NAMESPACE_URL, SERVICE_NAME)
50 request.META["HTTP_X_ONAP-RequestID"] = ReqeustID
51 MDC.put("requestID", ReqeustID)
52 # generate the unique id
53 InovocationID = uuid.uuid3(uuid.NAMESPACE_DNS, SERVICE_NAME)
54 MDC.put("invocationID", InovocationID)
55 MDC.put("serviceName", SERVICE_NAME)
57 MDC.put("serviceIP", self._getLastIp(request))
61 def process_response(self, request, response):
65 def __call__(self, request):
66 self.process_request(request)
67 response = self.get_response(request)
68 self.process_response(request, response)