Simon Hunt | f844f63 | 2015-05-20 19:06:35 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 18 | package org.onosproject.cord.gui.model; |
| 19 | |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 20 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Simon Hunt | f844f63 | 2015-05-20 19:06:35 -0700 | [diff] [blame] | 22 | import com.google.common.collect.ImmutableList; |
| 23 | |
| 24 | import java.util.List; |
| 25 | |
| 26 | /** |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 27 | * Utility factory for creating and/or operating on bundles. |
Simon Hunt | f844f63 | 2015-05-20 19:06:35 -0700 | [diff] [blame] | 28 | */ |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 29 | public 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 Hunt | f844f63 | 2015-05-20 19:06:35 -0700 | [diff] [blame] | 34 | |
| 35 | private static final String BASIC_ID = "basic"; |
| 36 | private static final String BASIC_DISPLAY_NAME = "Basic Bundle"; |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 37 | private static final String BASIC_DESCRIPTION = |
Bri Prebilic Cole | 8b66a85 | 2015-06-10 17:15:10 -0700 | [diff] [blame] | 38 | "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 Hunt | f844f63 | 2015-05-20 19:06:35 -0700 | [diff] [blame] | 42 | |
| 43 | private static final String FAMILY_ID = "family"; |
| 44 | private static final String FAMILY_DISPLAY_NAME = "Family Bundle"; |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 45 | private static final String FAMILY_DESCRIPTION = |
Bri Prebilic Cole | 8b66a85 | 2015-06-10 17:15:10 -0700 | [diff] [blame] | 46 | "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 Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 50 | |
Simon Hunt | f844f63 | 2015-05-20 19:06:35 -0700 | [diff] [blame] | 51 | |
| 52 | // no instantiation |
| 53 | private BundleFactory() {} |
| 54 | |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 55 | /** |
| 56 | * Designates the BASIC bundle. |
| 57 | */ |
| 58 | public static final BundleDescriptor BASIC_BUNDLE = |
Simon Hunt | f844f63 | 2015-05-20 19:06:35 -0700 | [diff] [blame] | 59 | new DefaultBundleDescriptor(BASIC_ID, BASIC_DISPLAY_NAME, |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 60 | BASIC_DESCRIPTION, |
Simon Hunt | f844f63 | 2015-05-20 19:06:35 -0700 | [diff] [blame] | 61 | XosFunctionDescriptor.INTERNET, |
Simon Hunt | 30abc9a | 2015-06-09 17:13:51 -0700 | [diff] [blame] | 62 | XosFunctionDescriptor.FIREWALL, |
| 63 | XosFunctionDescriptor.CDN); |
Simon Hunt | f844f63 | 2015-05-20 19:06:35 -0700 | [diff] [blame] | 64 | |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 65 | /** |
| 66 | * Designates the FAMILY bundle. |
| 67 | */ |
| 68 | public static final BundleDescriptor FAMILY_BUNDLE = |
Simon Hunt | f844f63 | 2015-05-20 19:06:35 -0700 | [diff] [blame] | 69 | new DefaultBundleDescriptor(FAMILY_ID, FAMILY_DISPLAY_NAME, |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 70 | FAMILY_DESCRIPTION, |
Simon Hunt | f844f63 | 2015-05-20 19:06:35 -0700 | [diff] [blame] | 71 | XosFunctionDescriptor.INTERNET, |
| 72 | XosFunctionDescriptor.FIREWALL, |
Simon Hunt | 30abc9a | 2015-06-09 17:13:51 -0700 | [diff] [blame] | 73 | XosFunctionDescriptor.CDN, |
Simon Hunt | a29c87b | 2015-05-21 09:56:19 -0700 | [diff] [blame] | 74 | XosFunctionDescriptor.URL_FILTER); |
Simon Hunt | f844f63 | 2015-05-20 19:06:35 -0700 | [diff] [blame] | 75 | |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 76 | // 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 Hunt | f844f63 | 2015-05-20 19:06:35 -0700 | [diff] [blame] | 82 | /** |
| 83 | * Returns the list of available bundles. |
| 84 | * |
| 85 | * @return available bundles |
| 86 | */ |
| 87 | public static List<BundleDescriptor> availableBundles() { |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 88 | 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 Hunt | ee6a737 | 2015-05-28 14:04:24 -0700 | [diff] [blame] | 108 | * Returns an object node representation of the given bundle. |
Simon Hunt | 30abc9a | 2015-06-09 17:13:51 -0700 | [diff] [blame] | 109 | * 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 Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 111 | * |
| 112 | * @param bundle the bundle |
Simon Hunt | ee6a737 | 2015-05-28 14:04:24 -0700 | [diff] [blame] | 113 | * @return object node |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 114 | */ |
Simon Hunt | ee6a737 | 2015-05-28 14:04:24 -0700 | [diff] [blame] | 115 | public static ObjectNode toObjectNode(Bundle bundle) { |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 116 | ObjectNode root = objectNode(); |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 117 | BundleDescriptor descriptor = bundle.descriptor(); |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 118 | |
| 119 | ObjectNode bnode = objectNode() |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 120 | .put(ID, descriptor.id()) |
| 121 | .put(NAME, descriptor.displayName()) |
| 122 | .put(DESC, descriptor.description()); |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 123 | |
| 124 | ArrayNode funcs = arrayNode(); |
| 125 | for (XosFunctionDescriptor xfd: bundle.descriptor().functions()) { |
Simon Hunt | 30abc9a | 2015-06-09 17:13:51 -0700 | [diff] [blame] | 126 | if (xfd.visible()) { |
| 127 | funcs.add(XosFunctionFactory.toObjectNode(xfd)); |
| 128 | } |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 129 | } |
| 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 Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 137 | .put(NAME, bd.displayName()) |
| 138 | .put(DESC, bd.description()); |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 139 | bundles.add(bdnode); |
| 140 | } |
| 141 | root.set(BUNDLES, bundles); |
Simon Hunt | ee6a737 | 2015-05-28 14:04:24 -0700 | [diff] [blame] | 142 | return root; |
Simon Hunt | f844f63 | 2015-05-20 19:06:35 -0700 | [diff] [blame] | 143 | } |
| 144 | } |