2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.groupbasedpolicy.sxp_ise_adapter.impl.util;
11 import com.sun.jersey.api.client.ClientResponse;
12 import com.sun.jersey.api.client.WebResource;
13 import java.io.StringReader;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Iterator;
18 import javax.xml.XMLConstants;
19 import javax.xml.namespace.NamespaceContext;
20 import javax.xml.xpath.XPath;
21 import javax.xml.xpath.XPathConstants;
22 import javax.xml.xpath.XPathExpressionException;
23 import javax.xml.xpath.XPathFactory;
24 import org.w3c.dom.Node;
25 import org.w3c.dom.NodeList;
26 import org.xml.sax.InputSource;
29 * Purpose: provide common functionality upon ise reply message
31 public class IseReplyUtil {
33 private static final String EXPRESSION_SGT_ALL_RESOURCES = "/ns3:searchResult/ns3:resources/ns5:resource";
34 private static final String EXPRESSION_SGT_DETAIL_LINK = "./link/@href";
35 private static final String EXPRESSION_SGT_DETAIL = "./ns4:sgt";
36 private static final String EXPRESSION_SGT_NAME_ATTR = "./@name";
37 private static final String EXPRESSION_SGT_UUID_ATTR = "./@id";
38 private static final String EXPRESSION_SGT_VALUE = "./value/text()";
40 private IseReplyUtil() {
41 throw new IllegalAccessError("util class - no instances supported");
45 public static String deliverResponse(final WebResource.Builder requestBuilder) {
46 return requestBuilder.get(ClientResponse.class).getEntity(String.class);
50 * @return initiated xpath with ise namespace context injected
52 public static XPath setupXpath() {
53 final NamespaceContext nsContext = new NamespaceContext() {
54 public String getNamespaceURI(String prefix) {
57 throw new NullPointerException("Null prefix");
60 if ("ns5".equals(prefix)) {
61 outcome = "ers.ise.cisco.com";
62 } else if ("ns3".equals(prefix)) {
63 outcome = "v2.ers.ise.cisco.com";
64 } else if ("ns4".equals(prefix)) {
65 outcome = "trustsec.ers.ise.cisco.com";
67 outcome = XMLConstants.NULL_NS_URI;
72 // This method isn't necessary for XPath processing.
73 public String getPrefix(String uri) {
74 throw new UnsupportedOperationException();
77 // This method isn't necessary for XPath processing either.
78 public Iterator getPrefixes(String uri) {
79 throw new UnsupportedOperationException();
83 XPath xpath = XPathFactory.newInstance().newXPath();
84 xpath.setNamespaceContext(nsContext);
88 public static InputSource createInputSource(final String rawSgtDetail) {
89 return new InputSource(new StringReader(rawSgtDetail));
93 * @param uuidToSgtMap map of existing sgts (by uuid)
94 * @param xpath xpath instance
95 * @param sgtResources input node list
96 * @return new/unknown sgts to explore
97 * @throws XPathExpressionException in case xpath processing fails
99 public static Collection<Node> filterNewResourcesByID(final Map<String, Integer> uuidToSgtMap, final XPath xpath,
100 final NodeList sgtResources)
101 throws XPathExpressionException {
102 final Collection<Node> nodesToExplore = new ArrayList<>();
103 for (int i = 0; i < sgtResources.getLength(); i++) {
104 final String uuid = ((Node) xpath.evaluate(EXPRESSION_SGT_UUID_ATTR, sgtResources.item(i), XPathConstants.NODE)).getNodeValue();
105 if (!uuidToSgtMap.containsKey(uuid)) {
107 (Node) xpath.evaluate(EXPRESSION_SGT_DETAIL_LINK, sgtResources.item(i), XPathConstants.NODE)
111 return nodesToExplore;
114 public static NodeList findAllSgtResourceNodes(final XPath xpath, final InputSource inputSource) throws XPathExpressionException {
115 return (NodeList) xpath.evaluate(EXPRESSION_SGT_ALL_RESOURCES, inputSource,
116 XPathConstants.NODESET);
119 public static Node gainSgtValue(final XPath xpath, final Node sgtNode) throws XPathExpressionException {
120 return (Node) xpath.evaluate(EXPRESSION_SGT_VALUE, sgtNode, XPathConstants.NODE);
123 public static Node gainSgtUuid(final XPath xpath, final Node sgtNode) throws XPathExpressionException {
124 return (Node) xpath.evaluate(EXPRESSION_SGT_UUID_ATTR, sgtNode, XPathConstants.NODE);
127 public static Node gainSgtName(final XPath xpath, final Node sgtNode) throws XPathExpressionException {
128 return (Node) xpath.evaluate(EXPRESSION_SGT_NAME_ATTR, sgtNode, XPathConstants.NODE);
131 public static Node findSgtDetailNode(final XPath xpath, final String rawSgtDetail) throws XPathExpressionException {
132 return (Node) xpath.evaluate(EXPRESSION_SGT_DETAIL, createInputSource(rawSgtDetail),
133 XPathConstants.NODE);