/*
 *
 *   Copyright 2016 RIFT.IO Inc
 *
 *   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.
 *
 *
 */

/**
 * @file rw-pkg-mgmt.yang
 * @author Varun Prasad
 * @date 2016/09/21
 * @brief Pacakage Management Yang
 */

module rw-pkg-mgmt
{
  namespace "http://riftio.com/ns/riftware-1.0/rw-pkg-mgmt";
  prefix "rw-pkg-mgmt";

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

  import rw-pb-ext {
    prefix "rwpb";
  }

  import rw-cli-ext {
    prefix "rwcli";
  }

  import rw-cloud {
    prefix "rwcloud";
  }

  import rwcal {
    prefix "rwcal";
  }

  import mano-types {
    prefix "manotypes";
  }

  import rw-vnfd {
    prefix "rwvnfd";
  }
  import rw-nsd {
    prefix "rwnsd";
  }

  revision 2016-06-01 {
    description
      "Initial revision.";
  }

  typedef task-status {
    type enumeration {
      enum QUEUED;
      enum IN_PROGRESS;
      enum DOWNLOADING;
      enum CANCELLED;
      enum COMPLETED;
      enum FAILED;
    }
  }

  typedef export-schema {
    type enumeration {
      enum RIFT;
      enum MANO;
    }
  }

  typedef export-grammar {
    type enumeration {
      enum OSM;
      enum TOSCA;
    }
  }

  typedef export-format {
    type enumeration {
      enum YAML;
      enum JSON;
    }
  }

  grouping external-url-data {
    leaf external-url {
      description "Url to download";
      type string;
    }

    leaf username {
      description "username if the url uses authentication";
      type string;
    }

    leaf password {
      description "password if the url uses authentication";
      type string;
    }
  }

  grouping package-identifer {
    leaf package-type {
      description "Type of the package";
      type manotypes:package-type;
    }

    leaf package-id {
      description "Id of the package";
      type string;
    }
  }

  grouping package-file-identifer {
    uses package-identifer;

    leaf package-path {
      description "Relative path in the package";
      type string;
    }
  }

  grouping download-task-status {
    leaf status {
      description "The status of the download task";
      type task-status;
      default QUEUED;
    }

    leaf detail {
      description "Detailed download status message";
      type string;
    }

    leaf progress-percent {
      description "The download progress percentage (0-100)";
      type uint8;
      default 0;
    }

    leaf bytes_downloaded {
      description "The number of bytes downloaded";
      type uint64;
      default 0;
    }

    leaf bytes_total {
      description "The total number of bytes to write";
      type uint64;
      default 0;
    }

    leaf bytes_per_second {
      description "The total number of bytes written per second";
      type uint32;
      default 0;
    }

    leaf start-time {
      description "start time (unix epoch)";
      type uint32;
    }

    leaf stop-time {
      description "stop time (unix epoch)";
      type uint32;
    }
  }

  grouping copy-task-status {
    leaf status {
      description "The status of the copy task";
      type task-status;
      default QUEUED;
    }
  }

  container download-jobs {
    rwpb:msg-new DownloadJobs;
    description "Download jobs";
    config false;

    list job {
      rwpb:msg-new DownloadJob;
      key "download-id";

      leaf download-id {
        description "Unique UUID";
        type string;
      }

      leaf url {
        description "URL of the download";
        type string;
      }

      uses package-file-identifer;
      uses download-task-status;
    }
  }

  container copy-jobs {
    rwpb:msg-new CopyJobs;
    description "Copy jobs";
    config false;

    list job {
      rwpb:msg-new CopyJob;
      key "transaction-id";

      leaf transaction-id {
        description "Unique UUID";
        type string;
      }

      uses copy-task-status;
    }
  }

  rpc get-package-endpoint {
    description "Retrieves the endpoint for the descriptor";

    input {
      uses package-identifer;
    }

    output {
     leaf endpoint {
        description "Endpoint that contains all the package-related data";
        type string;
      }
    }
  }

  rpc package-copy {
    description "Copies the package specified in input and returns the copied package id";

    input {
      uses package-identifer;

      leaf package-name {
        description "Name of destination package";
        type string;
      }
    }

    output {
     leaf transaction-id {
        description "Valid ID to track the status of the task";
        type string;
      }

     uses package-identifer;
    }
  }

  rpc get-package-schema {
    description "Retrieves the schema for the package type";

    input {
      leaf package-type {
        description "Type of the package";
        type manotypes:package-type;
      }
    }

    output {
      leaf-list schema {
        description "List of all top level directories for the package.";
        type string;
      }
    }
  }

  rpc package-create {
    description "Creates a new package";

    input {
      uses package-identifer;
      uses external-url-data;
    }

    output {
     leaf transaction-id {
        description "Valid ID to track the status of the task";
        type string;
      }
    }
  }

  rpc package-update {
    description "Creates a new package";

    input {
      uses package-identifer;
      uses external-url-data;
    }

    output {
     leaf transaction-id {
        description "Valid ID to track the status of the task";
        type string;
      }
    }
  }

  rpc package-export {
    description "Export a package";

    input {
      uses package-identifer;

      leaf export-schema {
        description "Schema to export";
        type export-schema;
        default RIFT;
      }

      leaf export-grammar {
        description "Schema to export";
        type export-grammar;
        default OSM;
      }

      leaf export-format {
        description "Format to export";
        type export-format;
        default YAML;
      }

    }

    output {
     leaf transaction-id {
        description "Valid ID to track the status of the task";
        type string;
      }

     leaf filename {
        description "Valid ID to track the status of the task";
        type string;
      }
    }
  }

  rpc package-file-add {
    description "Retrieves the file from the URL and store it in the package";

    input {
      uses package-file-identifer;
      uses external-url-data;
    }

    output {
     leaf task-id {
        description "Valid ID to track the status of the task";
        type string;
      }
    }
  }

  rpc package-file-delete {
    description "Retrieves the file from the URL and store it in the package";

    input {
      uses package-file-identifer;
    }

    output {
     leaf status {
        description "Status of the delte operation";
        type string;
      }

      leaf error-trace {
        description "Trace in case of a failure";
        type string;
      }

    }
  }

}
