cec780c9ecafc0f4486299b7f24cc056aaa43f70
[controller.git] / opendaylight / commons / filter-valve / src / main / java / org / opendaylight / controller / filtervalve / cors / jaxb / Context.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.controller.filtervalve.cors.jaxb;
10
11 import static com.google.common.base.Preconditions.checkArgument;
12 import static com.google.common.base.Preconditions.checkState;
13 import static java.lang.String.format;
14
15 import com.google.common.base.Optional;
16 import java.util.ArrayList;
17 import java.util.Collections;
18 import java.util.HashMap;
19 import java.util.LinkedHashMap;
20 import java.util.List;
21 import java.util.Map;
22 import javax.xml.bind.annotation.XmlAttribute;
23 import javax.xml.bind.annotation.XmlElement;
24 import javax.xml.bind.annotation.XmlRootElement;
25 import org.opendaylight.controller.filtervalve.cors.model.UrlMatcher;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 @XmlRootElement
30 public class Context {
31     private static final Logger logger = LoggerFactory.getLogger(Context.class);
32
33     private String path;
34     private List<Filter> filters = new ArrayList<>();
35     private List<FilterMapping> filterMappings = new ArrayList<>();
36     private boolean initialized;
37     private UrlMatcher<Filter> urlMatcher;
38
39
40     public synchronized void initialize(String fileName, Map<String, Filter> namesToTemplates) {
41         checkState(initialized == false, "Already initialized");
42         Map<String, Filter> namesToFilters = new HashMap<>();
43         for (Filter filter : filters) {
44             try {
45                 filter.initialize(fileName, Optional.fromNullable(namesToTemplates.get(filter.getFilterName())));
46             } catch (Exception e) {
47                 throw new IllegalStateException(format("Error while processing filter %s of context %s, defined in %s",
48                         filter.getFilterName(), path, fileName), e);
49             }
50             namesToFilters.put(filter.getFilterName(), filter);
51         }
52         filters = Collections.unmodifiableList(new ArrayList<>(filters));
53         LinkedHashMap<String, Filter> patternMap = new LinkedHashMap<>();
54         for (FilterMapping filterMapping : filterMappings) {
55             filterMapping.initialize();
56             Filter found = namesToFilters.get(filterMapping.getFilterName());
57             if (found != null) {
58                 patternMap.put(filterMapping.getUrlPattern(), found);
59             } else {
60                 logger.error("Cannot find matching filter for filter-mapping {} of context {}, defined in {}",
61                         filterMapping.getFilterName(), path, fileName);
62                 throw new IllegalStateException(format(
63                         "Cannot find filter for filter-mapping %s of context %s, defined in %s",
64                         filterMapping.getFilterName(), path, fileName));
65             }
66         }
67         filterMappings = Collections.unmodifiableList(new ArrayList<>(filterMappings));
68         urlMatcher = new UrlMatcher<>(patternMap);
69         initialized = true;
70     }
71
72     public List<Filter> findMatchingFilters(String path) {
73         logger.trace("findMatchingFilters({})", path);
74         checkState(initialized, "Not initialized");
75         return urlMatcher.findMatchingFilters(path);
76     }
77
78     @XmlAttribute(name = "path")
79     public String getPath() {
80         return path;
81     }
82
83     public void setPath(String path) {
84         checkArgument(initialized == false, "Already initialized");
85         this.path = path;
86     }
87
88     @XmlElement(name = "filter")
89     public List<Filter> getFilters() {
90         return filters;
91     }
92
93     public void setFilters(List<Filter> filters) {
94         checkArgument(initialized == false, "Already initialized");
95         this.filters = filters;
96     }
97
98     @XmlElement(name = "filter-mapping")
99     public List<FilterMapping> getFilterMappings() {
100         return filterMappings;
101     }
102
103     public void setFilterMappings(List<FilterMapping> filterMappings) {
104         checkArgument(initialized == false, "Already initialized");
105         this.filterMappings = filterMappings;
106     }
107
108     @Override
109     public String toString() {
110         return "Context{" +
111                 "path='" + path + '\'' +
112                 '}';
113     }
114 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.