|
| 1 | +/*** |
| 2 | +Copyright 2014 Cisco Systems Inc. All rights reserved. |
| 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 | +http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. |
| 14 | +*/ |
| 15 | + |
| 16 | +package mastercfg |
| 17 | + |
| 18 | +import ( |
| 19 | + "encoding/json" |
| 20 | + "fmt" |
| 21 | + "github.com/contiv/netplugin/core" |
| 22 | +) |
| 23 | + |
| 24 | +const ( |
| 25 | + bgpConfigPathPrefix = StateConfigPath + "bgp/" |
| 26 | + bgpConfigPath = bgpConfigPathPrefix + "%s" |
| 27 | +) |
| 28 | + |
| 29 | +// CfgBgpState is the router Bgp configuration for the host |
| 30 | +type CfgBgpState struct { |
| 31 | + core.CommonState |
| 32 | + Hostname string `json:"hostname"` |
| 33 | + As string `json:"as"` |
| 34 | + Neighbor string `json:"neighbor"` |
| 35 | +} |
| 36 | + |
| 37 | +// Write the state |
| 38 | +func (s *CfgBgpState) Write() error { |
| 39 | + key := fmt.Sprintf(bgpConfigPath, s.Hostname) |
| 40 | + return s.StateDriver.WriteState(key, s, json.Marshal) |
| 41 | +} |
| 42 | + |
| 43 | +// Read the state in for a given ID. |
| 44 | +func (s *CfgBgpState) Read(id string) error { |
| 45 | + key := fmt.Sprintf(bgpConfigPath, id) |
| 46 | + return s.StateDriver.ReadState(key, s, json.Unmarshal) |
| 47 | +} |
| 48 | + |
| 49 | +// ReadAll reads all the state for master bgp configurations and returns it. |
| 50 | +func (s *CfgBgpState) ReadAll() ([]core.State, error) { |
| 51 | + return s.StateDriver.ReadAllState(bgpConfigPathPrefix, s, json.Unmarshal) |
| 52 | +} |
| 53 | + |
| 54 | +// Clear removes the configuration from the state store. |
| 55 | +func (s *CfgBgpState) Clear() error { |
| 56 | + key := fmt.Sprintf(bgpConfigPath, s.Hostname) |
| 57 | + return s.StateDriver.ClearState(key) |
| 58 | +} |
| 59 | + |
| 60 | +// WatchAll state transitions and send them through the channel. |
| 61 | +func (s *CfgBgpState) WatchAll(rsps chan core.WatchState) error { |
| 62 | + return s.StateDriver.WatchAllState(bgpConfigPathPrefix, s, json.Unmarshal, |
| 63 | + rsps) |
| 64 | +} |
0 commit comments