Skip to content

Commit

Permalink
Created Go wrapper for NetconfSession, added Path API unit tests, Cis…
Browse files Browse the repository at this point in the history
  • Loading branch information
ygorelik committed Aug 7, 2021
1 parent 46900df commit ac4c694
Show file tree
Hide file tree
Showing 6 changed files with 227 additions and 62 deletions.
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
### 2021-08-30 version 0.8.5.3

#### New features and enhancements
* Create go wrapper for NetconfSession and other path APIs ([#779](https://github.com/CiscoDevNet/ydk-gen/issues/779))


### 2021-05-30 version 0.8.5.2

#### Resolved GitHub issues
Expand Down
140 changes: 113 additions & 27 deletions sdk/cpp/core/src/ydk.cpp
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
//////////////////////////////////////////////////////////////////
// @file ydk.cpp
//
// YANG Development Kit
// Copyright 2017 Cisco Systems. All rights reserved
//
////////////////////////////////////////////////////////////////
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//
//////////////////////////////////////////////////////////////////
/* ----------------------------------------------------------------
@file ydk.cpp
YDK - YANG Development Kit
Copyright 2017-2019 Cisco Systems. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-------------------------------------------------------------------
This file has been modified by Yan Gorelik, YDK Solutions.
All modifications in original under CiscoDevNet domain
introduced since October 2019 are copyrighted.
All rights reserved under Apache License, Version 2.0.
------------------------------------------------------------------*/

#include "crud_service.hpp"
#include "errors.hpp"
Expand Down Expand Up @@ -625,7 +623,7 @@ Session ServiceProviderGetSession(ServiceProvider provider)
return static_cast<void*>(&s);
}

RootSchemaNode SessionGetRootSchema(YDKStatePtr state, Session session)
RootSchemaWrapper SessionGetRootSchemaNode(YDKStatePtr state, Session session)
{
try {
ydk::path::Session * real_session = static_cast<ydk::path::Session*>(session);
Expand Down Expand Up @@ -899,7 +897,11 @@ const char* DataNodeGetSegmentPath(DataNode datanode)

void EnableLogging(LogLevel level)
{
auto console = spdlog::stdout_color_mt("ydk");
auto console = spdlog::get("ydk");
if (console == nullptr)
{
console = spdlog::stdout_color_mt("ydk");
}
switch(level)
{
case DEBUG:
Expand Down Expand Up @@ -974,3 +976,87 @@ void YLogError(const char* msg)
ydk::YLOG_ERROR(msg);
}

Session NetconfSessionInit(YDKStatePtr state, Repository repo,
const char * address, const char * username, const char * password, int port,
const char * protocol, boolean on_demand, boolean common_cache, int timeout,
const char * server_certificate, const char * private_key)
{
try
{
ydk::path::NetconfSession * real_session;
if (repo)
{
ydk::path::Repository* real_repo = static_cast<ydk::path::Repository*>(repo);
if (server_certificate && *server_certificate)
{
real_session = new ydk::path::NetconfSession(
*real_repo,
address, username,
private_key, server_certificate,
port, on_demand, timeout);
}
else
{
real_session = new ydk::path::NetconfSession(
*real_repo,
address, username, password, port,
protocol, on_demand, timeout);
}
}
else
{
if (server_certificate && *server_certificate)
{
real_session = new ydk::path::NetconfSession(
address, username,
private_key, server_certificate,
port, on_demand, common_cache, timeout);
}
else
{
real_session = new ydk::path::NetconfSession(
address, username, password, port,
protocol, on_demand, common_cache, timeout);
}
}
return static_cast<void*>(real_session);
}
catch(...)
{
YDKState* real_state = static_cast<YDKState*>(state);
handle_error(real_state);
return NULL;
}
}

void NetconfSessionFree(Session session)
{
ydk::path::NetconfSession * real_session = static_cast<ydk::path::NetconfSession*>(session);
if (real_session)
{
delete real_session;
}
}

DataNode SessionExecuteRpc(YDKStatePtr state, Session session, Rpc rpc)
{
try
{
RpcWrapper* rpc_wrapper = (RpcWrapper*)rpc;
ydk::path::Rpc* real_rpc = unwrap(rpc_wrapper);

ydk::path::Session * real_session = (ydk::path::Session *) session;
std::shared_ptr<ydk::path::DataNode> result = real_session->invoke(*real_rpc);

if (result)
return static_cast<void*>(wrap(result));
else
return nullptr;
}
catch(...)
{
YDKState* real_state = static_cast<YDKState*>(state);
handle_error(real_state);
return NULL;
}
}
59 changes: 32 additions & 27 deletions sdk/cpp/core/src/ydk.h
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
//////////////////////////////////////////////////////////////////
// @file ydk.h
//
// YANG Development Kit
// Copyright 2017 Cisco Systems. All rights reserved
//
////////////////////////////////////////////////////////////////
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//
//////////////////////////////////////////////////////////////////
/* ----------------------------------------------------------------
@file ydk.h
YDK - YANG Development Kit
Copyright 2017-2019 Cisco Systems. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-------------------------------------------------------------------
This file has been modified by Yan Gorelik, YDK Solutions.
All modifications in original under CiscoDevNet domain
introduced since October 2019 are copyrighted.
All rights reserved under Apache License, Version 2.0.
------------------------------------------------------------------*/

#ifndef _YDK_H_
#define _YDK_H_
Expand All @@ -42,7 +40,6 @@ typedef void* OpenDaylightServiceProvider;
typedef void* Capability;
typedef void* Repository;
typedef void* YDKStatePtr;

typedef void* Session;

typedef struct DataNodeChildren
Expand Down Expand Up @@ -120,11 +117,19 @@ RootSchemaNode ServiceProviderGetRootSchema(YDKStatePtr, ServiceProvider);
RootSchemaWrapper ServiceProviderGetRootSchemaNode(YDKStatePtr, ServiceProvider);
EncodingFormat ServiceProviderGetEncoding(ServiceProvider);
Session ServiceProviderGetSession(ServiceProvider);
RootSchemaNode SessionGetRootSchema(YDKStatePtr, Session);
RootSchemaWrapper SessionGetRootSchemaNode(YDKStatePtr, Session);
void NetconfServiceProviderFree(ServiceProvider);
int NetconfServiceProviderGetNumCapabilities(ServiceProvider);
const char* NetconfServiceProviderGetCapabilityByIndex(ServiceProvider, int);

Session NetconfSessionInit(YDKStatePtr state, Repository repo,
const char * address, const char * username, const char * password, int port,
const char * protocol, boolean on_demand, boolean common_cache, int timeout,
const char * server_certificate_path, const char * private_key_path);
void NetconfSessionFree(Session);

DataNode SessionExecuteRpc(YDKStatePtr state, Session session, Rpc rpc);

ServiceProvider RestconfServiceProviderInitWithRepo(YDKStatePtr state, Repository repo, const char * address, const char * username, const char * password, int port, EncodingFormat encoding, const char* config_url_root, const char* state_url_root);
void RestconfServiceProviderFree(ServiceProvider);

Expand Down
6 changes: 6 additions & 0 deletions sdk/go/CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
### 2021-08-30 version 0.8.5.3

#### New features and enhancements
* Create go wrapper for NetconfSession and other path APIs ([#779](https://github.com/CiscoDevNet/ydk-gen/issues/779))


### 2021-04-30 version 0.8.5.2

#### Resolved GitHub issues
Expand Down
14 changes: 9 additions & 5 deletions sdk/go/core/ydk/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import "fmt"
type LogLevel int

const (
Debug LogLevel = iota
Off LogLevel = iota
Debug
Info
Warning
Error
Expand All @@ -20,6 +21,9 @@ const (
// EnableLogging enables logging on the code that runs in C++
func EnableLogging(level LogLevel) {
switch level {
case Off:
C.EnableLogging(C.OFF)

case Debug:
C.EnableLogging(C.DEBUG)

Expand All @@ -36,20 +40,20 @@ func EnableLogging(level LogLevel) {

func YLogInfo(msg string){
msg = fmt.Sprintf("[Go] %s", msg)
C.YLogInfo(C.CString(msg));
C.YLogInfo(C.CString(msg));
}

func YLogDebug(msg string){
msg = fmt.Sprintf("[Go] %s", msg)
C.YLogDebug(C.CString(msg));
C.YLogDebug(C.CString(msg));
}

func YLogWarn(msg string){
msg = fmt.Sprintf("[Go] %s", msg)
C.YLogWarn(C.CString(msg));
C.YLogWarn(C.CString(msg));
}

func YLogError(msg string){
msg = fmt.Sprintf("[Go] %s", msg)
C.YLogError(C.CString(msg));
C.YLogError(C.CString(msg));
}
64 changes: 61 additions & 3 deletions sdk/go/core/ydk/path/path_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,36 @@
/* ----------------------------------------------------------------
YDK - YANG Development Kit
Copyright 2017-2019 Cisco Systems. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-------------------------------------------------------------------
This file has been modified by Yan Gorelik, YDK Solutions.
All modifications in original under CiscoDevNet domain
introduced since October 2019 are copyrighted.
All rights reserved under Apache License, Version 2.0.
------------------------------------------------------------------*/

package path

import (
"github.com/CiscoDevNet/ydk-go/ydk"
oc_bgp "github.com/CiscoDevNet/ydk-go/ydk/models/openconfig/bgp"
"github.com/CiscoDevNet/ydk-go/ydk/path"
"github.com/CiscoDevNet/ydk-go/ydk/types"
encoding "github.com/CiscoDevNet/ydk-go/ydk/types/encoding_format"
oc_bgp "github.com/CiscoDevNet/ydk-go/ydk/models/ydktest/openconfig_bgp"
"github.com/CiscoDevNet/ydk-go/ydk/providers"
"testing"
"runtime"
"path/filepath"
)

func TestExecuteRpc(t *testing.T) {
Expand All @@ -14,10 +39,43 @@ func TestExecuteRpc(t *testing.T) {
provider.Connect()

bgp := oc_bgp.Bgp{}
result := path.ExecuteRpc(&provider, &bgp, "ydk:create", "entity", false)
data := make(map[string]string)
result := provider.ExecuteRpc("create", &bgp, data)
if result.Private == nil {
t.Error("Operation failed")
} else {
provider.ExecuteRpc("delete", &bgp, data)
}

provider.Disconnect()
}

func TestNetconfSessionConnectNoRepo(t *testing.T) {
ydk.EnableLogging(ydk.Info)
session := NetconfSession{Address: "127.0.0.1", Username: "admin", Password: "admin", Port: 12022}
session.Connect()

rootSchema := session.GetRootSchemaNode()

bgp := CreateRootDataNode( rootSchema, "openconfig-bgp:bgp")
CreateDataNode( bgp, "global/config/as", 65172)
bgpCreatePayload := CodecEncode( bgp, encoding.XML, false)
ydk.YLogInfo("RPC Payload:\n" + bgpCreatePayload)

createRpc := CreateRpc( rootSchema, "ydk:create")
CreateDataNode( createRpc.Input, "entity", bgpCreatePayload)
session.ExecuteRpc(createRpc);

session.Disconnect()
}

func TestNetconfSessionConnectWithRepo(t *testing.T) {
ydk.EnableLogging(ydk.Debug)
_, callerFile, _, _ := runtime.Caller(0)
executablePath := filepath.Dir(callerFile)
repopath := executablePath + "/../../../../cpp/core/tests/models"
repo := types.Repository{Path: repopath}
session := NetconfSession{Repo: repo, Address: "127.0.0.1", Username: "admin", Password: "admin", Port: 12022}
session.Connect()
session.Disconnect()
}

0 comments on commit ac4c694

Please sign in to comment.