blob: c4c9d91c9575c45513d8c0a83e9041a39afa14ed [file] [log] [blame]
Simon Huntf844f632015-05-20 19:06:35 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Simon Huntf844f632015-05-20 19:06:35 -07003 *
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.model;
19
Simon Hunt41b943e2015-05-21 13:52:01 -070020import com.fasterxml.jackson.databind.node.ArrayNode;
21import com.fasterxml.jackson.databind.node.ObjectNode;
Simon Huntf844f632015-05-20 19:06:35 -070022import com.google.common.collect.ImmutableList;
23
24import java.util.List;
25
26/**
Simon Hunt41b943e2015-05-21 13:52:01 -070027 * Utility factory for creating and/or operating on bundles.
Simon Huntf844f632015-05-20 19:06:35 -070028 */
Simon Hunt41b943e2015-05-21 13:52:01 -070029public class BundleFactory extends JsonFactory {
30
31 private static final String BUNDLE = "bundle";
32 private static final String BUNDLES = "bundles";
33 private static final String FUNCTIONS = "functions";
Simon Huntf844f632015-05-20 19:06:35 -070034
35 private static final String BASIC_ID = "basic";
36 private static final String BASIC_DISPLAY_NAME = "Basic Bundle";
Simon Hunt09a32db2015-05-21 15:00:42 -070037 private static final String BASIC_DESCRIPTION =
Bri Prebilic Cole8b66a852015-06-10 17:15:10 -070038 "If the thing that matters most to you is high speed Internet" +
39 " connectivity delivered at a great price, then the basic" +
40 " bundle is right for you.\n" +
41 "Starting at $30 a month for 12 months.";
Simon Huntf844f632015-05-20 19:06:35 -070042
43 private static final String FAMILY_ID = "family";
44 private static final String FAMILY_DISPLAY_NAME = "Family Bundle";
Simon Hunt09a32db2015-05-21 15:00:42 -070045 private static final String FAMILY_DESCRIPTION =
Bri Prebilic Cole8b66a852015-06-10 17:15:10 -070046 "Enjoy great entertainment, peace of mind and big savings when " +
47 "you bundle high speed Internet and Firewall with" +
48 " Parental Control.\n" +
49 "Starting at $40 a month for 12 months.";
Simon Hunt09a32db2015-05-21 15:00:42 -070050
Simon Huntf844f632015-05-20 19:06:35 -070051
52 // no instantiation
53 private BundleFactory() {}
54
Simon Hunt41b943e2015-05-21 13:52:01 -070055 /**
56 * Designates the BASIC bundle.
57 */
58 public static final BundleDescriptor BASIC_BUNDLE =
Simon Huntf844f632015-05-20 19:06:35 -070059 new DefaultBundleDescriptor(BASIC_ID, BASIC_DISPLAY_NAME,
Simon Hunt09a32db2015-05-21 15:00:42 -070060 BASIC_DESCRIPTION,
Simon Huntf844f632015-05-20 19:06:35 -070061 XosFunctionDescriptor.INTERNET,
Simon Hunt30abc9a2015-06-09 17:13:51 -070062 XosFunctionDescriptor.FIREWALL,
63 XosFunctionDescriptor.CDN);
Simon Huntf844f632015-05-20 19:06:35 -070064
Simon Hunt41b943e2015-05-21 13:52:01 -070065 /**
66 * Designates the FAMILY bundle.
67 */
68 public static final BundleDescriptor FAMILY_BUNDLE =
Simon Huntf844f632015-05-20 19:06:35 -070069 new DefaultBundleDescriptor(FAMILY_ID, FAMILY_DISPLAY_NAME,
Simon Hunt09a32db2015-05-21 15:00:42 -070070 FAMILY_DESCRIPTION,
Simon Huntf844f632015-05-20 19:06:35 -070071 XosFunctionDescriptor.INTERNET,
72 XosFunctionDescriptor.FIREWALL,
Simon Hunt30abc9a2015-06-09 17:13:51 -070073 XosFunctionDescriptor.CDN,
Simon Hunta29c87b2015-05-21 09:56:19 -070074 XosFunctionDescriptor.URL_FILTER);
Simon Huntf844f632015-05-20 19:06:35 -070075
Simon Hunt41b943e2015-05-21 13:52:01 -070076 // all bundles, in the order they should be listed in the GUI
77 private static final List<BundleDescriptor> ALL_BUNDLES = ImmutableList.of(
78 BASIC_BUNDLE,
79 FAMILY_BUNDLE
80 );
81
Simon Huntf844f632015-05-20 19:06:35 -070082 /**
83 * Returns the list of available bundles.
84 *
85 * @return available bundles
86 */
87 public static List<BundleDescriptor> availableBundles() {
Simon Hunt41b943e2015-05-21 13:52:01 -070088 return ALL_BUNDLES;
89 }
90
91 /**
92 * Returns the bundle descriptor for the given identifier.
93 *
94 * @param bundleId bundle identifier
95 * @return bundle descriptor
96 * @throws IllegalArgumentException if bundle ID is unknown
97 */
98 public static BundleDescriptor bundleFromId(String bundleId) {
99 for (BundleDescriptor bd : ALL_BUNDLES) {
100 if (bd.id().equals(bundleId)) {
101 return bd;
102 }
103 }
104 throw new IllegalArgumentException("unknown bundle: " + bundleId);
105 }
106
107 /**
Simon Huntee6a7372015-05-28 14:04:24 -0700108 * Returns an object node representation of the given bundle.
Simon Hunt30abc9a2015-06-09 17:13:51 -0700109 * Note that some functions (such as CDN) are not added to the output
110 * as we don't want them to appear in the GUI.
Simon Hunt41b943e2015-05-21 13:52:01 -0700111 *
112 * @param bundle the bundle
Simon Huntee6a7372015-05-28 14:04:24 -0700113 * @return object node
Simon Hunt41b943e2015-05-21 13:52:01 -0700114 */
Simon Huntee6a7372015-05-28 14:04:24 -0700115 public static ObjectNode toObjectNode(Bundle bundle) {
Simon Hunt41b943e2015-05-21 13:52:01 -0700116 ObjectNode root = objectNode();
Simon Hunt09a32db2015-05-21 15:00:42 -0700117 BundleDescriptor descriptor = bundle.descriptor();
Simon Hunt41b943e2015-05-21 13:52:01 -0700118
119 ObjectNode bnode = objectNode()
Simon Hunt09a32db2015-05-21 15:00:42 -0700120 .put(ID, descriptor.id())
121 .put(NAME, descriptor.displayName())
122 .put(DESC, descriptor.description());
Simon Hunt41b943e2015-05-21 13:52:01 -0700123
124 ArrayNode funcs = arrayNode();
125 for (XosFunctionDescriptor xfd: bundle.descriptor().functions()) {
Simon Hunt30abc9a2015-06-09 17:13:51 -0700126 if (xfd.visible()) {
127 funcs.add(XosFunctionFactory.toObjectNode(xfd));
128 }
Simon Hunt41b943e2015-05-21 13:52:01 -0700129 }
130 bnode.set(FUNCTIONS, funcs);
131 root.set(BUNDLE, bnode);
132
133 ArrayNode bundles = arrayNode();
134 for (BundleDescriptor bd: BundleFactory.availableBundles()) {
135 ObjectNode bdnode = objectNode()
136 .put(ID, bd.id())
Simon Hunt09a32db2015-05-21 15:00:42 -0700137 .put(NAME, bd.displayName())
138 .put(DESC, bd.description());
Simon Hunt41b943e2015-05-21 13:52:01 -0700139 bundles.add(bdnode);
140 }
141 root.set(BUNDLES, bundles);
Simon Huntee6a7372015-05-28 14:04:24 -0700142 return root;
Simon Huntf844f632015-05-20 19:06:35 -0700143 }
144}