Modify update intent.
[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         if (connection.getEndNode()!=null){\r
86             List<EndNode> endNodeList = connection.getEndNode();\r
87             for (EndNode endNode : endNodeList){\r
88                 Boolean endNodeExist = false;\r
89                 if (tenantManage.getNode(userId)!=null){\r
90                     if (tenantManage.getNode(userId).containsKey(endNode.getNodeId())){\r
91                         endNodeExist = true;\r
92                     }\r
93                 }\r
94                 if (tenantManage.getNodeDataStore(userId)!=null){\r
95                     if (tenantManage.getNodeDataStore(userId).containsKey(endNode.getNodeId())){\r
96                         endNodeExist = true;\r
97                     }\r
98                 }\r
99                 if (!endNodeExist){\r
100                     return "The endnode "+ endNode.getNodeId().getValue() +" is not exist;";\r
101                 }\r
102             }\r
103         }\r
104         return null;\r
105     }\r
106 \r
107     private String checkDefinition(Connection connection){\r
108         String errorInfo = null;\r
109         Map<ConnectionType, ConnectionDefinition> connectionDefinitionMap = getDefinitions.getConnectionDefinition();\r
110         if (connectionDefinitionMap.isEmpty()){\r
111             return "This type of connection has not been defined.";\r
112         }\r
113         else {\r
114             if (connectionDefinitionMap.containsKey(connection.getConnectionType())){\r
115                 List<Property> connectionProperties = connection.getProperty();\r
116                 List<PropertyDefinition> propertyDefinitions = connectionDefinitionMap.get(connection.getConnectionType()).getPropertyDefinition();\r
117                 if (connectionProperties != null && propertyDefinitions ==null)\r
118                 {\r
119                     errorInfo = "There are no properties for this type of connection.";\r
120                 }\r
121                 else if (connectionProperties != null && propertyDefinitions != null)\r
122                 {\r
123                     errorInfo = checkProperty(connection.getProperty(), propertyDefinitions);\r
124                 }\r
125             }\r
126             else {\r
127                 return "This type of connection has not been defined.";\r
128             }\r
129         }\r
130         return errorInfo;\r
131     }\r
132 \r
133     private String checkProperty(List<Property> connectionProperty, List<PropertyDefinition> propertyDefinitionList ){\r
134         String errorInfo = null;\r
135         for (Property property : connectionProperty)\r
136         {\r
137             Boolean properyHasDefine = false;\r
138             if (errorInfo != null)\r
139             {\r
140                 break;\r
141             }\r
142             else\r
143             {\r
144                 for (PropertyDefinition propertyDefinition : propertyDefinitionList)\r
145                 {\r
146                     if (property.getPropertyName().equals(propertyDefinition.getPropertyName()))\r
147                     {\r
148                         properyHasDefine = true;\r
149                         PropertyValues propertyValues = property.getPropertyValues();\r
150                         PropertyDefinition.PropertyValueType propertyValueType = propertyDefinition.getPropertyValueType();\r
151 \r
152                         if (propertyValues != null && propertyValueType != null)\r
153                         {\r
154                             if (propertyDefinition.getIsReadOnly()!=null\r
155                                     && PropertyDefinition.IsReadOnly.ReadOnly == propertyDefinition.getIsReadOnly())\r
156                             {\r
157                                 if (propertyDefinition.getIsRequired().getIntValue() == 1)\r
158                                 {\r
159                                     errorInfo = "The property value type of " + property.getPropertyName().toString() + " is read only.";\r
160                                     break;\r
161                                 }\r
162                             }\r
163                             else\r
164                             {\r
165                                 if (propertyValueType.getIntValue() == 0 && !(propertyValues.getIntValue() == null && propertyValues.getStringValue() != null && propertyValues.getRangeValue() == null)) {\r
166                                     errorInfo = "The property value type of " + property.getPropertyName().toString() + " should be string.";\r
167                                     break;\r
168                                 }\r
169                                 if (propertyValueType.getIntValue() == 1 && !(propertyValues.getIntValue() != null && propertyValues.getStringValue() == null && propertyValues.getRangeValue() == null)) {\r
170                                     errorInfo = "The property value type of" + property.getPropertyName().toString() + " should be integer.";\r
171                                     break;\r
172                                 }\r
173                                 if (propertyValueType.getIntValue() == 2 && !(propertyValues.getIntValue() == null && propertyValues.getStringValue() == null && propertyValues.getRangeValue() != null)) {\r
174                                     errorInfo = "The property value type of" + property.getPropertyName().toString() + " should be range.";\r
175                                     break;\r
176                                 }\r
177                             }\r
178                         }\r
179                     }\r
180                 }\r
181                 if (!properyHasDefine) {\r
182                     errorInfo = "This type of property" + property.getPropertyName().toString() + " has not been defined.";\r
183                 }\r
184             }\r
185         }\r
186 \r
187         if (errorInfo == null)\r
188         {\r
189             Boolean requiredProperty = false;\r
190             for (PropertyDefinition propertyDefinition : propertyDefinitionList)\r
191             {\r
192                 if (propertyDefinition.getIsRequired()!=null)\r
193                 {\r
194                     if (propertyDefinition.getIsRequired().getIntValue() == 0)\r
195                     {\r
196                         for (Property property : connectionProperty)\r
197                         {\r
198                             if (property.getPropertyName().equals(propertyDefinition.getPropertyName()))\r
199                             {\r
200                                 requiredProperty = true;\r
201                             }\r
202                         }\r
203                         if (!requiredProperty)\r
204                         {\r
205                             errorInfo = "The required property" + propertyDefinition.getPropertyName().toString() + "is not included in the intent.";\r
206                         }\r
207                     }\r
208                 }\r
209             }\r
210         }\r
211         return errorInfo;\r
212     }\r
213 }\r