[AAI] Fix doc config files
[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
21 package org.onap.aai.extensions;
22
23 import java.io.UnsupportedEncodingException;
24 import java.net.URI;
25 import java.net.URISyntaxException;
26 import java.util.ArrayList;
27 import java.util.List;
28
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.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
40 public class OrphanLInterfaceHandler {
41
42     private QueryBuilder<Vertex> createLInterfaceQuery(AAIExtensionMap aaiReqMap, Introspector newvceObj)
43             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)
61             throws UnsupportedEncodingException, AAIException, URISyntaxException {
62         Loader loader = aaiReqMap.getLoader();
63         Introspector lint = loader.introspectorFromName("l-interface");
64         lint.setValue("interface-name", (String) linterface.property("interface-name").value());
65         String lintSegment = lint.getURI();
66
67         Introspector lagInterfaceForTrav = loader.introspectorFromName("lag-interface");
68         QueryBuilder<Vertex> lagIntQuery = aaiReqMap.getTransactionalGraphEngine().getQueryBuilder()
69                 .exactMatchQuery(lint).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 =
82                 aaiReqMap.getTransactionalGraphEngine().getQueryBuilder().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)
98             throws AAIException, UnsupportedEncodingException, URISyntaxException {
99         List<DBRequest> requests = new ArrayList<>();
100         QueryBuilder<Vertex> query = createLInterfaceQuery(aaiReqMap, newvce);
101         List<Vertex> linterfaces = query.toList();
102
103         for (Vertex lint : linterfaces) {
104             URI lintURI = buildLInterfaceURI(lint, aaiReqMap);
105             QueryParser parser = createLInterfaceQuery(aaiReqMap, newvce).createQueryFromObjectName("l-interface");
106             DBRequest originalDbRequest = aaiReqMap.getDbRequest();
107             DBRequest request =
108                     new DBRequest.Builder(HttpMethod.DELETE, lintURI, parser, newvce, originalDbRequest.getHeaders(),
109                             originalDbRequest.getInfo(), originalDbRequest.getTransactionId()).build();
110             requests.add(request);
111         }
112
113         return requests;
114     }
115 }