Adding nemo engine.
[nemo.git] / nemo-impl / src / main / java / org / opendaylight / nemo / user / vnspacemanager / syntaxcheck / ConnectionDefinitionCheck.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.common.rev151010.PropertyName;\r
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.objects.Connection;\r
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.ConnectionDefinitions;\r
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.connection.definitions.ConnectionDefinition;\r
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.connection.instance.Property;\r
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.property.definitions.PropertyDefinition;\r
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.property.instance.PropertyValues;\r
18 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;\r
19 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;\r
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;\r
21 import org.opendaylight.yangtools.yang.common.RpcResult;\r
22 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;\r
23 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;\r
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;\r
25 import com.google.common.base.Optional;\r
26 import com.google.common.base.Function;\r
27 import com.google.common.util.concurrent.CheckedFuture;\r
28 import com.google.common.util.concurrent.FutureCallback;\r
29 import com.google.common.util.concurrent.Futures;\r
30 import com.google.common.util.concurrent.ListenableFuture;\r
31 \r
32 import org.slf4j.Logger;\r
33 import org.slf4j.LoggerFactory;\r
34 import java.util.List;\r
35 \r
36 /**\r
37  * Created by z00293636 on 2015/9/2.\r
38  */\r
39 public class ConnectionDefinitionCheck {\r
40 \r
41     private DataBroker dataBroker;\r
42     private List<ConnectionDefinition> connectionDefinitionList;\r
43     private static final Logger LOG = LoggerFactory.getLogger(ConnectionDefinitionCheck.class);\r
44 \r
45     public ConnectionDefinitionCheck(DataBroker dataBroker)\r
46     {\r
47         this.dataBroker = dataBroker;\r
48         connectionDefinitionList = null;\r
49     }\r
50 \r
51     public String CheckConnectionDefinition(Connection connection)\r
52     {\r
53         fetchConnectionDefinitionList();\r
54         boolean ConnectionHasDefined = false;\r
55         String errorInfo = null;\r
56 \r
57         if (connectionDefinitionList != null)\r
58         {\r
59             for (ConnectionDefinition connectionDefinition : connectionDefinitionList)\r
60             {\r
61                 if (connectionDefinition.getConnectionType().equals(connection.getConnectionType()))\r
62                 {\r
63                     ConnectionHasDefined = true;\r
64                     List<Property> connectionProperties = connection.getProperty();\r
65                     List<PropertyDefinition> propertyDefinitions = connectionDefinition.getPropertyDefinition();\r
66 \r
67                     if (connectionProperties != null && propertyDefinitions ==null)\r
68                     {\r
69                         errorInfo = "There are no properties for this type of connection.";\r
70                         break;\r
71                     }\r
72                     else if (connectionProperties != null && propertyDefinitions != null)\r
73                     {\r
74                         errorInfo = CheckProperty(connection.getProperty(), connectionDefinition.getPropertyDefinition());\r
75                         if (errorInfo != null)\r
76                         {\r
77                             break;\r
78                         }\r
79                     }\r
80 \r
81                 }\r
82             }\r
83         }\r
84 \r
85         if (!ConnectionHasDefined)\r
86         {\r
87             return "This type of connection has not been defined.";\r
88         }\r
89         return errorInfo;\r
90     }\r
91 \r
92     private String CheckProperty(List<Property> connectionProperty, List<PropertyDefinition> propertyDefinitionList )\r
93     {\r
94         String errorInfo = null;\r
95         for (Property property : connectionProperty)\r
96         {\r
97             Boolean properyHasDefine = false;\r
98             if (errorInfo != null)\r
99             {\r
100                 break;\r
101             }\r
102             else\r
103             {\r
104                 for (PropertyDefinition propertyDefinition : propertyDefinitionList)\r
105                 {\r
106                     if (property.getPropertyName().equals(propertyDefinition.getPropertyName()))\r
107                     {\r
108                         properyHasDefine = true;\r
109                         PropertyValues propertyValues = property.getPropertyValues();\r
110                         PropertyDefinition.PropertyValueType propertyValueType = propertyDefinition.getPropertyValueType();\r
111 \r
112                         if (propertyValues != null && propertyValueType != null)\r
113                         {\r
114                             if (propertyDefinition.getIsReadOnly()!=null)\r
115                             {\r
116                                 if (propertyDefinition.getIsRequired().getIntValue() == 1)\r
117                                 {\r
118                                     errorInfo = "The property value type of" + property.getPropertyName().toString() + "is read only.";\r
119                                     break;\r
120                                 }\r
121                             }\r
122                             else\r
123                             {\r
124                                 if (propertyValueType.getIntValue() == 0 && !(propertyValues.getIntValue() == null && propertyValues.getStringValue() != null && propertyValues.getRangeValue() == null)) {\r
125                                     errorInfo = "The property value type of" + property.getPropertyName().toString() + " should be string.";\r
126                                     break;\r
127                                 }\r
128                                 if (propertyValueType.getIntValue() == 1 && !(propertyValues.getIntValue() != null && propertyValues.getStringValue() == null && propertyValues.getRangeValue() == null)) {\r
129                                     errorInfo = "The property value type of" + property.getPropertyName().toString() + " should be integer.";\r
130                                     break;\r
131                                 }\r
132                                 if (propertyValueType.getIntValue() == 2 && !(propertyValues.getIntValue() == null && propertyValues.getStringValue() == null && propertyValues.getRangeValue() != null)) {\r
133                                     errorInfo = "The property value type of" + property.getPropertyName().toString() + " should be range.";\r
134                                     break;\r
135                                 }\r
136                             }\r
137                         }\r
138                         }\r
139                 }\r
140                 if (!properyHasDefine) {\r
141                     errorInfo = "This type of property" + property.getPropertyName().toString() + " has not been defined.";\r
142                 }\r
143             }\r
144          }\r
145 \r
146         if (errorInfo == null)\r
147         {\r
148             Boolean requiredProperty = false;\r
149             for (PropertyDefinition propertyDefinition : propertyDefinitionList)\r
150             {\r
151                 if (propertyDefinition.getIsRequired()!=null)\r
152                 {\r
153                     if (propertyDefinition.getIsRequired().getIntValue() == 0)\r
154                     {\r
155                         for (Property property : connectionProperty)\r
156                         {\r
157                             if (property.getPropertyName().equals(propertyDefinition.getPropertyName()))\r
158                             {\r
159                                 requiredProperty = true;\r
160                             }\r
161                         }\r
162                         if (!requiredProperty)\r
163                         {\r
164                             errorInfo = "The required property" + propertyDefinition.getPropertyName().toString() + "is not included in the intent.";\r
165                         }\r
166                     }\r
167                 }\r
168             }\r
169         }\r
170         return errorInfo;\r
171     }\r
172 \r
173     private void fetchConnectionDefinitionList()\r
174     {\r
175         InstanceIdentifier<ConnectionDefinitions> connectiondefinitionId = InstanceIdentifier.builder(ConnectionDefinitions.class).build();\r
176         ListenableFuture<Optional<ConnectionDefinitions>> connectiondefinitionFuture = dataBroker.newReadOnlyTransaction().read(LogicalDatastoreType.CONFIGURATION, connectiondefinitionId);\r
177         Futures.addCallback(connectiondefinitionFuture, new FutureCallback<Optional<ConnectionDefinitions>>() {\r
178             @Override\r
179             public void onSuccess(Optional<ConnectionDefinitions> result) {\r
180                 setConnectionDefinitionList(result.get().getConnectionDefinition());\r
181                 return;\r
182             }\r
183 \r
184             @Override\r
185             public void onFailure(Throwable t) {\r
186                 LOG.error("Can not read connection definition information.", t);\r
187 \r
188                 return;\r
189             }\r
190         });\r
191         return;\r
192     }\r
193 \r
194     private void setConnectionDefinitionList(List<ConnectionDefinition> connectionDefinitionList)\r
195     {\r
196         this.connectionDefinitionList = connectionDefinitionList;\r
197     }\r
198 \r
199 }\r