blob: c83cc4d939c61dfb7d142c0d0ecaad0838bf13d1 [file] [log] [blame]
donNewtonAlpha1d2d6812018-09-14 16:00:02 -04001/*
2 Copyright 2017 the original author or authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15*/
16package tosca
17
18import (
19 "fmt"
20 "net"
21 "strings"
22
23 yaml "gopkg.in/yaml.v2"
24)
25
26var ontTemplate = `tosca_definitions_version: tosca_simple_yaml_1_0
27imports:
28 - custom_types/attworkflowdriverwhitelistentry.yaml
29 - custom_types/attworkflowdriverservice.yaml
30description: Create an entry in the whitelist
31topology_template:
32 node_templates:
33 service#att:
34 type: tosca.nodes.AttWorkflowDriverService
35 properties:
36 name: att-workflow-driver
37 must-exist: true
38 ont:
39 type: tosca.nodes.AttWorkflowDriverWhiteListEntry
40 properties:
41 serial_number: ALPHe3d1cf57
42 pon_port_id: 536870912
43 device_id: of:000000000a4001ce
44 requirements:
45 - owner:
46 node: service#att
47 relationship: tosca.relationships.BelongsToOne`
48
49type OntProvision struct {
50 ToscaDefinitionsVersion string `yaml:"tosca_definitions_version"`
51 Imports []string `yaml:"imports"`
52 Description string `yaml:"description"`
53 TopologyTemplate struct {
54 NodeTemplates struct {
55 ServiceATT struct {
56 Type string `yaml:"type"`
57 Properties struct {
58 Name string `yaml:"name"`
59 MustExist bool `yaml:"must-exist"`
60 } `yaml:"properties"`
61 } `yaml:"service#att"`
62 Ont struct {
63 DeviceType string `yaml:"type"`
64 Properties struct {
65 SerialNumber string `yaml:"serial_number"`
66 PonPortID int `yaml:"pon_port_id"`
67 DeviceID string `yaml:"device_id"`
68 } `yaml:"properties"`
69 Requirements []struct {
70 Owner struct {
71 Node string `yaml:"node"`
72 Relationship string `yaml:"relationship"`
73 } `yaml:"owner"`
74 } `yaml:"requirements"`
75 } `yaml:"ont"`
76 } `yaml:"node_templates"`
77 } `yaml:"topology_template"`
78}
79
80func NewOntProvision(serialNumber string, oltIP net.IP, ponPortNumber int) OntProvision {
81 offset := 1 << 29
82 o := OntProvision{}
83 err := yaml.Unmarshal([]byte(ontTemplate), &o)
84 if err != nil {
85 }
86 props := &o.TopologyTemplate.NodeTemplates.Ont.Properties
87 props.PonPortID = offset + ponPortNumber
88 props.SerialNumber = serialNumber
89 ipNum := []byte(oltIP[12:16]) //only handling ipv4
90 ofID := fmt.Sprintf("of:00000000%0x", ipNum)
91 props.DeviceID = ofID
92 fmt.Printf("%v\n", o)
93 return o
94
95}
96func (ont *OntProvision) ToYaml() (string, error) {
97 b, err := yaml.Marshal(ont)
98 ret := string(b)
99 // Damn dirty hack but what are you going to do????
100 serialNumber := ont.TopologyTemplate.NodeTemplates.Ont.Properties.SerialNumber
101 ret = strings.Replace(ret, "ont", serialNumber, -1)
102 return ret, err
103}