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 | 8483e9d | 2015-05-26 18:22:07 -0700 | [diff] [blame] | 43 | private static final String URI_BASE = "/rs/subscriber/"; |
Simon Hunt | a00b0ce | 2015-05-22 15:57:11 -0700 | [diff] [blame] | 44 | |
Simon Hunt | ee6a737 | 2015-05-28 14:04:24 -0700 | [diff] [blame^] | 45 | private final XosManagerRestUtils xosUtils = |
| 46 | new XosManagerRestUtils(TEST_XOS_SERVER_ADDRESS, |
| 47 | TEST_XOS_SERVER_PORT, URI_BASE); |
Simon Hunt | 8483e9d | 2015-05-26 18:22:07 -0700 | [diff] [blame] | 48 | private final Logger log = LoggerFactory.getLogger(getClass()); |
Simon Hunt | a00b0ce | 2015-05-22 15:57:11 -0700 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * No instantiation (except via unit test). |
| 52 | */ |
| 53 | XosManager() {} |
| 54 | |
Simon Hunt | ee6a737 | 2015-05-28 14:04:24 -0700 | [diff] [blame^] | 55 | /** |
| 56 | * Returns the subscriber ID to use for calls to the XOS backend. |
| 57 | * Right now, this is implemented to get a list of all subscribers |
| 58 | * in the system and return the first one. |
| 59 | * |
| 60 | * @return subscriber ID |
| 61 | */ |
| 62 | public int getSubscriberId() { |
| 63 | log.info("getSubscriberId() called"); |
| 64 | String result = xosUtils.getRest(); |
| 65 | log.info("from XOS: {}", result); |
| 66 | |
| 67 | JsonNode node; |
| 68 | try { |
| 69 | node = MAPPER.readTree(result); |
| 70 | } catch (IOException e) { |
| 71 | log.error("failed to read subscriber JSON", e); |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | ArrayNode subscribers = (ArrayNode) node.get("subscribers"); |
| 76 | if (subscribers.size() == 0) { |
| 77 | log.error("no subscribers found"); |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | ObjectNode first = (ObjectNode) subscribers.get(0); |
| 82 | int id = first.get("id").asInt(); |
| 83 | log.info("Using subscriber id {}.", id); |
| 84 | return id; |
| 85 | } |
| 86 | |
Simon Hunt | 8483e9d | 2015-05-26 18:22:07 -0700 | [diff] [blame] | 87 | |
| 88 | private String subId(int subscriberId) { |
| 89 | return String.format("%d/", subscriberId); |
| 90 | } |
| 91 | |
Simon Hunt | a00b0ce | 2015-05-22 15:57:11 -0700 | [diff] [blame] | 92 | /** |
| 93 | * Configure XOS to enable the functions that compose the given bundle, |
| 94 | * and disable all the others, for the given subscriber. |
| 95 | * |
| 96 | * @param subscriberId subscriber identifier |
| 97 | * @param bundle new bundle to set |
| 98 | */ |
| 99 | public void setNewBundle(int subscriberId, Bundle bundle) { |
Simon Hunt | 8483e9d | 2015-05-26 18:22:07 -0700 | [diff] [blame] | 100 | log.info("\n>> Set New Bundle : " + bundle.descriptor().id()); |
Simon Hunt | a00b0ce | 2015-05-22 15:57:11 -0700 | [diff] [blame] | 101 | |
Simon Hunt | 8483e9d | 2015-05-26 18:22:07 -0700 | [diff] [blame] | 102 | String uriFmt = subId(subscriberId) + "services/%s/%s"; |
Simon Hunt | a00b0ce | 2015-05-22 15:57:11 -0700 | [diff] [blame] | 103 | Set<XosFunctionDescriptor> inBundle = bundle.descriptor().functions(); |
| 104 | for (XosFunctionDescriptor xfd: XosFunctionDescriptor.values()) { |
Simon Hunt | 90dc8c5 | 2015-05-27 16:56:03 -0700 | [diff] [blame] | 105 | // only process the functions that have a real back-end on XOS |
| 106 | if (xfd.backend()) { |
| 107 | String uri = String.format(uriFmt, xfd.id(), inBundle.contains(xfd)); |
| 108 | String result = xosUtils.putRest(uri); |
| 109 | // TODO: convert JSON result to object and check (if we care) |
| 110 | } |
Simon Hunt | a00b0ce | 2015-05-22 15:57:11 -0700 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Configure XOS with new setting for given user and function, for the |
| 116 | * given subscriber account. |
| 117 | * |
| 118 | * @param subscriberId subscriber identifier |
| 119 | * @param func specific XOS function |
| 120 | * @param user user (containing function state) |
| 121 | */ |
| 122 | public void apply(int subscriberId, XosFunction func, SubscriberUser user) { |
Simon Hunt | 8483e9d | 2015-05-26 18:22:07 -0700 | [diff] [blame] | 123 | log.info("\n>> Apply : " + func + " for " + user); |
Simon Hunt | a00b0ce | 2015-05-22 15:57:11 -0700 | [diff] [blame] | 124 | |
Simon Hunt | 8483e9d | 2015-05-26 18:22:07 -0700 | [diff] [blame] | 125 | String uriPrefix = subId(subscriberId) + "users/" + user.id() + "/"; |
| 126 | String uri = uriPrefix + func.xosUrlApply(user); |
| 127 | String result = xosUtils.putRest(uri); |
| 128 | // TODO: convert JSON result to object and check (if we care) |
Simon Hunt | a00b0ce | 2015-05-22 15:57:11 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | |
| 132 | // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= |
| 133 | |
Simon Hunt | a00b0ce | 2015-05-22 15:57:11 -0700 | [diff] [blame] | 134 | /** |
| 135 | * Singleton instance. |
| 136 | */ |
| 137 | public static final XosManager INSTANCE = new XosManager(); |
Simon Hunt | 2dd48e6 | 2015-05-21 08:50:08 -0700 | [diff] [blame] | 138 | } |