module ietf-softwire {
  namespace "urn:ietf:params:xml:ns:yang:softwire";
  prefix "softwire";

  import ietf-inet-types {prefix inet; }
  import ietf-yang-types {prefix yang; }

  organization "Softwire Working Group";

  contact
    "
    Qi Sun sunqi@csnet1.cs.tsinghua.edu.cn
    Hao Wang wangh13@mails.tsinghua.edu.cn
    Yong Cui yong@csnet1.cs.tsinghua.edu.cn
    Ian Farrer ian.farrer@telekom.de
    Mohamed Boucadair mohamed.boucadair@orange.com
    Rajiv Asati rajiva@cisco.com
    ";

  description
    "This document defines a YANG data model for the configuration and
    management of IPv4-in-IPv6 Softwire Border Routers and Customer
    Premises Equipment. It covers Lightweight 4over6, MAP-E and MAP-T
    Softwire mechanisms.

    Copyright (c) 2014 IETF Trust and the persons identified
    as authors of the code. All rights reserved.
    This version of this YANG module is part of RFC XXX; see the RFC
    itself for full legal notices.";

  revision 2015-04-07 {
    description
      "Version-03: Integrate lw4over6; Updata state nodes; Correct
          grammar errors; Reuse groupings; Update descriptions.
          Simplify the model.";
  }

  revision 2015-02-10 {
    description
      "Version-02: Add notifications.";
  }

  revision 2015-02-06 {
    description
      "Version-01: Correct grammar errors; Reuse groupings; Update
      descriptions.";
  }

  revision 2015-02-02 {
    description
      "Initial revision.";
  }

/*
 * Features
 */

  feature lw4over6 {
    description
      "Lightweight 4over6 (lw4over6) is an IPv4-over-IPv6 tunnelling
       transition mechanism. Lightweight 4over6 is a solution designed
       specifically for complete independence between IPv6 subnet prefix
       (and /128 IPv6 address) and IPv4 address with or without IPv4
       address sharing. This is accomplished by maintaining state for
       each softwire (per-subscriber state) in the central lwAFTR and a
       hub-and-spoke forwarding architecture. In order to delegate the
       NAPT function and achieve IPv4 address sharing, port-restricted
       IPv4 addresses needs to be allocated to CEs.";
    reference
      "I-D.ietf-softwire-lw4over6";
  }

  feature lwaftr {
    if-feature lw4over6;
    description
      "The AFTRs (BRs) for Lightweight 4over6, so-called lwAFTR. This
       feature indicates that a instance functions as a lwAFTR. A lwAFTR
       is an IPv4-in-IPv6 tunnel concentrator that maintains
       per-subscriber IPv4-IPv6 address binding.";
  }

  feature lwb4 {
    if-feature lw4over6;
    description
      "The B4s (CEs) for Lightweight 4over6, so-called lwB4. This
      feature indicates that a instance functions as a lwB4. A lwB4 is
      an IPv4-in-IPv6 tunnel initiator. It is dual-stack capable node,
      either a directly connected end-host or a CE. It sources IPv4
      conncections using the configured port-set and the public IPv4
      address.";
  }

  feature map-e {
    description
      "MAP-E is an IPv6 transition mechanism for transporting IPv4
      packets across an IPv6 network using IP encapsulation. MAP-E
      allows for a reduction of the amount of centralized state using
      rules to express IPv4/IPv6 address mappings. This introduces an
      algorithmic relationship between the IPv6 subnet and IPv4 address.
      This relationship also allows the option of direct, meshed
      connectivity between users. Alternatively, MAP-E can be configured
      to support IPv4/IPv6 indepent binding. This feature indicates the
      instance functions as a MAP-E instance.";
    reference
      "I-D.ietf-softwire-map";
  }

  feature map-t {
    description
      "The Mapping of Address and Port - Translation (MAP-T)
      architecture is a double stateless NAT64 based solution. It uses
      the stateless algorithmic address & transport layer port mapping
      scheme defined in MAP-E. The MAP-T solution differs from MAP-E in
      the use of IPv4-IPv6 translation, rather than encapsulation, as
      the form of IPv6 domain transport. This feature indicates the
      instance functions as a MAP-T instance. ";
    reference
      "I-D.ietf-softwire-map-t";
  }

/*
 * Grouping
 */

  grouping port-set {
    description
      "Use the PSID algorithm to represent a range of transport layer
      ports.";
    leaf offset {
      mandatory true;
      type uint8 {
        range 0..16;
      }
      description
        "The number of offset bits. In Lightweight 4over6, the defaul
        value is 0 for assigning one contiguous port range. In MAP-E/T,
        the default value is 6, which excludes system ports by default
        and assigns distributed port ranges. If the this parameter is
        larger than 0, the value of offset MUST be greater than 0.";
    }
    leaf psid {
      mandatory true;
      type uint16;
      description
        "Port Set Identifier (PSID) value, which identifies a set of
        ports algorithmically.";
    }
    leaf psid-len {
      mandatory true;
      type uint8 {
        range 0..16;
      }
      description
        "The length of PSID, representing the sharing ratio for a IPv4
        address.";
    }
  }

  grouping binding-entry {
    description
      "The lwAFTR maintains an address binding table that contains the
      binding between the lwB4's IPv6 address, the allocated IPv4
      address and restricted port-set.";
/*
    choice binding-ipv6info {
      description
        "Allow for the binding table to be specified in one of two ways,
        either with an explicit /128 or a /64 with the IID automatically
        constructed from the IPv4 address and port set.";
      case ipv6addr {
        leaf binding-ipv6-addr {
          mandatory true;
          type inet:ipv6-address;
          description
            "The /128 IPv6 address of the lwB4, which is used to bind
            the address and port-set and source the tunnel.";
        }
      }
      case ipv6pref {
        leaf binding-ipv6-prefix {
          mandatory true;
          type inet:ipv6-prefix;
          description
            "The operator-assigned IPv6 prefix of the lwB4. The lwAFTR
            uses the prefix to construct the /128 IPv6 address to complete
            the binding entry. ";
          reference
            "http://tools.ietf.org/html/draft-ietf-softwire-lw4over6-13#section-5.1,
            Figure 3";
        }
      }
    }
*/
    leaf binding-ipv6info {
      mandatory true;
      type union {
        type inet:ipv6-address;
        type inet:ipv6-prefix;
      }
      description
        "The IPv6 information for a binding entry. If it's an IPv6 prefix,
        it indicates that the IPv6 source address of the lwB4 is constructed
        according to the description in [I-D.ietf-softwire-lw4over6];
        if it's an IPv6 address, it means the lwB4 uses any /128 address
        from the assigned IPv6 prefix.
        ";
    }
    leaf binding-ipv4-addr {
      mandatory true;
      type inet:ipv4-address;
      description
        "The IPv4 address assigned to the lwB4, which is used as the
        IPv4 External Address for lwB4 local NAPT44.";
    }
    container port-set {
      uses port-set {
        refine offset {
          default "0";
        }
      }
    }
    leaf lwaftr-ipv6-addr {
      type inet:ipv6-prefix;
      description
        "The IPv6 address for lwaftr. Optional for the binding entry.";
    }
    leaf lifetime {
      type uint32;
      units seconds;
    }
  }

/*
  grouping nat-table {
    description
      "Grouping 'nat-table' is not extended. The current mechanism is
      focusing on the provisioning of external IP address and port set;
      other NAT-specific considerations are out of scope for this
      model.";
  }
*/

  grouping map-instance {
    leaf id {
      mandatory true;
      type uint32;
    }
    leaf name {
      type string;
    }
    container map-rules {
      list map-rule {
        description
          "A set of parameters describing the mapping between an IPv4
          prefix, IPv4 address or shared IPv4 address and an IPv6 prefix
          or address. Each domain uses a differe mapping rule set.";
        key "id";
        leaf id {
          type uint8;
        }
        leaf map-rule-type {
          mandatory true;
          type enumeration {
            enum "BMR";
            enum "FMR";
          }
          description
            "The BMR and FMR share the rule format. BMR is used for a node
            to configure itself with IPv4 information retrived from the
            rule. FMR is designed for the in-domain 4-in-6 routing, used
            in mesh mode. A BMR can be FMR in some case. The DMR for map-t
            is defined separately.";
        }
        leaf rule-ipv6-prefix {
          type inet:ipv6-prefix;
          mandatory true;
          description
            "The Rule IPv6 prefix defined in the mapping rule.";
        }
        leaf rule-ipv4-prefix {
          type inet:ipv4-prefix;
          mandatory true;
          description
            "The Rule IPv4 prefix defined in the mapping rule.";
        }
        container port-set {
          uses port-set {
            refine offset {
              default "6";
            }
          }
        }
        leaf ea-len {
          mandatory true;
          type uint8;
          description
            "Embedded Address (EA) bits are the IPv4 EA-bits in the IPv6
            address identify an IPv4 prefix/address (or part thereof) or
            a shared IPv4 address (or part thereof) and a port-set
            identifier. The length of the EA-bits is defined as part of a
            MAP rule for a MAP domain.";
        }
      }
    }
  }
  grouping traffic-stat {
    leaf sentPacket {
      type yang:zero-based-counter64;
    }
    leaf sentByte {
      type yang:zero-based-counter64;
    }
    leaf rcvdPacket {
      type yang:zero-based-counter64;
    }
    leaf rcvdByte {
      type yang:zero-based-counter64;
    }
    leaf droppedPacket {
      type yang:zero-based-counter64;
    }
    leaf droppedByte {
      type yang:zero-based-counter64;
    }
  }

/*
 * Configuration Data Nodes
 */
  container softwire-config {
    description
      "The configuration data for Softwire instances. And the shared
      data describes the softwire data model which is common to all of
      the different softwire mechanisms, such as description.";
    leaf description {
      type string;
      description
        "A textual description of Softwire.";
    }
    container lwaftr {
      if-feature lwaftr;
      description
        "Indicate this instance supports the lwAFTR function. The
        instances advertise the lwaftr feature through the
        capability exchange mechanism when a NETCONF session is
        established.";
      leaf enable {
        type boolean;
        description
          "Enable/disable the lwAFTR function.";
      }
      container lwaftr-instances {
        description
          "A set of lwAFTRs to be configured.";
        list lwaftr-instance {
          key "id";
          leaf id {
            type uint32;
          }
          leaf name {
            type string;
            description "The name for the lwaftr.";
          }
          leaf softwire-num-threshold {
            mandatory true;
            type uint32;
            description
              "The maximum number of tunnels that can be created on
              the lwAFTR.";
          }
          leaf tunnel-mtu {
            mandatory true;
            type uint32;
            description
              "The MTU for Lightweight 4over6 tunnel.";
          }
          leaf fragment-mru {
            mandatory true;
            type uint16;
            description
              "The fragmentation MRU for Lightweight 4over6 tunnel.";
          }
          container binding-table {
            list binding-entry {
              key "binding-ipv6info";
              uses binding-entry;
            }
          }
        }
      }
    }
    container lwb4 {
      if-feature lwb4;
      description
        "Indicate this instance supports the lwB4 function. The
        instances advertise the lwb4 feature through the
        capability exchange mechanism when a NETCONF session is
        established.";
      leaf enable {
        type boolean;
        description
          "Enable/disable the lwB4 function.";
      }
      container lwb4-instances {
        description
          "A set of lwB4s to be configured.";
        list lwb4-instance {
          key "binding-ipv6info";
          leaf name {
            type string;
            description "The lwb4 name.";
          }
          leaf tunnel-mtu {
            mandatory true;
            type uint32;
            description
              "The MTU for Lightweight 4over6 tunnel.";
          }
          leaf fragment-mru {
            mandatory true;
            type uint16;
            description
              "The fragment MRU for Lightweight 4over6 tunnel.";
          }
          leaf b4-ipv6-addr-format {
            type boolean;
            mandatory true;
            description
              "The format of lwB4 IPv6 address. If set to true, it
              indicates that the IPv6 source address of the lwB4 is
              constructed according to the description in
              [I-D.ietf-softwire-lw4over6]; if set to false, the lwB4
              can use any /128 address from the assigned IPv6 prefix.
              [DISCUSS] Is this redudant? ";
          }
          uses binding-entry;
        }
      }
    }
    container map-e {
      if-feature map-e;
      description
        "Indicate the instances support the MAP-E function. The
        instances advertise the map-e feature through the capability
        exchange mechanism when a NETCONF session is established.";
      leaf enable {
        type boolean;
        default "true";
        description
          "Enable/disable the MAP-E function.";
      }
      container map-e-instances {
        description
          "A set of MAP-E instances to be configured, applying to BRs and
          CEs.";
        list map-e-instance {
          key "id";
          uses map-instance;
          leaf tunnel-mtu {
            mandatory true;
            type uint32;
            description
              "The MTU for MAP-E tunnel.";
          }
          leaf fragment-mru {
            mandatory true;
            type uint16;
            description
              "The fragment MRU for MAP-E tunnel.";
          }
          leaf br-ipv6-addr {
            type inet:ipv6-address;
            description
              "The IPv6 address of the MAP-E BR.";
          }
        }
      }
    }
    container map-t {
      if-feature map-t;
      description
        "Indicate the instances support the MAP-T function. The
        instances advertise the map-t feature through the capability
        exchange mechanism when a NETCONF session is established.";
      leaf enable {
        type boolean;
        default "true";
        description
          "Enable/disable the MAP-T function.";
      }
      container map-t-instances {
        description
          "A set of the MAP-T instances to be configured, applying to BRs
          and CEs.";
        list map-t-instance {
          key "id";
          uses map-instance;
          leaf dmr-ipv6-prefix {
            type inet:ipv6-prefix;
            description
              "The IPv6 prefix of the MAP-T BR. ";
          }
        }
      }
    }
  }

/*
 * Operational state Data Nodes
 */

  container softwire-state {
    config false;
    description
      "The operational state data for Softwire instances. ";
    leaf description {
      type string;
      description
        "A textual description of the softwire instances.";
    }
    container lwaftr {
      if-feature lwaftr;
      config false;
      description
        "Indicate this instance supports the lwAFTR function. The
        instances advertise the lwaftr feature through the
        capability exchange mechanism when a NETCONF session is
        established.";
      container lwaftr-instances {
        description
          "A set of lwAFTRs.";
        list lwaftr-instance {
          key "id";
          leaf id {
            type uint32;
          }
          leaf name {
            type string;
            description "The name for this lwaftr.";
          }
          uses traffic-stat;
          leaf active-softwire-num {
            type uint32;
            description
              "The number of currently active tunnels on the lw4over6
              instance.";
          }
          container binding-table {
            list binding-entry {
              key "binding-ipv6info";
              leaf binding-ipv6info {
                mandatory true;
                type union {
                  type inet:ipv6-address;
                  type inet:ipv6-prefix;
                }
                description
                  "The IPv6 information used to identify a binding entry. ";
              }
              leaf active {
                type boolean;
                description
                  "Status of a specific tunnel.";
              }
            }
          }
        }
      }
    }
    container lwb4 {
      if-feature lwb4;
      config false;
      description
        "Indicate this instance supports the lwB4 function. The
        instances advertise the lwb4 feature through the
        capability exchange mechanism when a NETCONF session is
        established.";
      container lwb4-instances {
        description
          "Status of the configured lwB4s.";
        list lwb4-instance {
          key "binding-ipv6info";
          leaf name {
            type string;
            description "The lwb4 name.";
          }
          leaf binding-ipv6info {
            mandatory true;
            type union {
              type inet:ipv6-address;
              type inet:ipv6-prefix;
            }
            description
              "The IPv6 information used to identify a binding entry. ";
          }
          uses traffic-stat;
        }
      }
    }
    container map-e {
      if-feature map-e;
      config false;
      description
        "Indicate the instances support the MAP-E function. The
        instances advertise the map-e feature through the capability
        exchange mechanism when a NETCONF session is established.";
      container map-e-instances {
        description
          "Status of MAP-E instance(s).";
        list map-e-instance {
          key "id";
          leaf id {
            type int32;
          }
          leaf name {
            type string;
            description "The map-e instance name.";
          }
          uses traffic-stat;
        }
      }
    }
    container map-t {
      if-feature map-t;
      config false;
      description
        "Indicate the instances support the MAP-T function. The
        instances advertise the map-t feature through the capability
        exchange mechanism when a NETCONF session is established.";
      container map-t-instances {
        description
          "Status of MAP-T instances.";
        list map-t-instance {
          key "id";
          leaf id {
            type int32;
          }
          leaf name {
            type string;
            description "The map-t instance name.";
          }
          uses traffic-stat;
        }
      }
    }
  }

/*
 * Notifications
 */
  notification softwire-lwaftr-event {
    if-feature lwaftr;
    leaf lwaftr-id {
      mandatory true;
      type leafref {
        path
          "/softwire-state/lwaftr/lwaftr-instances/"
          + "lwaftr-instance/id";
      }
    }
    leaf-list invalid-entry {
      type leafref {
        path
          "/softwire-config/lwaftr/lwaftr-instances/"
          + "lwaftr-instance[id=current()/../lwaftr-id]/"
          + "binding-table/binding-entry/binding-ipv6info";
      }
      description
        "Notify the client that a specific binding entry has been
        expired/invalid. The binding-ipv6info identifies an entry.";
    }
    leaf-list added-entry {
        type inet:ipv6-address;
        description
          "Notify the client that a binding entry has been added.
          The ipv6 address of that entry is the index. The client
          get other information from the lwaftr about the entry
          indexed by that ipv6 address.
          ";
    }
    leaf-list modified-entry {
        type leafref {
          path
            "/softwire-config/lwaftr/lwaftr-instances/"
            + "lwaftr-instance[id=current()/../lwaftr-id]/"
            + "binding-table/binding-entry/binding-ipv6info";
        }
    }
  }
  notification softwire-lwb4-event {
    if-feature lwb4;
    leaf lwb4-binding-ipv6-addr-change {
      mandatory true;
      type inet:ipv6-address;
      description
        "The source tunnel IPv6 address of the lwB4. If 'b4-ipv6-addr-format'
        is false, or the lwb4's binding-ipv6-address changes for any reason,
        it SHOULD notify the NETCONF client.";
    }
  }
  notification softwire-map-e-event {
    if-feature map-e;
    leaf map-e-id {
      mandatory true;
      type leafref {
        path
          "/softwire-config/map-e/map-e-instances/map-e-instance/id";
      }
    }
    leaf-list invalid-entry-id {
      type leafref {
        path
          "/softwire-config/map-e/map-e-instances/"
          + "map-e-instance[id=current()/../map-e-id]/map-rules/"
          + "map-rule/id";
      }
    }
    leaf-list added-entry {
      type uint32;
    }
    leaf-list modified-entry {
      type leafref {
        path
          "/softwire-config/map-e/map-e-instances/"
          + "map-e-instance[id=current()/../map-e-id]/map-rules/"
          + "map-rule/id";
      }
    }
  }
  notification softwire-map-t-event {
    if-feature map-t;
    leaf map-t-id {
      mandatory true;
      type leafref {
        path
          "/softwire-config/map-t/map-t-instances/map-t-instance/id";
      }
    }
    leaf-list invalid-entry-id {
      type leafref {
        path
          "/softwire-config/map-t/map-t-instances/"
          + "map-t-instance[id=current()/../map-t-id]/map-rules/"
          + "map-rule/id";
      }
    }
    leaf-list added-entry {
      type uint32;
    }
    leaf-list modified-entry {
      type leafref {
        path
          "/softwire-config/map-t/map-t-instances/"
          + "map-t-instance[id=current()/../map-t-id]/map-rules/"
          + "map-rule/id";
      }
    }
  }
}
