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.nemo.user.vnspacemanager.languagestyle.NEMOConstants;
\r
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.common.rev151010.NodeType;
\r
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.common.rev151010.UserId;
\r
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.Users;
\r
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.Objects;
\r
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.objects.Node;
\r
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.objects.NodeBuilder;
\r
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.objects.NodeKey;
\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.NodeDefinitions;
\r
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.node.definitions.NodeDefinition;
\r
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.node.instance.Property;
\r
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.property.definitions.PropertyDefinition;
\r
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.property.instance.PropertyValues;
\r
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.property.instance.property.values.StringValue;
\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 UpdateNode {
\r
53 private DataBroker dataBroker;
\r
54 private TenantManage tenantManage;
\r
55 private GetDefinitions getDefinitions;
\r
56 private ValueCheck valueCheck;
\r
57 private static final Logger LOG = LoggerFactory.getLogger(UpdateNode.class);
\r
59 public UpdateNode(DataBroker dataBroker,TenantManage tenantManage){
\r
60 this.dataBroker = dataBroker;
\r
61 this.tenantManage = tenantManage;
\r
62 getDefinitions = new GetDefinitions(dataBroker);
\r
63 valueCheck = new ValueCheck();
\r
67 public String NodeHandling(UserId userId,Node node){
\r
68 String errorInfo = null;
\r
69 errorInfo = checkDefinition(node);
\r
70 if (errorInfo !=null){
\r
74 errorInfo = checkInstance(userId, node);
\r
75 if (errorInfo!=null){
\r
79 WriteTransaction t = dataBroker.newWriteOnlyTransaction();
\r
80 if (userId != null && node.getNodeId() !=null){
\r
81 Node node1 = new NodeBuilder(node).build();
\r
82 NodeKey nodeKey = new NodeKey(node.getKey());
\r
83 UserKey userKey = new UserKey(userId);
\r
85 InstanceIdentifier<Node> nodeid = InstanceIdentifier.builder(Users.class).child(User.class, userKey).child(Objects.class).child(Node.class,nodeKey).build();
\r
86 t.put(LogicalDatastoreType.CONFIGURATION, nodeid, node1,true);
\r
87 CheckedFuture<Void, TransactionCommitFailedException> f = t.submit();
\r
88 Futures.addCallback(f, new FutureCallback<Void>() {
\r
90 public void onFailure(Throwable t) {
\r
91 LOG.error("Could not write endpoint base container", t);
\r
95 public void onSuccess(Void result) {
\r
104 private String checkInstance(UserId userId, Node node){
\r
105 String errorInfo = null;
\r
106 tenantManage.fetchVNSpace(userId);
\r
107 User user = tenantManage.getUser();
\r
110 if (user.getObjects()!=null){
\r
111 if (user.getObjects().getNode()!=null){
\r
112 List<Node> nodeList = user.getObjects().getNode();
\r
113 for (Node node1 : nodeList){
\r
114 if (node1.getNodeId().equals(node.getNodeId())){
\r
115 if ( !node1.getNodeName().equals(node.getNodeName())) {
\r
116 errorInfo = "The node name should not be changed.";
\r
119 if (!node1.getNodeType().equals(node.getNodeType())){
\r
120 errorInfo = "The node type should not be changed.";
\r
131 private String checkDefinition(Node node){
\r
132 String errorInfo = null;
\r
133 final Map<NodeType, NodeDefinition> map = getDefinitions.getNodeDefinition();
\r
134 if (map.isEmpty()){
\r
135 return "This type of Node has not been defined.";
\r
138 if (map.containsKey(node.getNodeType())){
\r
139 List<Property> nodeProperties = node.getProperty();
\r
140 List<PropertyDefinition> nodePropertyDefinitions = map.get(node.getNodeType()).getPropertyDefinition();
\r
142 if (nodeProperties != null && nodePropertyDefinitions == null){
\r
143 errorInfo = "This type of node has no properties.";
\r
145 else if (nodeProperties != null && nodePropertyDefinitions != null){
\r
146 errorInfo = checkProperty(nodeProperties, nodePropertyDefinitions);
\r
147 if (errorInfo != null){
\r
150 errorInfo = checkPredefine(nodeProperties);
\r
155 return "This type of Node has not been defined.";
\r
161 private String checkProperty(List<Property> nodeProperties, List<PropertyDefinition> nodePropertyDefinitions){
\r
162 Boolean propertyHasDefine = false;
\r
163 String errorInfo = null;
\r
165 for (Property property : nodeProperties){
\r
166 if (errorInfo != null){
\r
170 for (PropertyDefinition propertydefinition : nodePropertyDefinitions){
\r
171 if (property.getPropertyName().equals(propertydefinition.getPropertyName())){
\r
172 propertyHasDefine = true;
\r
173 PropertyValues propertyValues = property.getPropertyValues();
\r
174 PropertyDefinition.PropertyValueType propertyValueType = propertydefinition.getPropertyValueType();
\r
176 if (propertyValues != null && propertyValueType != null){
\r
177 if (propertydefinition.getIsReadOnly()!=null
\r
178 && PropertyDefinition.IsReadOnly.ReadOnly == propertydefinition.getIsReadOnly()){
\r
179 if (propertydefinition.getIsRequired().getIntValue() == 1){
\r
180 errorInfo = "The property "+ property.getPropertyName().getValue()+" is readonly, can not be written.";
\r
184 else if (propertyValueType.getIntValue() == 0 && !(propertyValues.getIntValue() == null
\r
185 && propertyValues.getStringValue() != null && propertyValues.getRangeValue() == null)){
\r
186 errorInfo = "The property value type "+property.getPropertyName().getValue()+" should be string";
\r
189 else if (propertyValueType.getIntValue() == 1 && !(propertyValues.getIntValue() != null
\r
190 && propertyValues.getStringValue() == null && propertyValues.getRangeValue() == null)){
\r
191 errorInfo = "The property value type "+property.getPropertyName().getValue()+" should be integer";
\r
194 else if (propertyValueType.getIntValue() == 2 && !(propertyValues.getIntValue() == null
\r
195 && propertyValues.getStringValue() == null && propertyValues.getRangeValue() != null)){
\r
196 errorInfo = "The property value type "+property.getPropertyName().getValue()+" should be range";
\r
203 if (!propertyHasDefine){
\r
204 errorInfo = "The property "+property.getPropertyName().getValue()+" has not been defined.";
\r
209 if (errorInfo == null){
\r
210 Boolean requiredProperty = false;
\r
211 for (PropertyDefinition propertyDefinition : nodePropertyDefinitions){
\r
212 if (propertyDefinition.getIsRequired()!=null){
\r
213 if (propertyDefinition.getIsRequired().getIntValue() ==0){
\r
214 for (Property property: nodeProperties){
\r
215 if (property.getPropertyName().equals(propertyDefinition.getPropertyName())){
\r
216 requiredProperty = true;
\r
219 if (!requiredProperty){
\r
220 errorInfo = "The required property "+ propertyDefinition.getPropertyName().getValue() + "is not included in the intent.";
\r
230 private String checkPredefine(List<Property> nodeProperties){
\r
231 String errorInfo = null;
\r
232 for (Property property : nodeProperties){
\r
233 if (property.getPropertyName().getValue().equals(NEMOConstants.mac_address)){
\r
234 List<StringValue> stringValues = property.getPropertyValues().getStringValue();
\r
235 Boolean legalValue = true;
\r
236 for (StringValue stringValue : stringValues){
\r
237 legalValue = valueCheck.checkMac(stringValue.getValue());
\r
240 errorInfo = "The " + NEMOConstants.mac_address + " is not legal.";
\r
243 if (property.getPropertyName().getValue().equals(NEMOConstants.ip_address)){
\r
244 List<StringValue> stringValues = property.getPropertyValues().getStringValue();
\r
245 Boolean legalValue = true;
\r
246 for (StringValue stringValue : stringValues){
\r
247 if (stringValue.getValue().contains("/")){
\r
248 legalValue = valueCheck.checkIpPrefix(stringValue.getValue());
\r
251 legalValue = valueCheck.checkIpAddress(stringValue.getValue());
\r
255 errorInfo = "The " + NEMOConstants.ip_address + " is not legal.";
\r
258 if (property.getPropertyName().getValue().equals(NEMOConstants.gateway_ip)){
\r
259 List<StringValue> stringValues = property.getPropertyValues().getStringValue();
\r
260 Boolean legalValue = true;
\r
261 for (StringValue stringValue : stringValues){
\r
262 legalValue = valueCheck.checkIpAddress(stringValue.getValue());
\r
265 errorInfo = "The " + NEMOConstants.gateway_ip + " is not legal.";
\r
268 if (property.getPropertyName().getValue().equals(NEMOConstants.ac_info_network)){
\r
269 List<StringValue> stringValues = property.getPropertyValues().getStringValue();
\r
270 Boolean legalValue = true;
\r
271 for (StringValue stringValue : stringValues){
\r
272 if (!(stringValue.getValue().equals(NEMOConstants.layer2)||stringValue.getValue().equals(NEMOConstants.layer3))){
\r
273 errorInfo = "The " + NEMOConstants.ac_info_network + " is not legal.";
\r
277 if (property.getPropertyName().getValue().equals(NEMOConstants.operating_mode)){
\r
278 List<StringValue> stringValues = property.getPropertyValues().getStringValue();
\r
279 Boolean legalValue = true;
\r
280 for (StringValue stringValue : stringValues){
\r
281 if (!(stringValue.getValue().equals(NEMOConstants.layer2)||stringValue.getValue().equals(NEMOConstants.layer3))){
\r
282 errorInfo = "The " + NEMOConstants.operating_mode + " is not legal.";
\r