/**
   * Creates package name from specified <code>basePackageName</code> (package name for module) and
   * <code>schemaPath</code> which crosses an augmentation.
   *
   * <p>Resulting package name is concatenation of <code>basePackageName</code> and all local names
   * of YANG nodes which are parents of some node for which <code>schemaPath</code> is specified.
   *
   * @param basePackageName string with package name of the module, MUST be normalized, otherwise
   *     this method may return an invalid string.
   * @param schemaPath list of names of YANG nodes which are parents of some node + name of this
   *     node
   * @return string with valid JAVA package name
   * @throws NullPointerException if any of the arguments are null
   */
  public static String packageNameForAugmentedGeneratedType(
      final String basePackageName, final SchemaPath schemaPath) {
    final int size = Iterables.size(schemaPath.getPathTowardsRoot());
    if (size == 0) {
      return basePackageName;
    }

    return generateNormalizedPackageName(basePackageName, schemaPath.getPathFromRoot(), size);
  }
  @Override
  public NetconfMessage toRpcRequest(SchemaPath rpc, final NormalizedNode<?, ?> payload) {
    // In case no input for rpc is defined, we can simply construct the payload here
    final QName rpcQName = rpc.getLastComponent();
    Map<QName, RpcDefinition> currentMappedRpcs = mappedRpcs;

    // Determine whether a base netconf operation is being invoked and also check if the device
    // exposed model for base netconf
    // If no, use pre built base netconf operations model
    final boolean needToUseBaseCtx = mappedRpcs.get(rpcQName) == null && isBaseRpc(rpcQName);
    if (needToUseBaseCtx) {
      currentMappedRpcs = MAPPED_BASE_RPCS;
    }

    Preconditions.checkNotNull(
        currentMappedRpcs.get(rpcQName),
        "Unknown rpc %s, available rpcs: %s",
        rpcQName,
        currentMappedRpcs.keySet());
    if (currentMappedRpcs.get(rpcQName).getInput() == null) {
      return new NetconfMessage(
          prepareDomResultForRpcRequest(rpcQName).getNode().getOwnerDocument());
    }

    Preconditions.checkNotNull(
        payload, "Transforming an rpc with input: %s, payload cannot be null", rpcQName);
    Preconditions.checkArgument(
        payload instanceof ContainerNode,
        "Transforming an rpc with input: %s, payload has to be a container, but was: %s",
        rpcQName,
        payload);

    // Set the path to the input of rpc for the node stream writer
    rpc = rpc.createChild(QName.create(rpcQName, "input").intern());
    final DOMResult result = prepareDomResultForRpcRequest(rpcQName);

    try {
      // If the schema context for netconf device does not contain model for base netconf
      // operations, use default pre build context with just the base model
      // This way operations like lock/unlock are supported even if the source for base model was
      // not provided
      writeNormalizedRpc(
          ((ContainerNode) payload),
          result,
          rpc,
          needToUseBaseCtx ? BASE_NETCONF_CTX : schemaContext);
    } catch (final XMLStreamException | IOException | IllegalStateException e) {
      throw new IllegalStateException("Unable to serialize " + rpc, e);
    }

    final Document node = result.getNode().getOwnerDocument();

    return new NetconfMessage(node);
  }
 @Override
 public boolean equals(final Object obj) {
   if (this == obj) {
     return true;
   }
   if (obj == null) {
     return false;
   }
   if (getClass() != obj.getClass()) {
     return false;
   }
   OutputEffectiveStatementImpl other = (OutputEffectiveStatementImpl) obj;
   if (qname == null) {
     if (other.qname != null) {
       return false;
     }
   } else if (!qname.equals(other.qname)) {
     return false;
   }
   if (path == null) {
     if (other.path != null) {
       return false;
     }
   } else if (!path.equals(other.path)) {
     return false;
   }
   return true;
 }
  @Test
  public void canCreateEnumerationType() {
    MockitoAnnotations.initMocks(this);

    QName qName = QName.create("TestQName");
    SchemaPath schemaPath = SchemaPath.create(Collections.singletonList(qName), true);

    List<EnumTypeDefinition.EnumPair> listEnumPair = Collections.singletonList(enumPair);
    Optional<EnumTypeDefinition.EnumPair> defaultValue = Optional.of(enumPair);

    EnumerationType enumerationType =
        EnumerationType.create(schemaPath, listEnumPair, defaultValue);

    assertNotEquals("Description is not null", null, enumerationType.getDescription());
    assertEquals("QName", BaseTypes.ENUMERATION_QNAME, enumerationType.getQName());
    assertEquals("Should be empty string", "", enumerationType.getUnits());
    assertNotEquals("Description should not be null", null, enumerationType.toString());
    assertNotEquals("Reference is not null", null, enumerationType.getReference());
    assertEquals("BaseType should be null", null, enumerationType.getBaseType());
    assertEquals("Default value should be enumPair", enumPair, enumerationType.getDefaultValue());
    assertEquals("getPath should equal schemaPath", schemaPath, enumerationType.getPath());
    assertEquals("Status should be CURRENT", Status.CURRENT, enumerationType.getStatus());
    assertEquals(
        "Should be empty list", Collections.EMPTY_LIST, enumerationType.getUnknownSchemaNodes());
    assertEquals("Values should be [enumPair]", listEnumPair, enumerationType.getValues());

    assertEquals(
        "Hash code of enumerationType should be equal",
        enumerationType.hashCode(),
        enumerationType.hashCode());
    assertNotEquals("EnumerationType shouldn't equal to null", null, enumerationType);
    assertEquals("EnumerationType should equals to itself", enumerationType, enumerationType);
    assertNotEquals(
        "EnumerationType shouldn't equal to object of other type", "str", enumerationType);
  }
Beispiel #5
0
  @Test
  public void canCreateBitsType() {
    MockitoAnnotations.initMocks(this);

    QName qName = QName.create("TestQName");
    SchemaPath schemaPath = SchemaPath.create(Collections.singletonList(qName), true);
    List<BitsTypeDefinition.Bit> listBit = Collections.singletonList(bit);
    BitsType bitsType = BitsType.create(schemaPath, listBit);

    assertNotEquals("Description is not null", null, bitsType.getDescription());
    assertEquals("QName", BaseTypes.BITS_QNAME, bitsType.getQName());
    assertEquals("Should be empty string", "", bitsType.getUnits());
    assertNotEquals("Description should not be null", null, bitsType.toString());
    assertNotEquals("Reference is not null", null, bitsType.getReference());
    assertEquals("BaseType should be null", null, bitsType.getBaseType());
    assertEquals("Default value should be null", null, bitsType.getDefaultValue());
    assertEquals("getPath should equal schemaPath", schemaPath, bitsType.getPath());
    assertEquals("Status should be CURRENT", Status.CURRENT, bitsType.getStatus());
    assertEquals("Should be empty list", Collections.EMPTY_LIST, bitsType.getUnknownSchemaNodes());
    assertEquals("Values should be [enumPair]", listBit, bitsType.getBits());

    assertEquals("Hash code of bitsType should be equal", bitsType.hashCode(), bitsType.hashCode());
    assertNotEquals("bitsType shouldn't equal to null", null, bitsType);
    assertEquals("bitsType should equals to itself", bitsType, bitsType);
    assertNotEquals("bitsType shouldn't equal to object of other type", "str", bitsType);
  }
  /**
   * Creates package name from specified <code>basePackageName</code> (package name for module) and
   * <code>schemaPath</code>.
   *
   * <p>Resulting package name is concatenation of <code>basePackageName</code> and all local names
   * of YANG nodes which are parents of some node for which <code>schemaPath</code> is specified.
   *
   * @param basePackageName string with package name of the module
   * @param schemaPath list of names of YANG nodes which are parents of some node + name of this
   *     node
   * @param isUsesAugment boolean true if using augment
   * @return string with valid JAVA package name
   * @deprecated Use {@link #packageNameForGeneratedType(String, SchemaPath)} or {@link
   *     #packageNameForAugmentedGeneratedType(String, SchemaPath)} instead.
   */
  @Deprecated
  public static String packageNameForGeneratedType(
      final String basePackageName, final SchemaPath schemaPath, final boolean isUsesAugment) {
    if (basePackageName == null) {
      throw new IllegalArgumentException("Base Package Name cannot be NULL!");
    }
    if (schemaPath == null) {
      throw new IllegalArgumentException("Schema Path cannot be NULL!");
    }

    final Iterable<QName> iterable = schemaPath.getPathFromRoot();
    final int size = Iterables.size(iterable);
    final int traversalSteps;
    if (isUsesAugment) {
      traversalSteps = size;
    } else {
      traversalSteps = size - 1;
    }

    if (traversalSteps == 0) {
      return BindingMapping.normalizePackageName(basePackageName);
    }

    return generateNormalizedPackageName(basePackageName, iterable, traversalSteps);
  }
 @Override
 public int hashCode() {
   final int prime = 31;
   int result = 1;
   result = prime * result + ((qname == null) ? 0 : qname.hashCode());
   result = prime * result + ((path == null) ? 0 : path.hashCode());
   return result;
 }
Beispiel #8
0
 public static SchemaPath createPath(
     final boolean absolute,
     final URI namespace,
     final Date revision,
     final String prefix,
     final String... names) {
   List<QName> path = new ArrayList<>();
   for (String name : names) {
     path.add(QName.create(namespace, revision, name));
   }
   return SchemaPath.create(path, absolute);
 }
 /**
  * This method implements on Notification. When there is a notification received by listener, that
  * should be parsed for the subscription-id and then processed.
  */
 @Override
 public void onNotification(DOMNotification notification) {
   LOG.info("Notification recieved {}", notification.getBody());
   QName qname = PushUpdate.QNAME;
   SchemaPath schemaPath = SchemaPath.create(true, qname);
   if (notification.getType().equals(schemaPath)) {
     ContainerNode conNode = notification.getBody();
     // If the subscription-id of notification same as
     // subscription-id set for this object then proceed.
     if (conNode.getChild(subid).get().getValue().toString().equals(subscription_id)) {
       LOG.info("onPushUpdate schema..");
       try {
         pushUpdateHandlder(notification);
       } catch (Exception e) {
         LOG.warn(e.toString());
       }
     } else {
       LOG.info(
           "Expected subscription-id {}, got {}. Skipping the notification processing",
           this.subscription_id,
           conNode.getChild(subid).get().getValue().toString());
     }
   }
 }
public class EmptyEffectiveStatementImpl extends EffectiveStatementBase<String, TypeStatement>
    implements EmptyTypeDefinition {

  public static final String LOCAL_NAME = TypeUtils.EMPTY;
  private static final QName QNAME = QName.create(YangConstants.RFC6020_YANG_MODULE, LOCAL_NAME);
  private static final SchemaPath PATH = SchemaPath.create(true, QNAME);
  private static final String DESCRIPTION =
      "The empty built-in type represents a leaf that does not have any value, it conveys information by its presence or absence.";
  private static final String REFERENCE = "https://tools.ietf.org/html/rfc6020#page-131";

  public EmptyEffectiveStatementImpl(
      final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx) {
    super(ctx);
  }

  @Override
  public EmptyTypeDefinition getBaseType() {
    return null;
  }

  @Override
  public String getUnits() {
    return null;
  }

  @Override
  public Object getDefaultValue() {
    return null;
  }

  @Override
  public QName getQName() {
    return QNAME;
  }

  @Override
  public SchemaPath getPath() {
    return PATH;
  }

  @Override
  public List<UnknownSchemaNode> getUnknownSchemaNodes() {
    return Collections.emptyList();
  }

  @Override
  public String getDescription() {
    return DESCRIPTION;
  }

  @Override
  public String getReference() {
    return REFERENCE;
  }

  @Override
  public Status getStatus() {
    return Status.CURRENT;
  }

  @Override
  public String toString() {
    return "type empty " + QNAME;
  }
}
  @Override
  public synchronized DOMRpcResult toRpcResult(final NetconfMessage message, final SchemaPath rpc) {
    final NormalizedNode<?, ?> normalizedNode;
    final QName rpcQName = rpc.getLastComponent();
    if (NetconfMessageTransformUtil.isDataRetrievalOperation(rpcQName)) {
      final Element xmlData = NetconfMessageTransformUtil.getDataSubtree(message.getDocument());
      final ContainerSchemaNode schemaForDataRead =
          NetconfMessageTransformUtil.createSchemaForDataRead(schemaContext);
      final ContainerNode dataNode;

      try {
        dataNode =
            parserFactory
                .getContainerNodeParser()
                .parse(Collections.singleton(xmlData), schemaForDataRead);
      } catch (IllegalArgumentException e) {
        throw new IllegalArgumentException(
            String.format("Failed to parse data response %s", xmlData), e);
      }

      normalizedNode =
          Builders.containerBuilder()
              .withNodeIdentifier(
                  new YangInstanceIdentifier.NodeIdentifier(
                      NetconfMessageTransformUtil.NETCONF_RPC_REPLY_QNAME))
              .withChild(dataNode)
              .build();
    } else {

      Map<QName, RpcDefinition> currentMappedRpcs = mappedRpcs;

      // Determine whether a base netconf operation is being invoked and also check if the device
      // exposed model for base netconf
      // If no, use pre built base netconf operations model
      final boolean needToUseBaseCtx = mappedRpcs.get(rpcQName) == null && isBaseRpc(rpcQName);
      if (needToUseBaseCtx) {
        currentMappedRpcs = MAPPED_BASE_RPCS;
      }

      final RpcDefinition rpcDefinition = currentMappedRpcs.get(rpcQName);
      Preconditions.checkArgument(
          rpcDefinition != null, "Unable to parse response of %s, the rpc is unknown", rpcQName);

      // In case no input for rpc is defined, we can simply construct the payload here
      if (rpcDefinition.getOutput() == null) {
        Preconditions.checkArgument(
            XmlElement.fromDomDocument(message.getDocument())
                .getOnlyChildElementWithSameNamespaceOptionally("ok")
                .isPresent(),
            "Unexpected content in response of rpc: %s, %s",
            rpcDefinition.getQName(),
            message);
        normalizedNode = null;
      } else {
        final Element element = message.getDocument().getDocumentElement();
        try {
          normalizedNode =
              parserFactory
                  .getContainerNodeParser()
                  .parse(Collections.singleton(element), rpcDefinition.getOutput());
        } catch (IllegalArgumentException e) {
          throw new IllegalArgumentException(
              String.format("Failed to parse RPC response %s", element), e);
        }
      }
    }
    return new DefaultDOMRpcResult(normalizedNode);
  }
Beispiel #12
0
/**
 * The <code>default</code> implementation of Boolean Type Definition interface.
 *
 * @see BooleanTypeDefinition
 * @deprecated Use {@link org.opendaylight.yangtools.yang.model.util.type.BaseTypes#booleanType()}
 *     instead
 */
@Deprecated
public final class BooleanType implements BooleanTypeDefinition {
  private static final BooleanType INSTANCE = new BooleanType();
  private static final SchemaPath PATH = SchemaPath.create(true, BaseTypes.BOOLEAN_QNAME);
  private static final String DESCRIPTION = "The boolean built-in type represents a boolean value.";
  private static final String REFERENCE = "https://tools.ietf.org/html/rfc6020#section-9.5";
  private static final String UNITS = "";

  /** Default constructor with default value set to "false". */
  private BooleanType() {}

  /**
   * Returns default instance of boolean built-in type.
   *
   * @return default instance of boolean built-in type.
   */
  public static BooleanType getInstance() {
    return INSTANCE;
  }

  /*
   * (non-Javadoc)
   *
   * @see
   * org.opendaylight.yangtools.yang.model.api.TypeDefinition#getBaseType()
   */
  @Override
  public BooleanTypeDefinition getBaseType() {
    return null;
  }

  /*
   * (non-Javadoc)
   *
   * @see org.opendaylight.yangtools.yang.model.api.TypeDefinition#getUnits()
   */
  @Override
  public String getUnits() {
    return UNITS;
  }

  /*
   * (non-Javadoc)
   *
   * @see
   * org.opendaylight.yangtools.yang.model.api.TypeDefinition#getDefaultValue
   * ()
   */
  @Override
  public Object getDefaultValue() {
    return null;
  }

  /*
   * (non-Javadoc)
   *
   * @see org.opendaylight.yangtools.yang.model.api.SchemaNode#getQName()
   */
  @Override
  public QName getQName() {
    return BaseTypes.BOOLEAN_QNAME;
  }

  /*
   * (non-Javadoc)
   *
   * @see org.opendaylight.yangtools.yang.model.api.SchemaNode#getPath()
   */
  @Override
  public SchemaPath getPath() {
    return PATH;
  }

  /*
   * (non-Javadoc)
   *
   * @see
   * org.opendaylight.yangtools.yang.model.api.SchemaNode#getDescription()
   */
  @Override
  public String getDescription() {
    return DESCRIPTION;
  }

  /*
   * (non-Javadoc)
   *
   * @see org.opendaylight.yangtools.yang.model.api.SchemaNode#getReference()
   */
  @Override
  public String getReference() {
    return REFERENCE;
  }

  /*
   * (non-Javadoc)
   *
   * @see org.opendaylight.yangtools.yang.model.api.SchemaNode#getStatus()
   */
  @Override
  public Status getStatus() {
    return Status.CURRENT;
  }

  @Override
  public List<UnknownSchemaNode> getUnknownSchemaNodes() {
    return Collections.emptyList();
  }

  @Override
  public String toString() {
    StringBuilder builder = new StringBuilder();
    builder.append("BooleanType [name=");
    builder.append(BaseTypes.BOOLEAN_QNAME);
    builder.append(", path=");
    builder.append(PATH);
    builder.append("]");
    return builder.toString();
  }
}