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.vnspacemanager.syntaxcheck;
\r
10 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
\r
11 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.objects.Flow;
\r
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.FlowPropertyDefinitions;
\r
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.MatchItemDefinitions;
\r
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.flow.instance.MatchItem;
\r
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.match.item.definitions.MatchItemDefinition;
\r
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.match.item.instance.MatchItemValue;
\r
17 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
\r
18 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
\r
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
\r
20 import org.opendaylight.yangtools.yang.common.RpcResult;
\r
21 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
\r
22 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
\r
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
\r
24 import com.google.common.base.Optional;
\r
25 import com.google.common.base.Function;
\r
26 import com.google.common.util.concurrent.CheckedFuture;
\r
27 import com.google.common.util.concurrent.FutureCallback;
\r
28 import com.google.common.util.concurrent.Futures;
\r
29 import com.google.common.util.concurrent.ListenableFuture;
\r
31 import org.slf4j.Logger;
\r
32 import org.slf4j.LoggerFactory;
\r
33 import java.util.List;
\r
36 * Created by z00293636 on 2015/9/2.
\r
38 public class FlowDefinitionCheck {
\r
40 private DataBroker dataBroker;
\r
41 private List<MatchItemDefinition> matchItemDefinitionList;
\r
42 private static final Logger LOG = LoggerFactory.getLogger(FlowDefinitionCheck.class);
\r
44 public FlowDefinitionCheck(DataBroker dataBroker)
\r
46 this.dataBroker = dataBroker;
\r
47 matchItemDefinitionList = null;
\r
50 public String CheckDefinition(Flow flow)
\r
52 String errorInfo = null;
\r
53 fetchMatchItemDefinitions();
\r
54 Boolean matchHasDefined = false;
\r
55 if (flow.getMatchItem() != null)
\r
57 if (matchItemDefinitionList != null )
\r
59 for (MatchItem matchItem : flow.getMatchItem())
\r
61 for (MatchItemDefinition matchItemDefinition : matchItemDefinitionList)
\r
63 if (matchItem.getMatchItemName().equals(matchItemDefinition.getMatchItemName()))
\r
65 matchHasDefined = true;
\r
66 MatchItemValue matchItemValue = matchItem.getMatchItemValue();
\r
67 MatchItemDefinition.MatchItemValueType matchItemValueType = matchItemDefinition.getMatchItemValueType();
\r
69 if (matchItemValue != null && matchItemValueType != null)
\r
71 if (matchItemValueType.getIntValue()==0 && !(matchItemValue.getIntValue()==null&&matchItemValue.getStringValue()!=null&&matchItemValue.getRangeValue()==null))
\r
73 errorInfo = "The match item value type for" +matchItem.getMatchItemName().toString()+"should be string.";
\r
77 if (matchItemValueType.getIntValue()==1 && !(matchItemValue.getIntValue()!=null&&matchItemValue.getStringValue()==null&&matchItemValue.getRangeValue()==null))
\r
79 errorInfo = "The match item value type for" + matchItem.getMatchItemName().toString()+"should be integer.";
\r
83 if (matchItemValueType.getIntValue()==2 && !(matchItemValue.getIntValue()==null&&matchItemValue.getStringValue()==null&&matchItemValue.getRangeValue()!=null))
\r
85 errorInfo = "The match item value type for" + matchItem.getMatchItemName().toString()+"should be range.";
\r
93 if (!matchHasDefined)
\r
95 errorInfo = "The match item has not been defined.";
\r
101 private void fetchMatchItemDefinitions()
\r
103 InstanceIdentifier<MatchItemDefinitions> matchitemdefinitionId = InstanceIdentifier.builder(MatchItemDefinitions.class).build();
\r
104 ListenableFuture<Optional<MatchItemDefinitions>> matchitemdefinitionFuture = dataBroker.newReadOnlyTransaction().read(LogicalDatastoreType.CONFIGURATION, matchitemdefinitionId);
\r
105 Futures.addCallback(matchitemdefinitionFuture, new FutureCallback<Optional<MatchItemDefinitions>>() {
\r
107 public void onSuccess(Optional<MatchItemDefinitions> result) {
\r
108 setMatchItemDefinitionList( result.get().getMatchItemDefinition());
\r
113 public void onFailure(Throwable t) {
\r
114 LOG.error("Can not read match item definition information.", t);
\r
122 private void setMatchItemDefinitionList(List<MatchItemDefinition> matchItemDefinitionList)
\r
124 this.matchItemDefinitionList = matchItemDefinitionList;
\r