blob: 2af231f0538015760021ef98bb423965a8d2e74f [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 Huntee6a7372015-05-28 14:04:24 -070020import com.fasterxml.jackson.databind.JsonNode;
21import com.fasterxml.jackson.databind.ObjectMapper;
22import com.fasterxml.jackson.databind.node.ArrayNode;
23import com.fasterxml.jackson.databind.node.ObjectNode;
Simon Hunta00b0ce2015-05-22 15:57:11 -070024import org.onosproject.cord.gui.model.Bundle;
25import org.onosproject.cord.gui.model.SubscriberUser;
26import org.onosproject.cord.gui.model.XosFunction;
27import org.onosproject.cord.gui.model.XosFunctionDescriptor;
Simon Hunt8483e9d2015-05-26 18:22:07 -070028import org.slf4j.Logger;
29import org.slf4j.LoggerFactory;
Simon Hunta00b0ce2015-05-22 15:57:11 -070030
Simon Huntee6a7372015-05-28 14:04:24 -070031import java.io.IOException;
Simon Hunta00b0ce2015-05-22 15:57:11 -070032import java.util.Set;
33
Simon Hunt2dd48e62015-05-21 08:50:08 -070034/**
35 * Encapsulation of interactions with XOS.
36 */
37public class XosManager {
38
Simon Huntee6a7372015-05-28 14:04:24 -070039 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 Hunt8483e9d2015-05-26 18:22:07 -070043 private static final String URI_BASE = "/rs/subscriber/";
Simon Hunta00b0ce2015-05-22 15:57:11 -070044
Simon Huntee6a7372015-05-28 14:04:24 -070045 private final XosManagerRestUtils xosUtils =
46 new XosManagerRestUtils(TEST_XOS_SERVER_ADDRESS,
47 TEST_XOS_SERVER_PORT, URI_BASE);
Simon Hunt8483e9d2015-05-26 18:22:07 -070048 private final Logger log = LoggerFactory.getLogger(getClass());
Simon Hunta00b0ce2015-05-22 15:57:11 -070049
50 /**
51 * No instantiation (except via unit test).
52 */
53 XosManager() {}
54
Simon Huntee6a7372015-05-28 14:04:24 -070055 /**
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 Hunt8483e9d2015-05-26 18:22:07 -070087
88 private String subId(int subscriberId) {
89 return String.format("%d/", subscriberId);
90 }
91
Simon Hunta00b0ce2015-05-22 15:57:11 -070092 /**
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 Hunt8483e9d2015-05-26 18:22:07 -0700100 log.info("\n>> Set New Bundle : " + bundle.descriptor().id());
Simon Hunta00b0ce2015-05-22 15:57:11 -0700101
Simon Hunt8483e9d2015-05-26 18:22:07 -0700102 String uriFmt = subId(subscriberId) + "services/%s/%s";
Simon Hunta00b0ce2015-05-22 15:57:11 -0700103 Set<XosFunctionDescriptor> inBundle = bundle.descriptor().functions();
104 for (XosFunctionDescriptor xfd: XosFunctionDescriptor.values()) {
Simon Hunt90dc8c52015-05-27 16:56:03 -0700105 // 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 Hunta00b0ce2015-05-22 15:57:11 -0700111 }
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 Hunt8483e9d2015-05-26 18:22:07 -0700123 log.info("\n>> Apply : " + func + " for " + user);
Simon Hunta00b0ce2015-05-22 15:57:11 -0700124
Simon Hunt8483e9d2015-05-26 18:22:07 -0700125 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 Hunta00b0ce2015-05-22 15:57:11 -0700129 }
130
131
132 // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
133
Simon Hunta00b0ce2015-05-22 15:57:11 -0700134 /**
135 * Singleton instance.
136 */
137 public static final XosManager INSTANCE = new XosManager();
Simon Hunt2dd48e62015-05-21 08:50:08 -0700138}