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 org.opendaylight.nemo.user.tenantmanager.AAA;
\r
11 import org.opendaylight.nemo.user.tenantmanager.TenantManage;
\r
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.common.rev151010.UserId;
\r
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.AdvancedNemoQueryInput;
\r
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.Users;
\r
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.advanced.nemo.query.input.QueryCondition;
\r
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.Objects;
\r
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.Operations;
\r
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.Results;
\r
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.objects.Connection;
\r
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.objects.Flow;
\r
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.objects.Node;
\r
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.operations.Operation;
\r
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.users.User;
\r
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.users.UserKey;
\r
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.flow.instance.MatchItem;
\r
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.flow.instance.Property;
\r
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.property.instance.property.values.IntValue;
\r
28 import com.google.common.base.Optional;
\r
29 import com.google.common.util.concurrent.FutureCallback;
\r
30 import com.google.common.util.concurrent.Futures;
\r
31 import com.google.common.util.concurrent.ListenableFuture;
\r
32 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
\r
33 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
\r
34 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
\r
35 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
\r
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
\r
38 import org.slf4j.Logger;
\r
39 import org.slf4j.LoggerFactory;
\r
41 import java.util.List;
\r
44 * Created by z00293636 on 2015/8/29.
\r
46 public class AdvancedQuery
\r
48 private DataBroker dataBroker;
\r
49 private TenantManage tenantManage;
\r
50 private QueryDefinitionCheck queryDefinitionCheck;
\r
51 private Objects objects;
\r
52 private Operations operations;
\r
53 private List<Results> resultsList;
\r
54 private static final Logger LOG = LoggerFactory.getLogger(QueryDefinitionCheck.class);
\r
56 public AdvancedQuery(DataBroker dataBroker, TenantManage tenantManage)
\r
58 this.dataBroker = dataBroker;
\r
59 this.tenantManage = tenantManage;
\r
60 queryDefinitionCheck = new QueryDefinitionCheck(dataBroker);
\r
66 public String advancedQuery(AAA aaa, AdvancedNemoQueryInput input)
\r
68 String errorInfo = null;
\r
69 List<QueryCondition> queryConditionList = null;
\r
71 errorInfo = aaa.CheckUser(input.getUserId(),input.getUserName(),input.getUserPassword(),input.getUserRole());
\r
73 if (errorInfo != null)
\r
79 if (input.getQueryCondition() != null)
\r
81 queryConditionList = input.getQueryCondition();
\r
82 for (QueryCondition queryCondition : queryConditionList)
\r
84 errorInfo = queryDefinitionCheck.CheckQueryDefinition(queryCondition);
\r
85 if (errorInfo != null)
\r
95 public String getAdvancedQueryReuslt(AdvancedNemoQueryInput advancedNemoQueryInput)
\r
97 UserId userId = advancedNemoQueryInput.getUserId();
\r
98 List<QueryCondition> queryConditionList = advancedNemoQueryInput.getQueryCondition();
\r
99 String queryResult = null;
\r
101 for (QueryCondition queryCondition : queryConditionList)
\r
103 if (queryCondition.getQueryIntentType() != null)
\r
105 if (queryCondition.getQueryIntentType().getIntValue() == 0)
\r
107 queryResult += nodeInstanceQuery(userId, queryCondition);
\r
109 if (queryCondition.getQueryIntentType().getIntValue() == 1)
\r
111 queryResult += connectionInstanceQuery(userId,queryCondition);
\r
113 if (queryCondition.getQueryIntentType().getIntValue() == 2)
\r
115 queryResult += flowInstanceQuery(userId,queryCondition);
\r
117 if (queryCondition.getQueryIntentType().getIntValue() == 3)
\r
119 queryResult += operationsInstanceQuery(userId,queryCondition);
\r
124 return queryResult;
\r
127 private String nodeInstanceQuery(UserId userId, QueryCondition queryCondition)
\r
129 String queryResult = null;
\r
131 fetchObjectsInstance(userId);
\r
133 if (objects != null)
\r
135 if (objects.getNode() != null)
\r
137 List<Node> nodeList = objects.getNode();
\r
139 for (Node node : nodeList)
\r
141 if (node.getProperty() != null)
\r
143 for(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.node.instance.Property property : node.getProperty())
\r
145 if (property.getPropertyName().equals(queryCondition.getQueryConditionName()))
\r
147 int operator = queryCondition.getQueryConditionMatchPattern().getIntValue();
\r
148 List<IntValue> values =property.getPropertyValues().getIntValue();
\r
149 if (operator == 0 && (property.getPropertyValues().getIntValue() != null))
\r
151 for(IntValue value:values)
\r
153 if (value.getValue()<queryCondition.getQueryConditionTargetValue().getIntValue())
\r
155 queryResult += node.getNodeId().toString()+node.getNodeName().toString()+node.getNodeType().toString()+node.getSubNode().toString()+node.getProperty().toString();
\r
159 if (operator == 1 && (property.getPropertyValues().getIntValue() != null))
\r
161 for(IntValue value:values)
\r
163 if (value.getValue()<=queryCondition.getQueryConditionTargetValue().getIntValue())
\r
165 queryResult += node.getNodeId().toString()+node.getNodeName().toString()+node.getNodeType().toString()+node.getSubNode().toString()+node.getProperty().toString();
\r
169 if (operator == 2 && (property.getPropertyValues().getIntValue() != null))
\r
171 for(IntValue value:values)
\r
173 if (value.getValue()==queryCondition.getQueryConditionTargetValue().getIntValue())
\r
175 queryResult += node.getNodeId().toString()+node.getNodeName().toString()+node.getNodeType().toString()+node.getSubNode().toString()+node.getProperty().toString();
\r
179 if (operator == 3 && (property.getPropertyValues().getIntValue() != null))
\r
181 for(IntValue value:values)
\r
183 if (value.getValue()!=queryCondition.getQueryConditionTargetValue().getIntValue())
\r
185 queryResult += node.getNodeId().toString()+node.getNodeName().toString()+node.getNodeType().toString()+node.getSubNode().toString()+node.getProperty().toString();
\r
189 if (operator == 4 && (property.getPropertyValues().getIntValue() != null))
\r
191 for(IntValue value:values)
\r
193 if (value.getValue()>queryCondition.getQueryConditionTargetValue().getIntValue())
\r
195 queryResult += node.getNodeId().toString()+node.getNodeName().toString()+node.getNodeType().toString()+node.getSubNode().toString()+node.getProperty().toString();
\r
199 if (operator == 5 && (property.getPropertyValues().getIntValue() != null))
\r
201 for(IntValue value:values)
\r
203 if (value.getValue()>=queryCondition.getQueryConditionTargetValue().getIntValue())
\r
205 queryResult += node.getNodeId().toString()+node.getNodeName().toString()+node.getNodeType().toString()+node.getSubNode().toString()+node.getProperty().toString();
\r
209 if (operator == 6 && (property.getPropertyValues().getIntValue() != null))
\r
222 return queryResult;
\r
225 private String connectionInstanceQuery(UserId userId, QueryCondition queryCondition)
\r
227 String queryResult = null;
\r
229 fetchObjectsInstance(userId);
\r
230 if (objects != null)
\r
232 if (objects.getConnection() != null)
\r
234 List<Connection> connectionList = objects.getConnection();
\r
236 for (Connection connection : connectionList)
\r
238 if (connection.getProperty() != null)
\r
240 for (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.connection.instance.Property property : connection.getProperty())
\r
242 if (property.getPropertyName().equals(queryCondition.getQueryConditionName()))
\r
244 int operator = queryCondition.getQueryConditionMatchPattern().getIntValue();
\r
245 List<IntValue> values =property.getPropertyValues().getIntValue();
\r
246 if (operator == 0 && (property.getPropertyValues().getIntValue() != null))
\r
248 for(IntValue value:values)
\r
250 if (value.getValue()<queryCondition.getQueryConditionTargetValue().getIntValue())
\r
252 queryResult += connection.getConnectionId().toString()+ connection.getConnectionName().toString()+connection.getConnectionType().toString()+connection.getEndNode().toString()+connection.getProperty().toString();
\r
256 if (operator == 1 && (property.getPropertyValues().getIntValue() != null))
\r
258 for(IntValue value:values)
\r
260 if (value.getValue()<=queryCondition.getQueryConditionTargetValue().getIntValue())
\r
262 queryResult += connection.getConnectionId().toString()+ connection.getConnectionName().toString()+connection.getConnectionType().toString()+connection.getEndNode().toString()+connection.getProperty().toString();
\r
266 if (operator == 2 && (property.getPropertyValues().getIntValue() != null))
\r
268 for(IntValue value:values)
\r
270 if (value.getValue()==queryCondition.getQueryConditionTargetValue().getIntValue())
\r
272 queryResult += connection.getConnectionId().toString()+ connection.getConnectionName().toString()+connection.getConnectionType().toString()+connection.getEndNode().toString()+connection.getProperty().toString();
\r
276 if (operator == 3 && (property.getPropertyValues().getIntValue() != null))
\r
278 for(IntValue value:values)
\r
280 if (value.getValue()!=queryCondition.getQueryConditionTargetValue().getIntValue())
\r
282 queryResult += connection.getConnectionId().toString()+ connection.getConnectionName().toString()+connection.getConnectionType().toString()+connection.getEndNode().toString()+connection.getProperty().toString();
\r
286 if (operator == 4 && (property.getPropertyValues().getIntValue() != null))
\r
288 for(IntValue value:values)
\r
290 if (value.getValue()>queryCondition.getQueryConditionTargetValue().getIntValue())
\r
292 queryResult += connection.getConnectionId().toString()+ connection.getConnectionName().toString()+connection.getConnectionType().toString()+connection.getEndNode().toString()+connection.getProperty().toString();
\r
296 if (operator == 5 && (property.getPropertyValues().getIntValue() != null))
\r
298 for(IntValue value:values)
\r
300 if (value.getValue()>=queryCondition.getQueryConditionTargetValue().getIntValue())
\r
302 queryResult += connection.getConnectionId().toString()+ connection.getConnectionName().toString()+connection.getConnectionType().toString()+connection.getEndNode().toString()+connection.getProperty().toString();
\r
306 if (operator == 6 && (property.getPropertyValues().getIntValue() != null))
\r
318 return queryResult;
\r
321 private String flowInstanceQuery(UserId userId, QueryCondition queryCondition)
\r
323 String queryResult = null;
\r
325 fetchObjectsInstance(userId);
\r
326 if (objects != null)
\r
328 if (objects.getFlow() != null)
\r
330 List<Flow> flowList = objects.getFlow();
\r
332 for (Flow flow: flowList)
\r
334 if (flow.getProperty() != null)
\r
336 for (Property property : flow.getProperty())
\r
338 if (property.getPropertyName().equals(queryCondition.getQueryConditionName()))
\r
340 int operator = queryCondition.getQueryConditionMatchPattern().getIntValue();
\r
341 List<IntValue> values = property.getPropertyValues().getIntValue();
\r
342 if (operator == 0 && (property.getPropertyValues().getIntValue() != null))
\r
344 for(IntValue value:values)
\r
346 if (value.getValue()<queryCondition.getQueryConditionTargetValue().getIntValue())
\r
348 queryResult += flow.getFlowId().toString() + flow.getFlowName().toString() + flow.getMatchItem().toString() + flow.getProperty().toString();
\r
352 if (operator == 1 && (property.getPropertyValues().getIntValue() != null))
\r
354 for(IntValue value:values)
\r
356 if (value.getValue()<=queryCondition.getQueryConditionTargetValue().getIntValue())
\r
358 queryResult += flow.getFlowId().toString() + flow.getFlowName().toString() + flow.getMatchItem().toString() + flow.getProperty().toString();
\r
362 if (operator == 2 && (property.getPropertyValues().getIntValue() != null))
\r
364 for(IntValue value:values)
\r
366 if (value.getValue()==queryCondition.getQueryConditionTargetValue().getIntValue())
\r
368 queryResult += flow.getFlowId().toString() + flow.getFlowName().toString() + flow.getMatchItem().toString() + flow.getProperty().toString();
\r
372 if (operator == 3 && (property.getPropertyValues().getIntValue() != null))
\r
374 for(IntValue value:values)
\r
376 if (value.getValue()!=queryCondition.getQueryConditionTargetValue().getIntValue())
\r
378 queryResult += flow.getFlowId().toString() + flow.getFlowName().toString() + flow.getMatchItem().toString() + flow.getProperty().toString();
\r
382 if (operator == 4 && (property.getPropertyValues().getIntValue() != null))
\r
384 for(IntValue value:values)
\r
386 if (value.getValue()>queryCondition.getQueryConditionTargetValue().getIntValue())
\r
388 queryResult += flow.getFlowId().toString() + flow.getFlowName().toString() + flow.getMatchItem().toString() + flow.getProperty().toString();
\r
392 if (operator == 5 && (property.getPropertyValues().getIntValue() != null))
\r
394 for(IntValue value:values)
\r
396 if (value.getValue()>=queryCondition.getQueryConditionTargetValue().getIntValue())
\r
398 queryResult += flow.getFlowId().toString() + flow.getFlowName().toString() + flow.getMatchItem().toString() + flow.getProperty().toString();
\r
402 if (operator == 6 && (property.getPropertyValues().getIntValue() != null))
\r
409 if (flow.getMatchItem() != null)
\r
411 for(MatchItem matchItem: flow.getMatchItem())
\r
413 if (matchItem.getMatchItemName().equals(queryCondition.getQueryConditionName()))
\r
415 int operator = queryCondition.getQueryConditionMatchPattern().getIntValue();
\r
416 if (operator == 0 && matchItem.getMatchItemValue().getIntValue()<queryCondition.getQueryConditionTargetValue().getIntValue())
\r
418 queryResult += flow.getFlowId().toString() + flow.getFlowName().toString() + flow.getMatchItem().toString() + flow.getProperty().toString();
\r
420 if (operator == 1 && matchItem.getMatchItemValue().getIntValue()<=queryCondition.getQueryConditionTargetValue().getIntValue())
\r
422 queryResult += flow.getFlowId().toString() + flow.getFlowName().toString() + flow.getMatchItem().toString() + flow.getProperty().toString();
\r
424 if (operator == 2 && matchItem.getMatchItemValue().getIntValue()==queryCondition.getQueryConditionTargetValue().getIntValue())
\r
426 queryResult += flow.getFlowId().toString() + flow.getFlowName().toString() + flow.getMatchItem().toString() + flow.getProperty().toString();
\r
428 if (operator == 3 && matchItem.getMatchItemValue().getIntValue()!=queryCondition.getQueryConditionTargetValue().getIntValue())
\r
430 queryResult += flow.getFlowId().toString() + flow.getFlowName().toString() + flow.getMatchItem().toString() + flow.getProperty().toString();
\r
432 if (operator == 4 && matchItem.getMatchItemValue().getIntValue()>queryCondition.getQueryConditionTargetValue().getIntValue())
\r
434 queryResult += flow.getFlowId().toString() + flow.getFlowName().toString() + flow.getMatchItem().toString() + flow.getProperty().toString();
\r
436 if (operator == 5 && matchItem.getMatchItemValue().getIntValue()>=queryCondition.getQueryConditionTargetValue().getIntValue())
\r
438 queryResult += flow.getFlowId().toString() + flow.getFlowName().toString() + flow.getMatchItem().toString() + flow.getProperty().toString();
\r
440 if (operator == 6 )
\r
451 return queryResult;
\r
454 private String operationsInstanceQuery(UserId userId, QueryCondition queryCondition)
\r
456 String queryResult = null;
\r
457 fetchOperationsInstance(userId);
\r
458 List<Operation> operationList = operations.getOperation();
\r
462 return queryResult;
\r
466 private void fetchObjectsInstance(UserId userId)
\r
468 UserKey userKey = new UserKey(userId);
\r
469 InstanceIdentifier<Objects> objectsId = InstanceIdentifier.builder(Users.class).child(User.class, userKey).child(Objects.class).build();
\r
470 ListenableFuture<Optional<Objects>> objectsFuture = dataBroker.newReadOnlyTransaction().read(LogicalDatastoreType.CONFIGURATION, objectsId);
\r
471 Futures.addCallback(objectsFuture, new FutureCallback<Optional<Objects>>() {
\r
473 public void onSuccess(Optional<Objects> result)
\r
475 setObjects(result.get());
\r
480 public void onFailure(Throwable t)
\r
482 LOG.error("Can not read objects instances.", t);
\r
489 private void fetchOperationsInstance(UserId userId)
\r
491 UserKey userKey = new UserKey(userId);
\r
492 InstanceIdentifier<Operations> operationsId = InstanceIdentifier.builder(Users.class).child(User.class, userKey).child(Operations.class).build();
\r
493 ListenableFuture<Optional<Operations>> operationsFuture = dataBroker.newReadOnlyTransaction().read(LogicalDatastoreType.CONFIGURATION, operationsId);
\r
494 Futures.addCallback(operationsFuture, new FutureCallback<Optional<Operations>>() {
\r
496 public void onSuccess(Optional<Operations> result)
\r
498 setOperations(result.get());
\r
503 public void onFailure(Throwable t)
\r
505 LOG.error("Can not read operations instances.", t);
\r
512 private void setObjects(Objects objects)
\r
514 this.objects = objects;
\r
516 private void setOperations(Operations operations)
\r
518 this.operations = operations;
\r