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.syntaxcheck;
\r
10 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
\r
11 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.common.rev151010.PropertyName;
\r
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.objects.Connection;
\r
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.ConnectionDefinitions;
\r
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.connection.definitions.ConnectionDefinition;
\r
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.connection.instance.Property;
\r
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.property.definitions.PropertyDefinition;
\r
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.property.instance.PropertyValues;
\r
18 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
\r
19 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
\r
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
\r
21 import org.opendaylight.yangtools.yang.common.RpcResult;
\r
22 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
\r
23 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
\r
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
\r
25 import com.google.common.base.Optional;
\r
26 import com.google.common.base.Function;
\r
27 import com.google.common.util.concurrent.CheckedFuture;
\r
28 import com.google.common.util.concurrent.FutureCallback;
\r
29 import com.google.common.util.concurrent.Futures;
\r
30 import com.google.common.util.concurrent.ListenableFuture;
\r
32 import org.slf4j.Logger;
\r
33 import org.slf4j.LoggerFactory;
\r
34 import java.util.List;
\r
37 * Created by z00293636 on 2015/9/2.
\r
39 public class ConnectionDefinitionCheck {
\r
41 private DataBroker dataBroker;
\r
42 private List<ConnectionDefinition> connectionDefinitionList;
\r
43 private static final Logger LOG = LoggerFactory.getLogger(ConnectionDefinitionCheck.class);
\r
45 public ConnectionDefinitionCheck(DataBroker dataBroker)
\r
47 this.dataBroker = dataBroker;
\r
48 connectionDefinitionList = null;
\r
51 public String CheckConnectionDefinition(Connection connection)
\r
53 fetchConnectionDefinitionList();
\r
54 boolean ConnectionHasDefined = false;
\r
55 String errorInfo = null;
\r
57 if (connectionDefinitionList != null)
\r
59 for (ConnectionDefinition connectionDefinition : connectionDefinitionList)
\r
61 if (connectionDefinition.getConnectionType().equals(connection.getConnectionType()))
\r
63 ConnectionHasDefined = true;
\r
64 List<Property> connectionProperties = connection.getProperty();
\r
65 List<PropertyDefinition> propertyDefinitions = connectionDefinition.getPropertyDefinition();
\r
67 if (connectionProperties != null && propertyDefinitions ==null)
\r
69 errorInfo = "There are no properties for this type of connection.";
\r
72 else if (connectionProperties != null && propertyDefinitions != null)
\r
74 errorInfo = CheckProperty(connection.getProperty(), connectionDefinition.getPropertyDefinition());
\r
75 if (errorInfo != null)
\r
85 if (!ConnectionHasDefined)
\r
87 return "This type of connection has not been defined.";
\r
92 private String CheckProperty(List<Property> connectionProperty, List<PropertyDefinition> propertyDefinitionList )
\r
94 String errorInfo = null;
\r
95 for (Property property : connectionProperty)
\r
97 Boolean properyHasDefine = false;
\r
98 if (errorInfo != null)
\r
104 for (PropertyDefinition propertyDefinition : propertyDefinitionList)
\r
106 if (property.getPropertyName().equals(propertyDefinition.getPropertyName()))
\r
108 properyHasDefine = true;
\r
109 PropertyValues propertyValues = property.getPropertyValues();
\r
110 PropertyDefinition.PropertyValueType propertyValueType = propertyDefinition.getPropertyValueType();
\r
112 if (propertyValues != null && propertyValueType != null)
\r
114 if (propertyDefinition.getIsReadOnly()!=null)
\r
116 if (propertyDefinition.getIsRequired().getIntValue() == 1)
\r
118 errorInfo = "The property value type of" + property.getPropertyName().toString() + "is read only.";
\r
124 if (propertyValueType.getIntValue() == 0 && !(propertyValues.getIntValue() == null && propertyValues.getStringValue() != null && propertyValues.getRangeValue() == null)) {
\r
125 errorInfo = "The property value type of" + property.getPropertyName().toString() + " should be string.";
\r
128 if (propertyValueType.getIntValue() == 1 && !(propertyValues.getIntValue() != null && propertyValues.getStringValue() == null && propertyValues.getRangeValue() == null)) {
\r
129 errorInfo = "The property value type of" + property.getPropertyName().toString() + " should be integer.";
\r
132 if (propertyValueType.getIntValue() == 2 && !(propertyValues.getIntValue() == null && propertyValues.getStringValue() == null && propertyValues.getRangeValue() != null)) {
\r
133 errorInfo = "The property value type of" + property.getPropertyName().toString() + " should be range.";
\r
140 if (!properyHasDefine) {
\r
141 errorInfo = "This type of property" + property.getPropertyName().toString() + " has not been defined.";
\r
146 if (errorInfo == null)
\r
148 Boolean requiredProperty = false;
\r
149 for (PropertyDefinition propertyDefinition : propertyDefinitionList)
\r
151 if (propertyDefinition.getIsRequired()!=null)
\r
153 if (propertyDefinition.getIsRequired().getIntValue() == 0)
\r
155 for (Property property : connectionProperty)
\r
157 if (property.getPropertyName().equals(propertyDefinition.getPropertyName()))
\r
159 requiredProperty = true;
\r
162 if (!requiredProperty)
\r
164 errorInfo = "The required property" + propertyDefinition.getPropertyName().toString() + "is not included in the intent.";
\r
173 private void fetchConnectionDefinitionList()
\r
175 InstanceIdentifier<ConnectionDefinitions> connectiondefinitionId = InstanceIdentifier.builder(ConnectionDefinitions.class).build();
\r
176 ListenableFuture<Optional<ConnectionDefinitions>> connectiondefinitionFuture = dataBroker.newReadOnlyTransaction().read(LogicalDatastoreType.CONFIGURATION, connectiondefinitionId);
\r
177 Futures.addCallback(connectiondefinitionFuture, new FutureCallback<Optional<ConnectionDefinitions>>() {
\r
179 public void onSuccess(Optional<ConnectionDefinitions> result) {
\r
180 setConnectionDefinitionList(result.get().getConnectionDefinition());
\r
185 public void onFailure(Throwable t) {
\r
186 LOG.error("Can not read connection definition information.", t);
\r
194 private void setConnectionDefinitionList(List<ConnectionDefinition> connectionDefinitionList)
\r
196 this.connectionDefinitionList = connectionDefinitionList;
\r