aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/aai/migration/v12/MigrateINVPhysicalInventory.java
blob: 0c85481081776cad363231673f126bb3c0abc5e6 (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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/**
 * ============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;
/*-
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * 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=========================================================
 */


import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.javatuples.Pair;
import org.onap.aai.db.props.AAIProperties;
import org.onap.aai.edges.EdgeIngestor;
import org.onap.aai.introspection.Introspector;
import org.onap.aai.introspection.LoaderFactory;
import org.onap.aai.migration.*;
import org.onap.aai.serialization.db.EdgeSerializer;
import org.onap.aai.serialization.engines.TransactionalGraphEngine;
import org.onap.aai.setup.SchemaVersions;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;


@MigrationPriority(25)
@MigrationDangerRating(100)
public class MigrateINVPhysicalInventory extends Migrator {

	private static final String NODE_TYPE_PNF = "pnf";
	private static final String NODE_TYPE_PINTERFACE = "p-interface";
	private static final String NODE_TYPE_PINTERFACES = "p-interfaces";
	private static final String PROPERTY_PNF_NAME = "pnf-name";
	private static final String PROPERTY_INTERFACE_NAME = "interface-name";
	protected final AtomicInteger skippedRowsCount = new AtomicInteger(0);
	protected final AtomicInteger processedRowsCount = new AtomicInteger(0);

	private boolean success = true;
    private boolean checkLog = false;
    private GraphTraversalSource g = null;
    protected int headerLength;

	protected final AtomicInteger falloutRowsCount = new AtomicInteger(0);
	private static final String homeDir = System.getProperty("AJSC_HOME");
	private static List<String> dmaapMsgList = new ArrayList<String>();

	public MigrateINVPhysicalInventory(TransactionalGraphEngine engine, LoaderFactory loaderFactory, EdgeIngestor edgeIngestor, EdgeSerializer edgeSerializer, SchemaVersions schemaVersions) {
		super(engine, loaderFactory, edgeIngestor, edgeSerializer, schemaVersions);
        this.g = this.engine.asAdmin().getTraversalSource();
    }

	@Override
    public void run() {
        logger.info("---------- Start migration of INV File Physical Inventory  ----------");
        String configDir = System.getProperty("BUNDLECONFIG_DIR");
        if (homeDir == null) {
            logger.info("ERROR: Could not find sys prop AJSC_HOME");
            success = false;
            return;
        }
        if (configDir == null) {
            success = false;
            return;
        }
        
        String feedDir = homeDir + "/" + configDir + "/" + "migration-input-files/sarea-inventory/";
        String fileName = feedDir+ "inv.csv";
        logger.info(fileName);
        logger.info("---------- Processing INV Entries from file  ----------");


		try {
			Map<String, Set<String>> data = loadFile(fileName);
			this.processData(data);
			
			logger.info("\n ******* Summary Report for Inv File Physical Migration *******");
			logger.info("Number of distinct pnfs processed: "+data.keySet().size());
			logger.info("Rows processed: " + processedRowsCount);
			logger.info("Rows skipped: "+ skippedRowsCount);
			logger.info("Fallout Rows count: " + falloutRowsCount);
			
		} catch (FileNotFoundException e) {
            logger.info("ERROR: Could not file file " + fileName, e.getMessage());
            success = false;
            checkLog = true;
        } catch (IOException e) {
            logger.info("ERROR: Issue reading file " + fileName, e);
            success = false;
        } catch (Exception e) {
            logger.info("encountered exception", e);
            e.printStackTrace();
            success = false;
        }
    }

	protected void processData(Map<String, Set<String>> data) throws Exception{

		for (Map.Entry<String, Set<String>> entry : data.entrySet()) {
			String pnfName = entry.getKey();
			final Set<String> newPInterfaces = entry.getValue();
			Introspector pnf;
			Vertex pnfVertex;
			EventAction eventAction = EventAction.UPDATE;
			boolean pnfChangesMade = false;

			if (pnfExists(pnfName)) {
				pnf = serializer.getLatestVersionView(getPnf(pnfName));
				pnfVertex = getPnf(pnfName);
			} else {
				pnf = loader.introspectorFromName(NODE_TYPE_PNF);
				pnf.setValue(PROPERTY_PNF_NAME, pnfName);
				pnfVertex = serializer.createNewVertex(pnf);
				eventAction = EventAction.CREATE;
				pnfChangesMade = true;
			}

			if (pnfChangesMade) {
				serializer.serializeSingleVertex(pnfVertex, pnf, getMigrationName());
				logger.info ("\t Pnf [" + pnfName +"] created with vertex id "+pnfVertex);
//				pnf = serializer.getLatestVersionView(pnfVertex);
//				this.notificationHelper.addEvent(pnfVertex, serializer.getLatestVersionView(pnfVertex), eventAction, this.serializer.getURIForVertex(pnfVertex, false));
//				logger.info("\t Dmaap notification sent for creation of pnf ");
				String dmaapMsg = System.nanoTime() + "_" + pnfVertex.id().toString() + "_"	+ pnfVertex.value("resource-version").toString();
				dmaapMsgList.add(dmaapMsg);
			} else {
				logger.info("\t Pnf ["+ pnfName +"] already exists ");
			}

			if (!newPInterfaces.isEmpty()) {
				Introspector pInterfacesIntrospector = pnf.getWrappedValue(NODE_TYPE_PINTERFACES);
				if ( pInterfacesIntrospector == null) {
					pInterfacesIntrospector = pnf.newIntrospectorInstanceOfProperty(NODE_TYPE_PINTERFACES);
					pnf.setValue(NODE_TYPE_PINTERFACES, pInterfacesIntrospector.getUnderlyingObject());
				}

				for (Introspector introspector : pInterfacesIntrospector.getWrappedListValue(NODE_TYPE_PINTERFACE)) {
					String interfaceName = introspector.getValue(PROPERTY_INTERFACE_NAME).toString();
					if (newPInterfaces.contains(interfaceName)) {
						newPInterfaces.remove(interfaceName);
					}
				}

				for (String pInterfaceName : newPInterfaces) {
					Introspector pInterface = loader.introspectorFromName(NODE_TYPE_PINTERFACE);
					pInterface.setValue(PROPERTY_INTERFACE_NAME, pInterfaceName);
					Vertex pInterfaceVertex = serializer.createNewVertex(pInterface);
					pInterfaceVertex.property(AAIProperties.AAI_URI, pnfVertex.property(AAIProperties.AAI_URI).value() + "/p-interfaces/p-interface/" + pInterfaceName);
					edgeSerializer.addTreeEdge(g, pnfVertex, pInterfaceVertex);
					eventAction = EventAction.CREATE;
					serializer.serializeSingleVertex(pInterfaceVertex, pInterface, getMigrationName());
					logger.info ("\t p-interface [" + pInterfaceName +"] created with vertex id "+ pInterfaceVertex + " on pnf ["+pnfName+"]");
//					pInterface = serializer.getLatestVersionView(pInterfaceVertex);
//					this.notificationHelper.addEvent(pInterfaceVertex, pInterface, eventAction, this.serializer.getURIForVertex(pInterfaceVertex, false));
//					logger.info("\t Dmaap notification sent for creation of p-interface ");
					String dmaapMsg = System.nanoTime() + "_" + pInterfaceVertex.id().toString() + "_"	+ pInterfaceVertex.value("resource-version").toString();
					dmaapMsgList.add(dmaapMsg);
				}
			}
		}
	}

	protected boolean pnfExists(String pnfName) {
		return g.V().has(PROPERTY_PNF_NAME, pnfName).has(AAIProperties.NODE_TYPE, NODE_TYPE_PNF).hasNext();
	}

	protected Vertex getPnf(String pnfName) {
		return g.V().has(PROPERTY_PNF_NAME, pnfName).has(AAIProperties.NODE_TYPE, NODE_TYPE_PNF).next();
	}

	/**
	 * Load file to the map for processing
	 * @param fileName
	 * @return
	 * @throws Exception
	 */
	protected Map<String,Set<String>> loadFile(String fileName) throws Exception {
		List<String> lines = Files.readAllLines(Paths.get(fileName));
		return this.getFileContents(lines);
	}

	/**
	 * Get lines from file.
	 * @param lines
	 * @return
	 * @throws Exception
	 */
	protected Map<String,Set<String>> getFileContents(List<String> lines) throws Exception {

		final Map<String,Set<String>> fileContents = new ConcurrentHashMap<>();

		processAndRemoveHeader(lines);
		
		logger.info("Total rows count excluding header: "+ lines.size());
		
		lines.stream()
			.filter(line -> !line.isEmpty())
			.map(line -> Arrays.asList(line.split("\\s*,\\s*", -1)))
//			.filter(this::verifyLine)
			.map(this::processLine)
			.filter(Optional::isPresent)
			.map(Optional::get)
			.forEach(p -> {
				processedRowsCount.getAndIncrement();
				String pnfName = p.getValue0();
				if (!fileContents.containsKey(pnfName)) {
					Set<String> s = new HashSet<>();
					fileContents.put(p.getValue0(), s);
				}
				if (p.getValue1() != null) {
					fileContents.get(p.getValue0()).add(p.getValue1());
				}
			})
		;
		
		return fileContents;


	}

	/**
	 * Verify line has the necessary details.
	 * @param line
	 * @return
	 */
	protected boolean verifyLine(List<String> line) {
		if (line.size() != headerLength) {
			logger.info("ERROR: INV line should contain " + headerLength + " columns, contains " + line.size() + " instead.");
			this.skippedRowsCount.getAndIncrement();
			return false;
		}
		return true;
	}

	/**
* 	 * Get the pnf name and interface name from the line.
	 * @param line
	 * @return
	 */
	protected Optional<Pair<String,String>> processLine(List<String> line) {
		logger.info("Processing line... " + line.toString());
		int lineSize = line.size();
		if (lineSize < 11){
			logger.info("Skipping line, does not contain pnf and/or port columns");
			skippedRowsCount.getAndIncrement();
			return Optional.empty();
		}
		
		String pnfName = line.get(0);
		String portAid = line.get(11).replaceAll("^\"|\"$", "").replaceAll("\\s+","");
		
		if (pnfName.isEmpty() && portAid.isEmpty()) {
			logger.info("Line missing pnf name and port " + line);
			falloutRowsCount.getAndIncrement();
			return Optional.empty();
		} else if (pnfName.isEmpty()) {
			logger.info("Line missing pnf name" + line);
			falloutRowsCount.getAndIncrement();
			return Optional.empty();
		} else if (portAid.isEmpty()) {
			logger.info("Line missing port " + line);
			return Optional.of(Pair.with(pnfName, null));
		}
		return Optional.of(Pair.with(pnfName, portAid));
	}

	/**
	 * Verify header of the csv and remove it from the list.
	 * @param lines
	 * @throws Exception
	 */
	protected String processAndRemoveHeader(List<String> lines) throws Exception {
		String firstLine;
		if (lines.isEmpty()) {
			String msg = "ERROR: Missing Header in file";
			success = false;
			logger.error(msg);
			throw new Exception(msg);
		} else {
			firstLine = lines.get(0);
		}

		this.headerLength = firstLine.split("\\s*,\\s*", -1).length;
		logger.info("headerLength: " + headerLength);
		if (this.headerLength < 21){
			String msg = "ERROR: Input file should have 21 columns";
			success = false;
			logger.error(msg);
			throw new Exception(msg);
		}

		return lines.remove(0);
	}


    @Override
    public Status getStatus() {
        if (checkLog) {
            return Status.CHECK_LOGS;
        }
        else if (success) {
            return Status.SUCCESS;
        }
        else {
            return Status.FAILURE;
        }
    }
    
    @Override
	public void commit() {
		engine.commit();
		createDmaapFiles(dmaapMsgList);
	}

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

    @Override
    public String getMigrationName() {
        return "MigrateINVPhysicalInventory";
    }

}