donNewtonAlpha | 1d2d681 | 2018-09-14 16:00:02 -0400 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package tosca |
| 17 | |
| 18 | import ( |
| 19 | "fmt" |
| 20 | "net" |
| 21 | "strings" |
| 22 | |
| 23 | yaml "gopkg.in/yaml.v2" |
| 24 | ) |
| 25 | |
| 26 | var ontTemplate = `tosca_definitions_version: tosca_simple_yaml_1_0 |
| 27 | imports: |
| 28 | - custom_types/attworkflowdriverwhitelistentry.yaml |
| 29 | - custom_types/attworkflowdriverservice.yaml |
| 30 | description: Create an entry in the whitelist |
| 31 | topology_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 | |
| 49 | type 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 | |
| 80 | func 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 |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame] | 87 | props.PonPortID = offset + (ponPortNumber - 1) |
donNewtonAlpha | 1d2d681 | 2018-09-14 16:00:02 -0400 | [diff] [blame] | 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 |
donNewtonAlpha | 1d2d681 | 2018-09-14 16:00:02 -0400 | [diff] [blame] | 92 | return o |
| 93 | |
| 94 | } |
| 95 | func (ont *OntProvision) ToYaml() (string, error) { |
| 96 | b, err := yaml.Marshal(ont) |
| 97 | ret := string(b) |
| 98 | // Damn dirty hack but what are you going to do???? |
| 99 | serialNumber := ont.TopologyTemplate.NodeTemplates.Ont.Properties.SerialNumber |
| 100 | ret = strings.Replace(ret, "ont", serialNumber, -1) |
| 101 | return ret, err |
| 102 | } |