summaryrefslogtreecommitdiffstats
path: root/framework/src/onos/apps/demo/cord-gui/src/main/java/org/onosproject/cord/gui/model/XosFunctionFactory.java
blob: dffabceb41ea75798fa8b72f23524e8e7e2389b1 (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
/*
 * 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.model;

import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import java.util.HashMap;
import java.util.Map;

import static org.onosproject.cord.gui.model.XosFunctionDescriptor.URL_FILTER;

/**
 * Utility factory for operating on XOS functions.
 */
public class XosFunctionFactory extends JsonFactory {

    private static final String PARAMS = "params";
    private static final String LEVEL = "level";
    private static final String LEVELS = "levels";


    // no instantiation
    private XosFunctionFactory() {}

    /**
     * Produces the JSON representation of the given XOS function descriptor.
     *
     * @param xfd function descriptor
     * @return JSON encoding
     */
    public static ObjectNode toObjectNode(XosFunctionDescriptor xfd) {
        ObjectNode root = objectNode()
                .put(ID, xfd.id())
                .put(NAME, xfd.displayName())
                .put(DESC, xfd.description());
        root.set(PARAMS, paramsForXfd(xfd));
        return root;
    }

    private static ObjectNode paramsForXfd(XosFunctionDescriptor xfd) {
        ParamsFactory psf = PARAM_MAP.get(xfd);
        if (psf == null) {
            psf = DEF_PARAMS_FACTORY;
        }
        return psf.params();
    }


    // ==== handling different parameter structures...
    private static final Map<XosFunctionDescriptor, ParamsFactory>
        PARAM_MAP = new HashMap<XosFunctionDescriptor, ParamsFactory>();

    private static final ParamsFactory DEF_PARAMS_FACTORY = new ParamsFactory();
    static {
        PARAM_MAP.put(URL_FILTER, new UrlFilterParamsFactory());
    }

    /**
     * Creates an object node representation of the profile for the
     * specified user.
     *
     * @param user the user
     * @return object node profile
     */
    public static ObjectNode profileForUser(SubscriberUser user) {
        ObjectNode root = objectNode();
        for (XosFunctionDescriptor xfd: XosFunctionDescriptor.values()) {
            XosFunction.Memento mem = user.getMemento(xfd);
            if (mem != null) {
                root.set(xfd.id(), mem.toObjectNode());
            }
        }
        return root;
    }


    // ===================================================================
    // === factories for creating parameter structures, both default
    //     and from a memento...

    // private parameter structure creator
    static class ParamsFactory {
        ObjectNode params() {
            return objectNode();
        }
    }

    static class UrlFilterParamsFactory extends ParamsFactory {
        @Override
        ObjectNode params() {
            ObjectNode result = objectNode();
            result.put(LEVEL, UrlFilterFunction.DEFAULT_LEVEL.name());
            ArrayNode levels = arrayNode();
            for (UrlFilterFunction.Level lvl: UrlFilterFunction.Level.values()) {
                levels.add(lvl.name());
            }
            result.set(LEVELS, levels);
            return result;
        }
    }
}