3da4e2271c820ec57b0f126cbbb10a765eab217b
[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 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.onap.aai.extensions;
23
24 import java.io.UnsupportedEncodingException;
25 import java.net.URI;
26 import java.net.URISyntaxException;
27 import java.util.ArrayList;
28 import java.util.List;
29
30 import org.apache.tinkerpop.gremlin.structure.Vertex;
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.introspection.exceptions.AAIUnknownObjectException;
35 import org.onap.aai.parsers.query.QueryParser;
36 import org.onap.aai.query.builder.QueryBuilder;
37 import org.onap.aai.rest.db.DBRequest;
38 import org.onap.aai.restcore.HttpMethod;
39 import org.onap.aai.serialization.db.EdgeType;
40
41 public class OrphanLInterfaceHandler {
42         
43         private QueryBuilder<Vertex> createLInterfaceQuery(AAIExtensionMap aaiReqMap, Introspector newvceObj) throws AAIException {
44                 Introspector uplinkLInterfaceTraversalIntro = aaiReqMap.getLoader().introspectorFromName("l-interface");
45                 
46                 Introspector customerUplinkLInterfaceTraversalIntro = aaiReqMap.getLoader().introspectorFromName("l-interface");
47                 
48                 Introspector logLinkIntroForTraversal = aaiReqMap.getLoader().introspectorFromName("logical-link");
49                 
50                 QueryBuilder<Vertex> query = aaiReqMap.getTransactionalGraphEngine().getQueryBuilder()
51                                                                                 .exactMatchQuery(newvceObj)
52                                                                                 .createEdgeTraversal(EdgeType.TREE, newvceObj, uplinkLInterfaceTraversalIntro)
53                                                                                 .getVerticesByProperty("interface-role", "UPLINK")
54                                                                                 .createEdgeTraversal(EdgeType.COUSIN, uplinkLInterfaceTraversalIntro, logLinkIntroForTraversal)
55                                                                                 .createEdgeTraversal(EdgeType.COUSIN, logLinkIntroForTraversal, customerUplinkLInterfaceTraversalIntro)
56                                                                                 .getVerticesByProperty("interface-role", "CUSTOMER-UPLINK").dedup();
57                 return query;
58         }
59         
60         private URI buildLInterfaceURI(Vertex linterface, AAIExtensionMap aaiReqMap) 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();
65                 
66                 Introspector lagInterfaceForTrav = loader.introspectorFromName("lag-interface");
67                 QueryBuilder<Vertex> lagIntQuery = aaiReqMap.getTransactionalGraphEngine().getQueryBuilder()
68                                                                                 .exactMatchQuery(lint)
69                                                                                 .createEdgeTraversal(EdgeType.TREE, linterface, lagInterfaceForTrav).dedup();
70                 List<Vertex> lagInterfaces = lagIntQuery.toList();
71                 if (lagInterfaces.isEmpty()) {
72                         throw new AAIException("AAI_6114");
73                 } else if (lagInterfaces.size() > 1) {
74                         throw new AAIException("AAI_6140");
75                 }
76                 Vertex lagInt = lagInterfaces.get(0);
77                 lagInterfaceForTrav.setValue("interface-name", (String)lagInt.property("interface-name").value());
78                 String lagSegment = lagInterfaceForTrav.getURI();
79                 
80                 Introspector gvVPEforTrav = loader.introspectorFromName("generic-vnf");
81                 QueryBuilder<Vertex> gvVPEquery = aaiReqMap.getTransactionalGraphEngine().getQueryBuilder()
82                                                                                 .exactMatchQuery(lagInterfaceForTrav)
83                                                                                 .createEdgeTraversal(EdgeType.TREE, lagInterfaceForTrav, gvVPEforTrav).dedup();
84                 List<Vertex> genvnfs = gvVPEquery.toList();
85                 if (genvnfs.isEmpty()) {
86                         throw new AAIException("AAI_6114");
87                 } else if (genvnfs.size() > 1) {
88                         throw new AAIException("AAI_6140");
89                 }
90                 Vertex genvnf = genvnfs.get(0);
91                 gvVPEforTrav.setValue("vnf-id", (String)genvnf.property("vnf-id").value());
92                 String gvSegment = gvVPEforTrav.getURI();
93                 
94                 return new URI(gvSegment + lagSegment + lintSegment);
95         }
96
97         public List<DBRequest> createOrphanLInterfaceDelRequests(AAIExtensionMap aaiReqMap, Introspector newvce) throws AAIException, UnsupportedEncodingException, URISyntaxException{
98                 List<DBRequest> requests = new ArrayList<>();
99                 QueryBuilder<Vertex> query = createLInterfaceQuery(aaiReqMap, newvce);
100                 List<Vertex> linterfaces = query.toList();
101                 
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();
106                         DBRequest request = new DBRequest.Builder(HttpMethod.DELETE, lintURI, parser, newvce, originalDbRequest.getHeaders(), originalDbRequest.getInfo(), originalDbRequest.getTransactionId()).build();
107                         requests.add(request);
108                 }
109                 
110                 return requests;
111         }
112 }