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 | |
| 41 | private static final String TEST_XOS_SERVER_ADDRESS = "10.254.1.22"; |
| 42 | private static final int TEST_XOS_SERVER_PORT = 8000; |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 43 | private static final String URI_RS = "/rs/"; |
| 44 | private static final String URI_SUBSCRIBER = "/rs/subscriber/%d/"; |
Simon Hunt | b124641 | 2015-06-01 13:37:26 -0700 | [diff] [blame^] | 45 | private static final String BUNDLE_URI_FORMAT = "services/%s/%s/"; |
| 46 | |
Simon Hunt | a00b0ce | 2015-05-22 15:57:11 -0700 | [diff] [blame] | 47 | |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 48 | private final XosManagerRestUtils xosUtilsRs = |
Simon Hunt | ee6a737 | 2015-05-28 14:04:24 -0700 | [diff] [blame] | 49 | new XosManagerRestUtils(TEST_XOS_SERVER_ADDRESS, |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 50 | TEST_XOS_SERVER_PORT, URI_RS); |
| 51 | |
| 52 | private XosManagerRestUtils xosUtils; |
| 53 | |
| 54 | |
Simon Hunt | 8483e9d | 2015-05-26 18:22:07 -0700 | [diff] [blame] | 55 | private final Logger log = LoggerFactory.getLogger(getClass()); |
Simon Hunt | a00b0ce | 2015-05-22 15:57:11 -0700 | [diff] [blame] | 56 | |
| 57 | /** |
| 58 | * No instantiation (except via unit test). |
| 59 | */ |
| 60 | XosManager() {} |
| 61 | |
Simon Hunt | ee6a737 | 2015-05-28 14:04:24 -0700 | [diff] [blame] | 62 | /** |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 63 | * Queries XOS for the Demo Subscriber ID and caches it for future calls. |
Simon Hunt | ee6a737 | 2015-05-28 14:04:24 -0700 | [diff] [blame] | 64 | */ |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 65 | public int initDemoSubscriber() { |
| 66 | log.info("intDemoSubscriber() called"); |
| 67 | String result = xosUtilsRs.getRest("initdemo/"); |
Simon Hunt | ee6a737 | 2015-05-28 14:04:24 -0700 | [diff] [blame] | 68 | log.info("from XOS: {}", result); |
| 69 | |
| 70 | JsonNode node; |
| 71 | try { |
| 72 | node = MAPPER.readTree(result); |
| 73 | } catch (IOException e) { |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 74 | log.error("failed to read demo subscriber JSON", e); |
Simon Hunt | ee6a737 | 2015-05-28 14:04:24 -0700 | [diff] [blame] | 75 | return 0; |
| 76 | } |
| 77 | |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 78 | ObjectNode obj = (ObjectNode) node; |
Simon Hunt | b124641 | 2015-06-01 13:37:26 -0700 | [diff] [blame^] | 79 | int demoId = obj.get("id").asInt(); |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 80 | log.info("Using DEMO subscriber ID {}.", demoId); |
| 81 | |
| 82 | String uri = String.format(URI_SUBSCRIBER, demoId); |
| 83 | xosUtils = new XosManagerRestUtils(TEST_XOS_SERVER_ADDRESS, |
| 84 | TEST_XOS_SERVER_PORT, uri); |
| 85 | return demoId; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Returns the array of users for the subscriber. |
| 90 | * |
| 91 | * @return list of users |
| 92 | */ |
| 93 | public ArrayNode getUserList() { |
| 94 | log.info("getUserList() called"); |
| 95 | String result = xosUtils.getRest("users/"); |
| 96 | |
| 97 | JsonNode node; |
| 98 | try { |
| 99 | node = MAPPER.readTree(result); |
| 100 | } catch (IOException e) { |
| 101 | log.error("failed to read user list JSON", e); |
| 102 | return null; |
Simon Hunt | ee6a737 | 2015-05-28 14:04:24 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 105 | ObjectNode obj = (ObjectNode) node; |
| 106 | return (ArrayNode) obj.get("users"); |
Simon Hunt | ee6a737 | 2015-05-28 14:04:24 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Simon Hunt | 8483e9d | 2015-05-26 18:22:07 -0700 | [diff] [blame] | 109 | |
Simon Hunt | a00b0ce | 2015-05-22 15:57:11 -0700 | [diff] [blame] | 110 | /** |
| 111 | * Configure XOS to enable the functions that compose the given bundle, |
| 112 | * and disable all the others, for the given subscriber. |
| 113 | * |
Simon Hunt | a00b0ce | 2015-05-22 15:57:11 -0700 | [diff] [blame] | 114 | * @param bundle new bundle to set |
| 115 | */ |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 116 | public void setNewBundle(Bundle bundle) { |
Simon Hunt | b124641 | 2015-06-01 13:37:26 -0700 | [diff] [blame^] | 117 | log.info(">> Set New Bundle : {}", bundle.descriptor().id()); |
Simon Hunt | a00b0ce | 2015-05-22 15:57:11 -0700 | [diff] [blame] | 118 | |
Simon Hunt | a00b0ce | 2015-05-22 15:57:11 -0700 | [diff] [blame] | 119 | Set<XosFunctionDescriptor> inBundle = bundle.descriptor().functions(); |
| 120 | for (XosFunctionDescriptor xfd: XosFunctionDescriptor.values()) { |
Simon Hunt | 90dc8c5 | 2015-05-27 16:56:03 -0700 | [diff] [blame] | 121 | // only process the functions that have a real back-end on XOS |
| 122 | if (xfd.backend()) { |
Simon Hunt | b124641 | 2015-06-01 13:37:26 -0700 | [diff] [blame^] | 123 | String uri = String.format(BUNDLE_URI_FORMAT, xfd.id(), |
| 124 | inBundle.contains(xfd)); |
| 125 | log.info("XOS-URI: {}", uri); |
Simon Hunt | 90dc8c5 | 2015-05-27 16:56:03 -0700 | [diff] [blame] | 126 | String result = xosUtils.putRest(uri); |
| 127 | // TODO: convert JSON result to object and check (if we care) |
| 128 | } |
Simon Hunt | a00b0ce | 2015-05-22 15:57:11 -0700 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Configure XOS with new setting for given user and function, for the |
| 134 | * given subscriber account. |
| 135 | * |
Simon Hunt | a00b0ce | 2015-05-22 15:57:11 -0700 | [diff] [blame] | 136 | * @param func specific XOS function |
| 137 | * @param user user (containing function state) |
| 138 | */ |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 139 | public void apply(XosFunction func, SubscriberUser user) { |
Simon Hunt | b124641 | 2015-06-01 13:37:26 -0700 | [diff] [blame^] | 140 | log.info(">> Apply : {} for {}", func, user); |
Simon Hunt | a00b0ce | 2015-05-22 15:57:11 -0700 | [diff] [blame] | 141 | |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 142 | String uriPrefix = "users/" + user.id() + "/"; |
Simon Hunt | 8483e9d | 2015-05-26 18:22:07 -0700 | [diff] [blame] | 143 | String uri = uriPrefix + func.xosUrlApply(user); |
Simon Hunt | b124641 | 2015-06-01 13:37:26 -0700 | [diff] [blame^] | 144 | log.info("XOS-URI: {}", uri); |
Simon Hunt | 8483e9d | 2015-05-26 18:22:07 -0700 | [diff] [blame] | 145 | String result = xosUtils.putRest(uri); |
| 146 | // TODO: convert JSON result to object and check (if we care) |
Simon Hunt | a00b0ce | 2015-05-22 15:57:11 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | |
| 150 | // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= |
| 151 | |
Simon Hunt | a00b0ce | 2015-05-22 15:57:11 -0700 | [diff] [blame] | 152 | /** |
| 153 | * Singleton instance. |
| 154 | */ |
| 155 | public static final XosManager INSTANCE = new XosManager(); |
Simon Hunt | 2dd48e6 | 2015-05-21 08:50:08 -0700 | [diff] [blame] | 156 | } |