aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/aai/migration/v12/UriMigration.java
blob: cc8ba55dd363088fcf6d4a8aede90cd9a57e7c7d (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
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright © 2017-2018 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.aai.migration.v12;

import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.onap.aai.db.props.AAIProperties;
import org.onap.aai.edges.EdgeIngestor;
import org.onap.aai.edges.enums.EdgeProperty;
import org.onap.aai.exceptions.AAIException;
import org.onap.aai.introspection.LoaderFactory;
import org.onap.aai.introspection.ModelType;
import org.onap.aai.serialization.db.EdgeSerializer;
import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
import org.onap.aai.migration.*;
import org.onap.aai.edges.enums.AAIDirection;
import org.onap.aai.serialization.db.DBSerializer;
import org.onap.aai.serialization.engines.TransactionalGraphEngine;
import org.onap.aai.setup.SchemaVersion;
import org.onap.aai.setup.SchemaVersions;
import org.springframework.web.util.UriUtils;

import javax.ws.rs.core.UriBuilder;
import java.io.UnsupportedEncodingException;
import java.util.*;
import java.util.stream.Collectors;

//@Enabled

@MigrationPriority(1000)
@MigrationDangerRating(1000)
public class UriMigration extends Migrator {

	private final SchemaVersion version;
	private final ModelType introspectorFactoryType;
	private GraphTraversalSource g;

	private Map<String, UriBuilder> nodeTypeToUri;
	private Map<String, Set<String>> nodeTypeToKeys;

	protected Set<Object> seen = new HashSet<>();

	/**
	 * Instantiates a new migrator.
	 *
	 * @param engine
	 */
	public UriMigration(TransactionalGraphEngine engine, LoaderFactory loaderFactory, EdgeIngestor edgeIngestor, EdgeSerializer edgeSerializer, SchemaVersions schemaVersions) throws AAIException {
		super(engine, loaderFactory, edgeIngestor, edgeSerializer, schemaVersions);
		version = schemaVersions.getDefaultVersion();
		introspectorFactoryType = ModelType.MOXY;
		loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, version);
		g = this.engine.asAdmin().getTraversalSource();
		this.serializer = new DBSerializer(version, this.engine, introspectorFactoryType, this.getMigrationName());

	}

	@Override
	public void run() {
		long start = System.currentTimeMillis();
		nodeTypeToUri = loader.getAllObjects().entrySet().stream().filter(e -> e.getValue().getGenericURI().contains("{")).collect(
				Collectors.toMap(
						e -> e.getKey(),
						e -> UriBuilder.fromPath(e.getValue().getFullGenericURI().replaceAll("\\{"+ e.getKey() + "-", "{"))
				));

		nodeTypeToKeys = loader.getAllObjects().entrySet().stream().filter(e -> e.getValue().getGenericURI().contains("{")).collect(
				Collectors.toMap(
						e -> e.getKey(),
						e -> e.getValue().getKeys()
				));

		Set<String> topLevelNodeTypes = loader.getAllObjects().entrySet().stream()
				.filter(e -> e.getValue().isTopLevel()).map(Map.Entry::getKey)
				.collect(Collectors.toSet());

		logger.info("Top level count : " + topLevelNodeTypes.size());
		topLevelNodeTypes.stream().forEach(topLevelNodeType -> {
			Set<Vertex> parentSet = g.V().has(AAIProperties.NODE_TYPE, topLevelNodeType).toSet();
			logger.info(topLevelNodeType + " : " + parentSet.size());
			try {
				this.verifyOrAddUri("", parentSet);
			} catch (AAIUnknownObjectException e) {
				e.printStackTrace();
			} catch (AAIException e) {
				e.printStackTrace();
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		});
		logger.info("RUNTIME: " + (System.currentTimeMillis() - start));
		logger.info("NO URI: " + g.V().hasNot(AAIProperties.AAI_URI).count().next());
		logger.info("NUM VERTEXES SEEN: " + seen.size());
		seen = new HashSet<>();

	}

	protected void verifyOrAddUri(String parentUri, Set<Vertex> vertexSet) throws UnsupportedEncodingException, AAIException {
		String correctUri;
		for (Vertex v : vertexSet) {
			seen.add(v.id());
			//if there is an issue generating the uri catch, log and move on;
			try {
				correctUri = parentUri + this.getUriForVertex(v);
			} catch (Exception e) {
				logger.error("Vertex has issue generating uri " + e.getMessage() + "\n\t" + this.asString(v));
				continue;
			}
			try {
				v.property(AAIProperties.AAI_URI, correctUri);
			} catch (Exception e) {
				logger.info(e.getMessage() + "\n\t" + this.asString(v));
			}
			if (!v.property(AAIProperties.AAI_UUID).isPresent()) {
				v.property(AAIProperties.AAI_UUID, UUID.randomUUID().toString());
			}
			this.verifyOrAddUri(correctUri, getChildren(v));
		}
	}

	protected Set<Vertex> getChildren(Vertex v) {

		Set<Vertex> children = g.V(v).bothE().not(__.has(EdgeProperty.CONTAINS.toString(), AAIDirection.NONE.toString())).otherV().toSet();

		return children.stream().filter(child -> !seen.contains(child.id())).collect(Collectors.toSet());
	}

	protected String getUriForVertex(Vertex v) {
		String aaiNodeType = v.property(AAIProperties.NODE_TYPE).value().toString();


		Map<String, String> parameters = this.nodeTypeToKeys.get(aaiNodeType).stream().collect(Collectors.toMap(
				key -> key,
				key -> encodeProp(v.property(key).value().toString())
		));

		return this.nodeTypeToUri.get(aaiNodeType).buildFromEncodedMap(parameters).toString();
	}

	private static String encodeProp(String s) {
		try {
			return UriUtils.encode(s, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			return "";
		}
	}

	@Override
	public Status getStatus() {
		return Status.SUCCESS;
	}

	@Override
	public Optional<String[]> getAffectedNodeTypes() {
		return Optional.empty();
	}

	@Override
	public String getMigrationName() {
		return UriMigration.class.getSimpleName();
	}
}