blob: a2e2f2c34990d997585cd65c760bcb460e8a846a [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 /**
32 * Denotes the URL filtering levels available.
33 */
34 public enum Level { PG, PG_13, R }
35
36 /**
37 * The default URL filtering level
38 */
39 public static final Level DEFAULT_LEVEL = Level.PG;
40
41 public UrlFilterFunction(XosFunctionDescriptor xfd) {
42 super(xfd);
43 }
44
45 @Override
Simon Hunt87b157c2015-05-22 12:09:59 -070046 public void applyParam(SubscriberUser user, String param, String value) {
47 Memento memo = user.getMemento(descriptor());
48 checkNotNull(memo, "missing memento for " + descriptor());
49 UrlFilterMemento ufMemo = (UrlFilterMemento) memo;
50
51 if (LEVEL.equals(param)) {
52 Level newLevel = Level.valueOf(value.toUpperCase());
53 ufMemo.setLevel(newLevel);
54 }
55 }
56
57 @Override
Simon Hunt6c2555b2015-05-21 18:17:56 -070058 public Memento createMemento() {
59 return new UrlFilterMemento();
60 }
61
62 class UrlFilterMemento implements Memento {
63 private Level level = DEFAULT_LEVEL;
64
65 public ObjectNode toObjectNode() {
66 ObjectNode node = MAPPER.createObjectNode();
67 node.put(LEVEL, level.name());
68 return node;
69 }
70
71 public void setLevel(Level level) {
72 this.level = level;
73 }
Simon Hunta00b0ce2015-05-22 15:57:11 -070074
75 public String level() {
76 return level.toString();
77 }
78 }
79
80 @Override
81 public String xosUrlApply(SubscriberUser user) {
82 XosFunctionDescriptor xfd = XosFunctionDescriptor.URL_FILTER;
83 UrlFilterMemento memo = (UrlFilterMemento) user.getMemento(xfd);
84 return xfd.id() + "/" + memo.level();
Simon Hunt6c2555b2015-05-21 18:17:56 -070085 }
86}