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=========================================================
20 package org.onap.aai.extensions;
22 import java.io.UnsupportedEncodingException;
24 import java.net.URISyntaxException;
25 import java.util.ArrayList;
26 import java.util.List;
28 import org.apache.tinkerpop.gremlin.structure.Vertex;
29 import org.onap.aai.exceptions.AAIException;
30 import org.onap.aai.introspection.Introspector;
31 import org.onap.aai.introspection.Loader;
32 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
33 import org.onap.aai.parsers.query.QueryParser;
34 import org.onap.aai.query.builder.QueryBuilder;
35 import org.onap.aai.rest.db.DBRequest;
36 import org.onap.aai.restcore.HttpMethod;
37 import org.onap.aai.edges.enums.EdgeType;
39 public class OrphanLInterfaceHandler {
41 private QueryBuilder<Vertex> createLInterfaceQuery(AAIExtensionMap aaiReqMap, Introspector newvceObj) throws AAIException {
42 Introspector uplinkLInterfaceTraversalIntro = aaiReqMap.getLoader().introspectorFromName("l-interface");
44 Introspector customerUplinkLInterfaceTraversalIntro = aaiReqMap.getLoader().introspectorFromName("l-interface");
46 Introspector logLinkIntroForTraversal = aaiReqMap.getLoader().introspectorFromName("logical-link");
48 QueryBuilder<Vertex> query = aaiReqMap.getTransactionalGraphEngine().getQueryBuilder()
49 .exactMatchQuery(newvceObj)
50 .createEdgeTraversal(EdgeType.TREE, newvceObj, uplinkLInterfaceTraversalIntro)
51 .getVerticesByProperty("interface-role", "UPLINK")
52 .createEdgeTraversal(EdgeType.COUSIN, uplinkLInterfaceTraversalIntro, logLinkIntroForTraversal)
53 .createEdgeTraversal(EdgeType.COUSIN, logLinkIntroForTraversal, customerUplinkLInterfaceTraversalIntro)
54 .getVerticesByProperty("interface-role", "CUSTOMER-UPLINK").dedup();
58 private URI buildLInterfaceURI(Vertex linterface, AAIExtensionMap aaiReqMap) throws UnsupportedEncodingException, AAIException, URISyntaxException {
59 Loader loader = aaiReqMap.getLoader();
60 Introspector lint = loader.introspectorFromName("l-interface");
61 lint.setValue("interface-name", (String)linterface.property("interface-name").value());
62 String lintSegment = lint.getURI();
64 Introspector lagInterfaceForTrav = loader.introspectorFromName("lag-interface");
65 QueryBuilder<Vertex> lagIntQuery = aaiReqMap.getTransactionalGraphEngine().getQueryBuilder()
66 .exactMatchQuery(lint)
67 .createEdgeTraversal(EdgeType.TREE, linterface, lagInterfaceForTrav).dedup();
68 List<Vertex> lagInterfaces = lagIntQuery.toList();
69 if (lagInterfaces.isEmpty()) {
70 throw new AAIException("AAI_6114");
71 } else if (lagInterfaces.size() > 1) {
72 throw new AAIException("AAI_6140");
74 Vertex lagInt = lagInterfaces.get(0);
75 lagInterfaceForTrav.setValue("interface-name", (String)lagInt.property("interface-name").value());
76 String lagSegment = lagInterfaceForTrav.getURI();
78 Introspector gvVPEforTrav = loader.introspectorFromName("generic-vnf");
79 QueryBuilder<Vertex> gvVPEquery = aaiReqMap.getTransactionalGraphEngine().getQueryBuilder()
80 .exactMatchQuery(lagInterfaceForTrav)
81 .createEdgeTraversal(EdgeType.TREE, lagInterfaceForTrav, gvVPEforTrav).dedup();
82 List<Vertex> genvnfs = gvVPEquery.toList();
83 if (genvnfs.isEmpty()) {
84 throw new AAIException("AAI_6114");
85 } else if (genvnfs.size() > 1) {
86 throw new AAIException("AAI_6140");
88 Vertex genvnf = genvnfs.get(0);
89 gvVPEforTrav.setValue("vnf-id", (String)genvnf.property("vnf-id").value());
90 String gvSegment = gvVPEforTrav.getURI();
92 return new URI(gvSegment + lagSegment + lintSegment);
95 public List<DBRequest> createOrphanLInterfaceDelRequests(AAIExtensionMap aaiReqMap, Introspector newvce) throws AAIException, UnsupportedEncodingException, URISyntaxException{
96 List<DBRequest> requests = new ArrayList<>();
97 QueryBuilder<Vertex> query = createLInterfaceQuery(aaiReqMap, newvce);
98 List<Vertex> linterfaces = query.toList();
100 for (Vertex lint : linterfaces) {
101 URI lintURI = buildLInterfaceURI(lint, aaiReqMap);
102 QueryParser parser = createLInterfaceQuery(aaiReqMap, newvce).createQueryFromObjectName("l-interface");
103 DBRequest originalDbRequest = aaiReqMap.getDbRequest();
104 DBRequest request = new DBRequest.Builder(HttpMethod.DELETE, lintURI, parser, newvce, originalDbRequest.getHeaders(), originalDbRequest.getInfo(), originalDbRequest.getTransactionId()).build();
105 requests.add(request);