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 | a00b0ce | 2015-05-22 15:57:11 -0700 | [diff] [blame] | 20 | import org.onosproject.cord.gui.model.Bundle; |
| 21 | import org.onosproject.cord.gui.model.SubscriberUser; |
| 22 | import org.onosproject.cord.gui.model.XosFunction; |
| 23 | import org.onosproject.cord.gui.model.XosFunctionDescriptor; |
Simon Hunt | 8483e9d | 2015-05-26 18:22:07 -0700 | [diff] [blame] | 24 | import org.slf4j.Logger; |
| 25 | import org.slf4j.LoggerFactory; |
Simon Hunt | a00b0ce | 2015-05-22 15:57:11 -0700 | [diff] [blame] | 26 | |
| 27 | import java.util.Set; |
| 28 | |
Simon Hunt | 2dd48e6 | 2015-05-21 08:50:08 -0700 | [diff] [blame] | 29 | /** |
| 30 | * Encapsulation of interactions with XOS. |
| 31 | */ |
| 32 | public class XosManager { |
| 33 | |
Simon Hunt | 8483e9d | 2015-05-26 18:22:07 -0700 | [diff] [blame] | 34 | private static final String URI_BASE = "/rs/subscriber/"; |
Simon Hunt | a00b0ce | 2015-05-22 15:57:11 -0700 | [diff] [blame] | 35 | |
Simon Hunt | 8483e9d | 2015-05-26 18:22:07 -0700 | [diff] [blame] | 36 | private final XosManagerRestUtils xosUtils = new XosManagerRestUtils(URI_BASE); |
| 37 | private final Logger log = LoggerFactory.getLogger(getClass()); |
Simon Hunt | a00b0ce | 2015-05-22 15:57:11 -0700 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * No instantiation (except via unit test). |
| 41 | */ |
| 42 | XosManager() {} |
| 43 | |
Simon Hunt | 8483e9d | 2015-05-26 18:22:07 -0700 | [diff] [blame] | 44 | |
| 45 | private String subId(int subscriberId) { |
| 46 | return String.format("%d/", subscriberId); |
| 47 | } |
| 48 | |
Simon Hunt | a00b0ce | 2015-05-22 15:57:11 -0700 | [diff] [blame] | 49 | /** |
| 50 | * Configure XOS to enable the functions that compose the given bundle, |
| 51 | * and disable all the others, for the given subscriber. |
| 52 | * |
| 53 | * @param subscriberId subscriber identifier |
| 54 | * @param bundle new bundle to set |
| 55 | */ |
| 56 | public void setNewBundle(int subscriberId, Bundle bundle) { |
Simon Hunt | 8483e9d | 2015-05-26 18:22:07 -0700 | [diff] [blame] | 57 | log.info("\n>> Set New Bundle : " + bundle.descriptor().id()); |
Simon Hunt | a00b0ce | 2015-05-22 15:57:11 -0700 | [diff] [blame] | 58 | |
Simon Hunt | 8483e9d | 2015-05-26 18:22:07 -0700 | [diff] [blame] | 59 | String uriFmt = subId(subscriberId) + "services/%s/%s"; |
Simon Hunt | a00b0ce | 2015-05-22 15:57:11 -0700 | [diff] [blame] | 60 | Set<XosFunctionDescriptor> inBundle = bundle.descriptor().functions(); |
| 61 | for (XosFunctionDescriptor xfd: XosFunctionDescriptor.values()) { |
Simon Hunt | 8483e9d | 2015-05-26 18:22:07 -0700 | [diff] [blame] | 62 | String uri = String.format(uriFmt, xfd.id(), inBundle.contains(xfd)); |
| 63 | String result = xosUtils.putRest(uri); |
| 64 | // TODO: convert JSON result to object and check (if we care) |
Simon Hunt | a00b0ce | 2015-05-22 15:57:11 -0700 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Configure XOS with new setting for given user and function, for the |
| 70 | * given subscriber account. |
| 71 | * |
| 72 | * @param subscriberId subscriber identifier |
| 73 | * @param func specific XOS function |
| 74 | * @param user user (containing function state) |
| 75 | */ |
| 76 | public void apply(int subscriberId, XosFunction func, SubscriberUser user) { |
Simon Hunt | 8483e9d | 2015-05-26 18:22:07 -0700 | [diff] [blame] | 77 | log.info("\n>> Apply : " + func + " for " + user); |
Simon Hunt | a00b0ce | 2015-05-22 15:57:11 -0700 | [diff] [blame] | 78 | |
Simon Hunt | 8483e9d | 2015-05-26 18:22:07 -0700 | [diff] [blame] | 79 | String uriPrefix = subId(subscriberId) + "users/" + user.id() + "/"; |
| 80 | String uri = uriPrefix + func.xosUrlApply(user); |
| 81 | String result = xosUtils.putRest(uri); |
| 82 | // TODO: convert JSON result to object and check (if we care) |
Simon Hunt | a00b0ce | 2015-05-22 15:57:11 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | |
| 86 | // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= |
| 87 | |
Simon Hunt | a00b0ce | 2015-05-22 15:57:11 -0700 | [diff] [blame] | 88 | /** |
| 89 | * Singleton instance. |
| 90 | */ |
| 91 | public static final XosManager INSTANCE = new XosManager(); |
Simon Hunt | 2dd48e6 | 2015-05-21 08:50:08 -0700 | [diff] [blame] | 92 | } |