summaryrefslogtreecommitdiffstats
path: root/ecomp-sdk/epsdk-core/src/main/java/org/onap/portalsdk/core/service/ElementLinkService.java
blob: f5cfdd3859c75a07ce15026e1a65cde8e538f6a3 (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
/*
 * ============LICENSE_START==========================================
 * ONAP Portal SDK
 * ===================================================================
 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
 * ===================================================================
 *
 * Unless otherwise specified, all software contained herein is licensed
 * under the Apache License, Version 2.0 (the "License");
 * you may not use this software 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.
 *
 * Unless otherwise specified, all documentation contained herein is licensed
 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
 * you may not use this documentation except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *             https://creativecommons.org/licenses/by/4.0/
 *
 * Unless required by applicable law or agreed to in writing, documentation
 * 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.portalsdk.core.service;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FilenameUtils;
import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
import org.onap.portalsdk.core.util.SystemProperties;
import org.onap.portalsdk.core.util.YamlUtils;

public class ElementLinkService {

	private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ElementLinkService.class);

	/**
	 * Builds renderable model of element links in the network map. Parses YAML
	 * files with metadata and builds input for JoinJS to render in the browser as
	 * SVG.
	 *
	 * @param args
	 *            arg 0 - realPath; arg 1 - callFlowName; arg 2 - callFlowStep.
	 * @return Renderable model of element links
	 * @throws IOException
	 */
	public String buildElementLinkYaml(String[] args) throws IOException {
		String relFilePath;
		if (args[1].startsWith("custom"))
			relFilePath = SystemProperties.getProperty("customCallFlow_path");
		else
			relFilePath = SystemProperties.getProperty("element_map_file_path");
		final String yamlDirPath = FilenameUtils.normalize(new File(args[0], relFilePath).getPath());

		String callFlowBusinessYml = "";
		String callFlowStep = "";

		if (args != null && args.length > 0) {
			if (args[1] != null)
				callFlowBusinessYml = args[1] + ".yml";
			if (args[2] != null)
				callFlowStep = args[2];
		}

		ElementLinkService mapper = new ElementLinkService();
		String linkYaml = mapper.createLinkFile(yamlDirPath, callFlowBusinessYml, callFlowStep);
		return linkYaml;
	}

	@SuppressWarnings("unchecked")
	protected String createLinkFile(String resourceFilePath, String callFLowBsFileName, String callFlowStep)
			throws IOException {
		Map<String, Object> callFlowBs = YamlUtils.readYamlFile(resourceFilePath, callFLowBsFileName);
		List<Map<String, Object>> callSteps = (List<Map<String, Object>>) callFlowBs.get("callSequenceSteps");
		String callFlowName = (String) callFlowBs.get("shortName");
		return addLinks(resourceFilePath, callFlowName, callSteps, callFlowStep);
	}

	@SuppressWarnings("unchecked")
	protected String createLinkFileAdditional(String resourceFilePath, String callFLowBsFileName, String callFlowStep)
			throws Exception {
		Map<String, Object> callFlowBs;
		try {
			callFlowBs = YamlUtils.readYamlFile(resourceFilePath, callFLowBsFileName);
			List<Map<String, Object>> callSteps = (List<Map<String, Object>>) callFlowBs.get("callSequenceSteps");
			String callFlowName = (String) callFlowBs.get("shortName");
			return addLinksAdditional(resourceFilePath, callFlowName, callSteps, callFlowStep);
		} catch (Exception e) {
			logger.error(EELFLoggerDelegate.errorLogger, "createLinkFileAdditional failed", e);
			return "";
		}

	}

	@SuppressWarnings("unchecked")
	protected String addLinks(String filePath, String callFlowName, List<Map<String, Object>> callSteps,
			String callFlowStep) throws IOException {

		Map<String, List<String>> checkDuplicateMap = new HashMap<>();

		for (Map<String, Object> callStep : callSteps) {

			if (((String) callStep.get("name")).split(":")[0].trim().replace(" ", "_").equals(callFlowStep)) {

				List<Map<String, Object>> links = new ArrayList<>();

				List<Map<String, Object>> subSteps = (List<Map<String, Object>>) callStep.get("subSteps");

				for (Map<String, Object> subStep : subSteps) {
					Map<String, Object> link = new HashMap<>();

					String source = (String) subStep.get("source_tosca_id");
					String destination = (String) subStep.get("destination_tosca_id");

					if ((checkDuplicateMap.get(source) == null || checkDuplicateMap.get(source).isEmpty()
							|| !checkDuplicateMap.get(source).contains(destination)) && !source.equals(destination)) {
						if (checkDuplicateMap.get(destination) == null) {
							List<String> toscaList = new ArrayList<>();
							checkDuplicateMap.put(destination, toscaList);
						}

						if (checkDuplicateMap.get(source) == null) {
							List<String> toscaList = new ArrayList<>();
							checkDuplicateMap.put(source, toscaList);
						}

						List<String> toscaSourceList = checkDuplicateMap.get(destination);
						toscaSourceList.add(source);

						List<String> toscaDestinationList = checkDuplicateMap.get(source);
						toscaDestinationList.add(destination);

						link.put("s", source);
						link.put("d", destination);
						links.add(link);
					}

				}

				Map<String, Object> callFlowUI = new HashMap<>();
				callFlowUI.put("linkList", links);

				return YamlUtils.returnYaml(callFlowUI);
			}

		}
		return "";
	}

	protected String addLinksAdditional(String filePath, String callFlowName, List<Map<String, Object>> callSteps,
			String callFlowStep) throws IOException {

		for (Map<String, Object> callStep : callSteps) {

			if (((String) callStep.get("name")).split(":")[0].trim().replace(" ", "_").equals(callFlowStep)) {

				Map<String, Object> callFlowUI = new HashMap<>();
				try {
					List<Map<String, Object>> links = addLinkVertices(callStep);
					callFlowUI.put("linkList", links);
				} catch (Exception e) {
					logger.error(EELFLoggerDelegate.errorLogger, "addLinksAdditional failed", e);
				}
				try {
					List<String> activeIds = addActiveNodes(callStep);
					callFlowUI.put("activeIds", activeIds);
				} catch (Exception e) {
					logger.error(EELFLoggerDelegate.errorLogger, "addLinksAdditional failed", e);
				}
				try {
					List<Map<String, Object>> disconnectLinks = addDisconnectLinks(callStep);
					callFlowUI.put("disconnectLinks", disconnectLinks);
				} catch (Exception e) {
					logger.error(EELFLoggerDelegate.errorLogger, "addLinksAdditional failed", e);
				}

				return YamlUtils.returnYaml(callFlowUI);
			}
		}

		return "";
	}

	@SuppressWarnings("unchecked")
	List<String> addActiveNodes(Map<String, Object> callStep) {
		List<String> activeIds = (List<String>) callStep.get("activeIds");
		return activeIds;
	}

	@SuppressWarnings("unchecked")
	List<Map<String, Object>> addDisconnectLinks(Map<String, Object> callStep) {
		List<Map<String, Object>> disconnectLinks = (List<Map<String, Object>>) callStep.get("disconnectLinks");
		return disconnectLinks;
	}

	@SuppressWarnings("unchecked")
	List<Map<String, Object>> addLinkVertices(Map<String, Object> callStep) {
		List<Map<String, Object>> links = new ArrayList<>();

		List<Map<String, Object>> vertices = (List<Map<String, Object>>) callStep.get("vertices");

		for (int i = 0; i < vertices.size() - 1; i++) {
			Map<String, Object> vertex = vertices.get(i);
			Map<String, Object> vertexNext = vertices.get(i + 1);

			Integer sourceX = (Integer) vertex.get("x");
			Integer sourceY = (Integer) vertex.get("y");
			String sourceD = (String) vertex.get("D");
			String sourceL = (vertex.get("L") != null) ? (String) vertex.get("L") : "-";

			if (sourceX == -999) // there is a break in the linkage
				continue;

			Integer destinationX = (Integer) vertexNext.get("x");
			Integer destinationY = (Integer) vertexNext.get("y");
			String destinationD = (String) vertexNext.get("D");

			if (destinationX == -999) // there is a break in the linkage
				continue;

			Map<String, Object> link = new HashMap<>();

			link.put("s", sourceX + "," + sourceY + "," + sourceD + "," + sourceL);
			link.put("d", destinationX + "," + destinationY + "," + destinationD);
			links.add(link);
		}
		return links;
	}

	public static void main(String[] args) throws Exception {
		String filePath = "\\D2Platform\\war\\WEB-INF\\resources\\trisim_files";
		String callFlowBusinessYml = "call_flow_hc-origination-termination-to-volteue-3.3.16-Override.yml";
		ElementLinkService mapper = new ElementLinkService();
		System.out.print(mapper.createLinkFileAdditional(filePath, callFlowBusinessYml, "Step_2"));
	}

}