2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.aai.extensions;
23 import java.io.UnsupportedEncodingException;
25 import java.net.URISyntaxException;
26 import java.util.ArrayList;
27 import java.util.List;
29 import org.apache.tinkerpop.gremlin.structure.Vertex;
30 import org.onap.aai.edges.enums.EdgeType;
31 import org.onap.aai.exceptions.AAIException;
32 import org.onap.aai.introspection.Introspector;
33 import org.onap.aai.introspection.Loader;
34 import org.onap.aai.parsers.query.QueryParser;
35 import org.onap.aai.query.builder.QueryBuilder;
36 import org.onap.aai.rest.db.DBRequest;
37 import org.onap.aai.restcore.HttpMethod;
39 public class OrphanLInterfaceHandler {
41 private QueryBuilder<Vertex> createLInterfaceQuery(AAIExtensionMap aaiReqMap, Introspector newvceObj)
43 Introspector uplinkLInterfaceTraversalIntro = aaiReqMap.getLoader().introspectorFromName("l-interface");
45 Introspector customerUplinkLInterfaceTraversalIntro = aaiReqMap.getLoader().introspectorFromName("l-interface");
47 Introspector logLinkIntroForTraversal = aaiReqMap.getLoader().introspectorFromName("logical-link");
49 QueryBuilder<Vertex> query = aaiReqMap.getTransactionalGraphEngine().getQueryBuilder()
50 .exactMatchQuery(newvceObj)
51 .createEdgeTraversal(EdgeType.TREE, newvceObj, uplinkLInterfaceTraversalIntro)
52 .getVerticesByProperty("interface-role", "UPLINK")
53 .createEdgeTraversal(EdgeType.COUSIN, uplinkLInterfaceTraversalIntro, logLinkIntroForTraversal)
54 .createEdgeTraversal(EdgeType.COUSIN, logLinkIntroForTraversal, customerUplinkLInterfaceTraversalIntro)
55 .getVerticesByProperty("interface-role", "CUSTOMER-UPLINK").dedup();
59 private URI buildLInterfaceURI(Vertex linterface, AAIExtensionMap aaiReqMap)
60 throws UnsupportedEncodingException, AAIException, URISyntaxException {
61 Loader loader = aaiReqMap.getLoader();
62 Introspector lint = loader.introspectorFromName("l-interface");
63 lint.setValue("interface-name", (String) linterface.property("interface-name").value());
64 String lintSegment = lint.getURI();
66 Introspector lagInterfaceForTrav = loader.introspectorFromName("lag-interface");
67 QueryBuilder<Vertex> lagIntQuery = aaiReqMap.getTransactionalGraphEngine().getQueryBuilder()
68 .exactMatchQuery(lint).createEdgeTraversal(EdgeType.TREE, linterface, lagInterfaceForTrav).dedup();
69 List<Vertex> lagInterfaces = lagIntQuery.toList();
70 if (lagInterfaces.isEmpty()) {
71 throw new AAIException("AAI_6114");
72 } else if (lagInterfaces.size() > 1) {
73 throw new AAIException("AAI_6140");
75 Vertex lagInt = lagInterfaces.get(0);
76 lagInterfaceForTrav.setValue("interface-name", (String) lagInt.property("interface-name").value());
77 String lagSegment = lagInterfaceForTrav.getURI();
79 Introspector gvVPEforTrav = loader.introspectorFromName("generic-vnf");
80 QueryBuilder<Vertex> gvVPEquery =
81 aaiReqMap.getTransactionalGraphEngine().getQueryBuilder().exactMatchQuery(lagInterfaceForTrav)
82 .createEdgeTraversal(EdgeType.TREE, lagInterfaceForTrav, gvVPEforTrav).dedup();
83 List<Vertex> genvnfs = gvVPEquery.toList();
84 if (genvnfs.isEmpty()) {
85 throw new AAIException("AAI_6114");
86 } else if (genvnfs.size() > 1) {
87 throw new AAIException("AAI_6140");
89 Vertex genvnf = genvnfs.get(0);
90 gvVPEforTrav.setValue("vnf-id", (String) genvnf.property("vnf-id").value());
91 String gvSegment = gvVPEforTrav.getURI();
93 return new URI(gvSegment + lagSegment + lintSegment);
96 public List<DBRequest> createOrphanLInterfaceDelRequests(AAIExtensionMap aaiReqMap, Introspector newvce)
97 throws AAIException, UnsupportedEncodingException, URISyntaxException {
98 List<DBRequest> requests = new ArrayList<>();
99 QueryBuilder<Vertex> query = createLInterfaceQuery(aaiReqMap, newvce);
100 List<Vertex> linterfaces = query.toList();
102 for (Vertex lint : linterfaces) {
103 URI lintURI = buildLInterfaceURI(lint, aaiReqMap);
104 QueryParser parser = createLInterfaceQuery(aaiReqMap, newvce).createQueryFromObjectName("l-interface");
105 DBRequest originalDbRequest = aaiReqMap.getDbRequest();
107 new DBRequest.Builder(HttpMethod.DELETE, lintURI, parser, newvce, originalDbRequest.getHeaders(),
108 originalDbRequest.getInfo(), originalDbRequest.getTransactionId()).build();
109 requests.add(request);