blob: e0ce191ccb31ca2782cafbab5ce8b81c5642e071 [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;
24
25import java.util.Set;
26
Simon Hunt2dd48e62015-05-21 08:50:08 -070027/**
28 * Encapsulation of interactions with XOS.
29 */
30public class XosManager {
31
Simon Hunta00b0ce2015-05-22 15:57:11 -070032 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 Hunt2dd48e62015-05-21 08:50:08 -0700105}