blob: 4264e6bc46c4c6a6eddf4a8799ebfecbfc7d0ad1 [file] [log] [blame]
Simon Hunt2dd48e62015-05-21 08:50:08 -07001/*
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
18package org.onosproject.cord.gui;
19
Simon Hunta00b0ce2015-05-22 15:57:11 -070020import org.onosproject.cord.gui.model.Bundle;
21import org.onosproject.cord.gui.model.SubscriberUser;
22import org.onosproject.cord.gui.model.XosFunction;
23import org.onosproject.cord.gui.model.XosFunctionDescriptor;
Simon Hunt8483e9d2015-05-26 18:22:07 -070024import org.slf4j.Logger;
25import org.slf4j.LoggerFactory;
Simon Hunta00b0ce2015-05-22 15:57:11 -070026
27import java.util.Set;
28
Simon Hunt2dd48e62015-05-21 08:50:08 -070029/**
30 * Encapsulation of interactions with XOS.
31 */
32public class XosManager {
33
Simon Hunt8483e9d2015-05-26 18:22:07 -070034 private static final String URI_BASE = "/rs/subscriber/";
Simon Hunta00b0ce2015-05-22 15:57:11 -070035
Simon Hunt8483e9d2015-05-26 18:22:07 -070036 private final XosManagerRestUtils xosUtils = new XosManagerRestUtils(URI_BASE);
37 private final Logger log = LoggerFactory.getLogger(getClass());
Simon Hunta00b0ce2015-05-22 15:57:11 -070038
39 /**
40 * No instantiation (except via unit test).
41 */
42 XosManager() {}
43
Simon Hunt8483e9d2015-05-26 18:22:07 -070044
45 private String subId(int subscriberId) {
46 return String.format("%d/", subscriberId);
47 }
48
Simon Hunta00b0ce2015-05-22 15:57:11 -070049 /**
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 Hunt8483e9d2015-05-26 18:22:07 -070057 log.info("\n>> Set New Bundle : " + bundle.descriptor().id());
Simon Hunta00b0ce2015-05-22 15:57:11 -070058
Simon Hunt8483e9d2015-05-26 18:22:07 -070059 String uriFmt = subId(subscriberId) + "services/%s/%s";
Simon Hunta00b0ce2015-05-22 15:57:11 -070060 Set<XosFunctionDescriptor> inBundle = bundle.descriptor().functions();
61 for (XosFunctionDescriptor xfd: XosFunctionDescriptor.values()) {
Simon Hunt8483e9d2015-05-26 18:22:07 -070062 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 Hunta00b0ce2015-05-22 15:57:11 -070065 }
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 Hunt8483e9d2015-05-26 18:22:07 -070077 log.info("\n>> Apply : " + func + " for " + user);
Simon Hunta00b0ce2015-05-22 15:57:11 -070078
Simon Hunt8483e9d2015-05-26 18:22:07 -070079 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 Hunta00b0ce2015-05-22 15:57:11 -070083 }
84
85
86 // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
87
Simon Hunta00b0ce2015-05-22 15:57:11 -070088 /**
89 * Singleton instance.
90 */
91 public static final XosManager INSTANCE = new XosManager();
Simon Hunt2dd48e62015-05-21 08:50:08 -070092}