aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/onos/apps/demo/cord-gui/src/main/java/org/onosproject/cord/gui/XosManager.java
blob: de0ae67d01cf99a5887d9b423aa5ce531c9306e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
 * Copyright 2015 Open Networking Laboratory
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package org.onosproject.cord.gui;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.onosproject.cord.gui.model.Bundle;
import org.onosproject.cord.gui.model.SubscriberUser;
import org.onosproject.cord.gui.model.XosFunction;
import org.onosproject.cord.gui.model.XosFunctionDescriptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.Set;

/**
 * Encapsulation of interactions with XOS.
 */
public class XosManager {

    private static final ObjectMapper MAPPER = new ObjectMapper();

    private static final String HEAD_NODE_IP = "headnodeip";
    private static final String HEAD_NODE_PORT = "headnodeport";
    private static final int PORT_MIN = 1025;
    private static final int PORT_MAX = 65535;

    private static final String TEST_XOS_SERVER_IP = "10.254.1.22";
    private static final String TEST_XOS_SERVER_PORT_STR = "8000";
    private static final int TEST_XOS_SERVER_PORT = 8000;
    private static final String URI_RS = "/rs/";
    private static final String URI_SUBSCRIBER = "/rs/subscriber/%d/";
    private static final String BUNDLE_URI_FORMAT = "services/%s/%s/";


    private String xosServerIp;
    private int xosServerPort;
    private XosManagerRestUtils xosUtilsRs;
    private XosManagerRestUtils xosUtils;


    private final Logger log = LoggerFactory.getLogger(getClass());

    /**
     * No instantiation (except via unit test).
     */
    XosManager() {}

    private String getXosServerIp() {
        return System.getProperty(HEAD_NODE_IP, TEST_XOS_SERVER_IP);
    }

    private int getXosServerPort() {
        String p = System.getProperty(HEAD_NODE_PORT, TEST_XOS_SERVER_PORT_STR);
        int port;
        try {
            port = Integer.valueOf(p);
        } catch (NumberFormatException e) {
            port = TEST_XOS_SERVER_PORT;
            log.warn("Could not parse port number [{}], using {}", p, port);
        }
        if (port < PORT_MIN || port > PORT_MAX) {
            log.warn("Bad port number [{}], using {}", port, TEST_XOS_SERVER_PORT);
            port = TEST_XOS_SERVER_PORT;
        }
        return port;
    }

    /**
     * Queries XOS for the Subscriber ID lookup data, and returns it.
     */
    public ObjectNode initXosSubscriberLookups() {
        log.info("intDemoSubscriberLookups() called");
        xosServerIp = getXosServerIp();
        xosServerPort = getXosServerPort();
        log.info("Using XOS server at {}:{}", xosServerIp, xosServerPort);

        xosUtilsRs = new XosManagerRestUtils(xosServerIp, xosServerPort, URI_RS);

        // ask XOS for the subscriber ID lookup info
        String result = xosUtilsRs.getRest("subidlookup/");
        log.info("lookup data from XOS: {}", result);

        JsonNode node;
        try {
            node = MAPPER.readTree(result);
        } catch (IOException e) {
            log.error("failed to read subscriber lookup JSON data", e);
            return null;
        }
        return (ObjectNode) node;
    }

    /**
     * Sets a new XOS utils object to bind URL patterns for the
     * given XOS subscriber ID.
     *
     * @param xosSubId XOS subscriber ID
     */
    public void setXosUtilsForSubscriber(int xosSubId) {
        String uri = String.format(URI_SUBSCRIBER, xosSubId);
        xosUtils = new XosManagerRestUtils(xosServerIp, xosServerPort, uri);
    }


    public void initDemoSubscriber() {
        log.info("initDemoSubscriber() called");
        String result = xosUtilsRs.getRest("initdemo/");
        log.info("initdemo data from XOS: {}", result);
    }

    /**
     * Returns the array of users for the subscriber.
     *
     * @return list of users
     */
    public ArrayNode getUserList() {
        log.info("getUserList() called");
        String result = xosUtils.getRest("users/");

        JsonNode node;
        try {
            node = MAPPER.readTree(result);
        } catch (IOException e) {
            log.error("failed to read user list JSON", e);
            return null;
        }

        ObjectNode obj = (ObjectNode) node;
        return (ArrayNode) obj.get("users");
    }


    /**
     * Configure XOS to enable the functions that compose the given bundle,
     * and disable all the others, for the given subscriber.
     *
     * @param bundle new bundle to set
     */
    public void setNewBundle(Bundle bundle) {
        log.info(">> Set New Bundle : {}", bundle.descriptor().id());

        Set<XosFunctionDescriptor> inBundle = bundle.descriptor().functions();
        for (XosFunctionDescriptor xfd: XosFunctionDescriptor.values()) {
            // only process the functions that have a real back-end on XOS
            if (xfd.backend()) {
                String uri = String.format(BUNDLE_URI_FORMAT, xfd.id(),
                                           inBundle.contains(xfd));
                log.info("XOS-URI: {}", uri);
                String result = xosUtils.putRest(uri);
                // TODO: convert JSON result to object and check (if we care)
            }
        }
    }

    /**
     * Configure XOS with new setting for given user and function, for the
     * given subscriber account.
     *
     * @param func specific XOS function
     * @param user user (containing function state)
     */
    public void apply(XosFunction func, SubscriberUser user) {
        log.info(">> Apply : {} for {}", func, user);

        String uriPrefix = "users/" + user.id() + "/";
        String uri = uriPrefix + func.xosUrlApply(user);
        log.info("XOS-URI: {}", uri);
        String result = xosUtils.putRest(uri);
        // TODO: convert JSON result to object and check (if we care)
    }


    // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

    /**
     * Singleton instance.
     */
    public static final XosManager INSTANCE = new XosManager();
}