summaryrefslogtreecommitdiffstats
path: root/framework/src/onos/apps/demo/cord-gui/src/main/java/org/onosproject/cord/gui/model/BundleFactory.java
blob: 813ad98b71ef6139e62b33fca86a7529eddff497 (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
/*
 * 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 com.google.common.collect.ImmutableList;

import java.util.List;

/**
 * Utility factory for creating and/or operating on bundles.
 */
public class BundleFactory extends JsonFactory {

    private static final String BUNDLE = "bundle";
    private static final String BUNDLES = "bundles";
    private static final String FUNCTIONS = "functions";

    private static final String BASIC_ID = "basic";
    private static final String BASIC_DISPLAY_NAME = "Basic Bundle";
    private static final String BASIC_DESCRIPTION =
            "If the thing that matters most to you is high speed Internet" +
                    " connectivity delivered at a great price, then the basic" +
                    " bundle is right for you.\n" +
                    "Starting at $30 a month for 12 months.";

    private static final String FAMILY_ID = "family";
    private static final String FAMILY_DISPLAY_NAME = "Family Bundle";
    private static final String FAMILY_DESCRIPTION =
            "Enjoy great entertainment, peace of mind and big savings when " +
                    "you bundle high speed Internet and Firewall with" +
                    " Parental Control.\n" +
                    "Starting at $40 a month for 12 months.";


    // no instantiation
    private BundleFactory() {}

    /**
     * Designates the BASIC bundle.
     */
    public static final BundleDescriptor BASIC_BUNDLE =
            new DefaultBundleDescriptor(BASIC_ID, BASIC_DISPLAY_NAME,
                                        BASIC_DESCRIPTION,
                                        XosFunctionDescriptor.INTERNET,
                                        XosFunctionDescriptor.FIREWALL,
                                        XosFunctionDescriptor.CDN);

    /**
     * Designates the FAMILY bundle.
     */
    public static final BundleDescriptor FAMILY_BUNDLE =
            new DefaultBundleDescriptor(FAMILY_ID, FAMILY_DISPLAY_NAME,
                                        FAMILY_DESCRIPTION,
                                        XosFunctionDescriptor.INTERNET,
                                        XosFunctionDescriptor.FIREWALL,
                                        XosFunctionDescriptor.CDN,
                                        XosFunctionDescriptor.URL_FILTER);

    // all bundles, in the order they should be listed in the GUI
    private static final List<BundleDescriptor> ALL_BUNDLES = ImmutableList.of(
            BASIC_BUNDLE,
            FAMILY_BUNDLE
    );

    /**
     * Returns the list of available bundles.
     *
     * @return available bundles
     */
    public static List<BundleDescriptor> availableBundles() {
        return ALL_BUNDLES;
    }

    /**
     * Returns the bundle descriptor for the given identifier.
     *
     * @param bundleId bundle identifier
     * @return bundle descriptor
     * @throws IllegalArgumentException if bundle ID is unknown
     */
    public static BundleDescriptor bundleFromId(String bundleId) {
        for (BundleDescriptor bd : ALL_BUNDLES) {
            if (bd.id().equals(bundleId)) {
                return bd;
            }
        }
        throw new IllegalArgumentException("unknown bundle: " + bundleId);
    }

    /**
     * Returns an object node representation of the given bundle.
     * Note that some functions (such as CDN) are not added to the output
     * as we don't want them to appear in the GUI.
     *
     * @param bundle the bundle
     * @return object node
     */
    public static ObjectNode toObjectNode(Bundle bundle) {
        ObjectNode root = objectNode();
        BundleDescriptor descriptor = bundle.descriptor();

        ObjectNode bnode = objectNode()
                .put(ID, descriptor.id())
                .put(NAME, descriptor.displayName())
                .put(DESC, descriptor.description());

        ArrayNode funcs = arrayNode();
        for (XosFunctionDescriptor xfd: bundle.descriptor().functions()) {
            if (xfd.visible()) {
                funcs.add(XosFunctionFactory.toObjectNode(xfd));
            }
        }
        bnode.set(FUNCTIONS, funcs);
        root.set(BUNDLE, bnode);

        ArrayNode bundles = arrayNode();
        for (BundleDescriptor bd: BundleFactory.availableBundles()) {
            ObjectNode bdnode = objectNode()
                    .put(ID, bd.id())
                    .put(NAME, bd.displayName())
                    .put(DESC, bd.description());
            bundles.add(bdnode);
        }
        root.set(BUNDLES, bundles);
        return root;
    }
}