aboutsummaryrefslogtreecommitdiffstats
path: root/sli/common/src/main/java/org/onap/ccsdk/sli/core/sli/SvcLogicLoader.java
blob: 95f73f960d3761dc2fcbc54f4dadc095c99fa46d (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
/*-
 * ============LICENSE_START=======================================================
 * openECOMP : SDN-C
 * ================================================================================
 * 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.
 * ============LICENSE_END=========================================================
 */

package org.onap.ccsdk.sli.core.sli;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SvcLogicLoader {
    private static final Logger LOGGER = LoggerFactory.getLogger(SvcLogicLoader.class);
    protected SvcLogicStore store;
    protected String directoryRoot;

    public SvcLogicLoader(String directoryRoot, SvcLogicStore store) {
        this.store = store;
        this.directoryRoot = directoryRoot;
    }

    public SvcLogicLoader(String directoryRoot, String propFile) {
        this.store = SvcLogicParser.getStore(propFile);
        this.directoryRoot = directoryRoot;
    }

    public void loadAndActivate() throws IOException {
        SvcLogicCrawler slc = new SvcLogicCrawler();
        Files.walkFileTree(Paths.get(directoryRoot), slc);
        loadGraphs(slc.getGraphPaths(), directoryRoot);
        List<ActivationEntry> activationEntries = processActivationFiles(slc.getActivationPaths());
        activateGraphs(activationEntries);
    }

    private List<ActivationEntry> processActivationFiles(List<Path> activationPaths) {
        List<ActivationEntry> activationEntries = new ArrayList<ActivationEntry>();
        for (Path activationFile : activationPaths) {
            activationEntries.addAll(getActivationEntries(activationFile));
        }
        return activationEntries;
    }

    private void activateGraphs(List<ActivationEntry> activationEntries) {
        for (ActivationEntry entry : activationEntries) {
            try {
                if (store.hasGraph(entry.module, entry.rpc, entry.version, entry.mode)) {
                    store.activate(entry.module, entry.rpc, entry.version, entry.mode);
                } else {
                    LOGGER.error("hasGraph returned false for " + entry.toString());
                }
            } catch (SvcLogicException e) {
                LOGGER.error("Failed to call hasGraph for " + entry.toString(), e);
            }
        }
    }

    protected List<ActivationEntry> getActivationEntries(Path activationFilePath) {
        List<ActivationEntry> activationEntries = new ArrayList<>();
        int lineNumber = 1;
        try (BufferedReader br = Files.newBufferedReader(activationFilePath, StandardCharsets.US_ASCII)) {
            String fileRead = br.readLine();
            while (fileRead != null) {
                String[] fields = fileRead.split("\\s");
                if (fields.length == 4) {
                    activationEntries.add(parseActivationEntry(fields));
                } else {
                    LOGGER.error("Activation entry [" + fileRead + "] is declared at line number " + lineNumber
                            + " in the file " + activationFilePath + " and is invalid.");
                }
                fileRead = br.readLine();
                lineNumber++;
            }
            return activationEntries;
        } catch (IOException ioe) {
            LOGGER.error("Couldn't read the activation file at " + activationFilePath, ioe);
            return null;
        }
    }

    protected void loadGraphs(List<Path> graphPaths, String directoryRoot) {
        for (Path graphPath : graphPaths) {
            try {
                saveGraph(graphPath.toString());
            } catch (Exception e) {
                LOGGER.error("Couldn't load graph at " + graphPath, e);
            }
        }
    }

    private void saveGraph(String xmlFile) throws SvcLogicException {
        File f = new File(xmlFile);
        if (!f.canRead()) {
            throw new ConfigurationException("Cannot read xml file (" + xmlFile + ")");
        }

        SvcLogicParser parser = new SvcLogicParser();
        LinkedList<SvcLogicGraph> graphs = null;

        try {
            graphs = parser.parse(xmlFile);
        } catch (Exception e) {
            throw new SvcLogicException(e.getMessage(), e);
        }

        if (graphs == null) {
            throw new SvcLogicException("Could not parse " + xmlFile);
        }

        for (Iterator<SvcLogicGraph> iter = graphs.iterator(); iter.hasNext();) {
            SvcLogicGraph graph = iter.next();
            try {
                LOGGER.info("Saving " + graph.toString() + " to database");
                store.store(graph);
            } catch (Exception e) {
                throw new SvcLogicException(e.getMessage(), e);
            }
        }
    }

    protected ActivationEntry parseActivationEntry(String[] fileInput) {
        return new ActivationEntry(fileInput[0], fileInput[1], fileInput[2], fileInput[3]);
    }

    protected String getValue(String raw, String attributeName) {
        raw = raw.substring(attributeName.length() + 1);
        if (raw.contains(">")) {
            raw = raw.substring(0, raw.lastIndexOf('>'));
        }
        if (raw.endsWith("'")) {
            raw = raw.substring(0, raw.lastIndexOf('\''));
        }
        if (raw.endsWith("\"")) {
            raw = raw.substring(0, raw.lastIndexOf('"'));
        }
        return raw;
    }

}