2 * ============LICENSE_START=======================================================
\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
12 * http://www.apache.org/licenses/LICENSE-2.0
\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
21 package org.onap.crud.parser.util;
\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
33 * Utility Class to extract data from the Edge Payload
\r
35 public class EdgePayloadUtil {
\r
37 private static final Pattern URL_MATCHER = Pattern.compile("services/inventory/(.*)/(.*)/(.*)");
\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
48 * Returns the node type from a vertex on the edge payload
\r
52 * @throws CrudException
\r
54 public static String getVertexNodeType(String vertex) throws CrudException {
\r
55 return getVertexMatcher(vertex).group(2);
\r
59 * Returns the node id from a vertex on the edge payload
\r
63 * @throws CrudException
\r
65 public static String getVertexNodeId(String vertex) throws CrudException {
\r
66 return getVertexMatcher(vertex).group(3);
\r
70 * Generates a key based on the edge payload content
\r
76 * @throws CrudException
\r
78 public static String generateEdgeKey(String source, String target, String type) throws CrudException {
\r
79 return getVertexNodeType(source) + ":" + getVertexNodeType(target) + ":" + type;
\r
83 * Returns an Edge Builder object from the payload properties
\r
89 * @throws CrudException
\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
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
101 * Returns an Edge Builder object from an Edge object properties
\r
106 public static Edge.Builder getBuilderFromEdge(Edge edge) {
\r
107 Edge.Builder edgeBuilder = new Edge.Builder(edge.getType()).id(edge.getId().get());
\r
110 .source(new Vertex.Builder(edge.getSource().getType()).id(edge.getSource().getId().get()).build());
\r
112 .target(new Vertex.Builder(edge.getTarget().getType()).id(edge.getTarget().getId().get()).build());
\r
114 return edgeBuilder;
\r
118 * Filter Edges by its source/target vertex type and the edge type
\r
120 * @param sourceTargetType the new Edge source/target type
\r
123 * @return List<Edge>
\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
134 return filteredEdges;
\r
137 private static boolean doesEdgeTypeMatch(Edge edge, String type) {
\r
138 return edge.getType() != null && edge.getType().equals(type);
\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