Skip to content

Commit

Permalink
2.4.0 dns based discovery of deployment-handler
Browse files Browse the repository at this point in the history
- policy-handler uses dns based discovery of
   deployment-handler - driven by config
- new data structure for deploy_handler section of config
    --  changed from string "deployment_handler" in 2.3.1
         to structure in 2.4.0
    deploy_handler :
        # name of deployment-handler service
        #     used by policy-handler for logging
        target_entity : "deployment_handler"
        # url of the deployment-handler service
        # for policy-handler to direct the policy-updates to
        #   - expecting dns to resolve the name
        #      deployment_handler to ip address
        url : "http://deployment_handler:8188"

- logic is backwards compatible with 2.3.1 format
- removed import pip from audit
   -- import pip broken in pip 9.0.2 (2018-03-19)
   -- import pip conflicts with requests
   -- pip API is not officially supported
   -- see links for more
      pypa/pip#5079
      pypa/pip#5081

Change-Id: Ifcaba6cfd714f3099ab7a25fe979a3696a6460fc
Signed-off-by: Alex Shatov <alexs@att.com>
Issue-ID: DCAEGEN2-404
  • Loading branch information
alex-sh2020 committed Mar 20, 2018
1 parent 7e220c8 commit 14411ac
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 24 deletions.
5 changes: 4 additions & 1 deletion etc_upload/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
},
"target_entity" : "policy_engine"
},
"deploy_handler" : "deployment_handler"
"deploy_handler" : {
"target_entity" : "deployment_handler",
"url" : "http://deployment_handler:8188"
}
}
}
38 changes: 29 additions & 9 deletions policyhandler/deploy_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
import requests

from .config import Config
from .customize import CustomizerUser
from .discovery import DiscoveryClient
from .onap.audit import REQUEST_X_ECOMP_REQUESTID, Audit, AuditHttpCode
from .customize import CustomizerUser

POOL_SIZE = 1

Expand All @@ -38,7 +38,7 @@ class DeployHandler(object):
_requests_session = None
_config = None
_url = None
_url_path = None
_url_policy = None
_target_entity = None
_custom_kwargs = None
_server_instance_uuid = None
Expand Down Expand Up @@ -66,10 +66,30 @@ def _lazy_init(audit):
requests.adapters.HTTPAdapter(pool_connections=POOL_SIZE, pool_maxsize=POOL_SIZE)
)

DeployHandler._target_entity = Config.config.get("deploy_handler", "deploy_handler")
DeployHandler._url = DiscoveryClient.get_service_url(audit, DeployHandler._target_entity)
DeployHandler._url_path = (DeployHandler._url or "") + '/policy'
DeployHandler._logger.info("DeployHandler url(%s)", DeployHandler._url)
config_dh = Config.config.get("deploy_handler")
if config_dh and isinstance(config_dh, dict):
# dns based routing to deployment-handler
# config for policy-handler >= 2.4.0
# "deploy_handler" : {
# "target_entity" : "deployment_handler",
# "url" : "http://deployment_handler:8188"
# }
DeployHandler._target_entity = config_dh.get("target_entity", "deployment_handler")
DeployHandler._url = config_dh.get("url")
DeployHandler._logger.info("dns based routing to %s: url(%s)",
DeployHandler._target_entity, DeployHandler._url)

if not DeployHandler._url:
# discover routing to deployment-handler at consul-services
if not isinstance(config_dh, dict):
# config for policy-handler <= 2.3.1
# "deploy_handler" : "deployment_handler"
DeployHandler._target_entity = str(config_dh or "deployment_handler")
DeployHandler._url = DiscoveryClient.get_service_url(audit, DeployHandler._target_entity)

DeployHandler._url_policy = str(DeployHandler._url or "") + '/policy'
DeployHandler._logger.info(
"got %s policy url(%s)", DeployHandler._target_entity, DeployHandler._url_policy)

@staticmethod
def policy_update(audit, message):
Expand All @@ -83,14 +103,14 @@ def policy_update(audit, message):

DeployHandler._lazy_init(audit)
sub_aud = Audit(aud_parent=audit, targetEntity=DeployHandler._target_entity,
targetServiceName=DeployHandler._url_path)
targetServiceName=DeployHandler._url_policy)
headers = {REQUEST_X_ECOMP_REQUESTID : sub_aud.request_id}

msg_str = json.dumps(message)
headers_str = json.dumps(headers)

log_action = "post to {0} at {1}".format(
DeployHandler._target_entity, DeployHandler._url_path)
DeployHandler._target_entity, DeployHandler._url_policy)
log_data = " msg={0} headers={1}".format(msg_str, headers_str)
log_line = log_action + log_data
DeployHandler._logger.info(log_line)
Expand All @@ -107,7 +127,7 @@ def policy_update(audit, message):
res = None
try:
res = DeployHandler._requests_session.post(
DeployHandler._url_path, json=message, headers=headers,
DeployHandler._url_policy, json=message, headers=headers,
**DeployHandler._custom_kwargs
)
except requests.exceptions.RequestException as ex:
Expand Down
14 changes: 5 additions & 9 deletions policyhandler/onap/audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,15 @@
audit = Audit(request_id=None, headers=None, msg=None)
"""

import copy
import json
import os
import sys
import json
import uuid
import time
import copy
import uuid
from datetime import datetime
from threading import Lock
from enum import Enum
from pip import utils as pip_utils
from threading import Lock

from .CommonLogger import CommonLogger
from .health import Health
Expand Down Expand Up @@ -125,8 +124,6 @@ class Audit(object):
_logger_audit = None
_health = Health()
_py_ver = sys.version.replace("\n", "")
_packages = sorted([pckg.project_name + "==" + pckg.version
for pckg in pip_utils.get_installed_distributions()])

@staticmethod
def init(service_name, service_version, config_file_path):
Expand Down Expand Up @@ -154,8 +151,7 @@ def health():
"started" : str(Audit._started),
"now" : str(now),
"uptime" : str(now - Audit._started),
"stats" : Audit._health.dump(),
"packages" : Audit._packages
"stats" : Audit._health.dump()
}

def __init__(self, request_id=None, req_message=None, aud_parent=None, **kwargs):
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ ECOMP is a trademark and service mark of AT&T Intellectual Property.
<groupId>org.onap.dcaegen2.platform</groupId>
<artifactId>policy-handler</artifactId>
<name>dcaegen2-platform-policy-handler</name>
<version>2.3.1-SNAPSHOT</version>
<version>2.4.0-SNAPSHOT</version>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
setup(
name='policyhandler',
description='DCAE-Controller policy-handler to communicate with policy-engine',
version="2.3.1",
version="2.4.0",
author='Alex Shatov',
packages=['policyhandler'],
zip_safe=False,
Expand Down
3 changes: 2 additions & 1 deletion tests/test_policyhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from datetime import datetime

import pytest

import cherrypy
from cherrypy.test.helper import CPWebCase

Expand All @@ -47,7 +48,7 @@
from policyhandler.policy_utils import PolicyUtils, Utils
from policyhandler.web_server import _PolicyWeb

POLICY_HANDLER_VERSION = "2.2.0"
POLICY_HANDLER_VERSION = "2.4.0"

class MonkeyHttpResponse(object):
"""Monkey http reposne"""
Expand Down
4 changes: 2 additions & 2 deletions version.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
major=2
minor=3
patch=1
minor=4
patch=0
base_version=${major}.${minor}.${patch}
release_version=${base_version}
snapshot_version=${base_version}-SNAPSHOT

0 comments on commit 14411ac

Please sign in to comment.