aboutsummaryrefslogtreecommitdiffstats
path: root/lib/events.js
blob: 11a3ec02b50ce921aeef6b6cb84f3e4b09489196 (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
/*
Copyright(c) 2017 AT&T Intellectual Property. All rights reserved. 

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.
*/

/* Handle the /events API */

"use strict";

const router = require('express').Router();
const bodyParser = require('body-parser');
const deploy = require('./deploy');
const middleware = require('./middleware');
const inventory = require('./inventory');
const logAccess = require('./logging').logAccess;
const services = require('./services');

/* Required properties for event POST */
const requiredProps = ['dcae_target_name','dcae_target_type','dcae_service_action','dcae_service_location'];

/* Pick up config exported by main */
const config = process.mainModule.exports.config;
const logger = config.logSource.getLogger('events');

/* Set up middleware stack for initial processing of request */
router.use(middleware.checkType('application/json'));		// Validate type
router.use(bodyParser.json({strict: true}));				// Parse body as JSON
router.use(middleware.checkProps(requiredProps));			// Make sure we have required properties
router.use(inventory.checkInventory);						// Get template(s) (deploy) or services (undeploy)
router.use(middleware.checkLocation);						// Check location and get location information
router.use(middleware.expandTemplates);						// Expand any blueprint templates


/* Accept an incoming event */
router.post('/', function(req, res, next) {
	let response = {requestId: req.dcaeReqId, deploymentIds:[]};
	
	if (req.body.dcae_service_action === 'deploy') {
		
		/* Deploy services for the VNF */
		
		/* req.dcae_blueprints has been populated by the expandTemplates middleware */
		logger.info(req.dcaeReqId + " services to deploy: " + JSON.stringify(req.dcae_blueprints));
		logger.debug(JSON.stringify(req.dcae_shareables, null, '\t'));
		logger.debug(JSON.stringify(req.dcae_locations, null, '\t'));
		
		/* Create a deployer function and use it for each of the services */
		let deployer = services.createDeployer(req);
		let outputs = req.dcae_blueprints.map(deployer);
		response.deploymentIds = req.dcae_blueprints.map(function(s) {return s.deploymentId;});
	}
	else {
		
		/* Undeploy services for the VNF */
		
		/* req.dcae_services has been populated by the checkInventory middleware */	
		logger.info(req.dcaeReqId + " deployments to undeploy: " + JSON.stringify(req.dcae_services));
		
		/* Create an undeployer function and use it for each of the services */
		let undeployer = services.createUndeployer(req);
		req.dcae_services.forEach(undeployer);
		response.deploymentIds = req.dcae_services.map(function(s) {return s.deploymentId;});
	}
	res.status(202).json(response);
	logAccess(req, 202);
});

module.exports = router;