Adding nemo engine.
[nemo.git] / nemo-impl / src / main / java / org / opendaylight / nemo / user / vnspacemanager / syntaxcheck / FlowDefinitionCheck.java
1 /*\r
2  * Copyright (c) 2015 Huawei, Inc. and others. All rights reserved.\r
3  *\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
7  */\r
8 package org.opendaylight.nemo.user.vnspacemanager.syntaxcheck;\r
9 \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
30 \r
31 import org.slf4j.Logger;\r
32 import org.slf4j.LoggerFactory;\r
33 import java.util.List;\r
34 \r
35 /**\r
36  * Created by z00293636 on 2015/9/2.\r
37  */\r
38 public class FlowDefinitionCheck {\r
39 \r
40     private DataBroker dataBroker;\r
41     private List<MatchItemDefinition> matchItemDefinitionList;\r
42     private static final Logger LOG = LoggerFactory.getLogger(FlowDefinitionCheck.class);\r
43 \r
44     public FlowDefinitionCheck(DataBroker dataBroker)\r
45     {\r
46         this.dataBroker = dataBroker;\r
47         matchItemDefinitionList = null;\r
48     }\r
49 \r
50     public String CheckDefinition(Flow flow)\r
51     {\r
52         String errorInfo = null;\r
53         fetchMatchItemDefinitions();\r
54         Boolean matchHasDefined = false;\r
55         if (flow.getMatchItem() != null)\r
56         {\r
57             if (matchItemDefinitionList != null )\r
58             {\r
59                 for (MatchItem matchItem : flow.getMatchItem())\r
60                 {\r
61                     for (MatchItemDefinition matchItemDefinition : matchItemDefinitionList)\r
62                     {\r
63                         if (matchItem.getMatchItemName().equals(matchItemDefinition.getMatchItemName()))\r
64                         {\r
65                             matchHasDefined = true;\r
66                             MatchItemValue matchItemValue = matchItem.getMatchItemValue();\r
67                             MatchItemDefinition.MatchItemValueType matchItemValueType = matchItemDefinition.getMatchItemValueType();\r
68 \r
69                             if (matchItemValue != null && matchItemValueType != null)\r
70                             {\r
71                                 if (matchItemValueType.getIntValue()==0 && !(matchItemValue.getIntValue()==null&&matchItemValue.getStringValue()!=null&&matchItemValue.getRangeValue()==null))\r
72                                 {\r
73                                     errorInfo = "The match item value type for" +matchItem.getMatchItemName().toString()+"should be string.";\r
74                                     break;\r
75                                 }\r
76 \r
77                                 if (matchItemValueType.getIntValue()==1 && !(matchItemValue.getIntValue()!=null&&matchItemValue.getStringValue()==null&&matchItemValue.getRangeValue()==null))\r
78                                 {\r
79                                     errorInfo = "The match item value type for" + matchItem.getMatchItemName().toString()+"should be integer.";\r
80                                     break;\r
81                                 }\r
82 \r
83                                 if (matchItemValueType.getIntValue()==2 && !(matchItemValue.getIntValue()==null&&matchItemValue.getStringValue()==null&&matchItemValue.getRangeValue()!=null))\r
84                                 {\r
85                                     errorInfo = "The match item value type for" + matchItem.getMatchItemName().toString()+"should be range.";\r
86                                     break;\r
87                                 }\r
88                             }\r
89                         }\r
90                     }\r
91                 }\r
92         }\r
93             if (!matchHasDefined)\r
94             {\r
95                 errorInfo = "The match item has not been defined.";\r
96             }\r
97     }\r
98         return errorInfo;\r
99     }\r
100 \r
101     private void fetchMatchItemDefinitions()\r
102     {\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
106             @Override\r
107             public void onSuccess(Optional<MatchItemDefinitions> result) {\r
108                setMatchItemDefinitionList( result.get().getMatchItemDefinition());\r
109                 return;\r
110             }\r
111 \r
112             @Override\r
113             public void onFailure(Throwable t) {\r
114                 LOG.error("Can not read match item definition information.", t);\r
115 \r
116                 return;\r
117             }\r
118         });\r
119         return ;\r
120     }\r
121 \r
122     private void setMatchItemDefinitionList(List<MatchItemDefinition> matchItemDefinitionList)\r
123     {\r
124         this.matchItemDefinitionList = matchItemDefinitionList;\r
125     }\r
126 }\r