-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #839 from gkvijay/vpp-integ
Added RemoteEndpoint/PolicyRule notifications in netplugin for VPP driver
- Loading branch information
Showing
10 changed files
with
413 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/*** | ||
Copyright 2017 Cisco Systems Inc. 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. | ||
*/ | ||
|
||
package mastercfg | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/contiv/netplugin/core" | ||
"github.com/contiv/ofnet" | ||
) | ||
|
||
const ( | ||
policyRuleConfigPathPrefix = StateConfigPath + "policyRule/" | ||
policyRuleConfigPath = policyRuleConfigPathPrefix + "%s" | ||
) | ||
|
||
// CfgPolicyRule implements the State interface for policy rules | ||
type CfgPolicyRule struct { | ||
core.CommonState | ||
ofnet.OfnetPolicyRule | ||
} | ||
|
||
// Write the state. | ||
func (s *CfgPolicyRule) Write() error { | ||
key := fmt.Sprintf(policyRuleConfigPath, s.RuleId) | ||
return s.StateDriver.WriteState(key, s, json.Marshal) | ||
} | ||
|
||
// Read the state for a given identifier. | ||
func (s *CfgPolicyRule) Read(id string) error { | ||
key := fmt.Sprintf(policyRuleConfigPath, id) | ||
return s.StateDriver.ReadState(key, s, json.Unmarshal) | ||
} | ||
|
||
// ReadAll reads all state objects for the policy rules. | ||
func (s *CfgPolicyRule) ReadAll() ([]core.State, error) { | ||
return s.StateDriver.ReadAllState(policyRuleConfigPathPrefix, s, json.Unmarshal) | ||
} | ||
|
||
// WatchAll fills a channel on each state event related to policy rules. | ||
func (s *CfgPolicyRule) WatchAll(rsps chan core.WatchState) error { | ||
return s.StateDriver.WatchAllState(policyRuleConfigPathPrefix, s, json.Unmarshal, | ||
rsps) | ||
} | ||
|
||
// Clear removes the state. | ||
func (s *CfgPolicyRule) Clear() error { | ||
key := fmt.Sprintf(policyRuleConfigPath, s.RuleId) | ||
return s.StateDriver.ClearState(key) | ||
} | ||
|
||
// addPolicyRuleState adds policy rule to state store | ||
func addPolicyRuleState(ofnetRule *ofnet.OfnetPolicyRule) error { | ||
ruleCfg := &CfgPolicyRule{} | ||
ruleCfg.StateDriver = stateStore | ||
ruleCfg.OfnetPolicyRule = (*ofnetRule) | ||
|
||
// Save the rule | ||
return ruleCfg.Write() | ||
} | ||
|
||
// delPolicyRuleState deletes policy rule from state store | ||
func delPolicyRuleState(ofnetRule *ofnet.OfnetPolicyRule) error { | ||
ruleCfg := &CfgPolicyRule{} | ||
ruleCfg.StateDriver = stateStore | ||
ruleCfg.OfnetPolicyRule = (*ofnetRule) | ||
|
||
// Delete the rule | ||
return ruleCfg.Clear() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/*** | ||
Copyright 2014 Cisco Systems Inc. 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. | ||
*/ | ||
|
||
package mastercfg | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/contiv/netplugin/core" | ||
) | ||
|
||
const ( | ||
testRuleID = "testPolicyRule" | ||
ruleCfgKey = policyRuleConfigPathPrefix + testRuleID | ||
) | ||
|
||
type testRuleStateDriver struct{} | ||
|
||
var policyRuleStateDriver = &testRuleStateDriver{} | ||
|
||
func (d *testRuleStateDriver) Init(instInfo *core.InstanceInfo) error { | ||
return core.Errorf("Shouldn't be called!") | ||
} | ||
|
||
func (d *testRuleStateDriver) Deinit() { | ||
} | ||
|
||
func (d *testRuleStateDriver) Write(key string, value []byte) error { | ||
return core.Errorf("Shouldn't be called!") | ||
} | ||
|
||
func (d *testRuleStateDriver) Read(key string) ([]byte, error) { | ||
return []byte{}, core.Errorf("Shouldn't be called!") | ||
} | ||
|
||
func (d *testRuleStateDriver) ReadAll(baseKey string) ([][]byte, error) { | ||
return [][]byte{}, core.Errorf("Shouldn't be called!") | ||
} | ||
|
||
func (d *testRuleStateDriver) WatchAll(baseKey string, rsps chan [2][]byte) error { | ||
return core.Errorf("not supported") | ||
} | ||
|
||
func (d *testRuleStateDriver) validateKey(key string) error { | ||
if key != ruleCfgKey { | ||
return core.Errorf("Unexpected key. recvd: %s expected: %s ", | ||
key, ruleCfgKey) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (d *testRuleStateDriver) ClearState(key string) error { | ||
return d.validateKey(key) | ||
} | ||
|
||
func (d *testRuleStateDriver) ReadState(key string, value core.State, | ||
unmarshal func([]byte, interface{}) error) error { | ||
return d.validateKey(key) | ||
} | ||
|
||
func (d *testRuleStateDriver) ReadAllState(key string, value core.State, | ||
unmarshal func([]byte, interface{}) error) ([]core.State, error) { | ||
return nil, core.Errorf("Shouldn't be called!") | ||
} | ||
|
||
func (d *testRuleStateDriver) WatchAllState(baseKey string, sType core.State, | ||
unmarshal func([]byte, interface{}) error, rsps chan core.WatchState) error { | ||
return core.Errorf("not supported") | ||
} | ||
|
||
func (d *testRuleStateDriver) WriteState(key string, value core.State, | ||
marshal func(interface{}) ([]byte, error)) error { | ||
return d.validateKey(key) | ||
} | ||
|
||
func TestCfgPolicyRuleRead(t *testing.T) { | ||
ruleCfg := &CfgPolicyRule{} | ||
ruleCfg.StateDriver = policyRuleStateDriver | ||
|
||
err := ruleCfg.Read(testRuleID) | ||
if err != nil { | ||
t.Fatalf("read config state failed. Error: %s", err) | ||
} | ||
} | ||
|
||
func TestCfgPolicyRuleWrite(t *testing.T) { | ||
ruleCfg := &CfgPolicyRule{} | ||
ruleCfg.StateDriver = policyRuleStateDriver | ||
ruleCfg.RuleId = testRuleID | ||
|
||
err := ruleCfg.Write() | ||
if err != nil { | ||
t.Fatalf("write config state failed. Error: %s", err) | ||
} | ||
} | ||
|
||
func TestCfgPolicyRuleClear(t *testing.T) { | ||
ruleCfg := &CfgPolicyRule{} | ||
ruleCfg.StateDriver = policyRuleStateDriver | ||
ruleCfg.RuleId = testRuleID | ||
|
||
err := ruleCfg.Clear() | ||
if err != nil { | ||
t.Fatalf("clear config state failed. Error: %s", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.