METRON-1503 Alerts are not getting populated in alerts UI when search engine is Solr...
[metron.git] / metron-platform / metron-solr / src / main / java / org / apache / metron / solr / dao / SolrDao.java
1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 package org.apache.metron.solr.dao;
19
20 import java.io.IOException;
21 import java.lang.invoke.MethodHandles;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Optional;
25 import org.apache.metron.indexing.dao.AccessConfig;
26 import org.apache.metron.indexing.dao.ColumnMetadataDao;
27 import org.apache.metron.indexing.dao.IndexDao;
28 import org.apache.metron.indexing.dao.search.FieldType;
29 import org.apache.metron.indexing.dao.search.GetRequest;
30 import org.apache.metron.indexing.dao.search.GroupRequest;
31 import org.apache.metron.indexing.dao.search.GroupResponse;
32 import org.apache.metron.indexing.dao.search.InvalidSearchException;
33 import org.apache.metron.indexing.dao.search.SearchRequest;
34 import org.apache.metron.indexing.dao.search.SearchResponse;
35 import org.apache.metron.indexing.dao.update.Document;
36 import org.apache.solr.client.solrj.SolrClient;
37 import org.apache.solr.client.solrj.impl.CloudSolrClient;
38 import org.apache.solr.client.solrj.impl.HttpClientUtil;
39 import org.apache.solr.client.solrj.impl.Krb5HttpClientConfigurer;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 public class SolrDao implements IndexDao {
44
45 private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
46 public static final String ROOT_FIELD = "_root_";
47 public static final String VERSION_FIELD = "_version_";
48
49 private transient SolrClient client;
50 private SolrSearchDao solrSearchDao;
51 private SolrUpdateDao solrUpdateDao;
52 private ColumnMetadataDao solrColumnMetadataDao;
53
54 private AccessConfig accessConfig;
55
56 protected SolrDao(SolrClient client,
57 AccessConfig config,
58 SolrSearchDao solrSearchDao,
59 SolrUpdateDao solrUpdateDao,
60 SolrColumnMetadataDao solrColumnMetadataDao) {
61 this.client = client;
62 this.accessConfig = config;
63 this.solrSearchDao = solrSearchDao;
64 this.solrUpdateDao = solrUpdateDao;
65 this.solrColumnMetadataDao = solrColumnMetadataDao;
66 }
67
68 public SolrDao() {
69 //uninitialized.
70 }
71
72 @Override
73 public void init(AccessConfig config) {
74 if (config.getKerberosEnabled()) {
75 enableKerberos();
76 }
77 if (this.client == null) {
78 Map<String, Object> globalConfig = config.getGlobalConfigSupplier().get();
79 String zkHost = (String) globalConfig.get("solr.zookeeper");
80 this.client = getSolrClient(zkHost);
81 this.accessConfig = config;
82 this.solrSearchDao = new SolrSearchDao(this.client, this.accessConfig);
83 this.solrUpdateDao = new SolrUpdateDao(this.client);
84 this.solrColumnMetadataDao = new SolrColumnMetadataDao(zkHost);
85 }
86 }
87
88 @Override
89 public SearchResponse search(SearchRequest searchRequest) throws InvalidSearchException {
90 return this.solrSearchDao.search(searchRequest);
91 }
92
93 @Override
94 public GroupResponse group(GroupRequest groupRequest) throws InvalidSearchException {
95 return this.solrSearchDao.group(groupRequest);
96 }
97
98 @Override
99 public Document getLatest(String guid, String collection) throws IOException {
100 return this.solrSearchDao.getLatest(guid, collection);
101 }
102
103 @Override
104 public Iterable<Document> getAllLatest(List<GetRequest> getRequests) throws IOException {
105 return this.solrSearchDao.getAllLatest(getRequests);
106 }
107
108 @Override
109 public void update(Document update, Optional<String> index) throws IOException {
110 this.solrUpdateDao.update(update, index);
111 }
112
113 @Override
114 public void batchUpdate(Map<Document, Optional<String>> updates) throws IOException {
115 this.solrUpdateDao.batchUpdate(updates);
116 }
117
118 @Override
119 public Map<String, FieldType> getColumnMetadata(List<String> indices) throws IOException {
120 return this.solrColumnMetadataDao.getColumnMetadata(indices);
121 }
122
123 protected SolrClient getSolrClient(String zkHost) {
124 return new CloudSolrClient.Builder().withZkHost(zkHost).build();
125 }
126
127 protected void enableKerberos() {
128 HttpClientUtil.addConfigurer(new Krb5HttpClientConfigurer());
129 }
130 }