Simon Hunt | 2dd48e6 | 2015-05-21 08:50:08 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | * |
| 16 | */ |
| 17 | |
| 18 | package org.onosproject.cord.gui; |
| 19 | |
Simon Hunt | ee6a737 | 2015-05-28 14:04:24 -0700 | [diff] [blame] | 20 | import com.fasterxml.jackson.databind.JsonNode; |
| 21 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 22 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 23 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Simon Hunt | a00b0ce | 2015-05-22 15:57:11 -0700 | [diff] [blame] | 24 | import org.onosproject.cord.gui.model.Bundle; |
| 25 | import org.onosproject.cord.gui.model.SubscriberUser; |
| 26 | import org.onosproject.cord.gui.model.XosFunction; |
| 27 | import org.onosproject.cord.gui.model.XosFunctionDescriptor; |
Simon Hunt | 8483e9d | 2015-05-26 18:22:07 -0700 | [diff] [blame] | 28 | import org.slf4j.Logger; |
| 29 | import org.slf4j.LoggerFactory; |
Simon Hunt | a00b0ce | 2015-05-22 15:57:11 -0700 | [diff] [blame] | 30 | |
Simon Hunt | ee6a737 | 2015-05-28 14:04:24 -0700 | [diff] [blame] | 31 | import java.io.IOException; |
Simon Hunt | a00b0ce | 2015-05-22 15:57:11 -0700 | [diff] [blame] | 32 | import java.util.Set; |
| 33 | |
Simon Hunt | 2dd48e6 | 2015-05-21 08:50:08 -0700 | [diff] [blame] | 34 | /** |
| 35 | * Encapsulation of interactions with XOS. |
| 36 | */ |
| 37 | public class XosManager { |
| 38 | |
Simon Hunt | ee6a737 | 2015-05-28 14:04:24 -0700 | [diff] [blame] | 39 | private static final ObjectMapper MAPPER = new ObjectMapper(); |
| 40 | |
Simon Hunt | 4d99c29 | 2015-06-04 18:09:35 -0700 | [diff] [blame] | 41 | private static final String HEAD_NODE_IP = "headnodeip"; |
| 42 | private static final String HEAD_NODE_PORT = "headnodeport"; |
| 43 | private static final int PORT_MIN = 1025; |
| 44 | private static final int PORT_MAX = 65535; |
| 45 | |
| 46 | private static final String TEST_XOS_SERVER_IP = "10.254.1.22"; |
| 47 | private static final String TEST_XOS_SERVER_PORT_STR = "8000"; |
Simon Hunt | ee6a737 | 2015-05-28 14:04:24 -0700 | [diff] [blame] | 48 | private static final int TEST_XOS_SERVER_PORT = 8000; |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 49 | private static final String URI_RS = "/rs/"; |
| 50 | private static final String URI_SUBSCRIBER = "/rs/subscriber/%d/"; |
Simon Hunt | b124641 | 2015-06-01 13:37:26 -0700 | [diff] [blame] | 51 | private static final String BUNDLE_URI_FORMAT = "services/%s/%s/"; |
| 52 | |
Simon Hunt | a00b0ce | 2015-05-22 15:57:11 -0700 | [diff] [blame] | 53 | |
Simon Hunt | 4d99c29 | 2015-06-04 18:09:35 -0700 | [diff] [blame] | 54 | private String xosServerIp; |
| 55 | private int xosServerPort; |
| 56 | private XosManagerRestUtils xosUtilsRs; |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 57 | private XosManagerRestUtils xosUtils; |
| 58 | |
| 59 | |
Simon Hunt | 8483e9d | 2015-05-26 18:22:07 -0700 | [diff] [blame] | 60 | private final Logger log = LoggerFactory.getLogger(getClass()); |
Simon Hunt | a00b0ce | 2015-05-22 15:57:11 -0700 | [diff] [blame] | 61 | |
| 62 | /** |
| 63 | * No instantiation (except via unit test). |
| 64 | */ |
| 65 | XosManager() {} |
| 66 | |
Simon Hunt | 4d99c29 | 2015-06-04 18:09:35 -0700 | [diff] [blame] | 67 | private String getXosServerIp() { |
| 68 | return System.getProperty(HEAD_NODE_IP, TEST_XOS_SERVER_IP); |
| 69 | } |
| 70 | |
| 71 | private int getXosServerPort() { |
| 72 | String p = System.getProperty(HEAD_NODE_PORT, TEST_XOS_SERVER_PORT_STR); |
| 73 | int port; |
| 74 | try { |
| 75 | port = Integer.valueOf(p); |
| 76 | } catch (NumberFormatException e) { |
| 77 | port = TEST_XOS_SERVER_PORT; |
| 78 | log.warn("Could not parse port number [{}], using {}", p, port); |
| 79 | } |
| 80 | if (port < PORT_MIN || port > PORT_MAX) { |
| 81 | log.warn("Bad port number [{}], using {}", port, TEST_XOS_SERVER_PORT); |
| 82 | port = TEST_XOS_SERVER_PORT; |
| 83 | } |
| 84 | return port; |
| 85 | } |
| 86 | |
Simon Hunt | ee6a737 | 2015-05-28 14:04:24 -0700 | [diff] [blame] | 87 | /** |
Simon Hunt | c686c6a | 2015-06-05 14:33:30 -0700 | [diff] [blame] | 88 | * Queries XOS for the Subscriber ID lookup data, and returns it. |
Simon Hunt | ee6a737 | 2015-05-28 14:04:24 -0700 | [diff] [blame] | 89 | */ |
Simon Hunt | c686c6a | 2015-06-05 14:33:30 -0700 | [diff] [blame] | 90 | public ObjectNode initXosSubscriberLookups() { |
| 91 | log.info("intDemoSubscriberLookups() called"); |
Simon Hunt | 4d99c29 | 2015-06-04 18:09:35 -0700 | [diff] [blame] | 92 | xosServerIp = getXosServerIp(); |
| 93 | xosServerPort = getXosServerPort(); |
| 94 | log.info("Using XOS server at {}:{}", xosServerIp, xosServerPort); |
| 95 | |
| 96 | xosUtilsRs = new XosManagerRestUtils(xosServerIp, xosServerPort, URI_RS); |
| 97 | |
Simon Hunt | c686c6a | 2015-06-05 14:33:30 -0700 | [diff] [blame] | 98 | // ask XOS for the subscriber ID lookup info |
| 99 | String result = xosUtilsRs.getRest("subidlookup/"); |
| 100 | log.info("lookup data from XOS: {}", result); |
Simon Hunt | ee6a737 | 2015-05-28 14:04:24 -0700 | [diff] [blame] | 101 | |
| 102 | JsonNode node; |
| 103 | try { |
| 104 | node = MAPPER.readTree(result); |
| 105 | } catch (IOException e) { |
Simon Hunt | c686c6a | 2015-06-05 14:33:30 -0700 | [diff] [blame] | 106 | log.error("failed to read subscriber lookup JSON data", e); |
| 107 | return null; |
Simon Hunt | ee6a737 | 2015-05-28 14:04:24 -0700 | [diff] [blame] | 108 | } |
Simon Hunt | c686c6a | 2015-06-05 14:33:30 -0700 | [diff] [blame] | 109 | return (ObjectNode) node; |
| 110 | } |
Simon Hunt | ee6a737 | 2015-05-28 14:04:24 -0700 | [diff] [blame] | 111 | |
Simon Hunt | c686c6a | 2015-06-05 14:33:30 -0700 | [diff] [blame] | 112 | /** |
| 113 | * Sets a new XOS utils object to bind URL patterns for the |
| 114 | * given XOS subscriber ID. |
| 115 | * |
| 116 | * @param xosSubId XOS subscriber ID |
| 117 | */ |
| 118 | public void setXosUtilsForSubscriber(int xosSubId) { |
| 119 | String uri = String.format(URI_SUBSCRIBER, xosSubId); |
Simon Hunt | 4d99c29 | 2015-06-04 18:09:35 -0700 | [diff] [blame] | 120 | xosUtils = new XosManagerRestUtils(xosServerIp, xosServerPort, uri); |
Simon Hunt | c686c6a | 2015-06-05 14:33:30 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | |
| 124 | public void initDemoSubscriber() { |
| 125 | log.info("initDemoSubscriber() called"); |
| 126 | String result = xosUtilsRs.getRest("initdemo/"); |
| 127 | log.info("initdemo data from XOS: {}", result); |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Returns the array of users for the subscriber. |
| 132 | * |
| 133 | * @return list of users |
| 134 | */ |
| 135 | public ArrayNode getUserList() { |
| 136 | log.info("getUserList() called"); |
| 137 | String result = xosUtils.getRest("users/"); |
| 138 | |
| 139 | JsonNode node; |
| 140 | try { |
| 141 | node = MAPPER.readTree(result); |
| 142 | } catch (IOException e) { |
| 143 | log.error("failed to read user list JSON", e); |
| 144 | return null; |
Simon Hunt | ee6a737 | 2015-05-28 14:04:24 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 147 | ObjectNode obj = (ObjectNode) node; |
| 148 | return (ArrayNode) obj.get("users"); |
Simon Hunt | ee6a737 | 2015-05-28 14:04:24 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Simon Hunt | 8483e9d | 2015-05-26 18:22:07 -0700 | [diff] [blame] | 151 | |
Simon Hunt | a00b0ce | 2015-05-22 15:57:11 -0700 | [diff] [blame] | 152 | /** |
| 153 | * Configure XOS to enable the functions that compose the given bundle, |
| 154 | * and disable all the others, for the given subscriber. |
| 155 | * |
Simon Hunt | a00b0ce | 2015-05-22 15:57:11 -0700 | [diff] [blame] | 156 | * @param bundle new bundle to set |
| 157 | */ |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 158 | public void setNewBundle(Bundle bundle) { |
Simon Hunt | b124641 | 2015-06-01 13:37:26 -0700 | [diff] [blame] | 159 | log.info(">> Set New Bundle : {}", bundle.descriptor().id()); |
Simon Hunt | a00b0ce | 2015-05-22 15:57:11 -0700 | [diff] [blame] | 160 | |
Simon Hunt | a00b0ce | 2015-05-22 15:57:11 -0700 | [diff] [blame] | 161 | Set<XosFunctionDescriptor> inBundle = bundle.descriptor().functions(); |
| 162 | for (XosFunctionDescriptor xfd: XosFunctionDescriptor.values()) { |
Simon Hunt | 90dc8c5 | 2015-05-27 16:56:03 -0700 | [diff] [blame] | 163 | // only process the functions that have a real back-end on XOS |
| 164 | if (xfd.backend()) { |
Simon Hunt | b124641 | 2015-06-01 13:37:26 -0700 | [diff] [blame] | 165 | String uri = String.format(BUNDLE_URI_FORMAT, xfd.id(), |
| 166 | inBundle.contains(xfd)); |
| 167 | log.info("XOS-URI: {}", uri); |
Simon Hunt | 90dc8c5 | 2015-05-27 16:56:03 -0700 | [diff] [blame] | 168 | String result = xosUtils.putRest(uri); |
| 169 | // TODO: convert JSON result to object and check (if we care) |
| 170 | } |
Simon Hunt | a00b0ce | 2015-05-22 15:57:11 -0700 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Configure XOS with new setting for given user and function, for the |
| 176 | * given subscriber account. |
| 177 | * |
Simon Hunt | a00b0ce | 2015-05-22 15:57:11 -0700 | [diff] [blame] | 178 | * @param func specific XOS function |
| 179 | * @param user user (containing function state) |
| 180 | */ |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 181 | public void apply(XosFunction func, SubscriberUser user) { |
Simon Hunt | b124641 | 2015-06-01 13:37:26 -0700 | [diff] [blame] | 182 | log.info(">> Apply : {} for {}", func, user); |
Simon Hunt | a00b0ce | 2015-05-22 15:57:11 -0700 | [diff] [blame] | 183 | |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 184 | String uriPrefix = "users/" + user.id() + "/"; |
Simon Hunt | 8483e9d | 2015-05-26 18:22:07 -0700 | [diff] [blame] | 185 | String uri = uriPrefix + func.xosUrlApply(user); |
Simon Hunt | b124641 | 2015-06-01 13:37:26 -0700 | [diff] [blame] | 186 | log.info("XOS-URI: {}", uri); |
Simon Hunt | 8483e9d | 2015-05-26 18:22:07 -0700 | [diff] [blame] | 187 | String result = xosUtils.putRest(uri); |
| 188 | // TODO: convert JSON result to object and check (if we care) |
Simon Hunt | a00b0ce | 2015-05-22 15:57:11 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | |
| 192 | // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= |
| 193 | |
Simon Hunt | a00b0ce | 2015-05-22 15:57:11 -0700 | [diff] [blame] | 194 | /** |
| 195 | * Singleton instance. |
| 196 | */ |
| 197 | public static final XosManager INSTANCE = new XosManager(); |
Simon Hunt | 2dd48e6 | 2015-05-21 08:50:08 -0700 | [diff] [blame] | 198 | } |