Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 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 | |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | |
Simon Hunt | 87b157c | 2015-05-22 12:09:59 -0700 | [diff] [blame] | 22 | import static com.google.common.base.Preconditions.checkNotNull; |
| 23 | |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 24 | /** |
| 25 | * Specialization of XosFunction for URL filtering. |
| 26 | */ |
| 27 | public class UrlFilterFunction extends DefaultXosFunction { |
| 28 | |
| 29 | private static final String LEVEL = "level"; |
Simon Hunt | b124641 | 2015-06-01 13:37:26 -0700 | [diff] [blame] | 30 | private static final String URI_PATTERN = "%s/%s/"; |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 31 | |
| 32 | /** |
Simon Hunt | ab5232e | 2015-05-26 16:59:46 -0700 | [diff] [blame] | 33 | * Denotes the URL filtering levels available. From most restrictive |
Simon Hunt | b13c8f1 | 2015-06-04 14:09:06 -0700 | [diff] [blame] | 34 | * to least restrictive. Note: <em>NONE</em> allows nothing; |
| 35 | * <em>ALL</em> allows everything. |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 36 | */ |
Simon Hunt | b13c8f1 | 2015-06-04 14:09:06 -0700 | [diff] [blame] | 37 | public enum Level { NONE, G, PG, PG_13, R, ALL } |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * The default URL filtering level |
| 41 | */ |
Simon Hunt | ab5232e | 2015-05-26 16:59:46 -0700 | [diff] [blame] | 42 | public static final Level DEFAULT_LEVEL = Level.G; |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 43 | |
Simon Hunt | ab5232e | 2015-05-26 16:59:46 -0700 | [diff] [blame] | 44 | public UrlFilterFunction() { |
| 45 | super(XosFunctionDescriptor.URL_FILTER); |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | @Override |
Simon Hunt | 87b157c | 2015-05-22 12:09:59 -0700 | [diff] [blame] | 49 | public void applyParam(SubscriberUser user, String param, String value) { |
| 50 | Memento memo = user.getMemento(descriptor()); |
| 51 | checkNotNull(memo, "missing memento for " + descriptor()); |
| 52 | UrlFilterMemento ufMemo = (UrlFilterMemento) memo; |
| 53 | |
| 54 | if (LEVEL.equals(param)) { |
| 55 | Level newLevel = Level.valueOf(value.toUpperCase()); |
| 56 | ufMemo.setLevel(newLevel); |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 57 | |
| 58 | // Also store the (string version) of the level |
| 59 | // (not in the memento). Hackish, but that's how it is for now. |
| 60 | user.setUrlFilterLevel(value); |
Simon Hunt | 87b157c | 2015-05-22 12:09:59 -0700 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | |
| 64 | @Override |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 65 | public Memento createMemento() { |
| 66 | return new UrlFilterMemento(); |
| 67 | } |
| 68 | |
| 69 | class UrlFilterMemento implements Memento { |
| 70 | private Level level = DEFAULT_LEVEL; |
| 71 | |
| 72 | public ObjectNode toObjectNode() { |
| 73 | ObjectNode node = MAPPER.createObjectNode(); |
| 74 | node.put(LEVEL, level.name()); |
| 75 | return node; |
| 76 | } |
| 77 | |
| 78 | public void setLevel(Level level) { |
| 79 | this.level = level; |
| 80 | } |
Simon Hunt | a00b0ce | 2015-05-22 15:57:11 -0700 | [diff] [blame] | 81 | |
| 82 | public String level() { |
| 83 | return level.toString(); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public String xosUrlApply(SubscriberUser user) { |
| 89 | XosFunctionDescriptor xfd = XosFunctionDescriptor.URL_FILTER; |
| 90 | UrlFilterMemento memo = (UrlFilterMemento) user.getMemento(xfd); |
Simon Hunt | b124641 | 2015-06-01 13:37:26 -0700 | [diff] [blame] | 91 | return String.format(URI_PATTERN, xfd.id(), memo.level()); |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 92 | } |
| 93 | } |