SEBA-542 Basic scaffolding for TT Workflow Driver service
Change-Id: Ia88a4d22f40b45299bb35e7bc5600cccbc453873
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..9cff527
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,8 @@
+.idea/
+*.pyc
+
+.coverage
+htmlcov
+coverage.xml
+nose2-junit.xml
+
diff --git a/Dockerfile.synchronizer b/Dockerfile.synchronizer
new file mode 100644
index 0000000..0d41c30
--- /dev/null
+++ b/Dockerfile.synchronizer
@@ -0,0 +1,55 @@
+# Copyright 2017-present Open Networking Foundation
+#
+# 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.
+
+# docker build -t xosproject/tt-workflow-driver-synchronizer:candidate -f Dockerfile.synchronizer .
+
+# xosproject/tt-workflow-driver-synchronizer
+
+FROM xosproject/xos-synchronizer-base:2.2.18
+
+COPY xos/synchronizer /opt/xos/synchronizers/tt-workflow-driver
+COPY VERSION /opt/xos/synchronizers/tt-workflow-driver/
+
+WORKDIR "/opt/xos/synchronizers/tt-workflow-driver"
+
+# Label image
+ARG org_label_schema_schema_version=1.0
+ARG org_label_schema_name=tt-workflow-driver-synchronizer
+ARG org_label_schema_version=unknown
+ARG org_label_schema_vcs_url=unknown
+ARG org_label_schema_vcs_ref=unknown
+ARG org_label_schema_build_date=unknown
+ARG org_opencord_vcs_commit_date=unknown
+ARG org_opencord_component_chameleon_version=unknown
+ARG org_opencord_component_chameleon_vcs_url=unknown
+ARG org_opencord_component_chameleon_vcs_ref=unknown
+ARG org_opencord_component_xos_version=unknown
+ARG org_opencord_component_xos_vcs_url=unknown
+ARG org_opencord_component_xos_vcs_ref=unknown
+
+LABEL org.label-schema.schema-version=$org_label_schema_schema_version \
+ org.label-schema.name=$org_label_schema_name \
+ org.label-schema.version=$org_label_schema_version \
+ org.label-schema.vcs-url=$org_label_schema_vcs_url \
+ org.label-schema.vcs-ref=$org_label_schema_vcs_ref \
+ org.label-schema.build-date=$org_label_schema_build_date \
+ org.opencord.vcs-commit-date=$org_opencord_vcs_commit_date \
+ org.opencord.component.chameleon.version=$org_opencord_component_chameleon_version \
+ org.opencord.component.chameleon.vcs-url=$org_opencord_component_chameleon_vcs_url \
+ org.opencord.component.chameleon.vcs-ref=$org_opencord_component_chameleon_vcs_ref \
+ org.opencord.component.xos.version=$org_opencord_component_xos_version \
+ org.opencord.component.xos.vcs-url=$org_opencord_component_xos_vcs_url \
+ org.opencord.component.xos.vcs-ref=$org_opencord_component_xos_vcs_ref
+
+CMD ["/usr/bin/python", "/opt/xos/synchronizers/tt-workflow-driver/tt-workflow-driver-synchronizer.py"]
diff --git a/LICENSE.txt b/LICENSE.txt
new file mode 100644
index 0000000..d9d9d30
--- /dev/null
+++ b/LICENSE.txt
@@ -0,0 +1,202 @@
+
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+ 2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+ 3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+ 4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+ 6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+ 7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+ 8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+ 9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+ END OF TERMS AND CONDITIONS
+
+ APPENDIX: How to apply the Apache License to your work.
+
+ To apply the Apache License to your work, attach the following
+ boilerplate notice, with the fields enclosed by brackets "[]"
+ replaced with your own identifying information. (Don't include
+ the brackets!) The text should be enclosed in the appropriate
+ comment syntax for the file format. We also recommend that a
+ file or class name and description of purpose be included on the
+ same "printed page" as the copyright notice for easier
+ identification within third-party archives.
+
+ Copyright 2016 Open Networking Foundation
+
+ 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.
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..6c6c0ba
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,22 @@
+REGISTRY ?=
+REPOSITORY ?=
+DOCKER_BUILD_ARGS ?=
+SERVICE ?= tt-workflow-driver
+MAKEFILE_DIR := $(dir $(realpath $(firstword $(MAKEFILE_LIST))))
+TAG ?= $(shell cat ${MAKEFILE_DIR}/VERSION)
+IMAGENAME := ${REGISTRY}${REPOSITORY}${SERVICE}-synchronizer:${TAG}
+SHELL := /bin/bash
+
+all: build push
+
+build:
+ docker build $(DOCKER_BUILD_ARGS) -t ${IMAGENAME} -f Dockerfile.synchronizer .
+
+push:
+ docker push ${IMAGENAME}
+
+test:
+ source ../../xos/venv-xos/bin/activate && cd xos && nose2 --verbose --coverage-report term || echo "Please install the XOS virtual environment"
+
+migrate:
+ source ../../xos/venv-xos/bin/activate && cd xos && xos-migrate -s $(SERVICE)
diff --git a/VERSION b/VERSION
new file mode 100644
index 0000000..0d4d124
--- /dev/null
+++ b/VERSION
@@ -0,0 +1 @@
+0.1.0-dev
diff --git a/docs/README.md b/docs/README.md
new file mode 100644
index 0000000..fcd2d98
--- /dev/null
+++ b/docs/README.md
@@ -0,0 +1,115 @@
+# Turk Telekom Workflow Driver Service
+
+This service implements the ONU and Subscriber management logic required by Turk Telekom.
+
+> NOTE: This service depends on models contained in the R-CORD and OLT Services, so make sure that the `rcord-synchronizer` and `volt-synchronzier` are running
+
+## Models
+
+This service is composed of the following models:
+
+- `TtWorkflowDriverServiceInstance`. This model holds various state associated with the state machine for validating a subscriber's ONU.
+ - `serial_number`. Serial number of ONU.
+ - `of_dpid`.
+ - `uni_port_id`.
+ - `onu_state`. [`AWAITING` | `ENABLED` | `DISABLED`]. State of the ONU.
+ - `status_message`.
+ - `dhcp_state`. [`AWAITING` | `DHCPDISCOVER` | `DHCPACK` | `DHCPREQUEST`]. Current DHCP state.
+ - `ip_address`. Subscriber ip address.
+ - `mac_address`. Subscriber mac address.
+- `TtWorkflowDriverWhiteListEntry`. This model holds a whitelist authorizing an ONU with a specific serial number to be connected to a specific PON Port on a specific OLT.
+ - `owner`. Relation to the TtWorkflowDriverService that owns this whitelist entry.
+ - `serial_number`. Serial number of ONU.
+ - `pon_port_id`. Pon port identifier.
+ - `device_id`. OLT device identifier.
+
+## Example Tosca - Create a whitelist entry
+
+```yaml
+tosca_definitions_version: tosca_simple_yaml_1_0
+imports:
+ - custom_types/ttworkflowdriverwhitelistentry.yaml
+ - custom_types/ttworkflowdriverservice.yaml
+description: Create an entry in the whitelist
+topology_template:
+ node_templates:
+
+ service#tt:
+ type: tosca.nodes.TtWorkflowDriverService
+ properties:
+ name: tt-workflow-driver
+ must-exist: true
+
+ whitelist:
+ type: tosca.nodes.TtWorkflowDriverWhiteListEntry
+ properties:
+ serial_number: BRCM22222222
+ pon_port_id: 536870912
+ device_id: of:000000000a5a0072
+ requirements:
+ - owner:
+ node: service#tt
+ relationship: tosca.relationships.BelongsToOne
+```
+
+## Integration with other Services
+
+This service integrates closely with the `R-CORD` and `vOLT` services, directly manipulating models (`RCORDSubscriber`, `ONUDevice`) in those services.
+
+## Synchronizer Workflows
+
+This synchronizer implements only event_steps and model_policies. It's job is to listen for events and execute a state machine associated with those events. Service Instances are created automatically when ONU events are received. As the state machine changes various states for authentication, etc., those changes will be propagated to the appropriate objects in the `R-CORD` and `vOLT` services.
+
+The state machine is described below.
+
+### Service Instances State Machine
+
+| Topic | Event | Actions | ONU State | Subscriber State | Message |
+|-------------------------|----------------------------------|-----------------------------------------------------------|--------------|----------------------|----------------------------------------------------------|
+| `onu.events` | `status: activated` | Validate against whitelist (successful) | enabled | enabled | ONU has been validated |
+| `onu.events` | `status: activated` | Validate against whitelist (failed, missing) | disabled | awaiting-auth | ONU not found in whitelist |
+| `onu.events` | `status: activated` | Validate against whitelist (failed, location) | disabled | awaiting-auth | ONU activated in wrong location |
+| `onu.events` | `status: disabled` | Mark ONU as disabled and revoke subscriber authentication | disabled | awaiting-auth | ONU has been disabled, revoked subscriber authentication |
+| `dhcp.events` | -- | Update subscriber ip and mac address | enabled | enabled | DHCP information added
+
+### Model Policy: TtWorkflowDriverServiceInstancePolicy
+
+This model policy is responsible for reacting to state changes that are caused by various event steps, implementing the state machine described above.
+
+### Event Step: SubscriberDhcpEventStep
+
+Listens on `dhcp.events` and updates the `dhcp_state`, `ip_address`, and `mac_address` fields of `TtWorkflowDriverServiceInstance`.
+
+### Event Step: ONUEventStep
+
+Listens on `onu.events` and updates the `onu_state` of `TtWorkflowDriverServiceInstance`. Automatically creates `TtWorkflowDriverServiceInstance` as necessary.
+
+
+## Events format
+
+This events are generated by various applications running on top of ONOS and published on a Kafka bus.
+Here is the structure of the events and their topics.
+
+### onu.events
+
+```json
+{
+ "timestamp": "2018-09-11T01:00:49.506Z",
+ "status": "activated", // or disabled
+ "serial_number": "ALPHe3d1cfde", // ONU serial number
+ "uni_port_id": 16, // uni port
+ "of_dpid": "of:000000000a5a0072" // OLT OpenFlow Id
+}
+```
+
+### dhcp.events
+
+```json
+{
+ "deviceId" : "of:000000000a5a0072",
+ "portNumber" : "16",
+ "macAddress" : "90:e2:ba:82:fa:81",
+ "ipAddress" : "10.11.1.1"
+}
+```
+
diff --git a/samples/whitelist.yaml b/samples/whitelist.yaml
new file mode 100644
index 0000000..b17f488
--- /dev/null
+++ b/samples/whitelist.yaml
@@ -0,0 +1,40 @@
+# Copyright 2017-present Open Networking Foundation
+#
+# 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.
+
+# curl -H "xos-username: admin@opencord.org" -H "xos-password: letmein" -X POST --data-binary @whitelist.yaml http://192.168.99.100:30007/run
+
+tosca_definitions_version: tosca_simple_yaml_1_0
+imports:
+ - custom_types/ttworkflowdriverwhitelistentry.yaml
+ - custom_types/ttworkflowdriverservice.yaml
+description: Create an entry in the whitelist
+topology_template:
+ node_templates:
+
+ service#tt:
+ type: tosca.nodes.TtWorkflowDriverService
+ properties:
+ name: tt-workflow-driver
+ must-exist: true
+
+ whitelist:
+ type: tosca.nodes.TtWorkflowDriverWhiteListEntry
+ properties:
+ serial_number: BRCM22222222
+ pon_port_id: 536870912
+ device_id: of:000000000a5a0072
+ requirements:
+ - owner:
+ node: service#tt
+ relationship: tosca.relationships.BelongsToOne
diff --git a/xos/synchronizer/__init__.py b/xos/synchronizer/__init__.py
new file mode 100644
index 0000000..8bbb6fa
--- /dev/null
+++ b/xos/synchronizer/__init__.py
@@ -0,0 +1,14 @@
+
+# Copyright 2017-present Open Networking Foundation
+#
+# 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.
\ No newline at end of file
diff --git a/xos/synchronizer/config.yaml b/xos/synchronizer/config.yaml
new file mode 100644
index 0000000..647efdc
--- /dev/null
+++ b/xos/synchronizer/config.yaml
@@ -0,0 +1,42 @@
+
+# Copyright 2017-present Open Networking Foundation
+#
+# 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.
+
+
+name: tt-workflow-driver
+core_version: ">=2.2.1"
+required_models:
+ - TtWorkflowDriverService
+ - TtWorkflowDriverServiceInstance
+ - RCORDSubscriber
+ - ONUDevice
+model_policies_dir: "/opt/xos/synchronizers/tt-workflow-driver/model_policies"
+models_dir: "/opt/xos/synchronizers/tt-workflow-driver/models"
+event_steps_dir: "/opt/xos/synchronizers/tt-workflow-driver/event_steps"
+logging:
+ version: 1
+ handlers:
+ console:
+ class: logging.StreamHandler
+ file:
+ class: logging.handlers.RotatingFileHandler
+ filename: /var/log/xos.log
+ maxBytes: 10485760
+ backupCount: 5
+ loggers:
+ 'multistructlog':
+ handlers:
+ - console
+ - file
+ level: DEBUG
diff --git a/xos/synchronizer/event_steps/dhcp_event.py b/xos/synchronizer/event_steps/dhcp_event.py
new file mode 100644
index 0000000..42f0858
--- /dev/null
+++ b/xos/synchronizer/event_steps/dhcp_event.py
@@ -0,0 +1,46 @@
+
+# Copyright 2017-present Open Networking Foundation
+#
+# 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.
+
+import json
+import time
+import os
+import sys
+from xossynchronizer.event_steps.eventstep import EventStep
+from helpers import TtHelpers
+
+class SubscriberDhcpEventStep(EventStep):
+ topics = ["dhcp.events"]
+ technology = "kafka"
+
+ def __init__(self, *args, **kwargs):
+ super(SubscriberDhcpEventStep, self).__init__(*args, **kwargs)
+
+ def process_event(self, event):
+ value = json.loads(event.value)
+
+ onu_sn = TtHelpers.get_onu_sn(self.model_accessor, self.log, value)
+ si = TtHelpers.get_si_by_sn(self.model_accessor, self.log, onu_sn)
+
+ if not si:
+ self.log.exception("dhcp.events: Cannot find tt-workflow-driver service instance for this event", kafka_event=value)
+ raise Exception("dhcp.events: Cannot find tt-workflow-driver service instance for this event")
+
+ self.log.info("dhcp.events: Got event for subscriber", event_value=value, onu_sn=onu_sn, si=si)
+
+ si.dhcp_state = value["messageType"]
+ si.ip_address = value["ipAddress"]
+ si.mac_address = value["macAddress"]
+
+ si.save_changed_fields(always_update_timestamp=True)
diff --git a/xos/synchronizer/event_steps/onu_event.py b/xos/synchronizer/event_steps/onu_event.py
new file mode 100644
index 0000000..386942f
--- /dev/null
+++ b/xos/synchronizer/event_steps/onu_event.py
@@ -0,0 +1,61 @@
+
+# Copyright 2017-present Open Networking Foundation
+#
+# 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.
+
+
+import json
+from xossynchronizer.event_steps.eventstep import EventStep
+
+class ONUEventStep(EventStep):
+ topics = ["onu.events"]
+ technology = "kafka"
+
+ max_onu_retry = 50
+
+ def __init__(self, *args, **kwargs):
+ super(ONUEventStep, self).__init__(*args, **kwargs)
+
+ def get_tt_si(self, event):
+ try:
+ tt_si = self.model_accessor.TtWorkflowDriverServiceInstance.objects.get(serial_number=event["serial_number"])
+ tt_si.no_sync = False;
+ tt_si.uni_port_id = event["uni_port_id"]
+ tt_si.of_dpid = event["of_dpid"]
+ self.log.debug("onu.events: Found existing TtWorkflowDriverServiceInstance", si=tt_si)
+ except IndexError:
+ # create an TtWorkflowDriverServiceInstance, the validation will be triggered in the corresponding sync step
+ tt_si = self.model_accessor.TtWorkflowDriverServiceInstance(
+ serial_number=event["serial_number"],
+ of_dpid=event["of_dpid"],
+ uni_port_id=event["uni_port_id"],
+ owner=self.model_accessor.TtWorkflowDriverService.objects.first() # we assume there is only one TtWorkflowDriverService
+ )
+ self.log.debug("onu.events: Created new TtWorkflowDriverServiceInstance", si=tt_si)
+ return tt_si
+
+ def process_event(self, event):
+ value = json.loads(event.value)
+ self.log.info("onu.events: received event", value=value)
+
+ if value["status"] == "activated":
+ self.log.info("onu.events: activated onu", value=value)
+ tt_si = self.get_tt_si(value)
+ tt_si.onu_state = "ENABLED"
+ tt_si.save_changed_fields(always_update_timestamp=True)
+ elif value["status"] == "disabled":
+ self.log.info("onu.events: disabled onu, not taking any action", value=value)
+ return
+ else:
+ self.log.warn("onu.events: Unknown status value: %s" % value["status"], value=value)
+ return
diff --git a/xos/synchronizer/event_steps/test_dhcp_event.py b/xos/synchronizer/event_steps/test_dhcp_event.py
new file mode 100644
index 0000000..d7934da
--- /dev/null
+++ b/xos/synchronizer/event_steps/test_dhcp_event.py
@@ -0,0 +1,110 @@
+# Copyright 2017-present Open Networking Foundation
+#
+# 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.
+
+import unittest
+from mock import patch, call, Mock, PropertyMock
+import json
+
+import os, sys
+
+test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
+
+class TestSubscriberAuthEvent(unittest.TestCase):
+
+ def setUp(self):
+
+ self.sys_path_save = sys.path
+
+ # Setting up the config module
+ from xosconfig import Config
+ config = os.path.join(test_path, "../test_config.yaml")
+ Config.clear()
+ Config.init(config, "synchronizer-config-schema.yaml")
+ from multistructlog import create_logger
+ log = create_logger(Config().get('logging'))
+ # END Setting up the config module
+
+ from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config
+ mock_modelaccessor_config(test_path, [("tt-workflow-driver", "tt-workflow-driver.xproto"),
+ ("olt-service", "volt.xproto"),
+ ("rcord", "rcord.xproto")])
+
+ import xossynchronizer.modelaccessor
+ import mock_modelaccessor
+ reload(mock_modelaccessor) # in case nose2 loaded it in a previous test
+ reload(xossynchronizer.modelaccessor) # in case nose2 loaded it in a previous test
+
+ from xossynchronizer.modelaccessor import model_accessor
+ from dhcp_event import SubscriberDhcpEventStep
+
+ # import all class names to globals
+ for (k, v) in model_accessor.all_model_classes.items():
+ globals()[k] = v
+
+ self.model_accessor = model_accessor
+ self.log = log
+
+ self.event_step = SubscriberDhcpEventStep(model_accessor=self.model_accessor, log=self.log)
+
+ self.event = Mock()
+
+ self.volt = Mock()
+ self.volt.name = "vOLT"
+ self.volt.leaf_model = Mock()
+
+ # self.subscriber = RCORDSubscriber()
+ # self.subscriber.onu_device = "BRCM1234"
+ # self.subscriber.save = Mock()
+
+ self.mac_address = "00:AA:00:00:00:01"
+ self.ip_address = "192.168.3.5"
+
+ self.si = TtWorkflowDriverServiceInstance()
+ self.si.serial_number = "BRCM1234"
+ self.si.save = Mock()
+
+
+ def tearDown(self):
+ sys.path = self.sys_path_save
+
+ def test_dhcp_subscriber(self):
+
+ self.event.value = json.dumps({
+ "deviceId" : "of:0000000000000001",
+ "portNumber" : "1",
+ "macAddress" : self.mac_address,
+ "ipAddress" : self.ip_address,
+ "messageType": "DHCPREQUEST"
+ })
+
+ with patch.object(VOLTService.objects, "get_items") as volt_service_mock, \
+ patch.object(TtWorkflowDriverServiceInstance.objects, "get_items") as si_mock, \
+ patch.object(self.volt, "get_onu_sn_from_openflow") as get_onu_sn:
+
+ self.assertTrue(VOLTService.objects.first() is not None)
+
+ volt_service_mock.return_value = [self.volt]
+ get_onu_sn.return_value = "BRCM1234"
+ si_mock.return_value = [self.si]
+
+ self.event_step.process_event(self.event)
+
+ self.si.save.assert_called()
+ self.assertEqual(self.si.dhcp_state, "DHCPREQUEST")
+ self.assertEqual(self.si.mac_address, self.mac_address)
+ self.assertEqual(self.si.ip_address, self.ip_address)
+
+if __name__ == '__main__':
+ sys.path.append("..") # for import of helpers.py
+ unittest.main()
\ No newline at end of file
diff --git a/xos/synchronizer/event_steps/test_onu_events.py b/xos/synchronizer/event_steps/test_onu_events.py
new file mode 100644
index 0000000..ea368d5
--- /dev/null
+++ b/xos/synchronizer/event_steps/test_onu_events.py
@@ -0,0 +1,135 @@
+# Copyright 2017-present Open Networking Foundation
+#
+# 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.
+
+import unittest
+from mock import patch, call, Mock, PropertyMock
+import json
+
+import os, sys
+
+test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
+
+class TestSyncOLTDevice(unittest.TestCase):
+
+ def setUp(self):
+
+ self.sys_path_save = sys.path
+
+ # Setting up the config module
+ from xosconfig import Config
+ config = os.path.join(test_path, "../test_config.yaml")
+ Config.clear()
+ Config.init(config, "synchronizer-config-schema.yaml")
+ # END Setting up the config module
+
+ from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config
+ mock_modelaccessor_config(test_path, [("tt-workflow-driver", "tt-workflow-driver.xproto"),
+ ("olt-service", "volt.xproto"),
+ ("rcord", "rcord.xproto")])
+
+ import xossynchronizer.modelaccessor
+ import mock_modelaccessor
+ reload(mock_modelaccessor) # in case nose2 loaded it in a previous test
+ reload(xossynchronizer.modelaccessor) # in case nose2 loaded it in a previous test
+
+ from xossynchronizer.modelaccessor import model_accessor
+ from onu_event import ONUEventStep
+
+ # import all class names to globals
+ for (k, v) in model_accessor.all_model_classes.items():
+ globals()[k] = v
+
+ self.model_accessor = model_accessor
+ self.log = Mock()
+
+ self.event_step = ONUEventStep(model_accessor=self.model_accessor, log=self.log)
+
+ self.event = Mock()
+ self.event_dict = {
+ 'status': 'activated',
+ 'serial_number': 'BRCM1234',
+ 'of_dpid': 'of:109299321',
+ 'uni_port_id': 16
+ }
+ self.event.value = json.dumps(self.event_dict)
+
+ self.tt = TtWorkflowDriverService(name="tt-workflow-driver")
+
+ def tearDown(self):
+ sys.path = self.sys_path_save
+
+
+ def test_create_instance(self):
+
+ with patch.object(TtWorkflowDriverServiceInstance.objects, "get_items") as tt_si_mock , \
+ patch.object(TtWorkflowDriverService.objects, "get_items") as service_mock, \
+ patch.object(TtWorkflowDriverServiceInstance, "save", autospec=True) as mock_save:
+
+ tt_si_mock.return_value = []
+ service_mock.return_value = [self.tt]
+
+ self.event_step.process_event(self.event)
+
+ tt_si = mock_save.call_args[0][0]
+
+ self.assertEqual(mock_save.call_count, 1)
+
+ self.assertEqual(tt_si.serial_number, self.event_dict['serial_number'])
+ self.assertEqual(tt_si.of_dpid, self.event_dict['of_dpid'])
+ self.assertEqual(tt_si.uni_port_id, self.event_dict['uni_port_id'])
+ self.assertEqual(tt_si.onu_state, "ENABLED")
+
+ def test_reuse_instance(self):
+
+ si = TtWorkflowDriverServiceInstance(
+ serial_number=self.event_dict["serial_number"],
+ of_dpid="foo",
+ uni_port_id="foo"
+ )
+
+ with patch.object(TtWorkflowDriverServiceInstance.objects, "get_items") as tt_si_mock , \
+ patch.object(TtWorkflowDriverServiceInstance, "save", autospec=True) as mock_save:
+
+ tt_si_mock.return_value = [si]
+
+ self.event_step.process_event(self.event)
+
+ tt_si = mock_save.call_args[0][0]
+
+ self.assertEqual(mock_save.call_count, 1)
+
+ self.assertEqual(tt_si.serial_number, self.event_dict['serial_number'])
+ self.assertEqual(tt_si.of_dpid, self.event_dict['of_dpid'])
+ self.assertEqual(tt_si.uni_port_id, self.event_dict['uni_port_id'])
+ self.assertEqual(tt_si.onu_state, "ENABLED")
+
+ def test_disable_onu(self):
+ self.event_dict = {
+ 'status': 'disabled',
+ 'serial_number': 'BRCM1234',
+ 'of_dpid': 'of:109299321',
+ 'uni_port_id': 16
+ }
+ self.event.value = json.dumps(self.event_dict)
+
+ with patch.object(TtWorkflowDriverServiceInstance, "save", autospec=True) as mock_save:
+
+ self.event_step.process_event(self.event)
+
+ self.assertEqual(mock_save.call_count, 0)
+
+
+if __name__ == '__main__':
+ sys.path.append("..") # for import of helpers.py
+ unittest.main()
\ No newline at end of file
diff --git a/xos/synchronizer/helpers.py b/xos/synchronizer/helpers.py
new file mode 100644
index 0000000..e67890a
--- /dev/null
+++ b/xos/synchronizer/helpers.py
@@ -0,0 +1,76 @@
+# Copyright 2017-present Open Networking Foundation
+#
+# 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.
+
+from xossynchronizer.steps.syncstep import DeferredException
+
+class TtHelpers():
+ @staticmethod
+ def validate_onu(model_accessor, log, tt_si):
+ """
+ This method validate an ONU against the whitelist and set the appropriate state.
+ It's expected that the deferred exception is managed in the caller method,
+ for example a model_policy or a sync_step.
+
+ :param tt_si: TtWorkflowDriverServiceInstance
+ :return: [boolean, string]
+ """
+
+ oss_service = tt_si.owner.leaf_model
+
+ # See if there is a matching entry in the whitelist.
+ matching_entries = model_accessor.TtWorkflowDriverWhiteListEntry.objects.filter(
+ owner_id=oss_service.id,
+ )
+ matching_entries = [e for e in matching_entries if e.serial_number.lower() == tt_si.serial_number.lower()]
+
+ if len(matching_entries) == 0:
+ log.warn("ONU not found in whitelist", object=str(tt_si), serial_number=tt_si.serial_number, **tt_si.tologdict())
+ return [False, "ONU not found in whitelist"]
+
+ whitelisted = matching_entries[0]
+ try:
+ pon_port = model_accessor.ONUDevice.objects.get(serial_number=tt_si.serial_number).pon_port
+ except IndexError:
+ raise DeferredException("ONU device %s is not know to XOS yet" % tt_si.serial_number)
+
+ if pon_port.port_no != whitelisted.pon_port_id or tt_si.of_dpid != whitelisted.device_id:
+ log.warn("ONU disable as location don't match",
+ object=str(tt_si),
+ serial_number=tt_si.serial_number,
+ pon_port=pon_port.port_no,
+ whitelisted_pon_port=whitelisted.pon_port_id,
+ device_id=tt_si.of_dpid,
+ whitelisted_device_id=whitelisted.device_id,
+ **tt_si.tologdict())
+ return [False, "ONU activated in wrong location"]
+
+ return [True, "ONU has been validated"]
+
+ @staticmethod
+ def get_onu_sn(model_accessor, log, event):
+ olt_service = model_accessor.VOLTService.objects.first()
+ onu_sn = olt_service.get_onu_sn_from_openflow(event["deviceId"], event["portNumber"])
+ if not onu_sn or onu_sn is None:
+ log.exception("Cannot find onu serial number for this event", kafka_event=event)
+ raise Exception("Cannot find onu serial number for this event")
+
+ return onu_sn
+
+ @staticmethod
+ def get_si_by_sn(model_accessor, log, serial_number):
+ try:
+ return model_accessor.TtWorkflowDriverServiceInstance.objects.get(serial_number=serial_number)
+ except IndexError:
+ log.exception("Cannot find tt-workflow-driver service instance for this serial number", serial_number=serial_number)
+ raise Exception("Cannot find tt-workflow-driver service instance for this serial number %s", serial_number)
\ No newline at end of file
diff --git a/xos/synchronizer/migrations/0001_initial.py b/xos/synchronizer/migrations/0001_initial.py
new file mode 100644
index 0000000..1b06660
--- /dev/null
+++ b/xos/synchronizer/migrations/0001_initial.py
@@ -0,0 +1,102 @@
+# Copyright 2017-present Open Networking Foundation
+#
+# 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.
+
+# -*- coding: utf-8 -*-
+# Generated by Django 1.11.11 on 2019-03-15 17:42
+from __future__ import unicode_literals
+
+import core.models.xosbase_header
+from django.db import migrations, models
+import django.db.models.deletion
+import django.utils.timezone
+
+
+class Migration(migrations.Migration):
+
+ initial = True
+
+ dependencies = [
+ ('core', '0002_initial_data'),
+ ]
+
+ operations = [
+ migrations.CreateModel(
+ name='TtWorkflowDriverService',
+ fields=[
+ ('service_decl_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='core.Service_decl')),
+ ],
+ options={
+ 'verbose_name': 'TtWorkflowDriver Service',
+ },
+ bases=('core.service',),
+ ),
+ migrations.CreateModel(
+ name='TtWorkflowDriverServiceInstance',
+ fields=[
+ ('serviceinstance_decl_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='core.ServiceInstance_decl')),
+ ('serial_number', models.CharField(help_text=b'Serial number of ONU', max_length=254, unique=True)),
+ ('of_dpid', models.CharField(help_text=b'OLT Openflow ID', max_length=254)),
+ ('uni_port_id', models.IntegerField(help_text=b'ONU UNI port ID')),
+ ('onu_state', models.CharField(choices=[(b'AWAITING', b'Awaiting'), (b'ENABLED', b'Enabled'), (b'DISABLED', b'Disabled')], default=b'AWAITING', help_text=b'ONU administrative state', max_length=254)),
+ ('status_message', models.CharField(blank=True, default=b'', help_text=b'Status text of current state machine state', max_length=254, null=True)),
+ ('dhcp_state', models.CharField(choices=[(b'AWAITING', b'Awaiting'), (b'DHCPDISCOVER', b'DHCPDISCOVER'), (b'DHCPACK', b'DHCPACK'), (b'DHCPREQUEST', b'DHCPREQUEST')], default=b'AWAITING', max_length=254)),
+ ('ip_address', models.CharField(blank=True, help_text=b'Management IP address, learned from DHCP', max_length=20, null=True)),
+ ('mac_address', models.CharField(blank=True, help_text=b'Management MAC address, leanred from DHCP', max_length=20, null=True)),
+ ],
+ options={
+ 'verbose_name': 'TtWorkflowDriver Service Instance',
+ },
+ bases=('core.serviceinstance',),
+ ),
+ migrations.CreateModel(
+ name='TtWorkflowDriverWhiteListEntry',
+ fields=[
+ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
+ ('created', models.DateTimeField(auto_now_add=True, help_text=b'Time this model was created')),
+ ('updated', models.DateTimeField(default=django.utils.timezone.now, help_text=b'Time this model was changed by a non-synchronizer')),
+ ('enacted', models.DateTimeField(blank=True, default=None, help_text=b'When synced, set to the timestamp of the data that was synced', null=True)),
+ ('policed', models.DateTimeField(blank=True, default=None, help_text=b'When policed, set to the timestamp of the data that was policed', null=True)),
+ ('backend_register', models.CharField(blank=True, default=b'{}', max_length=1024, null=True)),
+ ('backend_need_delete', models.BooleanField(default=False)),
+ ('backend_need_reap', models.BooleanField(default=False)),
+ ('backend_status', models.CharField(default=b'Provisioning in progress', max_length=1024)),
+ ('backend_code', models.IntegerField(default=0)),
+ ('deleted', models.BooleanField(default=False)),
+ ('write_protect', models.BooleanField(default=False)),
+ ('lazy_blocked', models.BooleanField(default=False)),
+ ('no_sync', models.BooleanField(default=False)),
+ ('no_policy', models.BooleanField(default=False)),
+ ('policy_status', models.CharField(blank=True, default=b'Policy in process', max_length=1024, null=True)),
+ ('policy_code', models.IntegerField(blank=True, default=0, null=True)),
+ ('leaf_model_name', models.CharField(help_text=b'The most specialized model in this chain of inheritance, often defined by a service developer', max_length=1024)),
+ ('backend_need_delete_policy', models.BooleanField(default=False, help_text=b'True if delete model_policy must be run before object can be reaped')),
+ ('xos_managed', models.BooleanField(default=True, help_text=b'True if xos is responsible for creating/deleting this object')),
+ ('backend_handle', models.CharField(blank=True, help_text=b'Handle used by the backend to track this object', max_length=1024, null=True)),
+ ('changed_by_step', models.DateTimeField(blank=True, default=None, help_text=b'Time this model was changed by a sync step', null=True)),
+ ('changed_by_policy', models.DateTimeField(blank=True, default=None, help_text=b'Time this model was changed by a model policy', null=True)),
+ ('serial_number', models.CharField(help_text=b'ONU Serial Number', max_length=254)),
+ ('pon_port_id', models.IntegerField(help_text=b'PON Port on which this ONU is expected to show up')),
+ ('device_id', models.CharField(help_text=b'OLT Device (logical device id) on which this ONU is expected to show up', max_length=54)),
+ ('owner', models.ForeignKey(help_text=b'TtWorkflowDriverService that owns this white list entry', on_delete=django.db.models.deletion.CASCADE, related_name='whitelist_entries', to='tt-workflow-driver.TtWorkflowDriverService')),
+ ],
+ options={
+ 'verbose_name': 'ONU Whitelist',
+ },
+ bases=(models.Model, core.models.xosbase_header.PlModelMixIn),
+ ),
+ migrations.AlterUniqueTogether(
+ name='ttworkflowdriverwhitelistentry',
+ unique_together=set([('owner', 'serial_number')]),
+ ),
+ ]
diff --git a/xos/synchronizer/migrations/__init__.py b/xos/synchronizer/migrations/__init__.py
new file mode 100644
index 0000000..eb28b96
--- /dev/null
+++ b/xos/synchronizer/migrations/__init__.py
@@ -0,0 +1,13 @@
+# Copyright 2017-present Open Networking Foundation
+#
+# 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.
\ No newline at end of file
diff --git a/xos/synchronizer/model_policies/model_policy_tt_workflow_driver_serviceinstance.py b/xos/synchronizer/model_policies/model_policy_tt_workflow_driver_serviceinstance.py
new file mode 100644
index 0000000..95081c8
--- /dev/null
+++ b/xos/synchronizer/model_policies/model_policy_tt_workflow_driver_serviceinstance.py
@@ -0,0 +1,170 @@
+
+# Copyright 2017-present Open Networking Foundation
+#
+# 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.
+
+
+
+from xossynchronizer.model_policies.policy import Policy
+
+import os
+import sys
+
+sync_path = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), ".."))
+sys.path.append(sync_path)
+
+from helpers import TtHelpers
+
+class DeferredException(Exception):
+ pass
+
+class TtWorkflowDriverServiceInstancePolicy(Policy):
+ model_name = "TtWorkflowDriverServiceInstance"
+
+ def handle_create(self, si):
+ self.logger.debug("MODEL_POLICY: handle_create for TtWorkflowDriverServiceInstance %s " % si.id)
+ self.handle_update(si)
+
+ def handle_update(self, si):
+ self.logger.debug("MODEL_POLICY: handle_update for TtWorkflowDriverServiceInstance %s " % (si.id), onu_state=si.onu_state)
+
+ self.process_onu_state(si)
+ self.process_dhcp_state(si)
+
+ self.validate_states(si)
+
+ # handling the subscriber status
+ # It's a combination of all the other states
+ subscriber = self.get_subscriber(si.serial_number)
+ if subscriber:
+ self.update_subscriber(subscriber, si)
+
+ si.save_changed_fields()
+
+ def process_onu_state(self, si):
+ [valid, message] = TtHelpers.validate_onu(self.model_accessor, self.logger, si)
+ if si.onu_state == "AWAITING" or si.onu_state == "ENABLED":
+ si.status_message = message
+ if valid:
+ si.onu_state = "ENABLED"
+ self.update_onu(si.serial_number, "ENABLED")
+ else:
+ si.onu_state = "DISABLED"
+ self.update_onu(si.serial_number, "DISABLED")
+ else: # DISABLED
+ if not valid:
+ si.status_message = message
+ else:
+ si.status_message = "ONU has been disabled"
+ self.update_onu(si.serial_number, "DISABLED")
+
+
+ # The DhcpL2Relay ONOS app generates events that update the fields below.
+ # It only sends events when it processes DHCP packets. It keeps no internal state.
+ def process_dhcp_state(self, si):
+ if si.onu_state in ["AWAITING", "DISABLED"]:
+ si.ip_address = ""
+ si.mac_address = ""
+ si.dhcp_state = "AWAITING"
+
+ # Make sure the object is in a legitimate state
+ # It should be after the above processing steps
+ # However this might still fail if an event has fired in the meantime
+ # Valid states:
+ # ONU | DHCP
+ # ===============================
+ # AWAITING | AWAITING
+ # ENABLED | *
+ # DISABLED | AWAITING
+ def validate_states(self, si):
+ if (si.onu_state == "AWAITING" or si.onu_state == "DISABLED") and si.dhcp_state == "AWAITING":
+ return
+ if si.onu_state == "ENABLED":
+ return
+ self.logger.warning("MODEL_POLICY (validate_states): invalid state combination", onu_state=si.onu_state, dhcp_state=si.dhcp_state)
+
+
+ def update_onu(self, serial_number, admin_state):
+ onu = [onu for onu in self.model_accessor.ONUDevice.objects.all() if onu.serial_number.lower() == serial_number.lower()][0]
+ if onu.admin_state == admin_state:
+ self.logger.debug("MODEL_POLICY: ONUDevice [%s] already has admin_state to %s" % (serial_number, admin_state))
+ else:
+ self.logger.debug("MODEL_POLICY: setting ONUDevice [%s] admin_state to %s" % (serial_number, admin_state))
+ onu.admin_state = admin_state
+ onu.save_changed_fields(always_update_timestamp=True)
+
+ def get_subscriber(self, serial_number):
+ try:
+ return [s for s in self.model_accessor.RCORDSubscriber.objects.all() if s.onu_device.lower() == serial_number.lower()][0]
+ except IndexError:
+ # If the subscriber doesn't exist we don't do anything
+ self.logger.debug("MODEL_POLICY: subscriber does not exists for this SI, doing nothing", onu_device=serial_number)
+ return None
+
+ def update_subscriber_ip(self, subscriber, ip):
+ # TODO check if the subscriber has an IP and update it,
+ # or create a new one
+ try:
+ ip = self.model_accessor.RCORDIpAddress.objects.filter(
+ subscriber_id=subscriber.id,
+ ip=ip
+ )[0]
+ self.logger.debug("MODEL_POLICY: found existing RCORDIpAddress for subscriber", onu_device=subscriber.onu_device, subscriber_status=subscriber.status, ip=ip)
+ ip.save_changed_fields()
+ except IndexError:
+ self.logger.debug("MODEL_POLICY: Creating new RCORDIpAddress for subscriber", onu_device=subscriber.onu_device, subscriber_status=subscriber.status, ip=ip)
+ ip = self.model_accessor.RCORDIpAddress(
+ subscriber_id=subscriber.id,
+ ip=ip,
+ description="DHCP Assigned IP Address"
+ )
+ ip.save()
+
+ def delete_subscriber_ip(self, subscriber, ip):
+ try:
+ ip = self.model_accessor.RCORDIpAddress.objects.filter(
+ subscriber_id=subscriber.id,
+ ip=ip
+ )[0]
+ self.logger.debug("MODEL_POLICY: delete RCORDIpAddress for subscriber", onu_device=subscriber.onu_device, subscriber_status=subscriber.status, ip=ip)
+ ip.delete()
+ except:
+ self.logger.warning("MODEL_POLICY: no RCORDIpAddress object found, cannot delete", ip=ip)
+
+ def update_subscriber(self, subscriber, si):
+ cur_status = subscriber.status
+ # Don't change state if someone has disabled the subscriber
+ if subscriber.status != "disabled":
+ if si.onu_state == "ENABLED":
+ subscriber.status = "enabled"
+ elif si.onu_state == "DISABLED":
+ subscriber.status = "awaiting-auth"
+
+ # NOTE we save the subscriber only if:
+ # - the status has changed
+ # - we get a DHCPACK event
+ if cur_status != subscriber.status or si.dhcp_state == "DHCPACK":
+ self.logger.debug("MODEL_POLICY: updating subscriber", onu_device=subscriber.onu_device, subscriber_status=subscriber.status)
+ if subscriber.status == "awaiting-auth":
+ self.delete_subscriber_ip(subscriber, si.ip_address)
+ subscriber.mac_address = ""
+ elif si.ip_address and si.mac_address:
+ self.update_subscriber_ip(subscriber, si.ip_address)
+ subscriber.mac_address = si.mac_address
+ subscriber.save_changed_fields(always_update_timestamp=True)
+ else:
+ self.logger.debug("MODEL_POLICY: subscriber status has not changed", onu_device=subscriber.onu_device,
+ subscriber_status=subscriber.status)
+
+ def handle_delete(self, si):
+ pass
diff --git a/xos/synchronizer/model_policies/model_policy_tt_workflow_driver_whitelistentry.py b/xos/synchronizer/model_policies/model_policy_tt_workflow_driver_whitelistentry.py
new file mode 100644
index 0000000..b16642a
--- /dev/null
+++ b/xos/synchronizer/model_policies/model_policy_tt_workflow_driver_whitelistentry.py
@@ -0,0 +1,75 @@
+
+# Copyright 2017-present Open Networking Foundation
+#
+# 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.
+
+
+from xossynchronizer.model_policies.policy import Policy
+import os
+import sys
+
+sync_path = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), ".."))
+sys.path.append(sync_path)
+
+from helpers import TtHelpers
+
+class TtWorkflowDriverWhiteListEntryPolicy(Policy):
+ model_name = "TtWorkflowDriverWhiteListEntry"
+
+ def handle_create(self, whitelist):
+ self.handle_update(whitelist)
+
+ # Update the SI if the onu_state has changed.
+ # The SI model policy will take care of updating other state.
+ def validate_onu_state(self, si):
+ [valid, message] = TtHelpers.validate_onu(self.model_accessor, self.logger, si)
+ if valid:
+ si.onu_state = "ENABLED"
+ else:
+ si.onu_state = "DISABLED"
+
+ self.logger.debug(
+ "MODEL_POLICY: activating TtWorkflowDriverServiceInstance because of change in the whitelist", si=si, onu_state=si.onu_state)
+ si.save_changed_fields(always_update_timestamp=True)
+
+ def handle_update(self, whitelist):
+ self.logger.debug("MODEL_POLICY: handle_update for TtWorkflowDriverWhiteListEntry", whitelist=whitelist)
+
+ sis = self.model_accessor.TtWorkflowDriverServiceInstance.objects.all()
+
+ for si in sis:
+
+ if si.serial_number.lower() != whitelist.serial_number.lower():
+ # NOTE we don't care about this SI as it has a different serial number
+ continue
+
+ self.validate_onu_state(si)
+
+ whitelist.backend_need_delete_policy=True
+ whitelist.save_changed_fields()
+
+ def handle_delete(self, whitelist):
+ self.logger.debug("MODEL_POLICY: handle_delete for TtWorkflowDriverWhiteListEntry", serial_number=whitelist.serial_number, pon_port=whitelist.pon_port_id, device=whitelist.device_id)
+
+ # BUG: Sometimes the delete policy is not called, because the reaper deletes
+
+ assert(whitelist.owner)
+
+ sis = self.model_accessor.TtWorkflowDriverServiceInstance.objects.all()
+ sis = [si for si in sis if si.serial_number.lower() == whitelist.serial_number.lower()]
+
+ for si in sis:
+ self.validate_onu_state(si)
+
+ whitelist.backend_need_reap=True
+ whitelist.save_changed_fields()
diff --git a/xos/synchronizer/model_policies/test_model_policy_tt_workflow_driver_serviceinstance.py b/xos/synchronizer/model_policies/test_model_policy_tt_workflow_driver_serviceinstance.py
new file mode 100644
index 0000000..a067779
--- /dev/null
+++ b/xos/synchronizer/model_policies/test_model_policy_tt_workflow_driver_serviceinstance.py
@@ -0,0 +1,270 @@
+
+# Copyright 2017-present Open Networking Foundation
+#
+# 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.
+
+
+import unittest
+from mock import patch, call, Mock, PropertyMock
+
+import os, sys
+
+test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
+
+
+class TestModelPolicyTtWorkflowDriverServiceInstance(unittest.TestCase):
+ def setUp(self):
+
+ self.sys_path_save = sys.path
+
+ config = os.path.join(test_path, "../test_config.yaml")
+ from xosconfig import Config
+ Config.clear()
+ Config.init(config, 'synchronizer-config-schema.yaml')
+
+ from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config
+ mock_modelaccessor_config(test_path, [("tt-workflow-driver", "tt-workflow-driver.xproto"),
+ ("olt-service", "volt.xproto"),
+ ("rcord", "rcord.xproto")])
+
+ import xossynchronizer.modelaccessor
+ import mock_modelaccessor
+ reload(mock_modelaccessor) # in case nose2 loaded it in a previous test
+ reload(xossynchronizer.modelaccessor) # in case nose2 loaded it in a previous test
+
+ from xossynchronizer.modelaccessor import model_accessor
+ from model_policy_tt_workflow_driver_serviceinstance import TtWorkflowDriverServiceInstancePolicy, TtHelpers
+ self.TtHelpers = TtHelpers
+
+ from mock_modelaccessor import MockObjectList
+
+ # import all class names to globals
+ for (k, v) in model_accessor.all_model_classes.items():
+ globals()[k] = v
+
+ # Some of the functions we call have side-effects. For example, creating a VSGServiceInstance may lead to creation of
+ # tags. Ideally, this wouldn't happen, but it does. So make sure we reset the world.
+ model_accessor.reset_all_object_stores()
+
+
+ self.policy = TtWorkflowDriverServiceInstancePolicy(model_accessor=model_accessor)
+ self.si = TtWorkflowDriverServiceInstance()
+ self.si.owner = TtWorkflowDriverService()
+ self.si.serial_number = "BRCM1234"
+
+ def tearDown(self):
+ sys.path = self.sys_path_save
+
+ def test_update_onu(self):
+
+ onu = ONUDevice(
+ serial_number="BRCM1234",
+ admin_state="ENABLED"
+ )
+ with patch.object(ONUDevice.objects, "get_items") as get_onu, \
+ patch.object(onu, "save") as onu_save:
+ get_onu.return_value = [onu]
+
+ self.policy.update_onu("brcm1234", "ENABLED")
+ onu_save.assert_not_called()
+
+ self.policy.update_onu("brcm1234", "DISABLED")
+ self.assertEqual(onu.admin_state, "DISABLED")
+ onu_save.assert_called_with(always_update_timestamp=True, update_fields=['admin_state', 'serial_number', 'updated'])
+
+
+ def test_enable_onu(self):
+ with patch.object(self.TtHelpers, "validate_onu") as validate_onu, \
+ patch.object(self.policy, "update_onu") as update_onu, \
+ patch.object(self.si, "save") as save_si:
+ validate_onu.return_value = [True, "valid onu"]
+
+ self.policy.process_onu_state(self.si)
+
+ update_onu.assert_called_once()
+ update_onu.assert_called_with("BRCM1234", "ENABLED")
+
+ self.assertIn("valid onu", self.si.status_message)
+
+ def test_disable_onu(self):
+ with patch.object(self.TtHelpers, "validate_onu") as validate_onu, \
+ patch.object(self.policy, "update_onu") as update_onu, \
+ patch.object(self.si, "save") as save_si:
+ validate_onu.return_value = [False, "invalid onu"]
+
+ self.policy.process_onu_state(self.si)
+
+ update_onu.assert_called_once()
+ update_onu.assert_called_with("BRCM1234", "DISABLED")
+
+ self.assertIn("invalid onu", self.si.status_message)
+
+ def test_handle_update_validate_onu(self):
+ """
+ Testing that handle_update calls validate_onu with the correct parameters
+ when necessary
+ """
+ with patch.object(self.policy, "process_onu_state") as process_onu_state, \
+ patch.object(self.policy, "update_onu") as update_onu, \
+ patch.object(self.policy, "get_subscriber") as get_subscriber:
+ update_onu.return_value = None
+ get_subscriber.return_value = None
+
+ self.si.onu_state = "AWAITING"
+ self.policy.handle_update(self.si)
+ process_onu_state.assert_called_with(self.si)
+
+ self.si.onu_state = "ENABLED"
+ self.policy.handle_update(self.si)
+ process_onu_state.assert_called_with(self.si)
+
+ self.si.onu_state = "DISABLED"
+ self.policy.handle_update(self.si)
+ process_onu_state.assert_called_with(self.si)
+
+
+ def test_get_subscriber(self):
+
+ sub = RCORDSubscriber(
+ onu_device="BRCM1234"
+ )
+
+ with patch.object(RCORDSubscriber.objects, "get_items") as get_subscribers:
+ get_subscribers.return_value = [sub]
+
+ res = self.policy.get_subscriber("BRCM1234")
+ self.assertEqual(res, sub)
+
+ res = self.policy.get_subscriber("brcm1234")
+ self.assertEqual(res, sub)
+
+ res = self.policy.get_subscriber("foo")
+ self.assertEqual(res, None)
+
+ def test_update_subscriber(self):
+
+ sub = RCORDSubscriber(
+ onu_device="BRCM1234"
+ )
+
+ self.si.status_message = "some content"
+
+ with patch.object(sub, "save") as sub_save:
+ self.si.onu_state = "ENABLED"
+ sub.status = "awaiting-auth"
+ self.policy.update_subscriber(sub, self.si)
+ self.assertEqual(sub.status, "enabled")
+ sub_save.assert_called()
+ sub_save.reset_mock()
+ sub.status = None
+
+ self.si.onu_state = "DISABLED"
+ sub.status = "enabled"
+ self.policy.update_subscriber(sub, self.si)
+ self.assertEqual(sub.status, "awaiting-auth")
+ sub_save.assert_called()
+ sub_save.reset_mock()
+ sub.status = None
+
+ def test_update_subscriber_not(self):
+ sub = RCORDSubscriber(
+ onu_device="BRCM1234"
+ )
+
+ with patch.object(sub, "save") as sub_save:
+ sub.status = "enabled"
+ self.si.onu_state = "ENABLED"
+ self.policy.update_subscriber(sub, self.si)
+ sub_save.assert_not_called()
+
+ sub.status = "awaiting-auth"
+ self.si.onu_state = "DISABLED"
+ self.policy.update_subscriber(sub, self.si)
+ sub_save.assert_not_called()
+
+ def test_update_subscriber_dhcp_with_exiting_ip(self):
+ sub = RCORDSubscriber(
+ id=10,
+ onu_device="BRCM1234"
+ )
+
+ ip = RCORDIpAddress(
+ subscriber_id=sub.id,
+ ip='10.11.2.23'
+ )
+
+ self.si.dhcp_state = "DHCPACK"
+ self.si.ip_address = "10.11.2.23"
+ self.si.mac_address = "4321"
+
+ with patch.object(sub, "save") as sub_save, \
+ patch.object(RCORDIpAddress.objects, "get_items") as get_ips, \
+ patch.object(ip, "save_changed_fields") as ip_mock:
+
+ get_ips.return_value = [ip]
+ ip_mock.return_value = []
+
+ self.policy.update_subscriber(sub, self.si)
+ sub_save.assert_called()
+ self.assertEqual(sub.mac_address, self.si.mac_address)
+
+ ip_mock.assert_called_with()
+
+ def test_update_subscriber_dhcp_with_new_ip(self):
+ sub = RCORDSubscriber(
+ id=10,
+ onu_device="BRCM1234"
+ )
+
+ self.si.dhcp_state = "DHCPACK"
+ self.si.ip_address = "10.11.2.23"
+ self.si.mac_address = "4321"
+
+ with patch.object(sub, "save") as sub_save, \
+ patch.object(RCORDIpAddress, "save", autospec=True) as ip_mock:
+
+ ip_mock.return_value = []
+
+ self.policy.update_subscriber(sub, self.si)
+ sub_save.assert_called()
+ self.assertEqual(sub.mac_address, self.si.mac_address)
+
+ saved_ip = ip_mock.call_args[0][0]
+ self.assertEqual(saved_ip.ip, self.si.ip_address)
+ self.assertEqual(saved_ip.subscriber_id, sub.id)
+ self.assertEqual(saved_ip.description, "DHCP Assigned IP Address")
+
+ def test_handle_update_subscriber(self):
+ self.si.onu_state = "DISABLED"
+
+ sub = RCORDSubscriber(
+ onu_device="BRCM1234"
+ )
+
+ with patch.object(self.policy, "get_subscriber") as get_subscriber, \
+ patch.object(self.policy, "update_onu") as update_onu, \
+ patch.object(self.policy, "update_subscriber") as update_subscriber:
+
+ get_subscriber.return_value = None
+ self.policy.handle_update(self.si)
+ update_onu.assert_called_with(sub.onu_device, "DISABLED");
+ self.assertEqual(update_subscriber.call_count, 0)
+
+ get_subscriber.return_value = sub
+ self.policy.handle_update(self.si)
+ update_subscriber.assert_called_with(sub, self.si)
+
+
+if __name__ == '__main__':
+ unittest.main()
+
diff --git a/xos/synchronizer/model_policies/test_model_policy_tt_workflow_driver_whitelistentry.py b/xos/synchronizer/model_policies/test_model_policy_tt_workflow_driver_whitelistentry.py
new file mode 100644
index 0000000..bee8971
--- /dev/null
+++ b/xos/synchronizer/model_policies/test_model_policy_tt_workflow_driver_whitelistentry.py
@@ -0,0 +1,119 @@
+
+# Copyright 2017-present Open Networking Foundation
+#
+# 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.
+
+
+import unittest
+from mock import patch, call, Mock, PropertyMock
+
+import os, sys
+
+test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
+
+class TestModelPolicyTtWorkflowDriverWhiteListEntry(unittest.TestCase):
+ def setUp(self):
+ self.sys_path_save = sys.path
+
+ config = os.path.join(test_path, "../test_config.yaml")
+ from xosconfig import Config
+ Config.clear()
+ Config.init(config, 'synchronizer-config-schema.yaml')
+
+ from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config
+ mock_modelaccessor_config(test_path, [("tt-workflow-driver", "tt-workflow-driver.xproto"),
+ ("olt-service", "volt.xproto"),
+ ("rcord", "rcord.xproto")])
+
+ import xossynchronizer.modelaccessor
+ import mock_modelaccessor
+ reload(mock_modelaccessor) # in case nose2 loaded it in a previous test
+ reload(xossynchronizer.modelaccessor) # in case nose2 loaded it in a previous test
+
+ from xossynchronizer.modelaccessor import model_accessor
+ from model_policy_tt_workflow_driver_whitelistentry import TtWorkflowDriverWhiteListEntryPolicy, TtHelpers
+ self.TtHelpers = TtHelpers
+
+ from mock_modelaccessor import MockObjectList
+ self.MockObjectList = MockObjectList
+
+ # import all class names to globals
+ for (k, v) in model_accessor.all_model_classes.items():
+ globals()[k] = v
+
+ # Some of the functions we call have side-effects. For example, creating a VSGServiceInstance may lead to creation of
+ # tags. Ideally, this wouldn't happen, but it does. So make sure we reset the world.
+ model_accessor.reset_all_object_stores()
+
+ self.policy = TtWorkflowDriverWhiteListEntryPolicy(model_accessor=model_accessor)
+
+ self.service = TtWorkflowDriverService()
+
+
+ def tearDown(self):
+ sys.path = self.sys_path_save
+ self.service = None
+
+ def test_enable_onu(self):
+ si = TtWorkflowDriverServiceInstance(serial_number="BRCM333", owner_id=self.service.id, valid="invalid")
+ with patch.object(self.TtHelpers, "validate_onu") as validate_onu, \
+ patch.object(si, "save") as save_si:
+ validate_onu.return_value = [True, "valid onu"]
+
+ self.policy.validate_onu_state(si)
+
+ save_si.assert_called_once()
+ save_si.assert_called_with(always_update_timestamp=True, update_fields=['onu_state', 'serial_number', 'updated'])
+
+ def test_disable_onu(self):
+ si = TtWorkflowDriverServiceInstance(serial_number="BRCM333", owner_id=self.service.id, valid="invalid")
+ with patch.object(self.TtHelpers, "validate_onu") as validate_onu, \
+ patch.object(si, "save") as save_si:
+ validate_onu.return_value = [False, "invalid onu"]
+
+ self.policy.validate_onu_state(si)
+
+ save_si.assert_called_once()
+ save_si.assert_called_with(always_update_timestamp=True, update_fields=['onu_state', 'serial_number', 'updated'])
+
+ def test_whitelist_update(self):
+ si = TtWorkflowDriverServiceInstance(serial_number="BRCM333", owner_id=self.service.id)
+ wle = TtWorkflowDriverWhiteListEntry(serial_number="brcm333", owner_id=self.service.id, owner=self.service)
+ with patch.object(TtWorkflowDriverServiceInstance.objects, "get_items") as oss_si_items, \
+ patch.object(self.policy, "validate_onu_state") as validate_onu_state, \
+ patch.object(wle, "save") as wle_save:
+ oss_si_items.return_value = [si]
+
+
+ self.policy.handle_update(wle)
+
+ validate_onu_state.assert_called_with(si)
+ self.assertTrue(wle.backend_need_delete_policy)
+ wle_save.assert_called_with(always_update_timestamp=False, update_fields=['backend_need_delete_policy', 'owner', 'serial_number'])
+
+ def test_whitelist_delete(self):
+ si = TtWorkflowDriverServiceInstance(serial_number="BRCM333", owner_id=self.service.id)
+ wle = TtWorkflowDriverWhiteListEntry(serial_number="brcm333", owner_id=self.service.id, owner=self.service)
+ with patch.object(TtWorkflowDriverServiceInstance.objects, "get_items") as oss_si_items, \
+ patch.object(self.policy, "validate_onu_state") as validate_onu_state, \
+ patch.object(wle, "save") as wle_save:
+ oss_si_items.return_value = [si]
+
+ self.policy.handle_delete(wle)
+
+ validate_onu_state.assert_called_with(si)
+ self.assertTrue(wle.backend_need_reap)
+ wle_save.assert_called_with(always_update_timestamp=False, update_fields=['backend_need_reap', 'owner', 'serial_number'])
+if __name__ == '__main__':
+ unittest.main()
+
diff --git a/xos/synchronizer/models/tt-workflow-driver.xproto b/xos/synchronizer/models/tt-workflow-driver.xproto
new file mode 100644
index 0000000..2b6c889
--- /dev/null
+++ b/xos/synchronizer/models/tt-workflow-driver.xproto
@@ -0,0 +1,69 @@
+option name = "tt-workflow-driver";
+option app_label = "tt-workflow-driver";
+
+message TtWorkflowDriverService (Service){
+ option verbose_name = "TtWorkflowDriver Service";
+ option kind = "control";
+ option description = "Service that manages the Turk Telekom Subscriber workflow";
+}
+
+message TtWorkflowDriverServiceInstance (ServiceInstance){
+ option owner_class_name = "TtWorkflowDriverService";
+ option verbose_name = "TtWorkflowDriver Service Instance";
+ option description = "Workflow settings for a particular ONU Device";
+
+ required string serial_number = 2 [
+ help_text = "Serial number of ONU",
+ max_length = 254,
+ tosca_key=True,
+ unique = True];
+ required string of_dpid = 4 [
+ help_text = "OLT Openflow ID",
+ max_length = 254];
+ required int32 uni_port_id = 5 [
+ help_text = "ONU UNI port ID"];
+ required string onu_state = 6 [
+ help_text = "ONU administrative state",
+ choices = "(('AWAITING', 'Awaiting'), ('ENABLED', 'Enabled'), ('DISABLED', 'Disabled'))",
+ default = "AWAITING",
+ feedback_state = True,
+ max_length = 254];
+ optional string status_message = 7 [
+ help_text = "Status text of current state machine state",
+ default = "",
+ max_length = 254];
+ required string dhcp_state = 8 [
+ max_length = 254,
+ default = "AWAITING",
+ choices = "(('AWAITING', 'Awaiting'), ('DHCPDISCOVER', 'DHCPDISCOVER'), ('DHCPACK', 'DHCPACK'), ('DHCPREQUEST', 'DHCPREQUEST'))",
+ feedback_state = True];
+ optional string ip_address = 9 [
+ help_text = "Management IP address, learned from DHCP",
+ feedback_state = True,
+ max_length = 20];
+ optional string mac_address = 10 [
+ help_text = "Management MAC address, leanred from DHCP",
+ feedback_state = True,
+ max_length = 20];
+}
+
+message TtWorkflowDriverWhiteListEntry (XOSBase) {
+ option verbose_name = "ONU Whitelist";
+ option plural = "ttworkflowdriverwhitelistentries";
+ option description = "White list entry that permits an ONU with a particular serial number on a particular OLT and PON";
+
+ required manytoone owner->TtWorkflowDriverService:whitelist_entries = 1:1001 [
+ help_text = "TtWorkflowDriverService that owns this white list entry",
+ db_index = True,
+ tosca_key = True];
+ required string serial_number = 2 [
+ help_text = "ONU Serial Number",
+ max_length = 254,
+ tosca_key = True,
+ unique_with = "owner"];
+ required int32 pon_port_id = 3 [
+ help_text = "PON Port on which this ONU is expected to show up"];
+ required string device_id = 4 [
+ help_text = "OLT Device (logical device id) on which this ONU is expected to show up",
+ max_length = 54];
+}
diff --git a/xos/synchronizer/test_config.yaml b/xos/synchronizer/test_config.yaml
new file mode 100644
index 0000000..3f33a21
--- /dev/null
+++ b/xos/synchronizer/test_config.yaml
@@ -0,0 +1,31 @@
+
+# Copyright 2017-present Open Networking Foundation
+#
+# 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.
+
+
+name: test-TtWorkflowDriverService-config
+accessor:
+ username: xosadmin@opencord.org
+ password: "sample"
+ kind: "testframework"
+logging:
+ version: 1
+ handlers:
+ console:
+ class: logging.StreamHandler
+ loggers:
+ 'multistructlog':
+ handlers:
+ - console
+# level: DEBUG
diff --git a/xos/synchronizer/test_helpers.py b/xos/synchronizer/test_helpers.py
new file mode 100644
index 0000000..e46cbc9
--- /dev/null
+++ b/xos/synchronizer/test_helpers.py
@@ -0,0 +1,163 @@
+# Copyright 2017-present Open Networking Foundation
+#
+# 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.
+
+import unittest
+from mock import patch, call, Mock, PropertyMock
+import json
+
+import os, sys
+
+test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
+
+
+class TestTtHelpers(unittest.TestCase):
+
+ def setUp(self):
+
+ self.sys_path_save = sys.path
+
+ # Setting up the config module
+ from xosconfig import Config
+ config = os.path.join(test_path, "test_config.yaml")
+ Config.clear()
+ Config.init(config, "synchronizer-config-schema.yaml")
+ # END Setting up the config module
+
+ from multistructlog import create_logger
+ self.log = create_logger(Config().get('logging'))
+
+ from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config
+ mock_modelaccessor_config(test_path, [("tt-workflow-driver", "tt-workflow-driver.xproto"),
+ ("olt-service", "volt.xproto"),
+ ("rcord", "rcord.xproto")])
+
+ import xossynchronizer.modelaccessor
+ import mock_modelaccessor
+ reload(mock_modelaccessor) # in case nose2 loaded it in a previous test
+ reload(xossynchronizer.modelaccessor) # in case nose2 loaded it in a previous test
+
+ from xossynchronizer.modelaccessor import model_accessor
+ from helpers import TtHelpers
+
+ # import all class names to globals
+ for (k, v) in model_accessor.all_model_classes.items():
+ globals()[k] = v
+
+ self.helpers = TtHelpers
+ self.model_accessor = model_accessor
+
+ self._volt = VOLTService()
+ self._volt.id = 1
+
+ self.volt = Service()
+ self.volt.id = 1
+ self.volt.name = "vOLT"
+ self.volt.leaf_model = self._volt
+
+ self.pon_port = PONPort()
+ self.pon_port.port_no = 1234
+
+ self.onu = ONUDevice()
+ self.onu.pon_port = self.pon_port
+ self.onu.serial_number = "BRCM1234"
+
+ self.tt_si = TtWorkflowDriverServiceInstance(
+ serial_number="BRCM1234",
+ owner=self.volt,
+ owner_id=self.volt.id,
+ of_dpid="of:1234"
+ )
+
+ self.whitelist_entry = TtWorkflowDriverWhiteListEntry(
+ serial_number="BRCM1234",
+ owner=self.volt,
+ owner_id=self.volt.id,
+ pon_port_id=1234,
+ device_id="of:1234"
+ )
+
+
+ def tearDown(self):
+ sys.path = self.sys_path_save
+
+ def test_not_in_whitelist(self):
+
+ with patch.object(TtWorkflowDriverWhiteListEntry.objects, "get_items") as whitelist_mock:
+ whitelist_mock.return_value = []
+
+ [res, message] = self.helpers.validate_onu(self.model_accessor, self.log, self.tt_si)
+
+ self.assertFalse(res)
+ self.assertEqual(message, "ONU not found in whitelist")
+
+ def test_wrong_location_port(self):
+ self.pon_port.port_no = 666
+ with patch.object(TtWorkflowDriverWhiteListEntry.objects, "get_items") as whitelist_mock, \
+ patch.object(ONUDevice.objects, "get_items") as onu_mock:
+ whitelist_mock.return_value = [self.whitelist_entry]
+ onu_mock.return_value = [self.onu]
+
+ [res, message] = self.helpers.validate_onu(self.model_accessor, self.log, self.tt_si)
+
+ self.assertFalse(res)
+ self.assertEqual(message, "ONU activated in wrong location")
+
+ def test_wrong_location_device(self):
+ self.tt_si.of_dpid = 666
+ with patch.object(TtWorkflowDriverWhiteListEntry.objects, "get_items") as whitelist_mock, \
+ patch.object(ONUDevice.objects, "get_items") as onu_mock:
+ whitelist_mock.return_value = [self.whitelist_entry]
+ onu_mock.return_value = [self.onu]
+
+ [res, message] = self.helpers.validate_onu(self.model_accessor, self.log, self.tt_si)
+
+ self.assertFalse(res)
+ self.assertEqual(message, "ONU activated in wrong location")
+
+ def test_deferred_validation(self):
+ with patch.object(TtWorkflowDriverWhiteListEntry.objects, "get_items") as whitelist_mock, \
+ patch.object(ONUDevice.objects, "get_items") as onu_mock:
+ whitelist_mock.return_value = [self.whitelist_entry]
+ onu_mock.return_value = []
+
+ with self.assertRaises(Exception) as e:
+ self.helpers.validate_onu(self.model_accessor, self.log, self.tt_si)
+
+ self.assertEqual(e.exception.message, "ONU device %s is not know to XOS yet" % self.tt_si.serial_number)
+
+ def test_validating_onu(self):
+ with patch.object(TtWorkflowDriverWhiteListEntry.objects, "get_items") as whitelist_mock, \
+ patch.object(ONUDevice.objects, "get_items") as onu_mock:
+ whitelist_mock.return_value = [self.whitelist_entry]
+ onu_mock.return_value = [self.onu]
+
+ [res, message] = self.helpers.validate_onu(self.model_accessor, self.log, self.tt_si)
+
+ self.assertTrue(res)
+ self.assertEqual(message, "ONU has been validated")
+
+ def test_validating_onu_lowercase(self):
+ self.whitelist_entry.serial_number = "brcm1234"
+ with patch.object(TtWorkflowDriverWhiteListEntry.objects, "get_items") as whitelist_mock, \
+ patch.object(ONUDevice.objects, "get_items") as onu_mock:
+ whitelist_mock.return_value = [self.whitelist_entry]
+ onu_mock.return_value = [self.onu]
+
+ [res, message] = self.helpers.validate_onu(self.model_accessor, self.log, self.tt_si)
+
+ self.assertTrue(res)
+ self.assertEqual(message, "ONU has been validated")
+
+if __name__ == '__main__':
+ unittest.main()
\ No newline at end of file
diff --git a/xos/synchronizer/tt-workflow-driver-synchronizer.py b/xos/synchronizer/tt-workflow-driver-synchronizer.py
new file mode 100644
index 0000000..9b420e7
--- /dev/null
+++ b/xos/synchronizer/tt-workflow-driver-synchronizer.py
@@ -0,0 +1,34 @@
+
+# Copyright 2017-present Open Networking Foundation
+#
+# 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.
+
+
+#!/usr/bin/env python
+
+# This imports and runs ../../xos-observer.py
+
+import os
+from xossynchronizer import Synchronizer
+from xosconfig import Config
+
+
+base_config_file = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + '/config.yaml')
+mounted_config_file = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + '/mounted_config.yaml')
+
+if os.path.isfile(mounted_config_file):
+ Config.init(base_config_file, 'synchronizer-config-schema.yaml', mounted_config_file)
+else:
+ Config.init(base_config_file, 'synchronizer-config-schema.yaml')
+
+Synchronizer().run()
\ No newline at end of file
diff --git a/xos/unittest.cfg b/xos/unittest.cfg
new file mode 100644
index 0000000..3db2d53
--- /dev/null
+++ b/xos/unittest.cfg
@@ -0,0 +1,14 @@
+[unittest]
+plugins=nose2.plugins.junitxml
+code-directories=synchronizer
+ model_policies
+ steps
+ pull_steps
+ event_steps
+
+[coverage]
+always-on = True
+coverage = synchronizer
+coverage-report = term
+coverage-report = html
+coverage-report = xml