blob: a31bf3f0b7eb5e53a4df167b2878ca7f316ebdfb [file] [log] [blame]
Matteo Scandoload0c1752018-08-09 15:47:16 -07001# Copyright 2017-present Open Networking Foundation
2#
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
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
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.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import unittest
16from mock import patch, call, Mock, PropertyMock
17import json
18
19import os, sys
20
21# Hack to load synchronizer framework
22test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
23service_dir=os.path.join(test_path, "../../../..")
24xos_dir=os.path.join(test_path, "../../..")
25if not os.path.exists(os.path.join(test_path, "new_base")):
26 xos_dir=os.path.join(test_path, "../../../../../../orchestration/xos/xos")
27 services_dir=os.path.join(xos_dir, "../../xos_services")
28# END Hack to load synchronizer framework
29
30# generate model from xproto
31def get_models_fn(service_name, xproto_name):
32 name = os.path.join(service_name, "xos", xproto_name)
33 if os.path.exists(os.path.join(services_dir, name)):
34 return name
35 else:
36 name = os.path.join(service_name, "xos", "synchronizer", "models", xproto_name)
37 if os.path.exists(os.path.join(services_dir, name)):
38 return name
39 raise Exception("Unable to find service=%s xproto=%s" % (service_name, xproto_name))
40# END generate model from xproto
41
42class TestSubscriberAuthEvent(unittest.TestCase):
43
44 def setUp(self):
45
46 self.sys_path_save = sys.path
47 sys.path.append(xos_dir)
48 sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base'))
49
50 # Setting up the config module
51 from xosconfig import Config
52 config = os.path.join(test_path, "../test_config.yaml")
53 Config.clear()
54 Config.init(config, "synchronizer-config-schema.yaml")
55 from multistructlog import create_logger
56 log = create_logger(Config().get('logging'))
57 # END Setting up the config module
58
59 from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor
60 # build_mock_modelaccessor(xos_dir, services_dir, [get_models_fn("olt-service", "volt.xproto")])
61
62 build_mock_modelaccessor(xos_dir, services_dir, [
63 get_models_fn("att-workflow-driver", "att-workflow-driver.xproto"),
64 get_models_fn("olt-service", "volt.xproto"),
65 get_models_fn("../profiles/rcord", "rcord.xproto")
66 ])
67 import synchronizers.new_base.modelaccessor
68 from dhcp_event import SubscriberDhcpEventStep, model_accessor
69
70 # import all class names to globals
71 for (k, v) in model_accessor.all_model_classes.items():
72 globals()[k] = v
73
74 self.log = log
75
76 self.event_step = SubscriberDhcpEventStep(self.log)
77
78 self.event = Mock()
79
80 self.volt = Mock()
81 self.volt.name = "vOLT"
82 self.volt.leaf_model = Mock()
83
Matteo Scandolode8cfa82018-10-16 13:49:05 -070084 # self.subscriber = RCORDSubscriber()
85 # self.subscriber.onu_device = "BRCM1234"
86 # self.subscriber.save = Mock()
Matteo Scandoload0c1752018-08-09 15:47:16 -070087
Matteo Scandoloccef5782018-08-13 13:25:17 -070088 self.mac_address = "00:AA:00:00:00:01"
Matteo Scandoload0c1752018-08-09 15:47:16 -070089 self.ip_address = "192.168.3.5"
90
Matteo Scandolode8cfa82018-10-16 13:49:05 -070091 self.si = AttWorkflowDriverServiceInstance()
92 self.si.serial_number = "BRCM1234"
93 self.si.save = Mock()
94
Matteo Scandoload0c1752018-08-09 15:47:16 -070095
96 def tearDown(self):
97 sys.path = self.sys_path_save
98
99 def test_dhcp_subscriber(self):
100
101 self.event.value = json.dumps({
102 "deviceId" : "of:0000000000000001",
103 "portNumber" : "1",
104 "macAddress" : self.mac_address,
Matteo Scandolode8cfa82018-10-16 13:49:05 -0700105 "ipAddress" : self.ip_address,
106 "messageType": "DHCPREQUEST"
Matteo Scandoload0c1752018-08-09 15:47:16 -0700107 })
108
109 with patch.object(VOLTService.objects, "get_items") as volt_service_mock, \
Matteo Scandolode8cfa82018-10-16 13:49:05 -0700110 patch.object(AttWorkflowDriverServiceInstance.objects, "get_items") as si_mock, \
Matteo Scandoload0c1752018-08-09 15:47:16 -0700111 patch.object(self.volt, "get_onu_sn_from_openflow") as get_onu_sn:
112
113 volt_service_mock.return_value = [self.volt]
114 get_onu_sn.return_value = "BRCM1234"
Matteo Scandolode8cfa82018-10-16 13:49:05 -0700115 si_mock.return_value = [self.si]
Matteo Scandoload0c1752018-08-09 15:47:16 -0700116
117 self.event_step.process_event(self.event)
118
Matteo Scandolode8cfa82018-10-16 13:49:05 -0700119 self.si.save.assert_called()
120 self.assertEqual(self.si.dhcp_state, "DHCPREQUEST")
121 self.assertEqual(self.si.mac_address, self.mac_address)
122 self.assertEqual(self.si.ip_address, self.ip_address)