Add Fabric code 97/1397/2
authorxinhuili <lxinhui@vmware.com>
Wed, 14 Aug 2019 06:02:04 +0000 (14:02 +0800)
committerxinhuili <lxinhui@vmware.com>
Wed, 14 Aug 2019 06:10:16 +0000 (06:10 +0000)
This patch is to add revised fabric code.

Signed-off-by: XINHUI LI <lxinhui@vmware.com>
wq!
Change-Id: I669951c0424d159473ec181fca7cab7bf697dcf6

26 files changed:
src/fabric/.gitignore [new file with mode: 0644]
src/fabric/.gitreview [new file with mode: 0644]
src/fabric/Dockerfile.synchronizer [new file with mode: 0644]
src/fabric/LICENSE.txt [new file with mode: 0644]
src/fabric/VERSION [new file with mode: 0644]
src/fabric/config/ciab-fabric-xconnect.json [new file with mode: 0644]
src/fabric/config/network-cfg-onlab-develop.json [new file with mode: 0644]
src/fabric/config/network-cfg-quickstart.json [new file with mode: 0644]
src/fabric/docs/README.md [new file with mode: 0644]
src/fabric/samples/fabric-config.yaml [new file with mode: 0644]
src/fabric/samples/nodes.yaml [new file with mode: 0644]
src/fabric/samples/service.yaml [new file with mode: 0644]
src/fabric/xos/synchronizer/config.yaml [new file with mode: 0644]
src/fabric/xos/synchronizer/event_steps/kubernetes_event.py [new file with mode: 0644]
src/fabric/xos/synchronizer/event_steps/test_kubernetes_event.py [new file with mode: 0644]
src/fabric/xos/synchronizer/fabric-synchronizer.py [new file with mode: 0755]
src/fabric/xos/synchronizer/model_policies/model_policy_compute_nodes.py [new file with mode: 0644]
src/fabric/xos/synchronizer/model_policies/test_model_policy_compute_node.py [new file with mode: 0644]
src/fabric/xos/synchronizer/models/fabric.xproto [new file with mode: 0644]
src/fabric/xos/synchronizer/steps/helpers.py [new file with mode: 0644]
src/fabric/xos/synchronizer/steps/sync_fabric_port.py [new file with mode: 0644]
src/fabric/xos/synchronizer/steps/sync_fabric_switch.py [new file with mode: 0644]
src/fabric/xos/synchronizer/steps/test_sync_fabric_port.py [new file with mode: 0644]
src/fabric/xos/synchronizer/steps/test_sync_fabric_switch.py [new file with mode: 0644]
src/fabric/xos/synchronizer/test_config.yaml [new file with mode: 0644]
src/fabric/xos/unittest.cfg [new file with mode: 0644]

diff --git a/src/fabric/.gitignore b/src/fabric/.gitignore
new file mode 100644 (file)
index 0000000..0f03cd1
--- /dev/null
@@ -0,0 +1,8 @@
+.idea
+*.pyc
+
+.coverage
+htmlcov
+coverage.xml
+nose2-junit.xml
+
diff --git a/src/fabric/.gitreview b/src/fabric/.gitreview
new file mode 100644 (file)
index 0000000..6604584
--- /dev/null
@@ -0,0 +1,5 @@
+[gerrit]
+host=gerrit.opencord.org
+port=29418
+project=fabric.git
+defaultremote=origin
diff --git a/src/fabric/Dockerfile.synchronizer b/src/fabric/Dockerfile.synchronizer
new file mode 100644 (file)
index 0000000..d276bbd
--- /dev/null
@@ -0,0 +1,54 @@
+# 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/fabric-synchronizer:candidate -f Dockerfile.synchronizer .
+
+# xosproject/fabric-synchronizer
+FROM xosproject/xos-synchronizer-base:2.1.25
+
+COPY xos/synchronizer /opt/xos/synchronizers/fabric
+COPY VERSION /opt/xos/synchronizers/fabric/
+
+WORKDIR "/opt/xos/synchronizers/fabric"
+
+# Label image
+ARG org_label_schema_schema_version=1.0
+ARG org_label_schema_name=fabric-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/fabric/fabric-synchronizer.py"]
diff --git a/src/fabric/LICENSE.txt b/src/fabric/LICENSE.txt
new file mode 100644 (file)
index 0000000..d9d9d30
--- /dev/null
@@ -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/src/fabric/VERSION b/src/fabric/VERSION
new file mode 100644 (file)
index 0000000..399088b
--- /dev/null
@@ -0,0 +1 @@
+2.1.6
diff --git a/src/fabric/config/ciab-fabric-xconnect.json b/src/fabric/config/ciab-fabric-xconnect.json
new file mode 100644 (file)
index 0000000..df1ee86
--- /dev/null
@@ -0,0 +1,15 @@
+{
+    "apps": {
+        "org.onosproject.segmentrouting": {
+            "xconnect": {
+                "of:0000cc37ab000011": [
+                    {
+                        "vlan": 222,
+                        "ports": [1, 2],
+                        "name": "test client"
+                    }
+                ]
+            }
+        }
+    }
+}
diff --git a/src/fabric/config/network-cfg-onlab-develop.json b/src/fabric/config/network-cfg-onlab-develop.json
new file mode 100644 (file)
index 0000000..9b7c022
--- /dev/null
@@ -0,0 +1,153 @@
+{
+    "ports" : {
+        "of:0000cc37ab6180ca/5" : {
+            "interfaces" : [
+                {
+                    "ips" : [ "10.6.1.254/24" ]
+                }
+            ]
+        },
+        "of:0000cc37ab6182d2/5" : {
+            "interfaces" : [
+                {
+                    "ips" : [ "10.6.2.254/24" ]
+                }
+            ]
+        },
+        "of:0000cc37ab6182d2/32" : {
+            "interfaces" : [
+                {
+                    "name" : "internet-router",
+                    "ips" : [ "10.231.254.202/30" ],
+                    "mac" : "00:16:3e:4b:5a:04"
+                }
+            ],
+            "pimInterface" : {
+                "interfaceName" : "internet-router",
+                "enabled" : true,
+                "helloInterval" : 1,
+                "holdTime" : 3,
+                "propagationDelay" : 500,
+                "overrideInterval" : 2500
+             }
+        }
+    },
+    "devices" : {
+        "of:0000cc37ab6180ca" : {
+            "segmentrouting" : {
+                "name" : "Leaf-R1",
+                "nodeSid" : 101,
+                "routerIp" : "10.6.0.8",
+                "routerMac" : "00:00:00:00:01:80",
+                "isEdgeRouter" : true,
+                "adjacencySids" : []
+            }
+        },
+        "of:0000cc37ab6182d2" : {
+            "segmentrouting" : {
+                "name" : "Leaf-R2",
+                "nodeSid" : 102,
+                "routerIp" : "10.6.0.11",
+                "routerMac" : "00:00:00:00:02:80",
+                "isEdgeRouter" : true,
+                "adjacencySids" : []
+            }
+        },
+        "of:0000cc37ab618048" : {
+            "segmentrouting" : {
+                "name" : "Spine-R1",
+                "nodeSid" : 103,
+                "routerIp" : "10.6.0.18",
+                "routerMac" : "00:00:01:00:11:80",
+                "isEdgeRouter" : false,
+                "adjacencySids" : []
+            }
+        },
+        "of:0000cc37ab617ec2" : {
+            "segmentrouting" : {
+                "name" : "Spine-R2",
+                "nodeSid" : 104,
+                "routerIp" : "10.6.0.17",
+                "routerMac" : "00:00:01:00:22:80",
+                "isEdgeRouter" : false,
+                "adjacencySids" : []
+            }
+        }
+    },
+    "links" : {
+        "of:0000cc37ab6180ca/1-of:0000cc37ab618048/1" : {
+            "basic" : {}
+        },
+        "of:0000cc37ab6180ca/3-of:0000cc37ab617ec2/1" : {
+            "basic" : {}
+        },
+        "of:0000cc37ab6182d2/1-of:0000cc37ab618048/3" : {
+            "basic" : {}
+        },
+        "of:0000cc37ab6182d2/3-of:0000cc37ab617ec2/3" : {
+            "basic" : {}
+        },
+        "of:0000cc37ab618048/1-of:0000cc37ab6180ca/1" : {
+            "basic" : {}
+        },
+        "of:0000cc37ab617ec2/1-of:0000cc37ab6180ca/3" : {
+            "basic" : {}
+        },
+        "of:0000cc37ab618048/3-of:0000cc37ab6182d2/1" : {
+            "basic" : {}
+        },
+        "of:0000cc37ab617ec2/3-of:0000cc37ab6182d2/3" : {
+            "basic" : {}
+        }
+    },
+    "apps" : {
+        "org.onosproject.core" : {
+            "core" : {
+                "linkDiscoveryMode" : "STRICT"
+            },
+            "multicast": {
+                "ingressVlan": "None",
+                "egressVlan": "None"
+            }
+        },
+        "org.onosproject.segmentrouting" : {
+            "segmentrouting" : {
+                "vRouterMacs" : [
+                    "a4:23:05:34:56:78", "a4:23:05:34:56:79"
+                ],
+                "vRouterId" : "of:0000cc37ab6182d2",
+                "suppressSubnet" : [
+                    "of:0000cc37ab6182d2/31", "of:0000cc37ab6182d2/32"
+                ],
+                "suppressHostByProvider" : [
+                    "org.onosproject.provider.host"
+                ],
+                "suppressHostByPort" : [
+                    "of:0000cc37ab6182d2/31", "of:0000cc37ab6182d2/32"
+                ]
+            }
+        },
+        "org.onosproject.router" : {
+            "router" : {
+                "controlPlaneConnectPoint" : "of:0000cc37ab6182d2/31",
+                "ospfEnabled" : "true",
+                "pimEnabled" : "true",
+                "interfaces" : [ "internet-router" ]
+            }
+        }
+    },
+    "hosts" : {
+        "00:02:c9:1e:b4:60/None" : {
+            "basic": {
+                "ips": ["10.6.2.1"],
+                "location": "of:0000cc37ab6182d2/5"
+            }
+        },
+        "00:02:c9:1e:b1:20/None" : {
+            "basic": {
+                "ips": ["10.6.1.1"],
+                "location": "of:0000cc37ab6180ca/5"
+            }
+        }
+    }
+}
diff --git a/src/fabric/config/network-cfg-quickstart.json b/src/fabric/config/network-cfg-quickstart.json
new file mode 100644 (file)
index 0000000..8609ef8
--- /dev/null
@@ -0,0 +1,100 @@
+{
+    "ports" : {
+        "of:0000000000000001/1" : {
+            "interfaces" : [
+                {
+                    "ips" : [ "10.6.1.0/24" ]
+                }
+            ]
+        },
+        "of:0000000000000001/2" : {
+            "interfaces" : [
+                {
+                    "ips" : [ "10.6.1.0/24" ]
+                }
+            ]
+        },
+        "of:0000000000000002/3" : {
+            "interfaces" : [
+                {
+                    "ips" : [ "10.6.2.0/24" ]
+                }
+            ]
+        },
+        "of:0000000000000002/4" : {
+            "interfaces" : [
+                {
+                    "ips" : [ "10.6.2.0/24" ]
+                }
+            ]
+        }
+    },
+    "devices" : {
+        "of:0000000000000001" : {
+            "segmentrouting" : {
+                "name" : "Leaf-R1",
+                "nodeSid" : 101,
+                "routerIp" : "10.6.1.254",
+                "routerMac" : "00:00:00:00:01:80",
+                "isEdgeRouter" : true,
+                "adjacencySids" : []
+            }
+        },
+        "of:0000000000000002" : {
+            "segmentrouting" : {
+                "name" : "Leaf-R2",
+                "nodeSid" : 102,
+                "routerIp" : "10.6.2.254",
+                "routerMac" : "00:00:00:00:02:80",
+                "isEdgeRouter" : true,
+                "adjacencySids" : []
+            }
+        },
+        "of:0000000000000191" : {
+            "segmentrouting" : {
+                "name" : "Spine-R1",
+                "nodeSid" : 103,
+                "routerIp" : "192.168.0.11",
+                "routerMac" : "00:00:01:00:11:80",
+                "isEdgeRouter" : false,
+                "adjacencySids" : []
+            }
+        },
+        "of:0000000000000192" : {
+            "segmentrouting" : {
+                "name" : "Spine-R2",
+                "nodeSid" : 104,
+                "routerIp" : "192.168.0.22",
+                "routerMac" : "00:00:01:00:22:80",
+                "isEdgeRouter" : false,
+                "adjacencySids" : []
+            }
+        }
+    },
+    "hosts" : {
+        "00:00:00:00:00:01/-1" : {
+            "basic": {
+                "ips": ["10.6.1.1"],
+                "location": "of:0000000000000001/1"
+            }
+        },
+        "00:00:00:00:00:02/-1" : {
+            "basic": {
+                "ips": ["10.6.1.2"],
+                "location": "of:0000000000000001/2"
+            }
+        },
+        "00:00:00:00:00:03/-1" : {
+            "basic": {
+                "ips": ["10.6.2.1"],
+                "location": "of:0000000000000002/3"
+            }
+        },
+        "00:00:00:00:00:04/-1" : {
+            "basic": {
+                "ips": ["10.6.2.2"],
+                "location": "of:0000000000000002/4"
+            }
+        }
+    }
+}
diff --git a/src/fabric/docs/README.md b/src/fabric/docs/README.md
new file mode 100644 (file)
index 0000000..303ac99
--- /dev/null
@@ -0,0 +1,181 @@
+# Fabric service
+
+The Fabric service is responsible to configure the fabric switches in ONOS.
+
+At this point we assume you followed the
+[Fabric setup](../fabric-setup.md) guide and your physical infrastructure
+is configured and ready to go.
+
+## How to configure fabric switches
+
+Here is an example of how to configure a `Switch` a `Port` and an `Interface`
+on your fabric.
+
+```yaml
+tosca_definitions_version: tosca_simple_yaml_1_0
+imports:
+  - custom_types/switch.yaml
+  - custom_types/switchport.yaml
+  - custom_types/portinterface.yaml
+description: Configures fabric switches and related ports
+topology_template:
+  node_templates:
+    switch#leaf1:
+      type: tosca.nodes.Switch
+      properties:
+        driver: ofdpa3 # The driver used by the SDN controller
+        ipv4Loopback: 192.168.0.201 # Fabric loopback interface
+        ipv4NodeSid: 17 # The MPLS label used by the switch [17 - 1048576]
+        isEdgeRouter: True # Whether is a leaf (True) or a spine (False)
+        name: leaf1
+        ofId: of:0000000000000001 # The unique OpenFlow ID of the fabric switch
+        routerMac: 00:00:02:01:06:01 # MAC address of the fabric switch used for all interfaces
+
+    # Setup a fabric switch port
+    port#port1:
+      type: tosca.nodes.SwitchPort
+      properties:
+        portId: 1 # The unique port OpenFlow port ID
+      requirements:
+        - switch:
+            node: switch#leaf1
+            relationship: tosca.relationships.BelongsToOne
+
+    # Setup a fabric switch port interface
+    interface#interface1:
+      type: tosca.nodes.PortInterface
+      properties:
+        ips: 192.168.0.254/24 # The interface IP address (xxx.yyy.www.zzz/nm)
+        name: port1
+      requirements:
+        - port:
+            node: port#port1
+            relationship: tosca.relationships.BelongsToOne
+```
+
+## How to attach Compute Nodes to the Fabric
+
+Assuming that you have the above mentioned fabric configuration
+and you want to attach a two compute nodes to the fabric,
+assuming that that:
+
+- `node1.cord.lab` has ip `10.6.1.17` on interface `fabricbridge` and it is attached to port `17` on the switch `leaf1` (and has a route like `10.6.2.0/24 via 10.6.1.254 dev fabricbridge` or the default route is pointing to the fabric)
+- `node2.cord.lab` has ip `10.6.2.18` on interface `fabricbridge` and it is attached to port `18` on the switch `leaf1` (and has a route like `10.6.1.0/24 via 10.6.2.254 dev fabricbridge` or the default route is pointing to the fabric)
+
+you can use the following TOSCA recipe to:
+
+- add the Nodes to XOS
+- add the two Ports to the Switch
+- associate each Node to a Port
+
+```yaml
+tosca_definitions_version: tosca_simple_yaml_1_0
+description: Set up Fabric service and attach it to ONOS (note that onos-service needs to be loaded)
+imports:
+  - custom_types/node.yaml
+  - custom_types/nodetoswitchport.yaml
+  - custom_types/switch.yaml
+  - custom_types/switchport.yaml
+  - custom_types/deployment.yaml
+  - custom_types/site.yaml
+  - custom_types/sitedeployment.yaml
+
+topology_template:
+  node_templates:
+
+    # nodes
+    node#node1:
+        type: tosca.nodes.Node
+        properties:
+            dataPlaneIntf: fabricbridge
+            dataPlaneIp: 10.6.1.17/24
+            name: node1.cord.lab
+        requirements:
+            - site_deployment:
+                node: site_deployment
+                relationship: tosca.relationships.BelongsToOne
+    
+    node#node2:
+        type: tosca.nodes.Node
+        properties:
+            dataPlaneIntf: fabricbridge
+            dataPlaneIp: 10.6.2.18/24
+            name: node2.cord.lab
+        requirements:
+            - site_deployment:
+                node: site_deployment
+                relationship: tosca.relationships.BelongsToOne
+
+    # ports (defined in the above recipe)
+    switch#leaf1:
+      type: tosca.nodes.Switch
+      properties:
+        name: leaf1
+        must-exist: true
+    
+    port#port17:
+      type: tosca.nodes.SwitchPort
+      properties:
+        portId: 17
+      requirements:
+        - switch:
+            node: switch#leaf1
+            relationship: tosca.relationships.BelongsToOne
+    
+    port#port18:
+      type: tosca.nodes.SwitchPort
+      properties:
+        portId: 18
+      requirements:
+        - switch:
+            node: switch#leaf1
+            relationship: tosca.relationships.BelongsToOne
+
+    # attaching nodes to ports
+    node1_to_port17:
+        type: tosca.nodes.NodeToSwitchPort
+        requirements:
+            - port:
+                node: port#port17
+                relationship: tosca.relationships.BelongsToOne
+            - node:
+                node: node#node1
+                relationship: tosca.relationships.BelongsToOne
+    
+    node2_to_port18:
+        type: tosca.nodes.NodeToSwitchPort
+        requirements:
+            - port:
+                node: port#port18
+                relationship: tosca.relationships.BelongsToOne
+            - node:
+                node: node#node2
+                relationship: tosca.relationships.BelongsToOne
+
+    # extra setup required by XOS (note that this needs to be customized to your installation)
+    mySite:
+      type: tosca.nodes.Site
+      properties:
+          name: mySite
+          login_base: opencord
+          abbreviated_name: ms
+          site_url: http://opencord.org/
+          hosts_nodes: true
+
+    myDeployment:
+      type: tosca.nodes.Deployment
+      properties:
+        name: myDeployment
+
+    site_deployment:
+      type: tosca.nodes.SiteDeployment
+      requirements:
+        - site:
+            node: mySite
+            relationship: tosca.relationships.BelongsToOne
+        - deployment:
+            node: myDeployment
+            relationship: tosca.relationships.BelongsToOne
+```
+
+This will cause the correct fabric configuration to be generated and pushed to the fabric.
diff --git a/src/fabric/samples/fabric-config.yaml b/src/fabric/samples/fabric-config.yaml
new file mode 100644 (file)
index 0000000..63ba218
--- /dev/null
@@ -0,0 +1,66 @@
+# 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 @fabric-config.yaml http://192.168.99.100:30007/run
+
+tosca_definitions_version: tosca_simple_yaml_1_0
+imports:
+  - custom_types/switch.yaml
+  - custom_types/switchport.yaml
+  - custom_types/portinterface.yaml
+description: Configures fabric switches and related ports
+topology_template:
+  node_templates:
+    switch#leaf1:
+      type: tosca.nodes.Switch
+      properties:
+        driver: ofdpa3
+        ipv4Loopback: 192.168.0.201
+        ipv4NodeSid: 17
+        isEdgeRouter: True
+        name: leaf1
+        ofId: of:0000000000000001
+        routerMac: 00:00:02:01:06:01
+
+    # Setup a fabric switch port
+    port#port1:
+      type: tosca.nodes.SwitchPort
+      properties:
+        portId: 1
+      requirements:
+        - switch:
+            node: switch#leaf1
+            relationship: tosca.relationships.BelongsToOne
+
+    # Setup a fabric switch port interface
+    interface#interface1:
+      type: tosca.nodes.PortInterface
+      properties:
+        ips: 192.168.0.254/24
+        name: port1
+      requirements:
+        - port:
+            node: port#port1
+            relationship: tosca.relationships.BelongsToOne
+
+    # Setup a fabric switch port with host_learning disabled
+    port#port2:
+      type: tosca.nodes.SwitchPort
+      properties:
+        portId: 2
+        host_learning: false
+      requirements:
+        - switch:
+            node: switch#leaf1
+            relationship: tosca.relationships.BelongsToOne
\ No newline at end of file
diff --git a/src/fabric/samples/nodes.yaml b/src/fabric/samples/nodes.yaml
new file mode 100644 (file)
index 0000000..578410a
--- /dev/null
@@ -0,0 +1,126 @@
+# 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 @nodes.yaml http://192.168.99.100:30007/run
+
+tosca_definitions_version: tosca_simple_yaml_1_0
+description: Set up Fabric service and attach it to ONOS (note that onos-service needs to be loaded)
+imports:
+  - custom_types/node.yaml
+  - custom_types/nodetoswitchport.yaml
+  - custom_types/switch.yaml
+  - custom_types/switchport.yaml
+  - custom_types/deployment.yaml
+  - custom_types/site.yaml
+  - custom_types/sitedeployment.yaml
+
+topology_template:
+  node_templates:
+
+    # nodes
+    node#node1:
+        type: tosca.nodes.Node
+        properties:
+            dataPlaneIntf: fabricbridge
+            dataPlaneIp: 10.6.1.1/24
+            name: node1.cord.lab
+        requirements:
+            - site_deployment:
+                node: site_deployment
+                relationship: tosca.relationships.BelongsToOne
+
+
+    node#node2:
+        type: tosca.nodes.Node
+        properties:
+            dataPlaneIntf: fabricbridge
+            dataPlaneIp: 10.6.1.2/24
+            name: node2.cord.lab
+        requirements:
+            - site_deployment:
+                node: site_deployment
+                relationship: tosca.relationships.BelongsToOne
+
+    # ports (defined in fabric-config.yaml)
+    switch#leaf1:
+      type: tosca.nodes.Switch
+      properties:
+        name: leaf1
+        must-exist: true
+    
+    port#port1:
+      type: tosca.nodes.SwitchPort
+      properties:
+        portId: 1
+        must-exist: true
+      requirements:
+        - switch:
+            node: switch#leaf1
+            relationship: tosca.relationships.BelongsToOne
+    
+    port#port2:
+      type: tosca.nodes.SwitchPort
+      properties:
+        portId: 2
+        must-exist: true
+      requirements:
+        - switch:
+            node: switch#leaf1
+            relationship: tosca.relationships.BelongsToOne
+
+    # attaching nodes to ports
+    node1_to_port1:
+        type: tosca.nodes.NodeToSwitchPort
+        requirements:
+            - port:
+                node: port#port1
+                relationship: tosca.relationships.BelongsToOne
+            - node:
+                node: node#node1
+                relationship: tosca.relationships.BelongsToOne
+    
+    node2_to_port2:
+        type: tosca.nodes.NodeToSwitchPort
+        requirements:
+            - port:
+                node: port#port2
+                relationship: tosca.relationships.BelongsToOne
+            - node:
+                node: node#node2
+                relationship: tosca.relationships.BelongsToOne
+
+    # extra setup required by XOS
+    mySite:
+      type: tosca.nodes.Site
+      properties:
+          name: mySite
+          login_base: opencord
+          abbreviated_name: ms
+          site_url: http://opencord.org/
+          hosts_nodes: true
+
+    myDeployment:
+      type: tosca.nodes.Deployment
+      properties:
+        name: myDeployment
+
+    site_deployment:
+      type: tosca.nodes.SiteDeployment
+      requirements:
+        - site:
+            node: mySite
+            relationship: tosca.relationships.BelongsToOne
+        - deployment:
+            node: myDeployment
+            relationship: tosca.relationships.BelongsToOne
\ No newline at end of file
diff --git a/src/fabric/samples/service.yaml b/src/fabric/samples/service.yaml
new file mode 100644 (file)
index 0000000..a6869ec
--- /dev/null
@@ -0,0 +1,51 @@
+# 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 @service.yaml http://192.168.99.100:30007/run
+
+tosca_definitions_version: tosca_simple_yaml_1_0
+description: Set up Fabric service and attach it to ONOS (note that onos-service needs to be loaded)
+imports:
+  - custom_types/fabricservice.yaml
+  - custom_types/onosservice.yaml
+  - custom_types/servicedependency.yaml
+
+topology_template:
+  node_templates:
+
+    service#ONOS_Fabric:
+      type: tosca.nodes.ONOSService
+      properties:
+          name: ONOS_Fabric
+          kind: platform
+          rest_hostname: onos-fabric-ui
+          rest_port: 8181
+
+    service#fabric:
+      type: tosca.nodes.FabricService
+      properties:
+        name: fabric
+        kind: platform
+
+    service_dependency#onos-fabric_fabric:
+      type: tosca.nodes.ServiceDependency
+      properties:
+        connect_method: None
+      requirements:
+        - subscriber_service:
+            node: service#fabric
+            relationship: tosca.relationships.BelongsToOne
+        - provider_service:
+            node: service#ONOS_Fabric
+            relationship: tosca.relationships.BelongsToOne
\ No newline at end of file
diff --git a/src/fabric/xos/synchronizer/config.yaml b/src/fabric/xos/synchronizer/config.yaml
new file mode 100644 (file)
index 0000000..c55603a
--- /dev/null
@@ -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.
+
+
+name: fabric
+required_models:
+  - FabricService
+steps_dir: "/opt/xos/synchronizers/fabric/steps"
+sys_dir: "/opt/xos/synchronizers/fabric/sys"
+models_dir: "/opt/xos/synchronizers/fabric/models"
+model_policies_dir: "/opt/xos/synchronizers/fabric/model_policies"
+event_steps_dir: "/opt/xos/synchronizers/fabric/event_steps"
+logging:
+  version: 1
+  handlers:
+    console:
+      class: logging.StreamHandler
+  loggers:
+    'multistructlog':
+      handlers:
+          - console
+      level: DEBUG
diff --git a/src/fabric/xos/synchronizer/event_steps/kubernetes_event.py b/src/fabric/xos/synchronizer/event_steps/kubernetes_event.py
new file mode 100644 (file)
index 0000000..03c17df
--- /dev/null
@@ -0,0 +1,74 @@
+
+# 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 os
+import sys
+from synchronizers.new_base.eventstep import EventStep
+from synchronizers.new_base.modelaccessor import model_accessor
+from synchronizers.new_base.modelaccessor import FabricService, Switch, Service
+from xosconfig import Config
+from multistructlog import create_logger
+
+log = create_logger(Config().get('logging'))
+
+class KubernetesPodDetailsEventStep(EventStep):
+    topics = ["xos.kubernetes.pod-details"]
+    technology = "kafka"
+
+    def __init__(self, *args, **kwargs):
+        super(KubernetesPodDetailsEventStep, self).__init__(*args, **kwargs)
+
+    @staticmethod
+    def get_fabric_onos(service):
+        service = Service.objects.get(id=service.id)
+        # get the onos_fabric service
+        fabric_onos = [s.leaf_model for s in service.provider_services if "onos" in s.name.lower()]
+
+        if len(fabric_onos) == 0:
+            raise Exception('Cannot find ONOS service in provider_services of Fabric')
+
+        return fabric_onos[0]
+
+    def process_event(self, event):
+        value = json.loads(event.value)
+
+        if (value.get("status") != "created"):
+            return
+
+        if "labels" not in value:
+            return
+
+        xos_service = value["labels"].get("xos_service")
+        if not xos_service:
+            return
+
+        for fabric_service in FabricService.objects.all():
+            onos_service = KubernetesPodDetailsEventStep.get_fabric_onos(fabric_service)
+            if (onos_service.name.lower() != xos_service.lower()):
+                continue
+
+            for switch in Switch.objects.all():
+                log.info("Dirtying Switch", switch=switch)
+                switch.backend_code=0
+                switch.backend_status="resynchronize due to kubernetes event"
+                switch.save(update_fields=["updated", "backend_code", "backend_status"], always_update_timestamp=True)
+
+                for port in switch.ports.all():
+                    log.info("Dirtying SwitchPort", port=port)
+                    port.backend_code=0
+                    port.backend_status="resynchronize due to kubernetes event"
+                    port.save(update_fields=["updated", "backend_code", "backend_status"], always_update_timestamp=True)
diff --git a/src/fabric/xos/synchronizer/event_steps/test_kubernetes_event.py b/src/fabric/xos/synchronizer/event_steps/test_kubernetes_event.py
new file mode 100644 (file)
index 0000000..22b0c95
--- /dev/null
@@ -0,0 +1,248 @@
+# 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
+import json
+import functools
+from mock import patch, call, Mock, PropertyMock
+import requests_mock
+
+import os, sys
+
+# Hack to load synchronizer framework
+test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
+xos_dir=os.path.join(test_path, "../../..")
+if not os.path.exists(os.path.join(test_path, "new_base")):
+    xos_dir=os.path.join(test_path, "../../../../../../orchestration/xos/xos")
+    services_dir = os.path.join(xos_dir, "../../xos_services")
+sys.path.append(xos_dir)
+sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base'))
+# END Hack to load synchronizer framework
+
+# generate model from xproto
+def get_models_fn(service_name, xproto_name):
+    name = os.path.join(service_name, "xos", xproto_name)
+    if os.path.exists(os.path.join(services_dir, name)):
+        return name
+    else:
+        name = os.path.join(service_name, "xos", "synchronizer", "models", xproto_name)
+        if os.path.exists(os.path.join(services_dir, name)):
+            return name
+    raise Exception("Unable to find service=%s xproto=%s" % (service_name, xproto_name))
+# END generate model from xproto
+
+class TestKubernetesEvent(unittest.TestCase):
+
+    def setUp(self):
+        global DeferredException
+
+        self.sys_path_save = sys.path
+        sys.path.append(xos_dir)
+        sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base'))
+
+        # 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 synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor
+        build_mock_modelaccessor(xos_dir, services_dir, [
+            get_models_fn("fabric", "fabric.xproto"),
+            get_models_fn("onos-service", "onos.xproto"),
+        ])
+        import synchronizers.new_base.mock_modelaccessor
+        reload(synchronizers.new_base.mock_modelaccessor) # in case nose2 loaded it in a previous test        
+        import synchronizers.new_base.modelaccessor
+        reload(synchronizers.new_base.modelaccessor)      # in case nose2 loaded it in a previous test         
+        from kubernetes_event import model_accessor
+        from mock_modelaccessor import MockObjectList
+
+        from kubernetes_event import KubernetesPodDetailsEventStep
+
+        # import all class names to globals
+        for (k, v) in model_accessor.all_model_classes.items():
+            globals()[k] = v
+
+        self.event_step = KubernetesPodDetailsEventStep
+
+        self.onos = ONOSService(name="myonos",
+                                id=1111,
+                                rest_hostname = "onos-url",
+                                rest_port = "8181",
+                                rest_username = "karaf",
+                                rest_password = "karaf",
+                                backend_code=1,
+                                backend_status="succeeded")
+
+        self.fabric_service = FabricService(name="fabric",
+                                                   id=1112,
+                                                   backend_code=1,
+                                                   backend_status="succeeded",
+                                                   provider_services=[self.onos])
+
+        self.switch = Switch(name="switch1",
+                             backend_code=1,
+                             backend_status="succeeded")
+
+        self.port1 = SwitchPort(name="switch1port1",
+                                switch=self.switch,
+                                backend_code=1,
+                                backend_status="succeeded")
+
+        self.port2 = SwitchPort(name="switch1port2",
+                                switch=self.switch,
+                                backend_code=1,
+                                backend_status="succeeded")
+
+        self.switch.ports = MockObjectList([self.port1, self.port2])
+
+        self.log = Mock()
+
+    def tearDown(self):
+        sys.path = self.sys_path_save
+
+    def test_process_event(self):
+        with patch.object(FabricService.objects, "get_items") as fabric_service_objects, \
+                patch.object(Service.objects, "get_items") as service_objects, \
+                patch.object(Switch.objects, "get_items") as switch_objects, \
+                patch.object(Switch, "save", autospec=True) as switch_save, \
+                patch.object(SwitchPort, "save", autospec=True) as switchport_save:
+            fabric_service_objects.return_value = [self.fabric_service]
+            service_objects.return_value = [self.onos, self.fabric_service]
+            switch_objects.return_value = [self.switch]
+
+            event_dict = {"status": "created",
+                          "labels": {"xos_service": "myonos"}}
+            event = Mock()
+            event.value = json.dumps(event_dict)
+
+            step = self.event_step(log=self.log)
+            step.process_event(event)
+
+            self.assertEqual(self.switch.backend_code, 0)
+            self.assertEqual(self.switch.backend_status, "resynchronize due to kubernetes event")
+
+            switch_save.assert_called_with(self.switch, update_fields=["updated", "backend_code", "backend_status"],
+                                            always_update_timestamp=True)
+
+            self.assertEqual(self.port1.backend_code, 0)
+            self.assertEqual(self.port1.backend_status, "resynchronize due to kubernetes event")
+
+            self.assertEqual(self.port2.backend_code, 0)
+            self.assertEqual(self.port2.backend_status, "resynchronize due to kubernetes event")
+
+            switchport_save.assert_has_calls([call(self.port1, update_fields=["updated", "backend_code", "backend_status"],
+                                            always_update_timestamp=True),
+                                       call(self.port2, update_fields=["updated", "backend_code", "backend_status"],
+                                            always_update_timestamp=True)])
+
+    def test_process_event_unknownstatus(self):
+        with patch.object(FabricService.objects, "get_items") as fabric_service_objects, \
+                patch.object(Service.objects, "get_items") as service_objects, \
+                patch.object(Switch.objects, "get_items") as switch_objects, \
+                patch.object(Switch, "save") as switch_save, \
+                patch.object(SwitchPort, "save") as switchport_save:
+            fabric_service_objects.return_value = [self.fabric_service]
+            service_objects.return_value = [self.onos, self.fabric_service]
+            switch_objects.return_value = [self.switch]
+
+            event_dict = {"status": "something_else",
+                          "labels": {"xos_service": "myonos"}}
+            event = Mock()
+            event.value = json.dumps(event_dict)
+
+            step = self.event_step(log=self.log)
+            step.process_event(event)
+
+            self.assertEqual(self.switch.backend_code, 1)
+            self.assertEqual(self.switch.backend_status, "succeeded")
+
+            switch_save.assert_not_called()
+
+            self.assertEqual(self.port1.backend_code, 1)
+            self.assertEqual(self.port1.backend_status, "succeeded")
+
+            self.assertEqual(self.port2.backend_code, 1)
+            self.assertEqual(self.port2.backend_status, "succeeded")
+
+            switchport_save.assert_not_called()
+
+    def test_process_event_unknownservice(self):
+        with patch.object(FabricService.objects, "get_items") as fabric_service_objects, \
+                patch.object(Service.objects, "get_items") as service_objects, \
+                patch.object(Switch.objects, "get_items") as switch_objects, \
+                patch.object(Switch, "save") as switch_save, \
+                patch.object(SwitchPort, "save") as switchport_save:
+            fabric_service_objects.return_value = [self.fabric_service]
+            service_objects.return_value = [self.onos, self.fabric_service]
+            switch_objects.return_value = [self.switch]
+
+            event_dict = {"status": "created",
+                          "labels": {"xos_service": "something_else"}}
+            event = Mock()
+            event.value = json.dumps(event_dict)
+
+            step = self.event_step(log=self.log)
+            step.process_event(event)
+
+            self.assertEqual(self.switch.backend_code, 1)
+            self.assertEqual(self.switch.backend_status, "succeeded")
+
+            switch_save.assert_not_called()
+
+            self.assertEqual(self.port1.backend_code, 1)
+            self.assertEqual(self.port1.backend_status, "succeeded")
+
+            self.assertEqual(self.port2.backend_code, 1)
+            self.assertEqual(self.port2.backend_status, "succeeded")
+
+            switchport_save.assert_not_called()
+
+    def test_process_event_nolabels(self):
+        with patch.object(FabricService.objects, "get_items") as fabric_service_objects, \
+                patch.object(Service.objects, "get_items") as service_objects, \
+                patch.object(Switch.objects, "get_items") as switch_objects, \
+                patch.object(Switch, "save") as switch_save, \
+                patch.object(SwitchPort, "save") as switchport_save:
+            fabric_service_objects.return_value = [self.fabric_service]
+            service_objects.return_value = [self.onos, self.fabric_service]
+            switch_objects.return_value = [self.switch]
+
+            event_dict = {"status": "created"}
+            event = Mock()
+            event.value = json.dumps(event_dict)
+
+            step = self.event_step(log=self.log)
+            step.process_event(event)
+
+            self.assertEqual(self.switch.backend_code, 1)
+            self.assertEqual(self.switch.backend_status, "succeeded")
+
+            switch_save.assert_not_called()
+
+            self.assertEqual(self.port1.backend_code, 1)
+            self.assertEqual(self.port1.backend_status, "succeeded")
+
+            self.assertEqual(self.port2.backend_code, 1)
+            self.assertEqual(self.port2.backend_status, "succeeded")
+
+            switchport_save.assert_not_called()
+
+if __name__ == '__main__':
+    unittest.main()
+
+
+
diff --git a/src/fabric/xos/synchronizer/fabric-synchronizer.py b/src/fabric/xos/synchronizer/fabric-synchronizer.py
new file mode 100755 (executable)
index 0000000..f9ea235
--- /dev/null
@@ -0,0 +1,37 @@
+
+# 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 importlib
+import os
+import sys
+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')
+
+observer_path = os.path.join(os.path.dirname(os.path.realpath(__file__)),"../../synchronizers/new_base")
+sys.path.append(observer_path)
+mod = importlib.import_module("xos-synchronizer")
+mod.main()
diff --git a/src/fabric/xos/synchronizer/model_policies/model_policy_compute_nodes.py b/src/fabric/xos/synchronizer/model_policies/model_policy_compute_nodes.py
new file mode 100644 (file)
index 0000000..91efd40
--- /dev/null
@@ -0,0 +1,100 @@
+
+# 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 ipaddress
+import random
+from synchronizers.new_base.modelaccessor import NodeToSwitchPort, PortInterface, model_accessor
+from synchronizers.new_base.policy import Policy
+
+from xosconfig import Config
+from multistructlog import create_logger
+
+from helpers import Helpers
+
+log = create_logger(Config().get('logging'))
+
+class ComputeNodePolicy(Policy):
+    model_name = "NodeToSwitchPort"
+
+    @staticmethod
+    def getLastAddress(network):
+        return str(network.network_address + network.num_addresses - 2) + "/" + str(network.prefixlen)
+        # return ipaddress.ip_interface(network.network_address + network.num_addresses - 2)
+
+    @staticmethod
+    def getPortCidrByIp(ip):
+        interface = ipaddress.ip_interface(ip)
+        network = ipaddress.ip_network(interface.network)
+        cidr = ComputeNodePolicy.getLastAddress(network)
+        return cidr
+
+    @staticmethod
+    def generateVlan(used_vlans):
+        availabel_tags = range(16, 4093)
+        valid_tags = list(set(availabel_tags) - set(used_vlans))
+        if len(valid_tags) == 0:
+            raise Exception("No VLANs left")
+        return random.choice(valid_tags)
+
+    @staticmethod
+    def getVlanByCidr(subnet):
+        # vlanUntagged is unique per subnet
+        same_subnet_ifaces = PortInterface.objects.filter(ips=str(subnet))
+
+        if len(same_subnet_ifaces) > 0:
+            return same_subnet_ifaces[0].vlanUntagged
+        else:
+            PortInterface.objects.all()
+            used_vlans = list(set([i.vlanUntagged for i in same_subnet_ifaces]))
+            log.info("MODEL_POLICY: used vlans", vlans=used_vlans, subnet=subnet)
+            return ComputeNodePolicy.generateVlan(used_vlans)
+
+    def handle_create(self, node_to_port):
+        return self.handle_update(node_to_port)
+
+    def handle_update(self, node_to_port):
+        log.info("MODEL_POLICY: NodeToSwitchPort %s handle update" % node_to_port.id, node=node_to_port.node, port=node_to_port.port, switch=node_to_port.port.switch)
+
+        compute_node = node_to_port.node
+
+        cidr = ComputeNodePolicy.getPortCidrByIp(compute_node.dataPlaneIp)
+
+        # check if an interface already exists
+        try:
+            PortInterface.objects.get(
+                port_id=node_to_port.port.id,
+                name=compute_node.dataPlaneIntf,
+                ips=str(cidr)
+            )
+        except IndexError:
+
+            vlan = self.getVlanByCidr(cidr)
+
+            log.info("MODEL_POLICY: choosen vlan", vlan=vlan, cidr=cidr)
+
+            interface = PortInterface(
+                port_id=node_to_port.port.id,
+                name=compute_node.dataPlaneIntf,
+                ips=str(cidr),
+                vlanUntagged=vlan
+            )
+
+            interface.save()
+        
+        # TODO if the model is updated I need to remove the old interface, how?
+
+
+    def handle_delete(self, node_to_port):
+        pass
diff --git a/src/fabric/xos/synchronizer/model_policies/test_model_policy_compute_node.py b/src/fabric/xos/synchronizer/model_policies/test_model_policy_compute_node.py
new file mode 100644 (file)
index 0000000..4586a71
--- /dev/null
@@ -0,0 +1,198 @@
+
+# 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
+import ipaddress
+from mock import patch, call, Mock, PropertyMock
+
+import os, sys
+
+test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
+service_dir=os.path.join(test_path, "../../../..")
+xos_dir=os.path.join(test_path, "../../..")
+if not os.path.exists(os.path.join(test_path, "new_base")):
+    xos_dir=os.path.join(test_path, "../../../../../../orchestration/xos/xos")
+    services_dir=os.path.join(xos_dir, "../../xos_services")
+
+# While transitioning from static to dynamic load, the path to find neighboring xproto files has changed. So check
+# both possible locations...
+def get_models_fn(service_name, xproto_name):
+    name = os.path.join(service_name, "xos", xproto_name)
+    if os.path.exists(os.path.join(services_dir, name)):
+        return name
+    else:
+        name = os.path.join(service_name, "xos", "synchronizer", "models", xproto_name)
+        if os.path.exists(os.path.join(services_dir, name)):
+            return name
+    raise Exception("Unable to find service=%s xproto=%s" % (service_name, xproto_name))
+
+class TestComputeNodePolicy(unittest.TestCase):
+    def setUp(self):
+        global ComputeNodePolicy, MockObjectList
+
+        self.sys_path_save = sys.path
+        sys.path.append(xos_dir)
+        sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base'))
+
+        config = os.path.join(test_path, "../test_config.yaml")
+        from xosconfig import Config
+        Config.clear()
+        Config.init(config, 'synchronizer-config-schema.yaml')
+
+        from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor
+        build_mock_modelaccessor(xos_dir, services_dir, [get_models_fn("fabric", "fabric.xproto")])
+
+        import synchronizers.new_base.modelaccessor
+
+        from model_policy_compute_nodes import ComputeNodePolicy, model_accessor
+
+        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 = ComputeNodePolicy
+        self.model = Mock()
+
+    def tearDown(self):
+        sys.path = self.sys_path_save
+
+    def test_getLastAddress(self):
+
+        dataPlaneIp = unicode("10.6.1.2/24", "utf-8")
+        interface = ipaddress.ip_interface(dataPlaneIp)
+        subnet = ipaddress.ip_network(interface.network)
+        last_ip = self.policy.getLastAddress(subnet)
+        self.assertEqual(str(last_ip), "10.6.1.254/24")
+
+    def test_generateVlan(self):
+
+        used_vlans = range(16, 4093)
+        used_vlans.remove(1000)
+
+        vlan = self.policy.generateVlan(used_vlans)
+
+        self.assertEqual(vlan, 1000)
+
+    def test_generateVlanFail(self):
+
+        used_vlans = range(16, 4093)
+
+        with self.assertRaises(Exception) as e:
+            self.policy.generateVlan(used_vlans)
+
+        self.assertEqual(e.exception.message, "No VLANs left")
+
+    def test_getVlanByCidr_same_subnet(self):
+
+        mock_pi_ip = unicode("10.6.1.2/24", "utf-8")
+        
+        mock_pi = Mock()
+        mock_pi.vlanUntagged = 1234
+        mock_pi.ips = str(self.policy.getPortCidrByIp(mock_pi_ip))
+
+        test_ip = unicode("10.6.1.1/24", "utf-8")
+        test_subnet = self.policy.getPortCidrByIp(test_ip)
+
+        with patch.object(PortInterface.objects, "get_items") as get_pi:
+
+            get_pi.return_value = [mock_pi]
+            vlan = self.policy.getVlanByCidr(test_subnet)
+
+            self.assertEqual(vlan, mock_pi.vlanUntagged)
+
+    def test_getVlanByCidr_different_subnet(self):
+
+        mock_pi_ip = unicode("10.6.1.2/24", "utf-8")
+        mock_pi = Mock()
+        mock_pi.vlanUntagged = 1234
+        mock_pi.ips = str(self.policy.getPortCidrByIp(mock_pi_ip))
+
+        test_ip = unicode("192.168.1.1/24", "utf-8")
+        test_subnet = self.policy.getPortCidrByIp(test_ip)
+
+        with patch.object(PortInterface.objects, "get_items") as get_pi:
+
+            get_pi.return_value = [mock_pi]
+            vlan = self.policy.getVlanByCidr(test_subnet)
+
+            self.assertNotEqual(vlan, mock_pi.vlanUntagged)
+
+    def test_handle_create(self):
+
+        policy = self.policy()
+        with patch.object(policy, "handle_update") as handle_update:
+            policy.handle_create(self.model)
+            handle_update.assert_called_with(self.model)
+
+    def test_handle_update_do_nothing(self):
+
+        mock_pi_ip = unicode("10.6.1.2/24", "utf-8")
+        mock_pi = Mock()
+        mock_pi.port_id = 1
+        mock_pi.name = "test_interface"
+        mock_pi.ips = str(self.policy.getPortCidrByIp(mock_pi_ip))
+
+        policy = self.policy()
+
+        self.model.port.id = 1
+        self.model.node.dataPlaneIntf = "test_interface"
+
+        with patch.object(PortInterface.objects, "get_items") as get_pi, \
+            patch.object(self.policy, "getPortCidrByIp") as get_subnet, \
+            patch.object(PortInterface, 'save') as mock_save:
+
+            get_pi.return_value = [mock_pi]
+            get_subnet.return_value = mock_pi.ips
+
+            policy.handle_update(self.model)
+
+            mock_save.assert_not_called()
+
+    def test_handle_update(self):
+
+        policy = self.policy()
+
+        self.model.port.id = 1
+        self.model.node.dataPlaneIntf = "test_interface"
+        self.model.node.dataPlaneIp = unicode("10.6.1.2/24", "utf-8")
+
+        with patch.object(PortInterface.objects, "get_items") as get_pi, \
+            patch.object(self.policy, "getVlanByCidr") as get_vlan, \
+            patch.object(PortInterface, "save", autospec=True) as mock_save:
+
+            get_pi.return_value = []
+            get_vlan.return_value = "1234"
+
+            policy.handle_update(self.model)
+
+            self.assertEqual(mock_save.call_count, 1)
+            pi = mock_save.call_args[0][0]
+
+            self.assertEqual(pi.name, self.model.node.dataPlaneIntf)
+            self.assertEqual(pi.port_id, self.model.port.id)
+            self.assertEqual(pi.vlanUntagged, "1234")
+            self.assertEqual(pi.ips, "10.6.1.254/24")
+
+
+if __name__ == '__main__':
+    unittest.main()
+
diff --git a/src/fabric/xos/synchronizer/models/fabric.xproto b/src/fabric/xos/synchronizer/models/fabric.xproto
new file mode 100644 (file)
index 0000000..aff47f8
--- /dev/null
@@ -0,0 +1,52 @@
+option app_label = "fabric";
+option name = "fabric";
+
+message FabricService(Service) {
+    option verbose_name = "Fabric Service";
+    
+    optional bool autoconfig = 1 [default = True, help_text="Autoconfigure the fabric", null=False];
+}
+
+message Switch(XOSBase) {
+    option verbose_name = "Fabric Switch";
+
+    required string ofId = 1 [help_text = "The unique OpenFlow ID of the fabric switch", max_length = 19,db_index = False];
+    required string name = 2 [help_text = "The unique name of the fabric switch", max_length = 254,db_index = False];
+    required string driver = 3 [help_text = "The driver used by the SDN controller", max_length = 254,db_index = False, default = "ofdpa3"];
+    required int32 ipv4NodeSid = 4 [help_text = "The MPLS label used by the switch [17 - 1048576]",db_index = False];
+    required string ipv4Loopback = 5 [help_text = "Fabric loopback interface", max_length = 17,db_index = False];
+    required string routerMac = 6 [help_text = "MAC address of the fabric switch used for all interfaces", max_length = 17,db_index = False];
+    required bool isEdgeRouter = 7 [default = True, help_text="Whether the fabric switch is a leaf or a spine", null=False];
+}
+
+message SwitchPort(XOSBase) {
+    option verbose_name = "Fabric Switch Port";
+
+    required manytoone switch->Switch:ports = 1:1001 [help_text = "The fabric switch the port belongs to", db_index = True, tosca_key=True];
+    required int32 portId = 2 [help_text = "The unique port OpenFlow port ID",db_index = False, tosca_key=True];
+    required bool host_learning = 3 [help_text = "whether or not to enable autodiscovery", default = True,db_index = False];
+}
+
+message PortInterface(XOSBase) {
+    option verbose_name = "Fabric Port Interface";
+
+    required manytoone port->SwitchPort:interfaces = 1:1001 [help_text = "The fabric switch port the interface belongs to", db_index = True];
+    required string name = 2 [help_text = "The unique name of the fabric switch port", max_length = 254,db_index = False];
+    optional int32 vlanUntagged = 3 [help_text = "The optional untagged VLAN ID for the interface", db_index = False];
+}
+
+message NodeToSwitchPort(XOSBase) {
+    option verbose_name = "Node to switch port";
+    option description = "Compute Node connection to a Fabric switch port";
+
+    required manytoone port->SwitchPort:node_to_switch_ports = 1:1002 [help_text = "The fabric switch port the node is connected to", db_index = True, tosca_key=True];
+    required manytoone node->Node:node_to_switch_ports = 2:1004 [help_text = "The ComputeNode this port is connected to", db_index = True, tosca_key=True];
+}
+
+message FabricIpAddress(XOSBase) {
+    option verbose_name = "IP address";
+
+    required manytoone interface->PortInterface:ips = 1:1001 [help_text = "The port interface the IP address belongs to", db_index = True];
+    required string ip = 2 [help_text = "The unique IP address (either IPv4 or IPv6 / netmask)", max_length = 52,db_index = False, unique_with = "interface"];
+    optional string description = 3 [help_text = "A short description of the IP address", max_length = 254,db_index = False];
+}
diff --git a/src/fabric/xos/synchronizer/steps/helpers.py b/src/fabric/xos/synchronizer/steps/helpers.py
new file mode 100644 (file)
index 0000000..08be81d
--- /dev/null
@@ -0,0 +1,29 @@
+# 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 synchronizers.new_base.modelaccessor import Service
+
+class Helpers():
+    @staticmethod
+    def format_url(url):
+        if 'http' in url:
+            return url
+        else:
+            return 'http://%s' % url
+    @staticmethod
+    def get_onos_fabric_service():
+        # FIXME do not select by name but follow ServiceDependency
+        fabric_service = Service.objects.get(name="fabric")
+        onos_fabric_service = fabric_service.provider_services[0].leaf_model
+        return onos_fabric_service
diff --git a/src/fabric/xos/synchronizer/steps/sync_fabric_port.py b/src/fabric/xos/synchronizer/steps/sync_fabric_port.py
new file mode 100644 (file)
index 0000000..9c37ffc
--- /dev/null
@@ -0,0 +1,115 @@
+
+# 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 requests
+import urllib
+from requests.auth import HTTPBasicAuth
+from synchronizers.new_base.syncstep import SyncStep, DeferredException, model_accessor
+from synchronizers.new_base.modelaccessor import FabricService, SwitchPort, PortInterface, FabricIpAddress
+
+from xosconfig import Config
+from multistructlog import create_logger
+
+from helpers import Helpers
+
+log = create_logger(Config().get('logging'))
+
+class SyncFabricPort(SyncStep):
+    provides = [SwitchPort]
+    observes = [SwitchPort, PortInterface, FabricIpAddress]
+
+    def sync_record(self, model):
+
+        if model.leaf_model_name == "PortInterface":
+            log.info("Receivent update for PortInterface", port=model.port.portId, interface=model)
+            return self.sync_record(model.port)
+
+        if model.leaf_model_name == "FabricIpAddress":
+            log.info("Receivent update for FabricIpAddress", port=model.interface.port.portId, interface=model.interface.name, ip=model.ip)
+            return self.sync_record(model.interface.port)
+
+        log.info("Adding port %s/%s to onos-fabric" % (model.switch.ofId, model.portId))
+        interfaces = []
+        for intf in model.interfaces.all():
+            i = {
+                "name" : intf.name,
+                "ips" : [ i.ip for i in intf.ips.all() ]
+            }
+            if intf.vlanUntagged:
+                i["vlan-untagged"] = intf.vlanUntagged
+            interfaces.append(i)
+
+        # Send port config to onos-fabric netcfg
+        data = {
+            "ports": {
+                "%s/%s" % (model.switch.ofId, model.portId) : {
+                    "interfaces": interfaces,
+                    "hostLearning": {
+                        "enabled": model.host_learning
+                    }
+                }
+            }
+        }
+
+        log.debug("Port %s/%s data" % (model.switch.ofId, model.portId), data=data)
+
+        onos = Helpers.get_onos_fabric_service()
+
+        url = 'http://%s:%s/onos/v1/network/configuration/' % (onos.rest_hostname, onos.rest_port)
+
+        r = requests.post(url, json=data, auth=HTTPBasicAuth(onos.rest_username, onos.rest_password))
+
+        if r.status_code != 200:
+            log.error(r.text)
+            raise Exception("Failed to add port  %s/%s into ONOS" % (model.switch.ofId, model.portId))
+        else:
+            try:
+                log.info("Port %s/%s response" % (model.switch.ofId, model.portId), json=r.json())
+            except Exception:
+                log.info("Port %s/%s response" % (model.switch.ofId, model.portId), text=r.text)
+
+    def delete_netcfg_item(self, partial_url):
+        onos = Helpers.get_onos_fabric_service()
+        url = 'http://%s:%s/onos/v1/network/configuration/ports/%s' % (onos.rest_hostname, onos.rest_port, partial_url)
+
+        r = requests.delete(url, auth=HTTPBasicAuth(onos.rest_username, onos.rest_password))
+
+        if r.status_code != 204:
+            log.error(r.text)
+            raise Exception("Failed to %s port %s from ONOS" % url)
+
+    def delete_record(self, model):
+        if model.leaf_model_name == "PortInterface":
+            log.info("Received update for PortInterface", port=model.port.portId, interface=model.name)
+            log.info("Removing port interface %s from port %s/%s in onos-fabric" % (model.name, model.port.switch.ofId, model.port.portId))
+
+            # resync the existing interfaces
+            return self.sync_record(model.port)
+
+        if model.leaf_model_name == "FabricIpAddress":
+            # TODO add unit tests
+            log.info("Received update for FabricIpAddress", port=model.interface.port.portId, interface=model.interface.name, ip=model.ip)
+            log.info("Removing IP %s from interface %s, on port %s/%s in onos-fabric" % (model.ip, model.interface.name, model.interface.port.switch.ofId, model.interface.port.portId))
+
+            # resync the existing interfaces
+            return self.sync_record(model.interface.port)
+
+        log.info("Removing port %s/%s from onos-fabric" % (model.switch.ofId, model.portId))
+
+        key = "%s/%s" % (model.switch.ofId, model.portId)
+        key = urllib.quote(key, safe='')
+
+        # deleting the port
+        self.delete_netcfg_item(key)
diff --git a/src/fabric/xos/synchronizer/steps/sync_fabric_switch.py b/src/fabric/xos/synchronizer/steps/sync_fabric_switch.py
new file mode 100644 (file)
index 0000000..7f5aa17
--- /dev/null
@@ -0,0 +1,78 @@
+
+# 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 requests
+from requests.auth import HTTPBasicAuth
+from synchronizers.new_base.syncstep import SyncStep, model_accessor
+from synchronizers.new_base.modelaccessor import FabricService, Switch
+
+from xosconfig import Config
+from multistructlog import create_logger
+
+from helpers import Helpers
+
+log = create_logger(Config().get('logging'))
+
+class SyncFabricSwitch(SyncStep):
+    provides = [Switch]
+    observes = Switch
+
+    def sync_record(self, model):
+        log.info("Adding switch %s to onos-fabric" % model.name)
+        # Send device info to onos-fabric netcfg
+        data = {
+          "devices": {
+            model.ofId: {
+              "basic": {
+                "name": model.name,
+                "driver": model.driver
+              },
+              "segmentrouting" : {
+                "name" : model.name,
+                "ipv4NodeSid" : model.ipv4NodeSid,
+                "ipv4Loopback" : model.ipv4Loopback,
+                "routerMac" : model.routerMac,
+                "isEdgeRouter" : model.isEdgeRouter,
+                "adjacencySids" : []
+              }
+            }
+          }
+        }
+
+        onos = Helpers.get_onos_fabric_service()
+
+        url = 'http://%s:%s/onos/v1/network/configuration/' % (onos.rest_hostname, onos.rest_port)
+        r = requests.post(url, json=data, auth=HTTPBasicAuth(onos.rest_username, onos.rest_password))
+
+        if r.status_code != 200:
+            log.error(r.text)
+            raise Exception("Failed to add device %s into ONOS" % model.name)
+        else:
+            try:
+                print r.json()
+            except Exception:
+                print r.text
+
+    def delete_record(self, model):
+        log.info("Removing switch %s from onos-fabric" % model.name)
+        onos = Helpers.get_onos_fabric_service()
+        url = 'http://%s:%s/onos/v1/network/configuration/devices/%s' % (
+        onos.rest_hostname, onos.rest_port, model.ofId)
+
+        r = requests.delete(url, auth=HTTPBasicAuth(onos.rest_username, onos.rest_password))
+
+        if r.status_code != 204:
+            log.error(r.text)
+            raise Exception("Failed to remove switch %s from ONOS" % model.name)
diff --git a/src/fabric/xos/synchronizer/steps/test_sync_fabric_port.py b/src/fabric/xos/synchronizer/steps/test_sync_fabric_port.py
new file mode 100644 (file)
index 0000000..8c6e773
--- /dev/null
@@ -0,0 +1,236 @@
+# 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
+import urllib
+import functools
+from mock import patch, call, Mock, PropertyMock
+import requests_mock
+import multistructlog
+from multistructlog import create_logger
+
+import os, sys
+
+# Hack to load synchronizer framework
+test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
+xos_dir=os.path.join(test_path, "../../..")
+if not os.path.exists(os.path.join(test_path, "new_base")):
+    xos_dir=os.path.join(test_path, "../../../../../../orchestration/xos/xos")
+    services_dir = os.path.join(xos_dir, "../../xos_services")
+sys.path.append(xos_dir)
+sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base'))
+# END Hack to load synchronizer framework
+
+# generate model from xproto
+def get_models_fn(service_name, xproto_name):
+    name = os.path.join(service_name, "xos", xproto_name)
+    if os.path.exists(os.path.join(services_dir, name)):
+        return name
+    else:
+        name = os.path.join(service_name, "xos", "synchronizer", "models", xproto_name)
+        if os.path.exists(os.path.join(services_dir, name)):
+            return name
+    raise Exception("Unable to find service=%s xproto=%s" % (service_name, xproto_name))
+# END generate model from xproto
+
+def match_json(desired, req):
+    if desired!=req.json():
+        raise Exception("Got request %s, but body is not matching" % req.url)
+        return False
+    return True
+
+class TestSyncFabricPort(unittest.TestCase):
+
+    def setUp(self):
+        global DeferredException
+
+        self.sys_path_save = sys.path
+        sys.path.append(xos_dir)
+        sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base'))
+
+        # 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 synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor
+        build_mock_modelaccessor(xos_dir, services_dir, [get_models_fn("fabric", "fabric.xproto")])
+        import synchronizers.new_base.modelaccessor
+
+        from sync_fabric_port import SyncFabricPort, model_accessor
+
+        # import all class names to globals
+        for (k, v) in model_accessor.all_model_classes.items():
+            globals()[k] = v
+
+        self.sync_step = SyncFabricPort
+        self.sync_step.log = Mock()
+
+        # mock onos-fabric
+        onos_fabric = Mock()
+        onos_fabric.name = "onos-fabric"
+        onos_fabric.rest_hostname = "onos-fabric"
+        onos_fabric.rest_port = "8181"
+        onos_fabric.rest_username = "onos"
+        onos_fabric.rest_password = "rocks"
+
+        onos_fabric_base = Mock()
+        onos_fabric_base.leaf_model = onos_fabric
+
+        self.fabric = Mock()
+        self.fabric.name = "fabric"
+        self.fabric.provider_services = [onos_fabric_base]
+
+    def tearDown(self):
+        sys.path = self.sys_path_save
+
+    @requests_mock.Mocker()
+    def test_sync_port(self, m):
+        # IPs
+        ip1 = Mock()
+        ip1.ip = "1.1.1.1/16"
+        ip1.description = "My IPv4 ip"
+        ip2 = Mock()
+        ip2.ip = "2001:0db8:85a3:0000:0000:8a2e:0370:7334/64"
+        ip2.description = "My IPv6 ip"
+        ip3 = Mock()
+        ip3.ip = "2.2.2.2/8"
+        ip3.description = "My other IPv4 ip"
+
+        intf1 = Mock()
+        intf1.name = "intf1"
+        intf1.vlanUntagged = None
+        intf1.ips.all.return_value = [ip1, ip2]
+        intf2 = Mock()
+        intf2.name = "intf2"
+        intf2.vlanUntagged = 42
+        intf2.ips.all.return_value = [ip3]
+
+        port = Mock()
+        port.id = 1
+        port.tologdict.return_value = {}
+        port.host_learning = True
+        port.interfaces.all.return_value = [intf1, intf2]
+        port.switch.ofId = "of:1234"
+        port.portId = "1"
+
+        expected_conf = {
+            "ports": {
+                "%s/%s" % (port.switch.ofId, port.portId): {
+                    "interfaces": [
+                        {
+                            "name": intf1.name,
+                            "ips": [ ip1.ip, ip2.ip ]
+                        },
+                        {
+                            "name": intf2.name,
+                            "ips": [ip3.ip],
+                            "vlan-untagged": intf2.vlanUntagged
+                        }
+                    ],
+                    "hostLearning": {
+                        "enabled": port.host_learning
+                    }
+                }
+            }
+        }
+
+        m.post("http://onos-fabric:8181/onos/v1/network/configuration/",
+               status_code=200,
+               additional_matcher=functools.partial(match_json, expected_conf))
+
+        with patch.object(Service.objects, "get") as onos_fabric_get:
+            onos_fabric_get.return_value = self.fabric
+            self.sync_step().sync_record(port)
+            self.assertTrue(m.called)
+
+    @requests_mock.Mocker()
+    def test_delete_port(self, m):
+        # create a mock SwitchPort instance
+        port = Mock()
+        port.id = 1
+        port.tologdict.return_value = {}
+        port.host_learning = True
+        port.switch.ofId = "of:1234"
+        port.portId = "1"
+
+        key = urllib.quote("of:1234/1", safe='')
+        m.delete("http://onos-fabric:8181/onos/v1/network/configuration/ports/%s" % key,
+            status_code=204)
+
+        with patch.object(Service.objects, "get") as onos_fabric_get:
+            onos_fabric_get.return_value = self.fabric
+            self.sync_step().delete_record(port)
+            self.assertTrue(m.called)
+
+    @requests_mock.Mocker()
+    def test_delete_interface(self, m):
+        ip1 = Mock()
+        ip1.ip = "1.1.1.1/16"
+        ip1.description = "My IPv4 ip"
+        ip2 = Mock()
+        ip2.ip = "2001:0db8:85a3:0000:0000:8a2e:0370:7334/64"
+        ip2.description = "My IPv6 ip"
+
+        # interfaces
+        intf1 = Mock()
+        intf1.name = "intf1"
+        intf1.vlanUntagged = None
+        intf1.ips.all.return_value = [ip1, ip2]
+
+        # bindings
+        # create a mock SwitchPort instance
+        interface_to_remove = Mock()
+        interface_to_remove.id = 1
+        interface_to_remove.tologdict.return_value = {}
+        interface_to_remove.leaf_model_name = "PortInterface"
+        interface_to_remove.port.interfaces.all.return_value = [intf1]
+        interface_to_remove.port.switch.ofId = "of:1234"
+        interface_to_remove.port.portId = "1"
+        interface_to_remove.port.host_learning = True
+
+        m.post("http://onos-fabric:8181/onos/v1/network/configuration/", status_code=200)
+
+        with patch.object(Service.objects, "get") as onos_fabric_get:
+            onos_fabric_get.return_value = self.fabric
+            self.sync_step().delete_record(interface_to_remove)
+            self.assertTrue(m.called)
+
+    @requests_mock.Mocker()
+    def test_delete_ip(self, m):
+        ip1 = Mock()
+        ip1.ip = "1.1.1.1/16"
+        ip1.description = "My IPv4 ip"
+
+        intf1 = Mock()
+        intf1.name = "intf1"
+        intf1.vlanUntagged = None
+        intf1.ips.all.return_value = [ip1]
+
+        ip_to_remove = Mock()
+        ip_to_remove.id = 1
+        ip_to_remove.leaf_model_name = "FabricIpAddress"
+        ip_to_remove.interface.port.interfaces.all.return_value = [intf1] 
+        ip_to_remove.interface.port.switch.ofId = "of:1234"
+        ip_to_remove.interface.port.portId = "1"
+        ip_to_remove.interface.port.host_learning = True
+
+        m.post("http://onos-fabric:8181/onos/v1/network/configuration/", status_code=200)
+
+        with patch.object(Service.objects, "get") as onos_fabric_get:
+            onos_fabric_get.return_value = self.fabric
+            self.sync_step().delete_record(ip_to_remove)
+            self.assertTrue(m.called)
diff --git a/src/fabric/xos/synchronizer/steps/test_sync_fabric_switch.py b/src/fabric/xos/synchronizer/steps/test_sync_fabric_switch.py
new file mode 100644 (file)
index 0000000..5761b73
--- /dev/null
@@ -0,0 +1,161 @@
+# 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
+
+import functools
+from mock import patch, call, Mock, PropertyMock
+import requests_mock
+import multistructlog
+from multistructlog import create_logger
+
+import os, sys
+
+# Hack to load synchronizer framework
+test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
+xos_dir=os.path.join(test_path, "../../..")
+if not os.path.exists(os.path.join(test_path, "new_base")):
+    xos_dir=os.path.join(test_path, "../../../../../../orchestration/xos/xos")
+    services_dir = os.path.join(xos_dir, "../../xos_services")
+sys.path.append(xos_dir)
+sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base'))
+# END Hack to load synchronizer framework
+
+# generate model from xproto
+def get_models_fn(service_name, xproto_name):
+    name = os.path.join(service_name, "xos", xproto_name)
+    if os.path.exists(os.path.join(services_dir, name)):
+        return name
+    else:
+        name = os.path.join(service_name, "xos", "synchronizer", "models", xproto_name)
+        if os.path.exists(os.path.join(services_dir, name)):
+            return name
+    raise Exception("Unable to find service=%s xproto=%s" % (service_name, xproto_name))
+# END generate model from xproto
+
+def match_json(desired, req):
+    if desired!=req.json():
+        raise Exception("Got request %s, but body is not matching" % req.url)
+        return False
+    return True
+
+class TestSyncFabricSwitch(unittest.TestCase):
+
+    def setUp(self):
+        global DeferredException
+
+        self.sys_path_save = sys.path
+        sys.path.append(xos_dir)
+        sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base'))
+
+        # 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 synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor
+        build_mock_modelaccessor(xos_dir, services_dir, [get_models_fn("fabric", "fabric.xproto")])
+        import synchronizers.new_base.modelaccessor
+
+        from sync_fabric_switch import SyncFabricSwitch, model_accessor
+
+        # import all class names to globals
+        for (k, v) in model_accessor.all_model_classes.items():
+            globals()[k] = v
+
+
+        self.sync_step = SyncFabricSwitch
+        self.sync_step.log = Mock()
+
+
+        # mock onos-fabric
+        onos_fabric = Mock()
+        onos_fabric.name = "onos-fabric"
+        onos_fabric.rest_hostname = "onos-fabric"
+        onos_fabric.rest_port = "8181"
+        onos_fabric.rest_username = "onos"
+        onos_fabric.rest_password = "rocks"
+
+        onos_fabric_base = Mock()
+        onos_fabric_base.leaf_model = onos_fabric
+
+        self.fabric = Mock()
+        self.fabric.name = "fabric"
+        self.fabric.provider_services = [onos_fabric_base]
+
+        # create a mock Switch instance
+        self.o = Mock()
+        self.o.name = "MockSwitch"
+        self.o.ofId = "of:1234"
+
+    def tearDown(self):
+        self.o = None
+        sys.path = self.sys_path_save
+
+    @requests_mock.Mocker()
+    def test_sync_switch(self, m):
+
+        self.o.ofId = "of:1234"
+        self.o.portId = "1"
+        self.o.driver = "ofdpa3"
+        self.o.ipv4NodeSid = "17"
+        self.o.ipv4Loopback = "192.168.0.201"
+        self.o.routerMac = "00:00:02:01:06:01"
+        self.o.isEdgeRouter = False
+
+        expected_conf = {
+            "devices": {
+                self.o.ofId: {
+                    "basic": {
+                    "name": self.o.name,
+                    "driver": self.o.driver
+                },
+                "segmentrouting" : {
+                    "name" : self.o.name,
+                    "ipv4NodeSid" : self.o.ipv4NodeSid,
+                    "ipv4Loopback" : self.o.ipv4Loopback,
+                    "routerMac" : self.o.routerMac,
+                    "isEdgeRouter" : self.o.isEdgeRouter,
+                    "adjacencySids" : []
+              }
+                }
+            }
+        }
+
+        m.post("http://onos-fabric:8181/onos/v1/network/configuration/",
+               status_code=200,
+               additional_matcher=functools.partial(match_json, expected_conf))
+
+        with patch.object(Service.objects, "get") as onos_fabric_get:
+            onos_fabric_get.return_value = self.fabric
+
+            self.sync_step().sync_record(self.o)
+
+            self.assertTrue(m.called)
+
+    @requests_mock.Mocker()
+    def test_delete_switch(self, m):
+        m.delete("http://onos-fabric:8181/onos/v1/network/configuration/devices/of:1234",
+            status_code=204)
+
+        self.o.ofId = "of:1234"
+
+        with patch.object(Service.objects, "get") as onos_fabric_get:
+            onos_fabric_get.return_value = self.fabric
+
+            self.sync_step().delete_record(self.o)
+
+            self.assertTrue(m.called)
diff --git a/src/fabric/xos/synchronizer/test_config.yaml b/src/fabric/xos/synchronizer/test_config.yaml
new file mode 100644 (file)
index 0000000..97a4327
--- /dev/null
@@ -0,0 +1,29 @@
+
+# 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: fabric
+accessor:
+  username: xosadmin@opencord.org
+  password: "sample"
+  kind: "testframework"
+logging:
+  version: 1
+  handlers:
+    console:
+      class: logging.StreamHandler
+  loggers:
+    'multistructlog':
+      handlers:
+          - console
diff --git a/src/fabric/xos/unittest.cfg b/src/fabric/xos/unittest.cfg
new file mode 100644 (file)
index 0000000..f28ec79
--- /dev/null
@@ -0,0 +1,12 @@
+[unittest]
+plugins=nose2.plugins.junitxml
+code-directories=synchronizer
+                 steps
+                 model_policies
+                 event_steps
+[coverage]
+always-on = True
+coverage = synchronizer
+coverage-report = term
+coverage-report = html
+coverage-report = xml