@@ -23,6 +23,8 @@ public class VsmCommand {
2323 private static final String s_ciscons = "http://www.cisco.com/nxos:1.0:ppm" ;
2424 private static final String s_configuremode = "__XML__MODE__exec_configure" ;
2525 private static final String s_portprofmode = "__XML__MODE_port-prof" ;
26+ private static final String s_policymapmode = "__XML__MODE_policy-map" ;
27+ private static final String s_classtypemode = "__XML__MODE_policy-map_class_type" ;
2628 private static final String s_paramvalue = "__XML__PARAM_value" ;
2729
2830 public enum PortProfileType {
@@ -53,7 +55,7 @@ public enum OperationType {
5355 }
5456
5557 public static String getAddPortProfile (String name , PortProfileType type ,
56- BindingType binding , SwitchPortMode mode , int vlanid , int networkRate ) {
58+ BindingType binding , SwitchPortMode mode , int vlanid ) {
5759 try {
5860 // Create the document and root element.
5961 DocumentBuilderFactory docFactory = DocumentBuilderFactory .newInstance ();
@@ -153,6 +155,105 @@ public static String getDeletePortProfile(String portName) {
153155 }
154156 }
155157
158+ public static String getPolicyMap (String name , int averageRate , int maxRate , int burstRate ) {
159+ try {
160+ // Create the document and root element.
161+ DocumentBuilderFactory docFactory = DocumentBuilderFactory .newInstance ();
162+ DocumentBuilder docBuilder = docFactory .newDocumentBuilder ();
163+ DOMImplementation domImpl = docBuilder .getDOMImplementation ();
164+ Document doc = createDocument (domImpl );
165+
166+ // Edit configuration command.
167+ Element editConfig = doc .createElement ("nf:edit-config" );
168+ doc .getDocumentElement ().appendChild (editConfig );
169+
170+ // Command to get into exec configure mode.
171+ Element target = doc .createElement ("nf:target" );
172+ Element running = doc .createElement ("nf:running" );
173+ target .appendChild (running );
174+ editConfig .appendChild (target );
175+
176+ // Command to create the port profile with the desired configuration.
177+ Element config = doc .createElement ("nf:config" );
178+ config .appendChild (policyMapDetails (doc , name , averageRate , maxRate , burstRate ));
179+ editConfig .appendChild (config );
180+
181+ return serialize (domImpl , doc );
182+ } catch (ParserConfigurationException e ) {
183+ s_logger .error ("Error while creating delete message : " + e .getMessage ());
184+ return null ;
185+ } catch (DOMException e ) {
186+ s_logger .error ("Error while creating delete message : " + e .getMessage ());
187+ return null ;
188+ }
189+ }
190+
191+ public static String getDeletePolicyMap (String name ) {
192+ try {
193+ // Create the document and root element.
194+ DocumentBuilderFactory docFactory = DocumentBuilderFactory .newInstance ();
195+ DocumentBuilder docBuilder = docFactory .newDocumentBuilder ();
196+ DOMImplementation domImpl = docBuilder .getDOMImplementation ();
197+ Document doc = createDocument (domImpl );
198+
199+ // Edit configuration command.
200+ Element editConfig = doc .createElement ("nf:edit-config" );
201+ doc .getDocumentElement ().appendChild (editConfig );
202+
203+ // Command to get into exec configure mode.
204+ Element target = doc .createElement ("nf:target" );
205+ Element running = doc .createElement ("nf:running" );
206+ target .appendChild (running );
207+ editConfig .appendChild (target );
208+
209+ // Command to create the port profile with the desired configuration.
210+ Element config = doc .createElement ("nf:config" );
211+ config .appendChild (deletePolicyMapDetails (doc , name ));
212+ editConfig .appendChild (config );
213+
214+ return serialize (domImpl , doc );
215+ } catch (ParserConfigurationException e ) {
216+ s_logger .error ("Error while creating delete message : " + e .getMessage ());
217+ return null ;
218+ } catch (DOMException e ) {
219+ s_logger .error ("Error while creating delete message : " + e .getMessage ());
220+ return null ;
221+ }
222+ }
223+
224+ public static String getAttachServicePolicy (String policyMap , String portProfile ) {
225+ try {
226+ // Create the document and root element.
227+ DocumentBuilderFactory docFactory = DocumentBuilderFactory .newInstance ();
228+ DocumentBuilder docBuilder = docFactory .newDocumentBuilder ();
229+ DOMImplementation domImpl = docBuilder .getDOMImplementation ();
230+ Document doc = createDocument (domImpl );
231+
232+ // Edit configuration command.
233+ Element editConfig = doc .createElement ("nf:edit-config" );
234+ doc .getDocumentElement ().appendChild (editConfig );
235+
236+ // Command to get into exec configure mode.
237+ Element target = doc .createElement ("nf:target" );
238+ Element running = doc .createElement ("nf:running" );
239+ target .appendChild (running );
240+ editConfig .appendChild (target );
241+
242+ // Command to create the port profile with the desired configuration.
243+ Element config = doc .createElement ("nf:config" );
244+ config .appendChild (attachServiceDetails (doc , policyMap , portProfile ));
245+ editConfig .appendChild (config );
246+
247+ return serialize (domImpl , doc );
248+ } catch (ParserConfigurationException e ) {
249+ s_logger .error ("Error while creating delete message : " + e .getMessage ());
250+ return null ;
251+ } catch (DOMException e ) {
252+ s_logger .error ("Error while creating delete message : " + e .getMessage ());
253+ return null ;
254+ }
255+ }
256+
156257 public static String getHello () {
157258 try {
158259 DocumentBuilderFactory docFactory = DocumentBuilderFactory .newInstance ();
@@ -333,6 +434,119 @@ private static Element deletePortProfileDetails(Document doc, String name) {
333434 return configure ;
334435 }
335436
437+ private static Element policyMapDetails (Document doc , String name ,
438+ int averageRate , int maxRate , int burstRate ) {
439+ Element configure = doc .createElementNS (s_ciscons , "nxos:configure" );
440+ Element modeConfigure = doc .createElement ("nxos:" + s_configuremode );
441+ configure .appendChild (modeConfigure );
442+
443+ // Policy map details
444+ Element policyMap = doc .createElement ("policy-map" );
445+ modeConfigure .appendChild (policyMap );
446+
447+ Element policyDetails = doc .createElement ("name" );
448+ policyMap .appendChild (policyDetails );
449+
450+ // Name of the policy to create/update.
451+ Element value = doc .createElement (s_paramvalue );
452+ value .setAttribute ("isKey" , "true" );
453+ value .setTextContent (name );
454+ policyDetails .appendChild (value );
455+
456+ Element policyMapMode = doc .createElement (s_policymapmode );
457+ policyDetails .appendChild (policyMapMode );
458+
459+ // Create the default class to match all trafic.
460+ Element classRoot = doc .createElement ("class" );
461+ Element classDefault = doc .createElement ("class-default" );
462+ policyMapMode .appendChild (classRoot );
463+ classRoot .appendChild (classDefault );
464+
465+ Element classMode = doc .createElement (s_classtypemode );
466+ classDefault .appendChild (classMode );
467+
468+ // Set the average, max and burst rate.
469+ // TODO: Add handling for max and burst.
470+ Element police = doc .createElement ("police" );
471+ classMode .appendChild (police );
472+
473+ // Set the committed information rate and its value in mbps.
474+ Element cir = doc .createElement ("cir" );
475+ police .appendChild (cir );
476+ Element cirValue = doc .createElement (s_paramvalue );
477+ Element mbps = doc .createElement ("mbps" );
478+ cirValue .setTextContent (Integer .toString (averageRate ));
479+ cir .appendChild (cirValue );
480+ cir .appendChild (mbps );
481+
482+ // Persist the configuration across reboots.
483+ modeConfigure .appendChild (persistConfiguration (doc ));
484+
485+ return configure ;
486+ }
487+
488+ private static Element deletePolicyMapDetails (Document doc , String name ) {
489+ Element configure = doc .createElementNS (s_ciscons , "nxos:configure" );
490+ Element modeConfigure = doc .createElement ("nxos:" + s_configuremode );
491+ configure .appendChild (modeConfigure );
492+
493+ // Delete Policy map details
494+ Element deletePolicyMap = doc .createElement ("no" );
495+ Element policyMap = doc .createElement ("policy-map" );
496+ deletePolicyMap .appendChild (policyMap );
497+ modeConfigure .appendChild (deletePolicyMap );
498+
499+ Element policyDetails = doc .createElement ("name" );
500+ policyMap .appendChild (policyDetails );
501+
502+ // Name of the policy to create/update.
503+ Element value = doc .createElement (s_paramvalue );
504+ value .setAttribute ("isKey" , "true" );
505+ value .setTextContent (name );
506+ policyDetails .appendChild (value );
507+
508+ // Persist the configuration across reboots.
509+ modeConfigure .appendChild (persistConfiguration (doc ));
510+
511+ return configure ;
512+ }
513+
514+ private static Element attachServiceDetails (Document doc , String policyMap , String portProfile ) {
515+ // In mode, exec_configure.
516+ Element configure = doc .createElementNS (s_ciscons , "nxos:configure" );
517+ Element modeConfigure = doc .createElement ("nxos:" + s_configuremode );
518+ configure .appendChild (modeConfigure );
519+
520+ // Port profile name and type configuration.
521+ Element profile = doc .createElement ("port-profile" );
522+ modeConfigure .appendChild (profile );
523+
524+ // Port profile type.
525+ Element portDetails = doc .createElement ("name" );
526+ profile .appendChild (portDetails );
527+
528+ // Name of the profile to update.
529+ Element value = doc .createElement (s_paramvalue );
530+ value .setAttribute ("isKey" , "true" );
531+ value .setTextContent (portProfile );
532+ portDetails .appendChild (value );
533+
534+ // element for port prof mode.
535+ Element portProfMode = doc .createElement (s_portprofmode );
536+ portDetails .appendChild (portProfMode );
537+
538+ // Associate the policy for input.
539+ portProfMode .appendChild (getServicePolicyCmd (doc , policyMap , "input" ));
540+
541+ // Associate the policy for output.
542+ portProfMode .appendChild (getServicePolicyCmd (doc , policyMap , "output" ));
543+
544+ // Persist the configuration across reboots.
545+ modeConfigure .appendChild (persistConfiguration (doc ));
546+
547+ return configure ;
548+ }
549+
336550 private static Element persistConfiguration (Document doc ) {
337551 Element copy = doc .createElement ("copy" );
338552 Element running = doc .createElement ("running-config" );
@@ -458,6 +672,21 @@ private static Element getSwitchPortMode(Document doc, SwitchPortMode mode) {
458672 return switchport ;
459673 }
460674
675+ private static Element getServicePolicyCmd (Document doc , String policyMap , String type ) {
676+ Element service = doc .createElement ("service-policy" );
677+ Element input = doc .createElement (type );
678+ service .appendChild (input );
679+
680+ Element name = doc .createElement ("name" );
681+ input .appendChild (name );
682+
683+ Element policyValue = doc .createElement (s_paramvalue );
684+ policyValue .setTextContent (policyMap );
685+ name .appendChild (policyValue );
686+
687+ return service ;
688+ }
689+
461690 private static Document createDocument (DOMImplementation dom ) {
462691 Document doc = dom .createDocument (s_namespace , "nf:rpc" , null );
463692 doc .getDocumentElement ().setAttribute ( "message-id" , "101" );
0 commit comments