optionally disable client auth in gizmo
[aai/gizmo.git] / src / main / java / org / onap / crud / parser / util / EdgePayloadUtil.java
1 /**\r
2  * ============LICENSE_START=======================================================\r
3  * org.onap.aai\r
4  * ================================================================================\r
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.\r
6  * Copyright © 2017-2018 Amdocs\r
7  * ================================================================================\r
8  * Licensed under the Apache License, Version 2.0 (the "License");\r
9  * you may not use this file except in compliance with the License.\r
10  * You may obtain a copy of the License at\r
11  *\r
12  *       http://www.apache.org/licenses/LICENSE-2.0\r
13  *\r
14  * Unless required by applicable law or agreed to in writing, software\r
15  * distributed under the License is distributed on an "AS IS" BASIS,\r
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
17  * See the License for the specific language governing permissions and\r
18  * limitations under the License.\r
19  * ============LICENSE_END=========================================================\r
20  */\r
21 package org.onap.crud.parser.util;\r
22 \r
23 import java.util.ArrayList;\r
24 import java.util.List;\r
25 import java.util.regex.Matcher;\r
26 import java.util.regex.Pattern;\r
27 import javax.ws.rs.core.Response.Status;\r
28 import org.onap.crud.entity.Edge;\r
29 import org.onap.crud.entity.Vertex;\r
30 import org.onap.crud.exception.CrudException;\r
31 \r
32 /**\r
33  * Utility Class to extract data from the Edge Payload\r
34  */\r
35 public class EdgePayloadUtil {\r
36 \r
37     private static final Pattern URL_MATCHER = Pattern.compile("services/inventory/(.*)/(.*)/(.*)");\r
38 \r
39     private static Matcher getVertexMatcher(String vertex) throws CrudException {\r
40         Matcher matcher = URL_MATCHER.matcher(vertex);\r
41         if (!matcher.matches()) {\r
42             throw new CrudException("Invalid Source/Target Urls", Status.BAD_REQUEST);\r
43         }\r
44         return matcher;\r
45     }\r
46 \r
47     /**\r
48      * Returns the node type from a vertex on the edge payload\r
49      *\r
50      * @param vertex\r
51      * @return\r
52      * @throws CrudException\r
53      */\r
54     public static String getVertexNodeType(String vertex) throws CrudException {\r
55         return getVertexMatcher(vertex).group(2);\r
56     }\r
57 \r
58     /**\r
59      * Returns the node id from a vertex on the edge payload\r
60      *\r
61      * @param vertex\r
62      * @return\r
63      * @throws CrudException\r
64      */\r
65     public static String getVertexNodeId(String vertex) throws CrudException {\r
66         return getVertexMatcher(vertex).group(3);\r
67     }\r
68 \r
69     /**\r
70      * Generates a key based on the edge payload content\r
71      *\r
72      * @param source\r
73      * @param target\r
74      * @param type\r
75      * @return\r
76      * @throws CrudException\r
77      */\r
78     public static String generateEdgeKey(String source, String target, String type) throws CrudException {\r
79         return getVertexNodeType(source) + ":" + getVertexNodeType(target) + ":" + type;\r
80     }\r
81 \r
82     /**\r
83      * Returns an Edge Builder object from the payload properties\r
84      *\r
85      * @param source\r
86      * @param target\r
87      * @param type\r
88      * @return\r
89      * @throws CrudException\r
90      */\r
91     public static Edge.Builder getBuilderFromEdgePayload(String source, String target, String type) throws CrudException {\r
92         Edge.Builder edgeBuilder = new Edge.Builder(type);\r
93 \r
94         edgeBuilder.source(new Vertex.Builder(getVertexNodeType(source)).id(getVertexNodeId(source)).build());\r
95         edgeBuilder.target(new Vertex.Builder(getVertexNodeType(target)).id(getVertexNodeId(target)).build());\r
96 \r
97         return edgeBuilder;\r
98     }\r
99 \r
100     /**\r
101      * Returns an Edge Builder object from an Edge object properties\r
102      *\r
103      * @param edge\r
104      * @return\r
105      */\r
106     public static Edge.Builder getBuilderFromEdge(Edge edge) {\r
107         Edge.Builder edgeBuilder = new Edge.Builder(edge.getType()).id(edge.getId().get());\r
108 \r
109         edgeBuilder\r
110                 .source(new Vertex.Builder(edge.getSource().getType()).id(edge.getSource().getId().get()).build());\r
111         edgeBuilder\r
112                 .target(new Vertex.Builder(edge.getTarget().getType()).id(edge.getTarget().getId().get()).build());\r
113 \r
114         return edgeBuilder;\r
115     }\r
116 \r
117     /**\r
118      * Filter Edges by its source/target vertex type and the edge type\r
119      *\r
120      * @param sourceTargetType the new Edge source/target type\r
121      * @param type\r
122      * @param edges\r
123      * @return List<Edge>\r
124      */\r
125     public static List<Edge> filterEdgesByRelatedVertexAndType(String sourceTargetType, String type, List<Edge> edges) {\r
126         List<Edge> filteredEdges = new ArrayList<>();\r
127         if (edges != null) {\r
128             for (Edge edge : edges) {\r
129                 if (doesEdgeTypeMatch(edge, type) && doesEdgeSourceTargetTypeMatch(edge, sourceTargetType)) {\r
130                     filteredEdges.add(edge);\r
131                 }\r
132             }\r
133         }\r
134         return filteredEdges;\r
135     }\r
136 \r
137     private static boolean doesEdgeTypeMatch(Edge edge, String type) {\r
138         return edge.getType() != null && edge.getType().equals(type);\r
139     }\r
140 \r
141     private static boolean doesEdgeSourceTargetTypeMatch(Edge edge, String sourceTargetType) {\r
142         return (edge.getSource().getType() != null && edge.getSource().getType().equals(sourceTargetType))\r
143                 || ((edge.getTarget().getType() != null && edge.getTarget().getType().equals(sourceTargetType)));\r
144     }\r
145 }\r