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; |
| 24 | |
| 25 | import java.util.Set; |
| 26 | |
Simon Hunt | 2dd48e6 | 2015-05-21 08:50:08 -0700 | [diff] [blame] | 27 | /** |
| 28 | * Encapsulation of interactions with XOS. |
| 29 | */ |
| 30 | public class XosManager { |
| 31 | |
Simon Hunt | a00b0ce | 2015-05-22 15:57:11 -0700 | [diff] [blame] | 32 | private static final String XOS_HOST = "10.254.1.22"; |
| 33 | private static final String XOS_PORT = "8000"; |
| 34 | |
| 35 | private static final String URL_FMT = "http://%s:%s/xoslib/rs/subscriber/"; |
| 36 | |
| 37 | private static final String BASE_URL = |
| 38 | String.format(URL_FMT, XOS_HOST, XOS_PORT); |
| 39 | |
| 40 | |
| 41 | /** |
| 42 | * No instantiation (except via unit test). |
| 43 | */ |
| 44 | XosManager() {} |
| 45 | |
| 46 | /** |
| 47 | * Configure XOS to enable the functions that compose the given bundle, |
| 48 | * and disable all the others, for the given subscriber. |
| 49 | * |
| 50 | * @param subscriberId subscriber identifier |
| 51 | * @param bundle new bundle to set |
| 52 | */ |
| 53 | public void setNewBundle(int subscriberId, Bundle bundle) { |
| 54 | System.out.println("\n>> Set New Bundle : " + bundle.descriptor().id()); |
| 55 | |
| 56 | String urlFmt = xosUrl(subscriberId) + "services/%s/%s"; |
| 57 | Set<XosFunctionDescriptor> inBundle = bundle.descriptor().functions(); |
| 58 | for (XosFunctionDescriptor xfd: XosFunctionDescriptor.values()) { |
| 59 | xosEnableFunction(urlFmt, xfd, inBundle.contains(xfd)); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Configure XOS with new setting for given user and function, for the |
| 65 | * given subscriber account. |
| 66 | * |
| 67 | * @param subscriberId subscriber identifier |
| 68 | * @param func specific XOS function |
| 69 | * @param user user (containing function state) |
| 70 | */ |
| 71 | public void apply(int subscriberId, XosFunction func, SubscriberUser user) { |
| 72 | System.out.println("\n>> Apply : " + func + " for " + user); |
| 73 | |
| 74 | String urlPrefix = xosUrl(subscriberId) + "users/" + user.id() + "/"; |
| 75 | String url = urlPrefix + func.xosUrlApply(user); |
| 76 | restPut(url); |
| 77 | } |
| 78 | |
| 79 | |
| 80 | // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= |
| 81 | |
| 82 | private String xosUrl(int subscriberId) { |
| 83 | return BASE_URL + String.format("%d/", subscriberId); |
| 84 | } |
| 85 | |
| 86 | private void xosEnableFunction(String urlFmt, XosFunctionDescriptor xfd, |
| 87 | boolean enable) { |
| 88 | String url = String.format(urlFmt, xfd.id(), enable); |
| 89 | restPut(url); |
| 90 | } |
| 91 | |
| 92 | // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= |
| 93 | |
| 94 | private void restPut(String url) { |
| 95 | // TODO: wire up to Jackson client... |
| 96 | System.out.println("<<PUT>> " + url); |
| 97 | } |
| 98 | |
| 99 | // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= |
| 100 | |
| 101 | /** |
| 102 | * Singleton instance. |
| 103 | */ |
| 104 | public static final XosManager INSTANCE = new XosManager(); |
Simon Hunt | 2dd48e6 | 2015-05-21 08:50:08 -0700 | [diff] [blame] | 105 | } |