df43c74d5e7463a33b80c9ac25701d94dad269b1
[nemo.git] / nemo-impl / src / main / java / org / opendaylight / nemo / user / vnspacemanager / structurestyle / updateintent / UpdateConnection.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.structurestyle.updateintent;\r
9 \r
10 import com.google.common.base.Optional;\r
11 import com.google.common.util.concurrent.CheckedFuture;\r
12 import com.google.common.util.concurrent.FutureCallback;\r
13 import com.google.common.util.concurrent.Futures;\r
14 import com.google.common.util.concurrent.ListenableFuture;\r
15 import com.google.common.util.concurrent.SettableFuture;\r
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;\r
17 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;\r
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;\r
19 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;\r
20 import org.opendaylight.nemo.user.tenantmanager.TenantManage;\r
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.common.rev151010.ConnectionType;\r
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.common.rev151010.UserId;\r
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.Users;\r
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.Objects;\r
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.objects.Connection;\r
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.objects.ConnectionBuilder;\r
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.objects.ConnectionKey;\r
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.objects.Node;\r
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.users.User;\r
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.users.UserKey;\r
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.ConnectionDefinitions;\r
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.connection.definitions.ConnectionDefinition;\r
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.connection.instance.EndNode;\r
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.connection.instance.Property;\r
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.property.definitions.PropertyDefinition;\r
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.property.instance.PropertyValues;\r
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;\r
38 import org.slf4j.Logger;\r
39 import org.slf4j.LoggerFactory;\r
40 \r
41 import java.util.HashMap;\r
42 import java.util.List;\r
43 import java.util.Map;\r
44 import java.util.concurrent.ExecutionException;\r
45 import java.util.concurrent.TimeUnit;\r
46 import java.util.concurrent.TimeoutException;\r
47 \r
48 /**\r
49  * Created by z00293636 on 2015/8/31.\r
50  */\r
51 public class UpdateConnection {\r
52 \r
53     private DataBroker dataBroker;\r
54     private TenantManage tenantManage;\r
55     private GetDefinitions getDefinitions;\r
56     private static final Logger LOG = LoggerFactory.getLogger(UpdateConnection.class);\r
57 \r
58     public UpdateConnection(DataBroker dataBroker, TenantManage tenantManage){\r
59         this.dataBroker = dataBroker;\r
60         this.tenantManage = tenantManage;\r
61         getDefinitions = new GetDefinitions(dataBroker);\r
62     }\r
63 \r
64     public String ConnectionHandling(UserId userId, Connection connection){\r
65         String errorInfo = null;\r
66         errorInfo =checkDefinition(connection);\r
67         if (errorInfo != null){\r
68             return errorInfo;\r
69         }\r
70         else {\r
71             errorInfo = checkInstance(userId,connection);\r
72             if (errorInfo != null){\r
73                 return errorInfo;\r
74             }\r
75             else {\r
76                 WriteTransaction t = dataBroker.newWriteOnlyTransaction();\r
77                 if (userId != null && connection != null){\r
78                     Connection connection1 = new ConnectionBuilder(connection).build();\r
79                     ConnectionKey connectionKey = new ConnectionKey(connection.getConnectionId());\r
80 \r
81                     UserKey userKey = new UserKey(userId);\r
82 \r
83                     InstanceIdentifier<Connection> connectionid = InstanceIdentifier.builder(Users.class).child(User.class, userKey).child(Objects.class).child(Connection.class,connectionKey).build();\r
84                     t.put(LogicalDatastoreType.CONFIGURATION, connectionid, connection1,true);\r
85                     CheckedFuture<Void, TransactionCommitFailedException> f = t.submit();\r
86                     Futures.addCallback(f, new FutureCallback<Void>() {\r
87                         @Override\r
88                         public void onFailure(Throwable t) {\r
89                             LOG.error("Could not write endpoint base container", t);\r
90                         }\r
91 \r
92                         @Override\r
93                         public void onSuccess(Void result) {\r
94                         }\r
95                     });\r
96                 }\r
97             }\r
98         }\r
99         return errorInfo;\r
100     }\r
101 \r
102     private String checkInstance(UserId userId, Connection connection){\r
103         String errorInfo = null;\r
104         tenantManage.fetchVNSpace(userId);\r
105         User user = tenantManage.getUser();\r
106 \r
107         if (user!=null){\r
108             if (user.getObjects()!=null){\r
109                 if (user.getObjects().getConnection()!=null){\r
110                     List<Connection> connectionList = tenantManage.getUser().getObjects().getConnection();\r
111 \r
112                     for (Connection connection1 : connectionList){\r
113                         if (connection1.getConnectionId().equals(connection.getConnectionId())){\r
114                             if (!connection1.getConnectionType().equals(connection.getConnectionType())){\r
115                                 errorInfo = "The connection type should not be changed.";\r
116                                 break;\r
117                             }\r
118                             if (!connection1.getConnectionName().equals(connection.getConnectionName())){\r
119                                 errorInfo = "The End node should not be changed.";\r
120                                 break;\r
121                             }\r
122 \r
123                         }\r
124                     }\r
125                 }\r
126                 if (connection.getEndNode()!=null){\r
127                     if (user.getObjects().getNode()!=null){\r
128                         List<EndNode> endnodeList = connection.getEndNode();\r
129                         List<Node> nodeList = user.getObjects().getNode();\r
130                         Boolean nodeExist = false;\r
131 \r
132                         for (EndNode endNode : endnodeList){\r
133                             for (Node node : nodeList){\r
134                                 if (endNode.getNodeId().equals(node.getNodeId())){\r
135                                     nodeExist = true;\r
136                                 }\r
137                             }\r
138                             if (!nodeExist){\r
139                                 errorInfo = "The endnode is not exist in user vn space.";\r
140                             }\r
141                         }\r
142                     }\r
143                     else\r
144                     {\r
145                         errorInfo = "The endnodes are not exist in the user vn space.";\r
146                     }\r
147                 }\r
148             }\r
149             else {\r
150                 if (connection.getEndNode() != null){\r
151                     errorInfo = "There are no nodes in user vn space.";\r
152                 }\r
153             }\r
154         }\r
155         else {\r
156             if (connection.getEndNode() != null){\r
157                 errorInfo = "There are no nodes in user vn space.";\r
158             }\r
159         }\r
160         return errorInfo;\r
161     }\r
162 \r
163     private String checkDefinition(Connection connection){\r
164         String errorInfo = null;\r
165         Map<ConnectionType, ConnectionDefinition> connectionDefinitionMap = getDefinitions.getConnectionDefinition();\r
166         if (connectionDefinitionMap.isEmpty()){\r
167             return "This type of connection has not been defined.";\r
168         }\r
169         else {\r
170             if (connectionDefinitionMap.containsKey(connection.getConnectionType())){\r
171                 List<Property> connectionProperties = connection.getProperty();\r
172                 List<PropertyDefinition> propertyDefinitions = connectionDefinitionMap.get(connection.getConnectionType()).getPropertyDefinition();\r
173                 if (connectionProperties != null && propertyDefinitions ==null)\r
174                 {\r
175                     errorInfo = "There are no properties for this type of connection.";\r
176                 }\r
177                 else if (connectionProperties != null && propertyDefinitions != null)\r
178                 {\r
179                     errorInfo = CheckProperty(connection.getProperty(), propertyDefinitions);\r
180                 }\r
181             }\r
182             else {\r
183                 return "This type of connection has not been defined.";\r
184             }\r
185         }\r
186         return errorInfo;\r
187     }\r
188 \r
189     private String CheckProperty(List<Property> connectionProperty, List<PropertyDefinition> propertyDefinitionList ){\r
190         String errorInfo = null;\r
191         for (Property property : connectionProperty)\r
192         {\r
193             Boolean properyHasDefine = false;\r
194             if (errorInfo != null)\r
195             {\r
196                 break;\r
197             }\r
198             else\r
199             {\r
200                 for (PropertyDefinition propertyDefinition : propertyDefinitionList)\r
201                 {\r
202                     if (property.getPropertyName().equals(propertyDefinition.getPropertyName()))\r
203                     {\r
204                         properyHasDefine = true;\r
205                         PropertyValues propertyValues = property.getPropertyValues();\r
206                         PropertyDefinition.PropertyValueType propertyValueType = propertyDefinition.getPropertyValueType();\r
207 \r
208                         if (propertyValues != null && propertyValueType != null)\r
209                         {\r
210                             if (propertyDefinition.getIsReadOnly()!=null\r
211                                     && PropertyDefinition.IsReadOnly.ReadOnly == propertyDefinition.getIsReadOnly())\r
212                             {\r
213                                 if (propertyDefinition.getIsRequired().getIntValue() == 1)\r
214                                 {\r
215                                     errorInfo = "The property value type of " + property.getPropertyName().toString() + " is read only.";\r
216                                     break;\r
217                                 }\r
218                             }\r
219                             else\r
220                             {\r
221                                 if (propertyValueType.getIntValue() == 0 && !(propertyValues.getIntValue() == null && propertyValues.getStringValue() != null && propertyValues.getRangeValue() == null)) {\r
222                                     errorInfo = "The property value type of " + property.getPropertyName().toString() + " should be string.";\r
223                                     break;\r
224                                 }\r
225                                 if (propertyValueType.getIntValue() == 1 && !(propertyValues.getIntValue() != null && propertyValues.getStringValue() == null && propertyValues.getRangeValue() == null)) {\r
226                                     errorInfo = "The property value type of" + property.getPropertyName().toString() + " should be integer.";\r
227                                     break;\r
228                                 }\r
229                                 if (propertyValueType.getIntValue() == 2 && !(propertyValues.getIntValue() == null && propertyValues.getStringValue() == null && propertyValues.getRangeValue() != null)) {\r
230                                     errorInfo = "The property value type of" + property.getPropertyName().toString() + " should be range.";\r
231                                     break;\r
232                                 }\r
233                             }\r
234                         }\r
235                     }\r
236                 }\r
237                 if (!properyHasDefine) {\r
238                     errorInfo = "This type of property" + property.getPropertyName().toString() + " has not been defined.";\r
239                 }\r
240             }\r
241         }\r
242 \r
243         if (errorInfo == null)\r
244         {\r
245             Boolean requiredProperty = false;\r
246             for (PropertyDefinition propertyDefinition : propertyDefinitionList)\r
247             {\r
248                 if (propertyDefinition.getIsRequired()!=null)\r
249                 {\r
250                     if (propertyDefinition.getIsRequired().getIntValue() == 0)\r
251                     {\r
252                         for (Property property : connectionProperty)\r
253                         {\r
254                             if (property.getPropertyName().equals(propertyDefinition.getPropertyName()))\r
255                             {\r
256                                 requiredProperty = true;\r
257                             }\r
258                         }\r
259                         if (!requiredProperty)\r
260                         {\r
261                             errorInfo = "The required property" + propertyDefinition.getPropertyName().toString() + "is not included in the intent.";\r
262                         }\r
263                     }\r
264                 }\r
265             }\r
266         }\r
267         return errorInfo;\r
268     }\r
269 }\r