BUG-6858: adapt to ise api, wire harvestAll to 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.ArrayList;
15 import java.util.Collection;
16 import java.util.Iterator;
17 import java.util.Map;
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;
27
28 /**
29  * Purpose: provide common functionality upon ise reply message
30  */
31 public class IseReplyUtil {
32
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()";
39
40     private IseReplyUtil() {
41         throw new IllegalAccessError("util class - no instances supported");
42     }
43
44
45     public static String deliverResponse(final WebResource.Builder requestBuilder) {
46         return requestBuilder.get(ClientResponse.class).getEntity(String.class);
47     }
48
49     /**
50      * @return initiated xpath with ise namespace context injected
51      */
52     public static XPath setupXpath() {
53         final NamespaceContext nsContext = new NamespaceContext() {
54             public String getNamespaceURI(String prefix) {
55                 final String outcome;
56                 if (prefix == null) {
57                     throw new NullPointerException("Null prefix");
58                 }
59
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";
66                 } else {
67                     outcome = XMLConstants.NULL_NS_URI;
68                 }
69                 return outcome;
70             }
71
72             // This method isn't necessary for XPath processing.
73             public String getPrefix(String uri) {
74                 throw new UnsupportedOperationException();
75             }
76
77             // This method isn't necessary for XPath processing either.
78             public Iterator getPrefixes(String uri) {
79                 throw new UnsupportedOperationException();
80             }
81         };
82
83         XPath xpath = XPathFactory.newInstance().newXPath();
84         xpath.setNamespaceContext(nsContext);
85         return xpath;
86     }
87
88     public static InputSource createInputSource(final String rawSgtDetail) {
89         return new InputSource(new StringReader(rawSgtDetail));
90     }
91
92     /**
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
98      */
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)) {
106                 nodesToExplore.add(
107                         (Node) xpath.evaluate(EXPRESSION_SGT_DETAIL_LINK, sgtResources.item(i), XPathConstants.NODE)
108                 );
109             }
110         }
111         return nodesToExplore;
112     }
113
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);
117     }
118
119     public static Node gainSgtValue(final XPath xpath, final Node sgtNode) throws XPathExpressionException {
120         return (Node) xpath.evaluate(EXPRESSION_SGT_VALUE, sgtNode, XPathConstants.NODE);
121     }
122
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);
125     }
126
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);
129     }
130
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);
134     }
135 }