blob: 0c06b10439f861ada2fee5329fe648f82d1ba641 [file] [log] [blame]
Simon Hunt6c2555b2015-05-21 18:17:56 -07001/*
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
18package org.onosproject.cord.gui.model;
19
20import com.fasterxml.jackson.databind.node.ObjectNode;
21
Simon Hunt87b157c2015-05-22 12:09:59 -070022import static com.google.common.base.Preconditions.checkNotNull;
23
Simon Hunt6c2555b2015-05-21 18:17:56 -070024/**
25 * Specialization of XosFunction for URL filtering.
26 */
27public class UrlFilterFunction extends DefaultXosFunction {
28
29 private static final String LEVEL = "level";
30
31 /**
Simon Huntab5232e2015-05-26 16:59:46 -070032 * Denotes the URL filtering levels available. From most restrictive
33 * to least restrictive. Note: <em>OFF</em> denies everything;
34 * <em>NONE</em> allows everything.
Simon Hunt6c2555b2015-05-21 18:17:56 -070035 */
Simon Huntab5232e2015-05-26 16:59:46 -070036 public enum Level { OFF, G, PG, PG_13, R, NONE }
Simon Hunt6c2555b2015-05-21 18:17:56 -070037
38 /**
39 * The default URL filtering level
40 */
Simon Huntab5232e2015-05-26 16:59:46 -070041 public static final Level DEFAULT_LEVEL = Level.G;
Simon Hunt6c2555b2015-05-21 18:17:56 -070042
Simon Huntab5232e2015-05-26 16:59:46 -070043 public UrlFilterFunction() {
44 super(XosFunctionDescriptor.URL_FILTER);
Simon Hunt6c2555b2015-05-21 18:17:56 -070045 }
46
47 @Override
Simon Hunt87b157c2015-05-22 12:09:59 -070048 public void applyParam(SubscriberUser user, String param, String value) {
49 Memento memo = user.getMemento(descriptor());
50 checkNotNull(memo, "missing memento for " + descriptor());
51 UrlFilterMemento ufMemo = (UrlFilterMemento) memo;
52
53 if (LEVEL.equals(param)) {
54 Level newLevel = Level.valueOf(value.toUpperCase());
55 ufMemo.setLevel(newLevel);
Simon Hunt7d02c082015-05-29 12:17:09 -070056
57 // Also store the (string version) of the level
58 // (not in the memento). Hackish, but that's how it is for now.
59 user.setUrlFilterLevel(value);
Simon Hunt87b157c2015-05-22 12:09:59 -070060 }
61 }
62
63 @Override
Simon Hunt6c2555b2015-05-21 18:17:56 -070064 public Memento createMemento() {
65 return new UrlFilterMemento();
66 }
67
68 class UrlFilterMemento implements Memento {
69 private Level level = DEFAULT_LEVEL;
70
71 public ObjectNode toObjectNode() {
72 ObjectNode node = MAPPER.createObjectNode();
73 node.put(LEVEL, level.name());
74 return node;
75 }
76
77 public void setLevel(Level level) {
78 this.level = level;
79 }
Simon Hunta00b0ce2015-05-22 15:57:11 -070080
81 public String level() {
82 return level.toString();
83 }
84 }
85
86 @Override
87 public String xosUrlApply(SubscriberUser user) {
88 XosFunctionDescriptor xfd = XosFunctionDescriptor.URL_FILTER;
89 UrlFilterMemento memo = (UrlFilterMemento) user.getMemento(xfd);
90 return xfd.id() + "/" + memo.level();
Simon Hunt6c2555b2015-05-21 18:17:56 -070091 }
92}