/
NewtsUtils.java
182 lines (154 loc) · 6.7 KB
/
NewtsUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*******************************************************************************
* This file is part of OpenNMS(R).
*
* Copyright (C) 2006-2015 The OpenNMS Group, Inc.
* OpenNMS(R) is Copyright (C) 1999-2015 The OpenNMS Group, Inc.
*
* OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
*
* OpenNMS(R) is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* OpenNMS(R) is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with OpenNMS(R). If not, see:
* http://www.gnu.org/licenses/
*
* For more information contact:
* OpenNMS(R) Licensing <license@opennms.org>
* http://www.opennms.org/
* http://www.opennms.com/
*******************************************************************************/
package org.opennms.netmgt.newts.support;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.opennms.core.sysprops.SystemProperties;
import org.opennms.netmgt.model.ResourcePath;
import org.opennms.newts.api.Context;
import org.opennms.newts.api.MetricType;
import org.opennms.newts.api.Resource;
import org.opennms.newts.api.Sample;
import org.opennms.newts.api.Timestamp;
import org.opennms.newts.api.ValueType;
import org.opennms.newts.api.search.BooleanQuery;
import org.opennms.newts.api.search.Operator;
import org.opennms.newts.api.search.Query;
import org.opennms.newts.api.search.Term;
import org.opennms.newts.api.search.TermQuery;
import org.opennms.newts.cassandra.search.CassandraIndexingOptions;
import org.opennms.newts.cassandra.search.EscapableResourceIdSplitter;
import org.opennms.newts.cassandra.search.ResourceIdSplitter;
/**
* Utility functions and constants.
*
* @author jwhite
*/
public abstract class NewtsUtils {
public static final boolean DISABLE_INDEXING = Boolean.getBoolean("org.opennms.newts.disable.indexing");
public static final int MAX_BATCH_SIZE = SystemProperties.getInteger("org.opennms.newts.config.max_batch_size", 16);
public static final int TTL = SystemProperties.getInteger("org.opennms.newts.config.ttl", 31536000);
public static final String HOSTNAME_PROPERTY = "org.opennms.newts.config.hostname";
public static final String KEYSPACE_PROPERTY = "org.opennms.newts.config.keyspace";
public static final String PORT_PROPERTY = "org.opennms.newts.config.port";
public static final String TTL_PROPERTY = "org.opennms.newts.config.ttl";
public static final String DEFAULT_HOSTNAME = "localhost";
public static final String DEFAULT_KEYSPACE = "newts";
public static final String DEFAULT_PORT = "9043";
public static final String DEFAULT_TTL = "" + 86400 * 365;
public static final CassandraIndexingOptions INDEXING_OPTIONS = new CassandraIndexingOptions.Builder()
.withHierarchicalIndexing(false)
.withIndexResourceTerms(false)
.withIndexUsingDefaultTerm(false)
.withMaxBatchSize(MAX_BATCH_SIZE)
.build();
private static final ResourceIdSplitter s_splitter = new EscapableResourceIdSplitter();
// Constants used when building mock samples in createSampleForIndexingStrings()
private static final Timestamp EPOCH = Timestamp.fromEpochMillis(0);
private static final ValueType<?> ZERO = ValueType.compose(0, MetricType.GAUGE);
/**
* Extends the attribute map with indices used by the {@link org.opennms.netmgt.dao.support.NewtsResourceStorageDao}.
*
* A resource path of the form [a, b, c, d] will be indexed with:
* <ul>
* <li> _idx1: (a, 4)
* <li> _idx2: (a:b, 4)
* <li> _idx3: (a:b:c, 4)
*/
public static void addIndicesToAttributes(ResourcePath path, Map<String, String> attributes) {
final List<String> els = Arrays.asList(path.elements());
final int N = els.size();
for (int i = 0; i < N; i++) {
final String id = s_splitter.joinElementsToId(els.subList(0, i+1));
attributes.put("_idx" + i, String.format("(%s,%d)", id, N));
}
}
/**
* Constructs a query used to find all of the resources that have
* one or more metrics at the given depth bellow the path.
*
* Requires resources to have been indexed using {@link #addIndicesToAttributes}.
*/
public static Query findResourcesWithMetricsAtDepth(ResourcePath path, int depth) {
// Numeric suffix for the index name, based on the length of parent path
int idxSuffix = path.elements().length - 1;
// The length of the resource ids we're interested in finding
int targetLen = idxSuffix + depth + 2;
TermQuery tq = new TermQuery(new Term(
"_idx"+idxSuffix, // key
String.format("(%s,%d)", toResourceId(path), targetLen) // value
));
BooleanQuery q = new BooleanQuery();
q.add(tq, Operator.OR);
return q;
}
/**
* Converts a {@link org.opennms.netmgt.model.ResourcePath} to a Newts resource id.
*
* @param path path to convert
* @return Newts resource id
*/
public static String toResourceId(ResourcePath path) {
return s_splitter.joinElementsToId(Arrays.asList(path.elements()));
}
/**
* Converts a Newts resource id to a {@link org.opennms.netmgt.model.ResourcePath}.
*
* @param resourceId Newts resource id
* @return path
*/
public static ResourcePath toResourcePath(String resourceId) {
if (resourceId == null) {
return null;
}
List<String> els = s_splitter.splitIdIntoElements(resourceId);
return ResourcePath.get(els.subList(0, els.size() - 1));
}
/**
* Extracts the metric name from the resource id.
*
* @param resourceId Newts resource id
* @return metric name
*/
public static String toMetricName(String resourceId) {
if (resourceId == null) {
return null;
}
List<String> els = s_splitter.splitIdIntoElements(resourceId);
return els.get(els.size() - 1);
}
/**
* Creates a sample used to index string attributes.
*
* These should only be index and not be persisted.
*/
public static Sample createSampleForIndexingStrings(Context context, Resource resource) {
return new Sample(EPOCH, context, resource, "strings", MetricType.GAUGE, ZERO);
}
}