2 * Copyright (c) 2015 Huawei, Inc. and others. All rights reserved.
\r
4 * This program and the accompanying materials are made available under the
\r
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
\r
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
\r
8 package org.opendaylight.nemo.user.advancedquery;
\r
10 import java.util.List;
\r
12 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
\r
13 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
\r
14 import org.opendaylight.nemo.user.tenantmanager.AAA;
\r
15 import org.opendaylight.nemo.user.tenantmanager.TenantManage;
\r
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.common.rev151010.UserId;
\r
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.AdvancedNemoQueryInput;
\r
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.Users;
\r
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.advanced.nemo.query.input.QueryCondition;
\r
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.Objects;
\r
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.Operations;
\r
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.Results;
\r
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.objects.Connection;
\r
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.objects.Flow;
\r
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.objects.Node;
\r
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.operations.Operation;
\r
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.users.User;
\r
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.users.UserKey;
\r
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.flow.instance.MatchItem;
\r
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.flow.instance.Property;
\r
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.property.instance.property.values.IntValue;
\r
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
\r
33 import org.slf4j.Logger;
\r
34 import org.slf4j.LoggerFactory;
\r
36 import com.google.common.base.Optional;
\r
37 import com.google.common.util.concurrent.FutureCallback;
\r
38 import com.google.common.util.concurrent.Futures;
\r
39 import com.google.common.util.concurrent.ListenableFuture;
\r
42 * Created by z00293636 on 2015/8/29.
\r
44 public class AdvancedQuery
\r
46 private DataBroker dataBroker;
\r
47 private TenantManage tenantManage;
\r
48 private QueryDefinitionCheck queryDefinitionCheck;
\r
49 private Objects objects;
\r
50 private Operations operations;
\r
51 private List<Results> resultsList;
\r
52 private static final Logger LOG = LoggerFactory.getLogger(QueryDefinitionCheck.class);
\r
54 public AdvancedQuery(DataBroker dataBroker, TenantManage tenantManage)
\r
56 this.dataBroker = dataBroker;
\r
57 this.tenantManage = tenantManage;
\r
58 queryDefinitionCheck = new QueryDefinitionCheck(dataBroker);
\r
64 public String advancedQuery(AAA aaa, AdvancedNemoQueryInput input)
\r
66 String errorInfo = null;
\r
67 List<QueryCondition> queryConditionList = null;
\r
69 errorInfo = aaa.checkUser(input);
\r
71 if (errorInfo != null)
\r
77 if (input.getQueryCondition() != null)
\r
79 queryConditionList = input.getQueryCondition();
\r
80 for (QueryCondition queryCondition : queryConditionList)
\r
82 errorInfo = queryDefinitionCheck.CheckQueryDefinition(queryCondition);
\r
83 if (errorInfo != null)
\r
93 public String getAdvancedQueryReuslt(AdvancedNemoQueryInput advancedNemoQueryInput)
\r
95 UserId userId = advancedNemoQueryInput.getUserId();
\r
96 List<QueryCondition> queryConditionList = advancedNemoQueryInput.getQueryCondition();
\r
97 String queryResult = null;
\r
99 for (QueryCondition queryCondition : queryConditionList)
\r
101 if (queryCondition.getQueryIntentType() != null)
\r
103 if (queryCondition.getQueryIntentType().getIntValue() == 0)
\r
105 queryResult += nodeInstanceQuery(userId, queryCondition);
\r
107 if (queryCondition.getQueryIntentType().getIntValue() == 1)
\r
109 queryResult += connectionInstanceQuery(userId,queryCondition);
\r
111 if (queryCondition.getQueryIntentType().getIntValue() == 2)
\r
113 queryResult += flowInstanceQuery(userId,queryCondition);
\r
115 if (queryCondition.getQueryIntentType().getIntValue() == 3)
\r
117 queryResult += operationsInstanceQuery(userId,queryCondition);
\r
122 return queryResult;
\r
125 private String nodeInstanceQuery(UserId userId, QueryCondition queryCondition)
\r
127 String queryResult = null;
\r
129 fetchObjectsInstance(userId);
\r
131 if (objects != null)
\r
133 if (objects.getNode() != null)
\r
135 List<Node> nodeList = objects.getNode();
\r
137 for (Node node : nodeList)
\r
139 if (node.getProperty() != null)
\r
141 for(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.node.instance.Property property : node.getProperty())
\r
143 if (property.getPropertyName().equals(queryCondition.getQueryConditionName()))
\r
145 int operator = queryCondition.getQueryConditionMatchPattern().getIntValue();
\r
146 List<IntValue> values =property.getPropertyValues().getIntValue();
\r
147 if (operator == 0 && (property.getPropertyValues().getIntValue() != null))
\r
149 for(IntValue value:values)
\r
151 if (value.getValue()<queryCondition.getQueryConditionTargetValue().getIntValue())
\r
153 queryResult += node.getNodeId().toString()+node.getNodeName().toString()+node.getNodeType().toString()+node.getSubNode().toString()+node.getProperty().toString();
\r
157 if (operator == 1 && (property.getPropertyValues().getIntValue() != null))
\r
159 for(IntValue value:values)
\r
161 if (value.getValue()<=queryCondition.getQueryConditionTargetValue().getIntValue())
\r
163 queryResult += node.getNodeId().toString()+node.getNodeName().toString()+node.getNodeType().toString()+node.getSubNode().toString()+node.getProperty().toString();
\r
167 if (operator == 2 && (property.getPropertyValues().getIntValue() != null))
\r
169 for(IntValue value:values)
\r
171 if (value.getValue()==queryCondition.getQueryConditionTargetValue().getIntValue())
\r
173 queryResult += node.getNodeId().toString()+node.getNodeName().toString()+node.getNodeType().toString()+node.getSubNode().toString()+node.getProperty().toString();
\r
177 if (operator == 3 && (property.getPropertyValues().getIntValue() != null))
\r
179 for(IntValue value:values)
\r
181 if (value.getValue()!=queryCondition.getQueryConditionTargetValue().getIntValue())
\r
183 queryResult += node.getNodeId().toString()+node.getNodeName().toString()+node.getNodeType().toString()+node.getSubNode().toString()+node.getProperty().toString();
\r
187 if (operator == 4 && (property.getPropertyValues().getIntValue() != null))
\r
189 for(IntValue value:values)
\r
191 if (value.getValue()>queryCondition.getQueryConditionTargetValue().getIntValue())
\r
193 queryResult += node.getNodeId().toString()+node.getNodeName().toString()+node.getNodeType().toString()+node.getSubNode().toString()+node.getProperty().toString();
\r
197 if (operator == 5 && (property.getPropertyValues().getIntValue() != null))
\r
199 for(IntValue value:values)
\r
201 if (value.getValue()>=queryCondition.getQueryConditionTargetValue().getIntValue())
\r
203 queryResult += node.getNodeId().toString()+node.getNodeName().toString()+node.getNodeType().toString()+node.getSubNode().toString()+node.getProperty().toString();
\r
207 if (operator == 6 && (property.getPropertyValues().getIntValue() != null))
\r
220 return queryResult;
\r
223 private String connectionInstanceQuery(UserId userId, QueryCondition queryCondition)
\r
225 String queryResult = null;
\r
227 fetchObjectsInstance(userId);
\r
228 if (objects != null)
\r
230 if (objects.getConnection() != null)
\r
232 List<Connection> connectionList = objects.getConnection();
\r
234 for (Connection connection : connectionList)
\r
236 if (connection.getProperty() != null)
\r
238 for (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.connection.instance.Property property : connection.getProperty())
\r
240 if (property.getPropertyName().equals(queryCondition.getQueryConditionName()))
\r
242 int operator = queryCondition.getQueryConditionMatchPattern().getIntValue();
\r
243 List<IntValue> values =property.getPropertyValues().getIntValue();
\r
244 if (operator == 0 && (property.getPropertyValues().getIntValue() != null))
\r
246 for(IntValue value:values)
\r
248 if (value.getValue()<queryCondition.getQueryConditionTargetValue().getIntValue())
\r
250 queryResult += connection.getConnectionId().toString()+ connection.getConnectionName().toString()+connection.getConnectionType().toString()+connection.getEndNode().toString()+connection.getProperty().toString();
\r
254 if (operator == 1 && (property.getPropertyValues().getIntValue() != null))
\r
256 for(IntValue value:values)
\r
258 if (value.getValue()<=queryCondition.getQueryConditionTargetValue().getIntValue())
\r
260 queryResult += connection.getConnectionId().toString()+ connection.getConnectionName().toString()+connection.getConnectionType().toString()+connection.getEndNode().toString()+connection.getProperty().toString();
\r
264 if (operator == 2 && (property.getPropertyValues().getIntValue() != null))
\r
266 for(IntValue value:values)
\r
268 if (value.getValue()==queryCondition.getQueryConditionTargetValue().getIntValue())
\r
270 queryResult += connection.getConnectionId().toString()+ connection.getConnectionName().toString()+connection.getConnectionType().toString()+connection.getEndNode().toString()+connection.getProperty().toString();
\r
274 if (operator == 3 && (property.getPropertyValues().getIntValue() != null))
\r
276 for(IntValue value:values)
\r
278 if (value.getValue()!=queryCondition.getQueryConditionTargetValue().getIntValue())
\r
280 queryResult += connection.getConnectionId().toString()+ connection.getConnectionName().toString()+connection.getConnectionType().toString()+connection.getEndNode().toString()+connection.getProperty().toString();
\r
284 if (operator == 4 && (property.getPropertyValues().getIntValue() != null))
\r
286 for(IntValue value:values)
\r
288 if (value.getValue()>queryCondition.getQueryConditionTargetValue().getIntValue())
\r
290 queryResult += connection.getConnectionId().toString()+ connection.getConnectionName().toString()+connection.getConnectionType().toString()+connection.getEndNode().toString()+connection.getProperty().toString();
\r
294 if (operator == 5 && (property.getPropertyValues().getIntValue() != null))
\r
296 for(IntValue value:values)
\r
298 if (value.getValue()>=queryCondition.getQueryConditionTargetValue().getIntValue())
\r
300 queryResult += connection.getConnectionId().toString()+ connection.getConnectionName().toString()+connection.getConnectionType().toString()+connection.getEndNode().toString()+connection.getProperty().toString();
\r
304 if (operator == 6 && (property.getPropertyValues().getIntValue() != null))
\r
316 return queryResult;
\r
319 private String flowInstanceQuery(UserId userId, QueryCondition queryCondition)
\r
321 String queryResult = null;
\r
323 fetchObjectsInstance(userId);
\r
324 if (objects != null)
\r
326 if (objects.getFlow() != null)
\r
328 List<Flow> flowList = objects.getFlow();
\r
330 for (Flow flow: flowList)
\r
332 if (flow.getProperty() != null)
\r
334 for (Property property : flow.getProperty())
\r
336 if (property.getPropertyName().equals(queryCondition.getQueryConditionName()))
\r
338 int operator = queryCondition.getQueryConditionMatchPattern().getIntValue();
\r
339 List<IntValue> values = property.getPropertyValues().getIntValue();
\r
340 if (operator == 0 && (property.getPropertyValues().getIntValue() != null))
\r
342 for(IntValue value:values)
\r
344 if (value.getValue()<queryCondition.getQueryConditionTargetValue().getIntValue())
\r
346 queryResult += flow.getFlowId().toString() + flow.getFlowName().toString() + flow.getMatchItem().toString() + flow.getProperty().toString();
\r
350 if (operator == 1 && (property.getPropertyValues().getIntValue() != null))
\r
352 for(IntValue value:values)
\r
354 if (value.getValue()<=queryCondition.getQueryConditionTargetValue().getIntValue())
\r
356 queryResult += flow.getFlowId().toString() + flow.getFlowName().toString() + flow.getMatchItem().toString() + flow.getProperty().toString();
\r
360 if (operator == 2 && (property.getPropertyValues().getIntValue() != null))
\r
362 for(IntValue value:values)
\r
364 if (value.getValue()==queryCondition.getQueryConditionTargetValue().getIntValue())
\r
366 queryResult += flow.getFlowId().toString() + flow.getFlowName().toString() + flow.getMatchItem().toString() + flow.getProperty().toString();
\r
370 if (operator == 3 && (property.getPropertyValues().getIntValue() != null))
\r
372 for(IntValue value:values)
\r
374 if (value.getValue()!=queryCondition.getQueryConditionTargetValue().getIntValue())
\r
376 queryResult += flow.getFlowId().toString() + flow.getFlowName().toString() + flow.getMatchItem().toString() + flow.getProperty().toString();
\r
380 if (operator == 4 && (property.getPropertyValues().getIntValue() != null))
\r
382 for(IntValue value:values)
\r
384 if (value.getValue()>queryCondition.getQueryConditionTargetValue().getIntValue())
\r
386 queryResult += flow.getFlowId().toString() + flow.getFlowName().toString() + flow.getMatchItem().toString() + flow.getProperty().toString();
\r
390 if (operator == 5 && (property.getPropertyValues().getIntValue() != null))
\r
392 for(IntValue value:values)
\r
394 if (value.getValue()>=queryCondition.getQueryConditionTargetValue().getIntValue())
\r
396 queryResult += flow.getFlowId().toString() + flow.getFlowName().toString() + flow.getMatchItem().toString() + flow.getProperty().toString();
\r
400 if (operator == 6 && (property.getPropertyValues().getIntValue() != null))
\r
407 if (flow.getMatchItem() != null)
\r
409 for(MatchItem matchItem: flow.getMatchItem())
\r
411 if (matchItem.getMatchItemName().equals(queryCondition.getQueryConditionName()))
\r
413 int operator = queryCondition.getQueryConditionMatchPattern().getIntValue();
\r
414 if (operator == 0 && matchItem.getMatchItemValue().getIntValue()<queryCondition.getQueryConditionTargetValue().getIntValue())
\r
416 queryResult += flow.getFlowId().toString() + flow.getFlowName().toString() + flow.getMatchItem().toString() + flow.getProperty().toString();
\r
418 if (operator == 1 && matchItem.getMatchItemValue().getIntValue()<=queryCondition.getQueryConditionTargetValue().getIntValue())
\r
420 queryResult += flow.getFlowId().toString() + flow.getFlowName().toString() + flow.getMatchItem().toString() + flow.getProperty().toString();
\r
422 if (operator == 2 && matchItem.getMatchItemValue().getIntValue()==queryCondition.getQueryConditionTargetValue().getIntValue())
\r
424 queryResult += flow.getFlowId().toString() + flow.getFlowName().toString() + flow.getMatchItem().toString() + flow.getProperty().toString();
\r
426 if (operator == 3 && matchItem.getMatchItemValue().getIntValue()!=queryCondition.getQueryConditionTargetValue().getIntValue())
\r
428 queryResult += flow.getFlowId().toString() + flow.getFlowName().toString() + flow.getMatchItem().toString() + flow.getProperty().toString();
\r
430 if (operator == 4 && matchItem.getMatchItemValue().getIntValue()>queryCondition.getQueryConditionTargetValue().getIntValue())
\r
432 queryResult += flow.getFlowId().toString() + flow.getFlowName().toString() + flow.getMatchItem().toString() + flow.getProperty().toString();
\r
434 if (operator == 5 && matchItem.getMatchItemValue().getIntValue()>=queryCondition.getQueryConditionTargetValue().getIntValue())
\r
436 queryResult += flow.getFlowId().toString() + flow.getFlowName().toString() + flow.getMatchItem().toString() + flow.getProperty().toString();
\r
438 if (operator == 6 )
\r
449 return queryResult;
\r
452 private String operationsInstanceQuery(UserId userId, QueryCondition queryCondition)
\r
454 String queryResult = null;
\r
455 fetchOperationsInstance(userId);
\r
456 List<Operation> operationList = operations.getOperation();
\r
460 return queryResult;
\r
464 private void fetchObjectsInstance(UserId userId)
\r
466 UserKey userKey = new UserKey(userId);
\r
467 InstanceIdentifier<Objects> objectsId = InstanceIdentifier.builder(Users.class).child(User.class, userKey).child(Objects.class).build();
\r
468 ListenableFuture<Optional<Objects>> objectsFuture = dataBroker.newReadOnlyTransaction().read(LogicalDatastoreType.CONFIGURATION, objectsId);
\r
469 Futures.addCallback(objectsFuture, new FutureCallback<Optional<Objects>>() {
\r
471 public void onSuccess(Optional<Objects> result)
\r
473 setObjects(result.get());
\r
478 public void onFailure(Throwable t)
\r
480 LOG.error("Can not read objects instances.", t);
\r
487 private void fetchOperationsInstance(UserId userId)
\r
489 UserKey userKey = new UserKey(userId);
\r
490 InstanceIdentifier<Operations> operationsId = InstanceIdentifier.builder(Users.class).child(User.class, userKey).child(Operations.class).build();
\r
491 ListenableFuture<Optional<Operations>> operationsFuture = dataBroker.newReadOnlyTransaction().read(LogicalDatastoreType.CONFIGURATION, operationsId);
\r
492 Futures.addCallback(operationsFuture, new FutureCallback<Optional<Operations>>() {
\r
494 public void onSuccess(Optional<Operations> result)
\r
496 setOperations(result.get());
\r
501 public void onFailure(Throwable t)
\r
503 LOG.error("Can not read operations instances.", t);
\r
510 private void setObjects(Objects objects)
\r
512 this.objects = objects;
\r
514 private void setOperations(Operations operations)
\r
516 this.operations = operations;
\r