BUG-6858: adapt to ise api, change lookup from ise
[groupbasedpolicy.git] / sxp-integration / sxp-ise-adapter / src / main / java / org / opendaylight / groupbasedpolicy / sxp_ise_adapter / impl / util / IseReplyUtil.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.groupbasedpolicy.sxp_ise_adapter.impl.util;
10
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.Iterator;
15 import javax.xml.XMLConstants;
16 import javax.xml.namespace.NamespaceContext;
17 import javax.xml.xpath.XPath;
18 import javax.xml.xpath.XPathFactory;
19 import org.xml.sax.InputSource;
20
21 /**
22  * Purpose: provide common functionality upon ise reply message
23  */
24 public class IseReplyUtil {
25
26     public static final String EXPRESSION_SGT_ALL_LINK_HREFS = "/ns3:searchResult/ns3:resources/ns5:resource/link/@href";
27     public static final String EXPRESSION_SGT_DETAIL = "./ns4:sgt";
28     public static final String EXPRESSION_SGT_NAME_ATTR = "./@name";
29     public static final String EXPRESSION_SGT_UUID_ATTR = "./@id";
30     public static final String EXPRESSION_SGT_VALUE = "./value/text()";
31
32     private IseReplyUtil() {
33         throw new IllegalAccessError("util class - no instances supported");
34     }
35
36
37     public static String deliverResponse(final WebResource.Builder requestBuilder) {
38         return requestBuilder.get(ClientResponse.class).getEntity(String.class);
39     }
40
41     /**
42      * @return initiated xpath with ise namespace context injected
43      */
44     public static XPath setupXpath() {
45         final NamespaceContext nsContext = new NamespaceContext() {
46             public String getNamespaceURI(String prefix) {
47                 final String outcome;
48                 if (prefix == null) {
49                     throw new NullPointerException("Null prefix");
50                 }
51
52                 if ("ns5".equals(prefix)) {
53                     outcome = "ers.ise.cisco.com";
54                 } else if ("ns3".equals(prefix)) {
55                     outcome = "v2.ers.ise.cisco.com";
56                 } else if ("ns4".equals(prefix)) {
57                     outcome = "trustsec.ers.ise.cisco.com";
58                 } else {
59                     outcome = XMLConstants.NULL_NS_URI;
60                 }
61                 return outcome;
62             }
63
64             // This method isn't necessary for XPath processing.
65             public String getPrefix(String uri) {
66                 throw new UnsupportedOperationException();
67             }
68
69             // This method isn't necessary for XPath processing either.
70             public Iterator getPrefixes(String uri) {
71                 throw new UnsupportedOperationException();
72             }
73         };
74
75         XPath xpath = XPathFactory.newInstance().newXPath();
76         xpath.setNamespaceContext(nsContext);
77         return xpath;
78     }
79
80     public static InputSource createInputSource(final String rawSgtDetail) {
81         return new InputSource(new StringReader(rawSgtDetail));
82     }
83 }