summaryrefslogtreecommitdiffstats
path: root/source/promise.yaml
blob: 98dcdc47e6d9525ab9b0a33f1eb638fc4b3196b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#
# Author: Peter K. Lee (peter@corenova.com)
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
#

name: opnfv-promise
description: Resource Management for Virtualized Infrastructure
author: Peter K. Lee <peter@corenova.com>
license: Apache-2.0
homepage: http://wiki.opnfv.org/promise
repository: git://github.com/opnfv/promise.git
yangforge: "0.11.x"
keywords:
  - opnfv
  - promise
  - vim
  - nfvi
  - infrastructure
  - openstack
  - nbi
  - yangforge
  - resource
  - reservation
  - capacity
  - allocation

schema: !yang schema/opnfv-promise.yang

# below config provides default parameters
# NOTE: uncomment locally for testing with pre-existing data
#config: !json config/demo.json

dependencies:
  access-control-models: !yang schema/access-control-models.yang
  nfv-infrastructure:    !yang schema/nfv-infrastructure.yang

# MODULE model active bindings
module:
  opnfv-promise:
    # rebind to be a computed property
    promise.capacity.total: !coffee/function |
      (prev) -> @computed (->
        combine = (a, b) ->
          for k, v of b.capacity when v?
            a[k] ?= 0
            a[k] += v
          return a
        (@parent.get 'pools')
        .filter (entry) -> entry.active is true
        .reduce combine, {}
      ), type: prev
    # rebind to be a computed property
    promise.capacity.reserved: !coffee/function |
      (prev) -> @computed (->
        combine = (a, b) ->
          for k, v of b.remaining when v?
            a[k] ?= 0
            a[k] += v
          return a
        (@parent.get 'reservations')
        .filter (entry) -> entry.active is true
        .reduce combine, {}
      ), type: prev
    # rebind to be a computed property
    promise.capacity.usage: !coffee/function |
      (prev) -> @computed (->
        combine = (a, b) ->
          for k, v of b.capacity when v?
            a[k] ?= 0
            a[k] += v
          return a
        (@parent.get 'allocations')
        .filter (entry) -> entry.active is true
        .reduce combine, {}
      ), type: prev
    # rebind to be a computed property
    promise.capacity.available: !coffee/function |
      (prev) -> @computed (->
        total = @get 'total'
        reserved = @get 'reserved'
        usage = @get 'usage'
        for k, v of total when v?
          total[k] -= reserved[k] if reserved[k]?
          total[k] -= usage[k] if usage[k]?
        total
      ), type: prev

# RPC definitions (INTENT interfaces)
rpc: !require spec/promise-intents.coffee

# COMPLEX-TYPE model active bindings (controller logic)
complex-type:
  ResourceElement:
    #properties
    id: !coffee/function |
      (prev) -> prev.set 'default', -> @uuid()

  ResourceCollection:
    # properties
    start: !coffee/function |
      (prev) -> prev.set 'default', -> (new Date).toJSON()

    active: !coffee/function |
      (prev) -> @computed (->
        now = new Date
        start = new Date (@get 'start')
        end = switch
          when (@get 'end')? then new Date (@get 'end')
          else now
        (@get 'enabled') and (start <= now <= end)
      ), type: prev

  ResourceReservation:
    end: !coffee/function |
      (prev) -> prev.set 'default', ->
        end = (new Date @get 'start')
        max = @parent.get 'promise.policy.reservation.max-duration'
        return unless max?
        end.setTime (end.getTime() + (max*60*60*1000))
        end.toJSON()

    allocations: !coffee/function |
      (prev) -> @computed (->
        res = (@store.find 'ResourceAllocation', reservation: @id)
        res.map (x) -> x.get 'id'
      ), type: 'array'

    remaining: !coffee/function |
      (prev) -> @computed (->
        total = @get 'capacity'
        records = @store.find 'ResourceAllocation', id: (@get 'allocations'), active: true
        for entry in records
          usage = entry.get 'capacity'
          for k, v of usage
            total[k] -= v
        total
      ), type: prev

    # methods
    validate: !coffee/function |
      (prev) -> (value={}, resolve, reject) ->
        # validate that request contains sufficient data
        for k, v of value.capacity when v? and !!v
          hasCapacity = true
        if (not hasCapacity) and value.elements.length is 0
          return reject "unable to validate reservation record without anything being reserved"
        # time range verifications
        now = new Date
        start = (new Date value.start) if value.start?
        end   = (new Date value.end) if value.end?
        # if start? and start < now
        #   return reject "requested start time #{value.start} cannot be in the past"
        if end? and end < now
          return reject "requested end time #{value.end} cannot be in the past"
        if start? and end? and start > end
          retun reject "requested start time must be earlier than end time"
        resolve this

    update: !coffee/function |
      (prev) -> (req, resolve, reject) ->
        req.start ?= @get 'start'
        req.end   ?= @get 'end'

        # TODO: should validate here...
        @parent.invoke 'query-capacity',
          start: req.start
          end: req.end
          capacity: 'available'
          without: @id
        .then (res) =>
          collections = res.get 'collections'
          unless collections.length > 0
            return reject 'no resource capacity available during requested start/end time'

          pools = collections.filter (e) -> /^ResourcePool/.test e
          # should do some policy or check to see if more than one pool acceptable to reservee

          entries = res.get 'utilization'
          start = new Date req.start
          end   = new Date req.end

          for x in [0..entries.length-1]
            t1 = new Date entries[x].timestamp
            break unless t1 < end

            if x < entries.length-1
              t2 = new Date entries[x+1].timestamp
              continue unless t2 > start

            available = entries[x].capacity
            for k, v of req.capacity when v? and !!v
              unless available[k] >= v
                return reject "requested #{k}=#{v} exceeds available #{available[k]} between #{t1} and #{t2}"

          @set req
          @set 'pools', pools
          resolve this
        .catch (err) -> reject err

    save: !coffee/function |
      (prev) -> (resolve, reject) ->
        @invoke 'validate', @get()
        .then (res) ->
          # should do something about this reservation record...
          now = (new Date).toJSON()
          unless (res.get 'created-on')?
            res.set 'created-on', now
          res.set 'modified-on', now
          resolve res
        .catch (e) -> reject e

  ResourceAllocation:
    # properties
    priority: !coffee/function |
      (prev) -> @computed (->
        switch
          when not (@get 'reservation')? then 3
          when not (@get 'active') then 2
          else 1
      ), type: prev

  ResourcePool:
    save: !coffee/function |
      (prev) -> (resolve, reject) ->
        # validate that record contains sufficient data
        value = @get()

        for k, v of value.capacity when v? and !!v
          hasCapacity = true
        if (not hasCapacity) and value.elements.length is 0
          return reject "unable to save pool record without any capacity values"

        resolve this

  ResourceProvider:
    # properties
    token: !coffee/function |
      (prev) -> prev.set 'private', true

    pools: !coffee/function |
      (prev) -> @computed (->
        (@store.find 'ResourcePool', source: (@get 'name')).map (x) -> x.get 'id'
      ), type: 'array'

    # methods
    # XXX - this method is OpenStack-specific only, will need to revise later
    update: !coffee/function |
      (prev) -> (services=[], resolve, reject) ->
        return reject "unable to update provider without list of services" unless services.length
        request = @store.parent.require 'superagent'
        services.forEach (service) =>
          switch service.type
            when 'compute'
              url = service.endpoints[0].url
              @set 'services.compute.endpoint', url
              request
                .get "#{url}/limits"
                  .set 'X-Auth-Token', @get 'token'
                  .set 'Accept', 'application/json'
                  .end (err, res) =>
                    if err? or !res.ok
                      console.warn "request to discover capacity limits failed"
                      return

                    capacity = res.body.limits?.absolute
                    #console.log "\ndiscovered capacity:"
                    #console.log capacity

                    (@access 'capacity').set {
                      cores: capacity.maxTotalCores
                      ram: capacity.maxTotalRAMSize
                      instances: capacity.maxTotalInstances
                      addresses: capacity.maxTotalFloatingIps
                    }
              request
                .get "#{url}/flavors/detail"
                  .set 'X-Auth-Token', @get 'token'
                  .set 'Accept', 'application/json'
                  .end (err, res) =>
                    if err? or !res.ok
                      console.warn "request to discover compute flavors failed"
                      return

                    flavors = res.body.flavors
                    # console.log "\ndiscovered flavors:"
                    # console.log flavors
                    try
                      flavors = flavors.map (x) -> ResourceFlavor: x
                      (@access 'services.compute.flavors').push flavors...
                    catch er
                      console.warn "failed to update flavors into the provider due to validation errors"

        # XXX - update should do promise.all
        resolve this