494e01d45f196d44c006d467823500f5698abb1d
[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 org.opendaylight.controller.md.sal.binding.api.DataBroker;\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.ConnectionType;\r
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.common.rev151010.UserId;\r
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.objects.Connection;\r
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.connection.definitions.ConnectionDefinition;\r
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.connection.instance.EndNode;\r
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.connection.instance.Property;\r
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.property.definitions.PropertyDefinition;\r
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.property.instance.PropertyValues;\r
20 \r
21 import java.util.List;\r
22 import java.util.Map;\r
23 \r
24 /**\r
25  * Created by z00293636 on 2015/8/31.\r
26  */\r
27 public class UpdateConnection {\r
28     private TenantManage tenantManage;\r
29     private GetDefinitions getDefinitions;\r
30 \r
31     public UpdateConnection(DataBroker dataBroker, TenantManage tenantManage){\r
32         this.tenantManage = tenantManage;\r
33         getDefinitions = new GetDefinitions(dataBroker);\r
34     }\r
35 \r
36     public String ConnectionHandling(UserId userId, Connection connection){\r
37         String errorInfo = null;\r
38         errorInfo =checkDefinition(connection);\r
39         if (errorInfo != null){\r
40             return errorInfo;\r
41         }\r
42         else {\r
43             errorInfo = checkInstance(userId,connection);\r
44             if (errorInfo != null){\r
45                 return errorInfo;\r
46             }\r
47             else {\r
48                 tenantManage.setConnection(userId,connection.getConnectionId(),connection);\r
49             }\r
50         }\r
51         return errorInfo;\r
52     }\r
53 \r
54     private String checkInstance(UserId userId, Connection connection){\r
55         if (tenantManage.getConnection(userId)!=null){\r
56             if (tenantManage.getConnection(userId).containsKey(connection.getConnectionId())){\r
57                 Connection connExist = tenantManage.getConnection(userId).get(connection.getConnectionId());\r
58                 if (!connExist.getConnectionName().equals(connection.getConnectionName())){\r
59                     return "The connection name should not be changed.";\r
60                 }\r
61                 if (!connExist.getConnectionType().equals(connection.getConnectionType())){\r
62                     return "The connection type should not be changed.";\r
63                 }\r
64                 if (!connExist.getEndNode().equals(connection.getEndNode())){\r
65                     return "The connection end node should not be changed.";\r
66                 }\r
67             }\r
68         }\r
69 \r
70         if (tenantManage.getConnectionDataStore(userId)!=null){\r
71             if (tenantManage.getConnectionDataStore(userId).containsKey(connection.getConnectionId())){\r
72                 Connection connExist = tenantManage.getConnectionDataStore(userId).get(connection.getConnectionId());\r
73                 if (!connExist.getConnectionName().equals(connection.getConnectionName())){\r
74                     return "The connection name should not be changed.";\r
75                 }\r
76                 if (!connExist.getConnectionType().equals(connection.getConnectionType())){\r
77                     return "The connection type should not be changed.";\r
78                 }\r
79                 if (!connExist.getEndNode().equals(connection.getEndNode())){\r
80                     return "The connection end node should not be changed.";\r
81                 }\r
82             }\r
83         }\r
84 \r
85         List<EndNode> endNodeList = connection.getEndNode();\r
86         for (EndNode endNode : endNodeList){\r
87             Boolean endNodeExist = false;\r
88             if (tenantManage.getNode(userId)!=null){\r
89                 if (tenantManage.getNode(userId).containsKey(endNode.getNodeId())){\r
90                     endNodeExist = true;\r
91                 }\r
92             }\r
93             if (tenantManage.getNodeDataStore(userId)!=null){\r
94                 if (tenantManage.getNodeDataStore(userId).containsKey(endNode.getNodeId())){\r
95                     endNodeExist = true;\r
96                 }\r
97             }\r
98             if (!endNodeExist){\r
99                 return "The endnode "+ endNode.getNodeId().getValue() +" is not exist;";\r
100             }\r
101         }\r
102         return null;\r
103     }\r
104 \r
105     private String checkDefinition(Connection connection){\r
106         String errorInfo = null;\r
107         Map<ConnectionType, ConnectionDefinition> connectionDefinitionMap = getDefinitions.getConnectionDefinition();\r
108         if (connectionDefinitionMap.isEmpty()){\r
109             return "This type of connection has not been defined.";\r
110         }\r
111         else {\r
112             if (connectionDefinitionMap.containsKey(connection.getConnectionType())){\r
113                 List<Property> connectionProperties = connection.getProperty();\r
114                 List<PropertyDefinition> propertyDefinitions = connectionDefinitionMap.get(connection.getConnectionType()).getPropertyDefinition();\r
115                 if (connectionProperties != null && propertyDefinitions ==null)\r
116                 {\r
117                     errorInfo = "There are no properties for this type of connection.";\r
118                 }\r
119                 else if (connectionProperties != null && propertyDefinitions != null)\r
120                 {\r
121                     errorInfo = checkProperty(connection.getProperty(), propertyDefinitions);\r
122                 }\r
123             }\r
124             else {\r
125                 return "This type of connection has not been defined.";\r
126             }\r
127         }\r
128         return errorInfo;\r
129     }\r
130 \r
131     private String checkProperty(List<Property> connectionProperty, List<PropertyDefinition> propertyDefinitionList ){\r
132         String errorInfo = null;\r
133         for (Property property : connectionProperty)\r
134         {\r
135             Boolean properyHasDefine = false;\r
136             if (errorInfo != null)\r
137             {\r
138                 break;\r
139             }\r
140             else\r
141             {\r
142                 for (PropertyDefinition propertyDefinition : propertyDefinitionList)\r
143                 {\r
144                     if (property.getPropertyName().equals(propertyDefinition.getPropertyName()))\r
145                     {\r
146                         properyHasDefine = true;\r
147                         PropertyValues propertyValues = property.getPropertyValues();\r
148                         PropertyDefinition.PropertyValueType propertyValueType = propertyDefinition.getPropertyValueType();\r
149 \r
150                         if (propertyValues != null && propertyValueType != null)\r
151                         {\r
152                             if (propertyDefinition.getIsReadOnly()!=null\r
153                                     && PropertyDefinition.IsReadOnly.ReadOnly == propertyDefinition.getIsReadOnly())\r
154                             {\r
155                                 if (propertyDefinition.getIsRequired().getIntValue() == 1)\r
156                                 {\r
157                                     errorInfo = "The property value type of " + property.getPropertyName().toString() + " is read only.";\r
158                                     break;\r
159                                 }\r
160                             }\r
161                             else\r
162                             {\r
163                                 if (propertyValueType.getIntValue() == 0 && !(propertyValues.getIntValue() == null && propertyValues.getStringValue() != null && propertyValues.getRangeValue() == null)) {\r
164                                     errorInfo = "The property value type of " + property.getPropertyName().toString() + " should be string.";\r
165                                     break;\r
166                                 }\r
167                                 if (propertyValueType.getIntValue() == 1 && !(propertyValues.getIntValue() != null && propertyValues.getStringValue() == null && propertyValues.getRangeValue() == null)) {\r
168                                     errorInfo = "The property value type of" + property.getPropertyName().toString() + " should be integer.";\r
169                                     break;\r
170                                 }\r
171                                 if (propertyValueType.getIntValue() == 2 && !(propertyValues.getIntValue() == null && propertyValues.getStringValue() == null && propertyValues.getRangeValue() != null)) {\r
172                                     errorInfo = "The property value type of" + property.getPropertyName().toString() + " should be range.";\r
173                                     break;\r
174                                 }\r
175                             }\r
176                         }\r
177                     }\r
178                 }\r
179                 if (!properyHasDefine) {\r
180                     errorInfo = "This type of property" + property.getPropertyName().toString() + " has not been defined.";\r
181                 }\r
182             }\r
183         }\r
184 \r
185         if (errorInfo == null)\r
186         {\r
187             Boolean requiredProperty = false;\r
188             for (PropertyDefinition propertyDefinition : propertyDefinitionList)\r
189             {\r
190                 if (propertyDefinition.getIsRequired()!=null)\r
191                 {\r
192                     if (propertyDefinition.getIsRequired().getIntValue() == 0)\r
193                     {\r
194                         for (Property property : connectionProperty)\r
195                         {\r
196                             if (property.getPropertyName().equals(propertyDefinition.getPropertyName()))\r
197                             {\r
198                                 requiredProperty = true;\r
199                             }\r
200                         }\r
201                         if (!requiredProperty)\r
202                         {\r
203                             errorInfo = "The required property" + propertyDefinition.getPropertyName().toString() + "is not included in the intent.";\r
204                         }\r
205                     }\r
206                 }\r
207             }\r
208         }\r
209         return errorInfo;\r
210     }\r
211 }\r