blob: f3007e7c87bcd2840a31aeaea04b5d33c1d91c4e [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 Hunt7d02c082015-05-29 12:17:09 -070043 private static final String URI_RS = "/rs/";
44 private static final String URI_SUBSCRIBER = "/rs/subscriber/%d/";
Simon Huntb1246412015-06-01 13:37:26 -070045 private static final String BUNDLE_URI_FORMAT = "services/%s/%s/";
46
Simon Hunta00b0ce2015-05-22 15:57:11 -070047
Simon Hunt7d02c082015-05-29 12:17:09 -070048 private final XosManagerRestUtils xosUtilsRs =
Simon Huntee6a7372015-05-28 14:04:24 -070049 new XosManagerRestUtils(TEST_XOS_SERVER_ADDRESS,
Simon Hunt7d02c082015-05-29 12:17:09 -070050 TEST_XOS_SERVER_PORT, URI_RS);
51
52 private XosManagerRestUtils xosUtils;
53
54
Simon Hunt8483e9d2015-05-26 18:22:07 -070055 private final Logger log = LoggerFactory.getLogger(getClass());
Simon Hunta00b0ce2015-05-22 15:57:11 -070056
57 /**
58 * No instantiation (except via unit test).
59 */
60 XosManager() {}
61
Simon Huntee6a7372015-05-28 14:04:24 -070062 /**
Simon Hunt7d02c082015-05-29 12:17:09 -070063 * Queries XOS for the Demo Subscriber ID and caches it for future calls.
Simon Huntee6a7372015-05-28 14:04:24 -070064 */
Simon Hunt7d02c082015-05-29 12:17:09 -070065 public int initDemoSubscriber() {
66 log.info("intDemoSubscriber() called");
67 String result = xosUtilsRs.getRest("initdemo/");
Simon Huntee6a7372015-05-28 14:04:24 -070068 log.info("from XOS: {}", result);
69
70 JsonNode node;
71 try {
72 node = MAPPER.readTree(result);
73 } catch (IOException e) {
Simon Hunt7d02c082015-05-29 12:17:09 -070074 log.error("failed to read demo subscriber JSON", e);
Simon Huntee6a7372015-05-28 14:04:24 -070075 return 0;
76 }
77
Simon Hunt7d02c082015-05-29 12:17:09 -070078 ObjectNode obj = (ObjectNode) node;
Simon Huntb1246412015-06-01 13:37:26 -070079 int demoId = obj.get("id").asInt();
Simon Hunt7d02c082015-05-29 12:17:09 -070080 log.info("Using DEMO subscriber ID {}.", demoId);
81
82 String uri = String.format(URI_SUBSCRIBER, demoId);
83 xosUtils = new XosManagerRestUtils(TEST_XOS_SERVER_ADDRESS,
84 TEST_XOS_SERVER_PORT, uri);
85 return demoId;
86 }
87
88 /**
89 * Returns the array of users for the subscriber.
90 *
91 * @return list of users
92 */
93 public ArrayNode getUserList() {
94 log.info("getUserList() called");
95 String result = xosUtils.getRest("users/");
96
97 JsonNode node;
98 try {
99 node = MAPPER.readTree(result);
100 } catch (IOException e) {
101 log.error("failed to read user list JSON", e);
102 return null;
Simon Huntee6a7372015-05-28 14:04:24 -0700103 }
104
Simon Hunt7d02c082015-05-29 12:17:09 -0700105 ObjectNode obj = (ObjectNode) node;
106 return (ArrayNode) obj.get("users");
Simon Huntee6a7372015-05-28 14:04:24 -0700107 }
108
Simon Hunt8483e9d2015-05-26 18:22:07 -0700109
Simon Hunta00b0ce2015-05-22 15:57:11 -0700110 /**
111 * Configure XOS to enable the functions that compose the given bundle,
112 * and disable all the others, for the given subscriber.
113 *
Simon Hunta00b0ce2015-05-22 15:57:11 -0700114 * @param bundle new bundle to set
115 */
Simon Hunt7d02c082015-05-29 12:17:09 -0700116 public void setNewBundle(Bundle bundle) {
Simon Huntb1246412015-06-01 13:37:26 -0700117 log.info(">> Set New Bundle : {}", bundle.descriptor().id());
Simon Hunta00b0ce2015-05-22 15:57:11 -0700118
Simon Hunta00b0ce2015-05-22 15:57:11 -0700119 Set<XosFunctionDescriptor> inBundle = bundle.descriptor().functions();
120 for (XosFunctionDescriptor xfd: XosFunctionDescriptor.values()) {
Simon Hunt90dc8c52015-05-27 16:56:03 -0700121 // only process the functions that have a real back-end on XOS
122 if (xfd.backend()) {
Simon Huntb1246412015-06-01 13:37:26 -0700123 String uri = String.format(BUNDLE_URI_FORMAT, xfd.id(),
124 inBundle.contains(xfd));
125 log.info("XOS-URI: {}", uri);
Simon Hunt90dc8c52015-05-27 16:56:03 -0700126 String result = xosUtils.putRest(uri);
127 // TODO: convert JSON result to object and check (if we care)
128 }
Simon Hunta00b0ce2015-05-22 15:57:11 -0700129 }
130 }
131
132 /**
133 * Configure XOS with new setting for given user and function, for the
134 * given subscriber account.
135 *
Simon Hunta00b0ce2015-05-22 15:57:11 -0700136 * @param func specific XOS function
137 * @param user user (containing function state)
138 */
Simon Hunt7d02c082015-05-29 12:17:09 -0700139 public void apply(XosFunction func, SubscriberUser user) {
Simon Huntb1246412015-06-01 13:37:26 -0700140 log.info(">> Apply : {} for {}", func, user);
Simon Hunta00b0ce2015-05-22 15:57:11 -0700141
Simon Hunt7d02c082015-05-29 12:17:09 -0700142 String uriPrefix = "users/" + user.id() + "/";
Simon Hunt8483e9d2015-05-26 18:22:07 -0700143 String uri = uriPrefix + func.xosUrlApply(user);
Simon Huntb1246412015-06-01 13:37:26 -0700144 log.info("XOS-URI: {}", uri);
Simon Hunt8483e9d2015-05-26 18:22:07 -0700145 String result = xosUtils.putRest(uri);
146 // TODO: convert JSON result to object and check (if we care)
Simon Hunta00b0ce2015-05-22 15:57:11 -0700147 }
148
149
150 // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
151
Simon Hunta00b0ce2015-05-22 15:57:11 -0700152 /**
153 * Singleton instance.
154 */
155 public static final XosManager INSTANCE = new XosManager();
Simon Hunt2dd48e62015-05-21 08:50:08 -0700156}