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.languagestyle.updateintentlang;
\r
10 import org.opendaylight.nemo.user.tenantmanager.TenantManage;
\r
11 import org.opendaylight.nemo.user.vnspacemanager.languagestyle.NEMOConstants;
\r
12 import org.opendaylight.nemo.user.vnspacemanager.structurestyle.updateintent.UpdateConnection;
\r
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.common.rev151010.*;
\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.intent.rev151010.user.intent.objects.ConnectionBuilder;
\r
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.objects.ConnectionKey;
\r
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.objects.Node;
\r
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.users.User;
\r
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.connection.instance.*;
\r
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.property.instance.PropertyValuesBuilder;
\r
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.property.instance.property.values.*;
\r
22 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
\r
28 * Created by z00293636 on 2015/11/5.
\r
30 public class UpdateConnectionLang {
\r
31 private TenantManage tenantManage;
\r
32 private Connection connection;
\r
33 private UpdateConnection updateConnection;
\r
35 public UpdateConnectionLang(DataBroker dataBroker, TenantManage tenantManage){
\r
36 this.tenantManage = tenantManage;
\r
37 updateConnection = new UpdateConnection(dataBroker,tenantManage);
\r
40 public String ConnectionHandling(UserId userId,String connectionname, String connectiontype, List<String> endnodes, LinkedHashMap<String, LinkedHashMap<String,String>> propertyList){
\r
41 String errorInfo = null;
\r
42 if (!propertyList.isEmpty()){
\r
43 errorInfo = checkProperty(propertyList);
\r
45 if (errorInfo==null){
\r
46 errorInfo = createConnection(userId,connectionname,connectiontype,endnodes,propertyList);
\r
47 if (errorInfo == null){
\r
48 return updateConnection.ConnectionHandling(userId,this.connection);
\r
54 private String checkProperty(LinkedHashMap<String, LinkedHashMap<String,String>> propertyList){
\r
55 String errorInfo = null;
\r
56 for (String propertyName : propertyList.keySet()){
\r
57 LinkedHashMap<String, String> propertyValues = propertyList.get(propertyName);
\r
58 Iterator<String> iterator = propertyValues.values().iterator();
\r
59 String valueType = iterator.next();
\r
61 while (iterator.hasNext()){
\r
62 if (valueType.equals(NEMOConstants.range)) {
\r
63 errorInfo = "The property should just has one range value.";
\r
66 else if (valueType.equals(NEMOConstants.string)){
\r
67 if (!iterator.next().equals(NEMOConstants.string)){
\r
68 return "The property " + propertyValues.get(iterator.next()) + " should be string.";
\r
71 else if (valueType.equals(NEMOConstants.integer)){
\r
72 if (!iterator.next().equals(NEMOConstants.integer)){
\r
73 return "The property " + propertyValues.get(iterator.next()) + " should be int.";
\r
81 private String createConnection(UserId userId, String connectionname, String connectiontype, List<String> endnodes, LinkedHashMap<String, LinkedHashMap<String,String>> propertyList){
\r
82 String errorInfo = null;
\r
83 tenantManage.fetchVNSpace(userId);
\r
84 User user = tenantManage.getUser();
\r
85 List<Connection> connectionList = new ArrayList<Connection>();
\r
86 List<Node> nodeList = new ArrayList<Node>();
\r
87 Boolean ConnExist = false;
\r
89 if (user.getObjects()!=null){
\r
90 if (user.getObjects().getNode()!=null){
\r
91 nodeList = user.getObjects().getNode();
\r
93 if (user.getObjects().getConnection()!=null){
\r
94 connectionList = user.getObjects().getConnection();
\r
97 ConnectionBuilder connectionBuilder = new ConnectionBuilder();
\r
98 if (!connectionList.isEmpty()){
\r
99 for (Connection connection1:connectionList) {
\r
100 if (connection1.getConnectionName().getValue().equals(connectionname)){
\r
102 connectionBuilder.setKey(connection1.getKey());
\r
103 connectionBuilder.setConnectionId(connection1.getConnectionId());
\r
108 ConnectionId connectionId = new ConnectionId(UUID.randomUUID().toString());
\r
109 connectionBuilder.setKey(new ConnectionKey(connectionId));
\r
110 connectionBuilder.setConnectionId(connectionId);
\r
113 connectionBuilder.setConnectionName(new ConnectionName(connectionname));
\r
114 connectionBuilder.setConnectionType(new ConnectionType(connectiontype));
\r
116 if (!endnodes.isEmpty() && errorInfo==null){
\r
117 List<EndNode> endNodeList = new ArrayList<EndNode>();
\r
119 if (nodeList.isEmpty()){
\r
120 errorInfo = "The EndNode is not exist in the user vn space.";
\r
125 for (String endnode : endnodes){
\r
126 Boolean endnodeexist = false;
\r
127 for (Node node : nodeList) {
\r
128 if (node.getNodeName().getValue().equals(endnode)){
\r
129 endnodeexist = true;
\r
130 EndNodeBuilder endNodeBuilder = new EndNodeBuilder();
\r
131 endNodeBuilder.setKey(new EndNodeKey(node.getNodeId()));
\r
132 endNodeBuilder.setNodeId(node.getNodeId());
\r
133 endNodeBuilder.setOrder((long) order);
\r
134 endNodeList.add(endNodeBuilder.build());
\r
137 if (!endnodeexist) {
\r
138 errorInfo = "The EndNode is not exist in the user vn space.";
\r
144 connectionBuilder.setEndNode(endNodeList);
\r
147 if (!propertyList.isEmpty() && errorInfo==null){
\r
148 PropertyBuilder propertyBuilder = new PropertyBuilder();
\r
149 List<Property> connectionproperty = new ArrayList<Property>();
\r
150 for (String propertyname : propertyList.keySet()){
\r
151 propertyBuilder.setKey(new PropertyKey(new PropertyName(propertyname)));
\r
152 propertyBuilder.setPropertyName(new PropertyName(propertyname));
\r
154 PropertyValuesBuilder propertyValuesBuilder = new PropertyValuesBuilder();
\r
155 LinkedHashMap<String, String> valuetype = propertyList.get(propertyname);
\r
156 List<IntValue> intValueList = new ArrayList<IntValue>();
\r
157 List<StringValue> stringValueList = new ArrayList<StringValue>();
\r
158 RangeValue Rangevalue = null;
\r
160 for (String value : valuetype.keySet()){
\r
161 if (valuetype.get(value).equals(NEMOConstants.string)){
\r
162 StringValueBuilder stringValueBuilder = new StringValueBuilder();
\r
163 stringValueBuilder.setKey(new StringValueKey((long)order,value));
\r
164 stringValueBuilder.setValue(value);
\r
165 stringValueBuilder.setOrder((long)order);
\r
166 stringValueList.add(stringValueBuilder.build());
\r
168 if (valuetype.get(value).equals(NEMOConstants.integer)){
\r
169 IntValueBuilder intValueBuilder = new IntValueBuilder();
\r
170 intValueBuilder.setKey(new IntValueKey((long)order,Long.parseLong(value)));
\r
171 intValueBuilder.setValue(Long.parseLong(value));
\r
172 intValueBuilder.setOrder((long)order);
\r
173 intValueList.add(intValueBuilder.build());
\r
175 if (valuetype.get(value).equals(NEMOConstants.range)){
\r
176 String[] rangevalue = new String[2];
\r
177 rangevalue = value.split(",");
\r
178 RangeValueBuilder rangeValueBuilder = new RangeValueBuilder();
\r
179 if (Long.parseLong(rangevalue[0])<Long.parseLong(rangevalue[1])){
\r
180 rangeValueBuilder.setMin(Long.parseLong(rangevalue[0]));
\r
181 rangeValueBuilder.setMax(Long.parseLong(rangevalue[1]));
\r
184 rangeValueBuilder.setMin(Long.parseLong(rangevalue[1]));
\r
185 rangeValueBuilder.setMax(Long.parseLong(rangevalue[0]));
\r
187 Rangevalue = rangeValueBuilder.build();
\r
188 propertyValuesBuilder.setRangeValue(Rangevalue);
\r
192 if (intValueList.isEmpty()){
\r
193 intValueList = null;
\r
194 propertyValuesBuilder.setIntValue(intValueList);
\r
196 if (stringValueList.isEmpty()){
\r
197 stringValueList = null;
\r
198 propertyValuesBuilder.setStringValue(stringValueList);
\r
200 propertyValuesBuilder.setIntValue(intValueList).setStringValue(stringValueList).setRangeValue(Rangevalue);
\r
201 propertyBuilder.setPropertyValues(propertyValuesBuilder.build());
\r
202 connectionproperty.add(propertyBuilder.build());
\r
204 connectionBuilder.setProperty(connectionproperty);
\r
206 this.connection = connectionBuilder.build();
\r