Illyoung Choi | 59820ed | 2019-06-24 17:01:00 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2019-present Open Networking Foundation |
| 3 | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | (function () { |
| 18 | 'use strict'; |
| 19 | |
| 20 | const path = require('path'); |
| 21 | const fs = require('fs'); |
| 22 | const _ = require('lodash'); |
| 23 | const Workflow = require('../types/workflow.js'); |
| 24 | const logger = require('../config/logger.js'); |
| 25 | |
| 26 | const loadEssence = (essenceFilename, absPath=false) => { |
| 27 | let filepath; |
| 28 | if(!absPath) { |
| 29 | filepath = path.join(__dirname, essenceFilename); |
| 30 | } |
| 31 | else { |
| 32 | filepath = essenceFilename; |
| 33 | } |
| 34 | |
| 35 | try { |
| 36 | if (fs.existsSync(filepath)) { |
| 37 | logger.log('debug', `Loading an essence - ${filepath}`); |
| 38 | let rawdata = fs.readFileSync(filepath); |
| 39 | let essence = null; |
| 40 | try { |
| 41 | essence = JSON.parse(rawdata); |
| 42 | } |
| 43 | catch (objError) { |
| 44 | if (objError instanceof SyntaxError) { |
| 45 | logger.log('warn', `failed to parse a json data (syntax error) - ${rawdata}`); |
| 46 | } |
| 47 | else { |
| 48 | logger.log('warn', `failed to parse a json data - ${rawdata}`); |
| 49 | } |
| 50 | } |
| 51 | return essence; |
| 52 | } |
| 53 | else { |
| 54 | logger.log('warn', `No ${filepath} found`); |
| 55 | return null; |
| 56 | } |
| 57 | } |
| 58 | catch(e) { |
| 59 | logger.log('warn', `Cannot read ${filepath} - ${e}`); |
| 60 | return null; |
| 61 | } |
| 62 | }; |
| 63 | |
| 64 | const loadWorkflows = (essenceFilename) => { |
| 65 | let filepath = path.join(__dirname, essenceFilename); |
| 66 | |
| 67 | try { |
| 68 | if (fs.existsSync(filepath)) { |
| 69 | logger.log('debug', `Loading an essence - ${filepath}`); |
| 70 | let rawdata = fs.readFileSync(filepath); |
| 71 | let workflows = []; |
| 72 | |
| 73 | try { |
| 74 | let essence = JSON.parse(rawdata); |
| 75 | |
| 76 | // an essence can have multiple workflows |
| 77 | _.forOwn(essence, (workflowEssence, workflowId) => { |
| 78 | let workflow = Workflow.Workflow.fromEssence(workflowEssence); |
| 79 | if(workflow) { |
| 80 | workflows.push(workflow); |
| 81 | logger.log('debug', `Loaded workflow: ${workflowId}`); |
| 82 | } |
| 83 | }); |
| 84 | } |
| 85 | catch (objError) { |
| 86 | if (objError instanceof SyntaxError) { |
| 87 | logger.log('warn', `failed to parse a json data (syntax error) - ${rawdata}`); |
| 88 | } |
| 89 | else { |
| 90 | logger.log('warn', `failed to parse a json data - ${rawdata}`); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | return workflows |
| 95 | } |
| 96 | else { |
| 97 | logger.log('warn', `No ${filepath} found`); |
| 98 | return null; |
| 99 | } |
| 100 | } |
| 101 | catch(e) { |
| 102 | logger.log('warn', `Cannot read ${filepath} - ${e}`); |
| 103 | return null; |
| 104 | } |
| 105 | }; |
| 106 | |
| 107 | const loadAllWorkflows = () => { |
| 108 | let dirpath = __dirname; |
| 109 | |
| 110 | let allWorkflows = []; |
| 111 | let dirEntries = fs.readdirSync(dirpath); |
| 112 | dirEntries.forEach((dirEntry) => { |
| 113 | if(dirEntry.endsWith('.json')) { |
| 114 | // found workflow essence file in json format |
| 115 | let workflows = loadWorkflows(dirEntry); |
| 116 | if(workflows) { |
| 117 | for(let workflow in workflows) { |
| 118 | allWorkflows.push[workflow]; |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | }); |
| 123 | return allWorkflows; |
| 124 | }; |
| 125 | |
| 126 | module.exports = { |
| 127 | loadEssence: loadEssence, |
| 128 | loadAllWorkflows: loadAllWorkflows, |
| 129 | loadWorkflows: loadWorkflows |
| 130 | }; |
| 131 | })(); |