blob: de0ae67d01cf99a5887d9b423aa5ce531c9306e8 [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
Simon Hunt4d99c292015-06-04 18:09:35 -070041 private static final String HEAD_NODE_IP = "headnodeip";
42 private static final String HEAD_NODE_PORT = "headnodeport";
43 private static final int PORT_MIN = 1025;
44 private static final int PORT_MAX = 65535;
45
46 private static final String TEST_XOS_SERVER_IP = "10.254.1.22";
47 private static final String TEST_XOS_SERVER_PORT_STR = "8000";
Simon Huntee6a7372015-05-28 14:04:24 -070048 private static final int TEST_XOS_SERVER_PORT = 8000;
Simon Hunt7d02c082015-05-29 12:17:09 -070049 private static final String URI_RS = "/rs/";
50 private static final String URI_SUBSCRIBER = "/rs/subscriber/%d/";
Simon Huntb1246412015-06-01 13:37:26 -070051 private static final String BUNDLE_URI_FORMAT = "services/%s/%s/";
52
Simon Hunta00b0ce2015-05-22 15:57:11 -070053
Simon Hunt4d99c292015-06-04 18:09:35 -070054 private String xosServerIp;
55 private int xosServerPort;
56 private XosManagerRestUtils xosUtilsRs;
Simon Hunt7d02c082015-05-29 12:17:09 -070057 private XosManagerRestUtils xosUtils;
58
59
Simon Hunt8483e9d2015-05-26 18:22:07 -070060 private final Logger log = LoggerFactory.getLogger(getClass());
Simon Hunta00b0ce2015-05-22 15:57:11 -070061
62 /**
63 * No instantiation (except via unit test).
64 */
65 XosManager() {}
66
Simon Hunt4d99c292015-06-04 18:09:35 -070067 private String getXosServerIp() {
68 return System.getProperty(HEAD_NODE_IP, TEST_XOS_SERVER_IP);
69 }
70
71 private int getXosServerPort() {
72 String p = System.getProperty(HEAD_NODE_PORT, TEST_XOS_SERVER_PORT_STR);
73 int port;
74 try {
75 port = Integer.valueOf(p);
76 } catch (NumberFormatException e) {
77 port = TEST_XOS_SERVER_PORT;
78 log.warn("Could not parse port number [{}], using {}", p, port);
79 }
80 if (port < PORT_MIN || port > PORT_MAX) {
81 log.warn("Bad port number [{}], using {}", port, TEST_XOS_SERVER_PORT);
82 port = TEST_XOS_SERVER_PORT;
83 }
84 return port;
85 }
86
Simon Huntee6a7372015-05-28 14:04:24 -070087 /**
Simon Huntc686c6a2015-06-05 14:33:30 -070088 * Queries XOS for the Subscriber ID lookup data, and returns it.
Simon Huntee6a7372015-05-28 14:04:24 -070089 */
Simon Huntc686c6a2015-06-05 14:33:30 -070090 public ObjectNode initXosSubscriberLookups() {
91 log.info("intDemoSubscriberLookups() called");
Simon Hunt4d99c292015-06-04 18:09:35 -070092 xosServerIp = getXosServerIp();
93 xosServerPort = getXosServerPort();
94 log.info("Using XOS server at {}:{}", xosServerIp, xosServerPort);
95
96 xosUtilsRs = new XosManagerRestUtils(xosServerIp, xosServerPort, URI_RS);
97
Simon Huntc686c6a2015-06-05 14:33:30 -070098 // ask XOS for the subscriber ID lookup info
99 String result = xosUtilsRs.getRest("subidlookup/");
100 log.info("lookup data from XOS: {}", result);
Simon Huntee6a7372015-05-28 14:04:24 -0700101
102 JsonNode node;
103 try {
104 node = MAPPER.readTree(result);
105 } catch (IOException e) {
Simon Huntc686c6a2015-06-05 14:33:30 -0700106 log.error("failed to read subscriber lookup JSON data", e);
107 return null;
Simon Huntee6a7372015-05-28 14:04:24 -0700108 }
Simon Huntc686c6a2015-06-05 14:33:30 -0700109 return (ObjectNode) node;
110 }
Simon Huntee6a7372015-05-28 14:04:24 -0700111
Simon Huntc686c6a2015-06-05 14:33:30 -0700112 /**
113 * Sets a new XOS utils object to bind URL patterns for the
114 * given XOS subscriber ID.
115 *
116 * @param xosSubId XOS subscriber ID
117 */
118 public void setXosUtilsForSubscriber(int xosSubId) {
119 String uri = String.format(URI_SUBSCRIBER, xosSubId);
Simon Hunt4d99c292015-06-04 18:09:35 -0700120 xosUtils = new XosManagerRestUtils(xosServerIp, xosServerPort, uri);
Simon Huntc686c6a2015-06-05 14:33:30 -0700121 }
122
123
124 public void initDemoSubscriber() {
125 log.info("initDemoSubscriber() called");
126 String result = xosUtilsRs.getRest("initdemo/");
127 log.info("initdemo data from XOS: {}", result);
Simon Hunt7d02c082015-05-29 12:17:09 -0700128 }
129
130 /**
131 * Returns the array of users for the subscriber.
132 *
133 * @return list of users
134 */
135 public ArrayNode getUserList() {
136 log.info("getUserList() called");
137 String result = xosUtils.getRest("users/");
138
139 JsonNode node;
140 try {
141 node = MAPPER.readTree(result);
142 } catch (IOException e) {
143 log.error("failed to read user list JSON", e);
144 return null;
Simon Huntee6a7372015-05-28 14:04:24 -0700145 }
146
Simon Hunt7d02c082015-05-29 12:17:09 -0700147 ObjectNode obj = (ObjectNode) node;
148 return (ArrayNode) obj.get("users");
Simon Huntee6a7372015-05-28 14:04:24 -0700149 }
150
Simon Hunt8483e9d2015-05-26 18:22:07 -0700151
Simon Hunta00b0ce2015-05-22 15:57:11 -0700152 /**
153 * Configure XOS to enable the functions that compose the given bundle,
154 * and disable all the others, for the given subscriber.
155 *
Simon Hunta00b0ce2015-05-22 15:57:11 -0700156 * @param bundle new bundle to set
157 */
Simon Hunt7d02c082015-05-29 12:17:09 -0700158 public void setNewBundle(Bundle bundle) {
Simon Huntb1246412015-06-01 13:37:26 -0700159 log.info(">> Set New Bundle : {}", bundle.descriptor().id());
Simon Hunta00b0ce2015-05-22 15:57:11 -0700160
Simon Hunta00b0ce2015-05-22 15:57:11 -0700161 Set<XosFunctionDescriptor> inBundle = bundle.descriptor().functions();
162 for (XosFunctionDescriptor xfd: XosFunctionDescriptor.values()) {
Simon Hunt90dc8c52015-05-27 16:56:03 -0700163 // only process the functions that have a real back-end on XOS
164 if (xfd.backend()) {
Simon Huntb1246412015-06-01 13:37:26 -0700165 String uri = String.format(BUNDLE_URI_FORMAT, xfd.id(),
166 inBundle.contains(xfd));
167 log.info("XOS-URI: {}", uri);
Simon Hunt90dc8c52015-05-27 16:56:03 -0700168 String result = xosUtils.putRest(uri);
169 // TODO: convert JSON result to object and check (if we care)
170 }
Simon Hunta00b0ce2015-05-22 15:57:11 -0700171 }
172 }
173
174 /**
175 * Configure XOS with new setting for given user and function, for the
176 * given subscriber account.
177 *
Simon Hunta00b0ce2015-05-22 15:57:11 -0700178 * @param func specific XOS function
179 * @param user user (containing function state)
180 */
Simon Hunt7d02c082015-05-29 12:17:09 -0700181 public void apply(XosFunction func, SubscriberUser user) {
Simon Huntb1246412015-06-01 13:37:26 -0700182 log.info(">> Apply : {} for {}", func, user);
Simon Hunta00b0ce2015-05-22 15:57:11 -0700183
Simon Hunt7d02c082015-05-29 12:17:09 -0700184 String uriPrefix = "users/" + user.id() + "/";
Simon Hunt8483e9d2015-05-26 18:22:07 -0700185 String uri = uriPrefix + func.xosUrlApply(user);
Simon Huntb1246412015-06-01 13:37:26 -0700186 log.info("XOS-URI: {}", uri);
Simon Hunt8483e9d2015-05-26 18:22:07 -0700187 String result = xosUtils.putRest(uri);
188 // TODO: convert JSON result to object and check (if we care)
Simon Hunta00b0ce2015-05-22 15:57:11 -0700189 }
190
191
192 // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
193
Simon Hunta00b0ce2015-05-22 15:57:11 -0700194 /**
195 * Singleton instance.
196 */
197 public static final XosManager INSTANCE = new XosManager();
Simon Hunt2dd48e62015-05-21 08:50:08 -0700198}