BUG-6650: ep-ip/sgt, implement and wire template provider
[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_VALUE = "./value/text()";
30
31     private IseReplyUtil() {
32         throw new IllegalAccessError("util class - no instances supported");
33     }
34
35
36     public static String deliverResponse(final WebResource.Builder requestBuilder) {
37         return requestBuilder.get(ClientResponse.class).getEntity(String.class);
38     }
39
40     /**
41      * @return initiated xpath with ise namespace context injected
42      */
43     public static XPath setupXpath() {
44         final NamespaceContext nsContext = new NamespaceContext() {
45             public String getNamespaceURI(String prefix) {
46                 final String outcome;
47                 if (prefix == null) {
48                     throw new NullPointerException("Null prefix");
49                 }
50
51                 if ("ns5".equals(prefix)) {
52                     outcome = "ers.ise.cisco.com";
53                 } else if ("ns3".equals(prefix)) {
54                     outcome = "v2.ers.ise.cisco.com";
55                 } else if ("ns4".equals(prefix)) {
56                     outcome = "trustsec.ers.ise.cisco.com";
57                 } else {
58                     outcome = XMLConstants.NULL_NS_URI;
59                 }
60                 return outcome;
61             }
62
63             // This method isn't necessary for XPath processing.
64             public String getPrefix(String uri) {
65                 throw new UnsupportedOperationException();
66             }
67
68             // This method isn't necessary for XPath processing either.
69             public Iterator getPrefixes(String uri) {
70                 throw new UnsupportedOperationException();
71             }
72         };
73
74         XPath xpath = XPathFactory.newInstance().newXPath();
75         xpath.setNamespaceContext(nsContext);
76         return xpath;
77     }
78
79     public static InputSource createInputSource(final String rawSgtDetail) {
80         return new InputSource(new StringReader(rawSgtDetail));
81     }
82 }