This repository has been archived by the owner on Nov 2, 2023. It is now read-only.
/
nc_create_20.go
113 lines (98 loc) · 3.15 KB
/
nc_create_20.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
* ------------------------------------------------------------------
* 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.
*----------------------------------------------
*/
package main
import (
"flag"
"fmt"
"strconv"
"strings"
clockCfg "github.com/CiscoDevNet/ydk-go/ydk/models/cisco_ios_xr/infra_infra_clock_linux_cfg"
"github.com/CiscoDevNet/ydk-go/ydk/providers"
"github.com/CiscoDevNet/ydk-go/ydk/services"
"github.com/CiscoDevNet/ydk-go/ydk"
)
// configClock adds config data to clock object
func configClock(clock *clockCfg.Clock) {
// time zone configuration
clock.TimeZone.TimeZoneName = "PST"
clock.TimeZone.AreaName = "PST8PDT"
}
// main execute main program.
func main() {
defer func() {
if r := recover(); r != nil {
fmt.Println("\nError occured!!\n ", r)
}
}()
// args
vPtr := flag.Bool("v", false, "Enable verbose")
devicePtr := flag.String(
"device",
"",
"NETCONF device (ssh://user:password@host:port)")
flag.Parse()
// log debug messages if verbose argument specified
if *vPtr {
ydk.EnableLogging(ydk.Info)
}
if (*devicePtr == "") {
panic("Missing device arg see --help for details")
}
ydk.YLogDebug(*devicePtr)
denominators := []string{"://", ":", "@", ":"}
keys := []string {"protocol", "username", "password", "address", "port"}
device := make(map[string]string)
var split []string
unprocessed := *devicePtr
for i := 0; i < 4; i++ {
if (!strings.Contains(unprocessed, denominators[i])) {
panic(fmt.Sprintln("Device arg: device must be entered in",
"ssh://user:password@host:port format"))
}
split = strings.SplitN(unprocessed, denominators[i], 2)
device[keys[i]] = split[0]
unprocessed = split[1]
}
device[keys[4]] = unprocessed
port, err := strconv.Atoi(device["port"])
if (err != nil) {
panic("Device arg: port must be an int")
}
// create NETCONF provider
provider := providers.NetconfServiceProvider{
Address: device["address"],
Username: device["username"],
Password: device["password"],
Port: port,
Protocol: device["protocol"]}
provider.Connect()
// create CRUD service
service := services.CrudService{}
// read data from NETCONF device
clock := clockCfg.Clock{} // create object
configClock(&clock) // add object configuration
// create configuration on NETCONF device
service.Create(&provider, &clock)
}