Merge "Fix sonar issues in SchemaGenerator"
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / extensions / OrphanLInterfaceHandler.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.onap.aai.extensions;
21
22 import java.io.UnsupportedEncodingException;
23 import java.net.URI;
24 import java.net.URISyntaxException;
25 import java.util.ArrayList;
26 import java.util.List;
27
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.serialization.db.EdgeType;
38
39 public class OrphanLInterfaceHandler {
40         
41         private QueryBuilder<Vertex> createLInterfaceQuery(AAIExtensionMap aaiReqMap, Introspector newvceObj) throws AAIException {
42                 Introspector uplinkLInterfaceTraversalIntro = aaiReqMap.getLoader().introspectorFromName("l-interface");
43                 
44                 Introspector customerUplinkLInterfaceTraversalIntro = aaiReqMap.getLoader().introspectorFromName("l-interface");
45                 
46                 Introspector logLinkIntroForTraversal = aaiReqMap.getLoader().introspectorFromName("logical-link");
47                 
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();
55                 return query;
56         }
57         
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();
63                 
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");
73                 }
74                 Vertex lagInt = lagInterfaces.get(0);
75                 lagInterfaceForTrav.setValue("interface-name", (String)lagInt.property("interface-name").value());
76                 String lagSegment = lagInterfaceForTrav.getURI();
77                 
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");
87                 }
88                 Vertex genvnf = genvnfs.get(0);
89                 gvVPEforTrav.setValue("vnf-id", (String)genvnf.property("vnf-id").value());
90                 String gvSegment = gvVPEforTrav.getURI();
91                 
92                 return new URI(gvSegment + lagSegment + lintSegment);
93         }
94
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();
99                 
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);
106                 }
107                 
108                 return requests;
109         }
110 }