2 * Copyright (c) 2015 Huawei, Inc. and others. All rights reserved.
\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
8 package org.opendaylight.nemo.user.vnspacemanager.structurestyle.updateintent;
\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
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
49 * Created by z00293636 on 2015/8/31.
\r
51 public class UpdateConnection {
\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
58 public UpdateConnection(DataBroker dataBroker, TenantManage tenantManage){
\r
59 this.dataBroker = dataBroker;
\r
60 this.tenantManage = tenantManage;
\r
61 getDefinitions = new GetDefinitions(dataBroker);
\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
71 errorInfo = checkInstance(userId,connection);
\r
72 if (errorInfo != null){
\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
81 UserKey userKey = new UserKey(userId);
\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
88 public void onFailure(Throwable t) {
\r
89 LOG.error("Could not write endpoint base container", t);
\r
93 public void onSuccess(Void result) {
\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
108 if (user.getObjects()!=null){
\r
109 if (user.getObjects().getConnection()!=null){
\r
110 List<Connection> connectionList = tenantManage.getUser().getObjects().getConnection();
\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
118 if (!connection1.getConnectionName().equals(connection.getConnectionName())){
\r
119 errorInfo = "The End node should not be changed.";
\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
132 for (EndNode endNode : endnodeList){
\r
133 for (Node node : nodeList){
\r
134 if (endNode.getNodeId().equals(node.getNodeId())){
\r
139 errorInfo = "The endnode is not exist in user vn space.";
\r
145 errorInfo = "The endnodes are not exist in the user vn space.";
\r
150 if (connection.getEndNode() != null){
\r
151 errorInfo = "There are no nodes in user vn space.";
\r
156 if (connection.getEndNode() != null){
\r
157 errorInfo = "There are no nodes in user vn space.";
\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
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
175 errorInfo = "There are no properties for this type of connection.";
\r
177 else if (connectionProperties != null && propertyDefinitions != null)
\r
179 errorInfo = CheckProperty(connection.getProperty(), propertyDefinitions);
\r
183 return "This type of connection has not been defined.";
\r
189 private String CheckProperty(List<Property> connectionProperty, List<PropertyDefinition> propertyDefinitionList ){
\r
190 String errorInfo = null;
\r
191 for (Property property : connectionProperty)
\r
193 Boolean properyHasDefine = false;
\r
194 if (errorInfo != null)
\r
200 for (PropertyDefinition propertyDefinition : propertyDefinitionList)
\r
202 if (property.getPropertyName().equals(propertyDefinition.getPropertyName()))
\r
204 properyHasDefine = true;
\r
205 PropertyValues propertyValues = property.getPropertyValues();
\r
206 PropertyDefinition.PropertyValueType propertyValueType = propertyDefinition.getPropertyValueType();
\r
208 if (propertyValues != null && propertyValueType != null)
\r
210 if (propertyDefinition.getIsReadOnly()!=null
\r
211 && PropertyDefinition.IsReadOnly.ReadOnly == propertyDefinition.getIsReadOnly())
\r
213 if (propertyDefinition.getIsRequired().getIntValue() == 1)
\r
215 errorInfo = "The property value type of " + property.getPropertyName().toString() + " is read only.";
\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
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
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
237 if (!properyHasDefine) {
\r
238 errorInfo = "This type of property" + property.getPropertyName().toString() + " has not been defined.";
\r
243 if (errorInfo == null)
\r
245 Boolean requiredProperty = false;
\r
246 for (PropertyDefinition propertyDefinition : propertyDefinitionList)
\r
248 if (propertyDefinition.getIsRequired()!=null)
\r
250 if (propertyDefinition.getIsRequired().getIntValue() == 0)
\r
252 for (Property property : connectionProperty)
\r
254 if (property.getPropertyName().equals(propertyDefinition.getPropertyName()))
\r
256 requiredProperty = true;
\r
259 if (!requiredProperty)
\r
261 errorInfo = "The required property" + propertyDefinition.getPropertyName().toString() + "is not included in the intent.";
\r