From 6a69d47145484958fbbf006a327bb0ad79555a64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fred=20S=C3=B6derberg?= Date: Mon, 12 Sep 2016 11:11:04 +0200 Subject: [PATCH 01/27] Yang rewrite to support from-prefix --- nso-nipap/nso-nipap/src/yang/nipap.yang | 184 ++++++++++++++++-------- 1 file changed, 122 insertions(+), 62 deletions(-) diff --git a/nso-nipap/nso-nipap/src/yang/nipap.yang b/nso-nipap/nso-nipap/src/yang/nipap.yang index 75d543367..a60b3a207 100644 --- a/nso-nipap/nso-nipap/src/yang/nipap.yang +++ b/nso-nipap/nso-nipap/src/yang/nipap.yang @@ -14,6 +14,29 @@ module nipap { import tailf-ncs { prefix ncs; } + + grouping prefix-attributes { + + leaf customer_id { + description "Customer identifier"; + type string; + } + + leaf description { + description "Description of prefix"; + type string; + } + + leaf node { + description "Node, e.g. FQDN"; + type string; + } + + leaf order_id { + description "Order identifier"; + type string; + } + } augment /ncs:services { description "NIPAP settings"; @@ -58,8 +81,8 @@ module nipap { // TODO: make sure only one backend is marked as default } } - - list pool { + + list from-pool { key name; leaf name { tailf:info "Name of pool to request prefix from"; @@ -70,86 +93,123 @@ module nipap { list request { key name; leaf name { - tailf:info "Name of pool to request prefix from"; + tailf:info "Request name"; tailf:cli-allow-range; type string; } - leaf description { - description "Description of prefix"; - type string; + container attributes { + description "Prefix attributes"; + uses prefix-attributes; } - leaf node { - description "Node, e.g. FQDN"; - type string; - } - - leaf family { - description "Address-family to request"; - default 4; - type enumeration { - enum 4 { - value 4; - } - enum 6 { - value 6; + container arguments { + description "Prefix arguments to the request"; + + leaf family { + description "Address family"; + type enumeration { + enum 4 { + value 4; + } + enum 6 { + value 6; + } } } - } - - leaf prefix_id { - type uint64; - config false; - tailf:cdb-oper { - tailf:persistent true; - } - } - leaf prefix { - type inet:ip-prefix; - config false; - tailf:cdb-oper { - tailf:persistent true; + leaf prefix-length { + description "Prefix length"; + type uint8; } } - leaf redeploy-service { - description "Redeploy service after succesful allocation"; - type string; - } + list from-prefix-request { + description "Request prefix from the requested prefix"; + key name; + leaf name { + description "Uniqe identifier for the prefix"; + type string; + } + ordered-by user; + uses prefix-attributes; + } + + container response { + description "CDB Subscriber will write the response here"; + config false; + tailf:cdb-oper { + tailf:persistent true; + } + + choice response-choice { + case error { + leaf error { + type string; + } + } + case ok { + } + } - } + container from-pool { + config false; + tailf:cdb-oper { + tailf:persistent true; + } + uses prefix-attributes; + + leaf prefix { + type inet:ip-prefix; + config false; + tailf:cdb-oper { + tailf:persistent true; + } + } + leaf prefix_id { + type uint64; + config false; + tailf:cdb-oper { + tailf:persistent true; + } + } + } + list from-prefix { + config false; + tailf:cdb-oper { + tailf:persistent true; + } + key name; + leaf name { + type string; + } + + leaf prefix { + type inet:ip-prefix; + config false; + tailf:cdb-oper { + tailf:persistent true; + } + } - } + leaf prefix_id { + type uint64; + config false; + tailf:cdb-oper { + tailf:persistent true; + } + } + uses prefix-attributes; + } - list prefix { - key name; - leaf name { - tailf:info "Name of request"; - tailf:cli-allow-range; - type string; - } - leaf backend { - type leafref { - path "../../backend/name"; } - } - - leaf from-pool { - type string; - } - - leaf prefix { - type inet:ip-prefix; - config false; - tailf:cdb-oper { - tailf:persistent true; + leaf redeploy-service { + description "Redeploy service after succesful allocation"; + type string; } } - } } } From f05317799a7a9854a934a7e9893f9030db966e11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fred=20S=C3=B6derberg?= Date: Mon, 12 Sep 2016 11:11:35 +0200 Subject: [PATCH 02/27] Cleanup and added support for the new yang model --- .../net/spritelink/nsonipap/ConfigCdbSub.java | 397 ++++++++++++------ 1 file changed, 276 insertions(+), 121 deletions(-) diff --git a/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java b/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java index c517dd302..1b2266b24 100644 --- a/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java +++ b/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java @@ -47,11 +47,11 @@ public ConfigCdbSub() { qualifier="reactive-fm-m") private Maapi maapi; - private int th = -1; - private NavuContainer ncsRoot; - private NavuContainer operRoot; + private int th = -1; + private NavuContainer ncsRoot; + private NavuContainer operRoot; - private Connection nipapCon; + private Connection nipapCon; public void init() { LOGGER.info("Starting the CDB Connection..."); @@ -62,14 +62,21 @@ public void init() { new String[] {"admin"}, MaapiUserSessionFlag.PROTO_TCP); - th = maapi.startTrans(Conf.DB_RUNNING, Conf.MODE_READ); - NavuContainer root = new NavuContainer(new NavuContext(maapi, th)); - ncsRoot = root.container(Ncs.hash); - NavuContainer cdbRoot = new NavuContainer(new NavuContext(cdb)); - NavuContainer operRoot = cdbRoot.container(Ncs.hash); + th = maapi.startTrans(Conf.DB_RUNNING, Conf.MODE_READ); + NavuContainer root = new NavuContainer(new NavuContext(maapi, th)); + ncsRoot = root.container(Ncs.hash); + + NavuContainer cdbRoot = new NavuContainer(new NavuContext(cdb)); + NavuContainer operRoot = cdbRoot.container(Ncs.hash); + + sub = cdb.newSubscription(); + + sub.subscribe(1, new nipap(), + "/services/" + + nipap.prefix + "/" + + nipap._from_pool_ + "/" + + nipap._request_); - sub = cdb.newSubscription(); - sub.subscribe(1, new nipap(), "/services/nipap/pool/request"); // Tell CDB we are ready for notifications sub.subscribeDone(); @@ -79,6 +86,120 @@ public void init() { } } + protected Prefix getPrefixAttributes(int th, Maapi maapi_, String attributePath) throws Exception + { + Prefix p = new Prefix(); + + if (maapi_.exists(th, attributePath + "/" + nipap._customer_id_)) { + ConfValue rCustomer_id = maapi.getElem(th, attributePath + "/" + nipap._customer_id_); + p.customer_id = String.valueOf(rCustomer_id); + } + if (maapi_.exists(th, attributePath + "/" + nipap._description_)) { + ConfValue rDescription = maapi.getElem(th, attributePath + "/" + nipap._description_); + p.description = String.valueOf(rDescription); + } + if (maapi.exists(th, attributePath + "/" + nipap._node_)) { + ConfValue rNode = maapi.getElem(th, attributePath + "/" + nipap._node_); + p.node = String.valueOf(rNode); + } + if (maapi_.exists(th, attributePath + "/" + nipap._order_id_)) { + ConfValue rOrder_id = maapi.getElem(th, attributePath + "/" + nipap._customer_id_); + p.order_id = String.valueOf(rOrder_id); + } + + return p; + } + + protected AddPrefixOptions getPrefixOptions (int th, Maapi maapi_, String argumentPath) throws Exception { + AddPrefixOptions opts = new AddPrefixOptions(); + + ConfEnumeration family = (ConfEnumeration)maapi_.getElem( + th, argumentPath + "/" + nipap._family_); + opts.put("family", family.getOrdinalValue()); + + if (maapi_.exists(th, argumentPath + "/" + nipap._prefix_length_)) { + ConfValue pfx_length = maapi_.getElem(th, argumentPath + "/" + nipap._prefix_length_); + opts.put("prefix_length", String.valueOf(pfx_length)); + } + + return opts; + } + + protected void writeResponse(Prefix p, String responsePath) throws ConfException, Exception { + + if (p.family == 4) { + ConfIPv4Prefix prefixValue = new ConfIPv4Prefix(p.prefix); + LOGGER.info("SET: " + responsePath + "/prefix -> " + prefixValue); + wsess.setElem(prefixValue, responsePath + "/" + nipap._prefix_); + } else if (p.family == 6) { + + ConfIPv6Prefix prefixValue = new ConfIPv6Prefix(p.prefix); + LOGGER.info("SET: " + responsePath + "/prefix -> " + prefixValue); + wsess.setElem(prefixValue, responsePath + "/" + nipap._prefix_); + } + + ConfUInt64 prefixIdValue = new ConfUInt64(p.id); + wsess.setElem(prefixIdValue, responsePath + "/" + nipap._prefix_id_); + + ConfBuf customerIdValue = new ConfBuf(p.customer_id); + if(customerIdValue != null){ + wsess.setElem(customerIdValue, responsePath + "/" + nipap._customer_id_); + } + + if(p.description != null){ + ConfBuf descriptionValue = new ConfBuf(p.description); + wsess.setElem(descriptionValue, responsePath + "/" + nipap._description_); + } + + if(p.node != null){ + ConfBuf nodeValue = new ConfBuf(p.node); + wsess.setElem(nodeValue, responsePath + "/" + nipap._node_); + } + + if(p.order_id != null){ + ConfBuf orderIdValue = new ConfBuf(p.order_id); + wsess.setElem(orderIdValue, responsePath + "/" + nipap._order_id_); + } + + + } + + protected void removeResponse(String responsePath) throws ConfException, Exception { + //unset case + + LOGGER.info("remove response " + responsePath); + try { + wsess.delete(responsePath + "/" + nipap._prefix_); + wsess.delete(responsePath + "/" + nipap._prefix_id_); + wsess.delete(responsePath + "/" + nipap._customer_id_); + wsess.delete(responsePath + "/" + nipap._description_); + wsess.delete(responsePath + "/" + nipap._node_); + wsess.delete(responsePath + "/" + nipap._order_id_); + } catch (CdbException e ){ + } + + + + } + + protected void removeChildPrefix(String prefixPath) throws ConfException, Exception { + + removePrefix(prefixPath); + LOGGER.info("delete path + " + prefixPath); + wsess.delete(prefixPath); + } + + protected void removePrefix(String prefixPath) throws ConfException, Exception { + try { + ConfUInt64 p_id = (ConfUInt64)wsess.getElem(prefixPath + "/" + nipap._prefix_id_); + LOGGER.info("Removing prefix ID: " + p_id); + Prefix p = Prefix.get(nipapCon, Integer.parseInt(String.valueOf(p_id))); + p.remove(nipapCon); + } catch (Exception e) { + LOGGER.error("Unable to remove prefix from NIPAP: " + e.getMessage(),e); + } + } + public void run() { LOGGER.info("Starting the CDB subscriber..."); try { @@ -115,104 +236,121 @@ public void run() { for (Request req : reqs) { LOGGER.debug("Requested NIPAP action, op=" + req.op + " , type=" + req.t); - try { - // TODO: make backend configurable (now it is 'default') - ConfValue bHost = maapi.getElem(th, "/services/nipap/backend{default}/hostname"); - ConfValue bPort = maapi.getElem(th, "/services/nipap/backend{default}/port"); - ConfValue bUser = maapi.getElem(th, "/services/nipap/backend{default}/username"); - ConfValue bPass = maapi.getElem(th, "/services/nipap/backend{default}/password"); - - URL url = new URL("http://" + String.valueOf(bHost) + ":" + String.valueOf(bPort) + "/RPC2"); - nipapCon = new Connection(url, String.valueOf(bUser), String.valueOf(bPass)); - nipapCon.authoritative_source = "ncs"; - - } catch (Exception e) { - LOGGER.error("Unable to initiate connection to NIPAP: " + e.getMessage()); - continue; - } - - - // allocate prefix - if ((req.op == Operation.ALLOCATE) && - (req.t == Type.Prefix)) { - - LOGGER.info("Trying to allocate a prefix for: " + req.request_key + " from pool: " + req.pool_key); - String poolName = String.valueOf(req.pool_key).replaceAll("[{}]", ""); - - Prefix p = new Prefix(); - // Gather prefix data and perform NIPAP request - try { - // Pool - HashMap poolSpec = new HashMap(); - poolSpec.put("name", poolName); - List poolRes = Pool.list(nipapCon, poolSpec); - - // options, like address-family - AddPrefixOptions opts = new AddPrefixOptions(); - ConfEnumeration family = (ConfEnumeration)maapi.getElem(th, req.path + "/family"); - opts.put("family", family.getOrdinalValue()); - - ConfValue rDescription = maapi.getElem(th, req.path + "/description"); - if (rDescription != null) - p.description = String.valueOf(rDescription); - - ConfValue rNode = maapi.getElem(th, req.path + "/node"); - if (rNode != null) - p.node = String.valueOf(rNode); - - p.save(nipapCon, (Pool)poolRes.get(0), opts); - - } catch (Exception e) { - LOGGER.error("Unable to get prefix from NIPAP: " + e.getMessage(), e); - continue; - } - - // Write the result - if (p.family == 4) { - ConfIPv4Prefix prefixValue = new ConfIPv4Prefix(p.prefix); - LOGGER.info("SET: " + req.path + "/prefix -> " + prefixValue); - wsess.setElem(prefixValue, req.path + "/prefix"); - } else if (p.family == 6) { - ConfIPv6Prefix prefixValue = new ConfIPv6Prefix(p.prefix); - LOGGER.info("SET: " + req.path + "/prefix -> " + prefixValue); - wsess.setElem(prefixValue, req.path + "/prefix"); - } - ConfUInt64 prefixIdValue = new ConfUInt64(p.id); - wsess.setElem(prefixIdValue, req.path + "/prefix_id"); - - // Redeploy - try { - ConfValue redeployPath = maapi.getElem(th, req.path + "/redeploy-service"); - LOGGER.info("redeploy-service: " + redeployPath); - redeploy(redeployPath.toString()); - } catch (Exception e) { - LOGGER.error("Redeploy failed: " + e.getMessage()); - } - } + try { + // TODO: make backend configurable (now it is 'default') + ConfValue bHost = maapi.getElem(th, "/services/nipap/backend{default}/hostname"); + ConfValue bPort = maapi.getElem(th, "/services/nipap/backend{default}/port"); + ConfValue bUser = maapi.getElem(th, "/services/nipap/backend{default}/username"); + ConfValue bPass = maapi.getElem(th, "/services/nipap/backend{default}/password"); - else if (req.op == Operation.DEALLOCATE && - (req.t == Type.Prefix)) { - //Deallocate prefix - - try { - ConfUInt64 p_id = (ConfUInt64)wsess.getElem(req.path + "/prefix_id"); - LOGGER.info("Removing prefix ID: " + p_id); - Prefix p = Prefix.get(nipapCon, Integer.parseInt(String.valueOf(p_id))); - p.remove(nipapCon); - wsess.delete(req.path + "/prefix_id"); - wsess.delete(req.path + "/prefix"); - } catch (Exception e) { - LOGGER.error("Unable to remove prefix from NIPAP: " + e.getMessage(),e); - continue; - } + URL url = new URL("http://" + String.valueOf(bHost) + ":" + String.valueOf(bPort) + "/RPC2"); + nipapCon = new Connection(url, String.valueOf(bUser), String.valueOf(bPass)); + nipapCon.authoritative_source = "ncs"; - } + } catch (Exception e) { + LOGGER.error("Unable to initiate connection to NIPAP: " + e.getMessage()); + continue; + } + + + // allocate prefix + if (req.op == Operation.ALLOCATE) { + LOGGER.info("Trying to allocate a prefix for: " + req.request_key + " from pool: " + req.pool_key); + + String poolName = String.valueOf(req.pool_key).replaceAll("[{}]", ""); + + Prefix p = new Prefix(); + // Gather prefix data and perform NIPAP request + try { + // Pool + HashMap poolSpec = new HashMap(); + poolSpec.put("name", poolName); + List poolRes = Pool.list(nipapCon, poolSpec); + + // options, like address-family + AddPrefixOptions opts = getPrefixOptions(th, maapi, req.path + "/" + nipap._arguments_); + + //set prefix attributes + String attrPath = req.path + "/" + nipap._attributes_; + p = getPrefixAttributes(th, maapi, attrPath); + + p.save(nipapCon, (Pool)poolRes.get(0), opts); + + } catch (Exception e) { + LOGGER.error("Unable to get prefix from NIPAP: " + e.getMessage(), e); + wsess.setElem(new ConfBuf(e.getMessage()), req.path + "/" + nipap._response_ + "/" + nipap._error_); + wsess.setCase(nipap._response_choice_, nipap._error_, req.path + "/" + nipap._response_); + continue; + } + + // Write the result + String resPath = req.path + "/" + nipap._response_ + "/" + nipap._from_pool_; + writeResponse(p, resPath); + //set case + wsess.setCase(nipap._response_choice_, nipap._ok_, req.path + "/" + nipap._response_ ); + + + // Request prefix from prefix + if(maapi.exists(th, req.path + "/" + nipap._from_prefix_request_)){ + MaapiCursor pfx_cur = maapi.newCursor(th, req.path + "/" + nipap._from_prefix_request_); + ConfKey pfx = null; + while((pfx = maapi.getNext(pfx_cur)) != null) { + Prefix child_prefix = new Prefix(); + try { + + AddPrefixOptions child_opts = new AddPrefixOptions(); + child_opts.put("prefix_length", "32"); + + child_prefix = getPrefixAttributes(th, maapi, req.path + "/" + nipap._from_prefix_request_ + pfx ); + child_prefix.type = "host"; + child_prefix.save(nipapCon, (Prefix)p, child_opts); + + }catch (Exception e ) { + LOGGER.error("Unable to get prefix from prefix" + e.getMessage(), e); + continue; } + //write response + wsess.create(req.path + "/" + nipap._response_ + "/" + nipap._from_prefix_ + pfx); + writeResponse(child_prefix, req.path + "/" + nipap._response_ + "/" + nipap._from_prefix_ + pfx); + } + + } - // Tell the subscription we are done - sub.sync(CdbSubscriptionSyncType.DONE_PRIORITY); + // Redeploy + try { + ConfValue redeployPath = maapi.getElem(th, req.path + "/redeploy-service"); + LOGGER.info("redeploy-service: " + redeployPath); + redeploy(redeployPath.toString()); + } catch (Exception e) { + LOGGER.error("Redeploy failed: " + e.getMessage()); } + } + + else if (req.op == Operation.DEALLOCATE && + (req.t == Type.Request)) { + //Deallocate prefix + LOGGER.info(req.path + "/" + nipap._response_ + "/" + nipap._from_prefix_); + String path = req.path + "/" + nipap._response_ + "/" + nipap._from_prefix_; + int n = wsess.getNumberOfInstances(path); + try { + List objArrList = wsess.getObjects(4, 0, n, path); + for ( int i = 0; i < objArrList.size(); i++){ + ConfObject[] objArr = objArrList.get(i); + LOGGER.info(objArr[0]); + ConfKey t = new ConfKey(objArr[0]); + removeChildPrefix(req.path+ "/" + nipap._response_ + "/" + nipap._from_prefix_ + t); + } + }catch (Exception e){ + } + + removePrefix(req.path + "/" + nipap._response_ + "/" + nipap._from_pool_); + removeResponse(req.path + "/" + nipap._response_ + "/" + nipap._from_pool_); + } + } + // Tell the subscription we are done + sub.sync(CdbSubscriptionSyncType.DONE_PRIORITY); + } } catch (SocketException e) { // silence here, normal close (redeploy/reload package) @@ -240,13 +378,13 @@ private void safeclose(Cdb s) { private enum Operation { ALLOCATE, DEALLOCATE } - private enum Type { Prefix } + private enum Type { Request, FromPrefixRequest, Prefix } private class Request { Operation op; Type t; ConfPath path; - ConfKey pool_key; + ConfKey pool_key; ConfKey request_key; } @@ -269,25 +407,42 @@ public DiffIterateResultFlag iterate( try { ConfPath p = new ConfPath(kp); LOGGER.info("ITER " + op + " " + p); + LOGGER.info("kp= " + kp); + LOGGER.info("length " + kp.length); // The kp array contains the keypath to the ConfObject in reverse order, for example: - // /ncs:services/nipap:nipap/prefix{bar} -> ["{bar}", "nipap:prefix", "nipap:nipap", "ncs:services" ] - // Since we are subscribing to the changes on /ncs:services/ura:ura, the 3rd node from the end of the list always contains the service name (list key) Request r = new Request(); r.path = p; - if (kp[1].toString().equals("nipap:request")) { - r.request_key = (ConfKey)kp[0]; - if (kp[3].toString().equals("nipap:pool")) { - r.t = Type.Prefix; - r.pool_key = (ConfKey)kp[2]; - } - if (op == DiffIterateOperFlag.MOP_CREATED) { - r.op = Operation.ALLOCATE; - reqs.add(r); - } else if (op == DiffIterateOperFlag.MOP_DELETED) { - r.op = Operation.DEALLOCATE; - reqs.add(r); - } - } + + r.pool_key = (ConfKey)kp[2]; + r.request_key = (ConfKey)kp[0]; + + switch(op) { + + case MOP_CREATED: { + // new request + if(kp[1].toString().equals("nipap:request") && + kp.length == 6){ + r.t = Type.Request; + r.op = Operation.ALLOCATE; + reqs.add(r); + //the request is new, we dont need to look at children + return DiffIterateResultFlag.ITER_CONTINUE; + } + + break; + } + case MOP_DELETED: { + if(kp[1].toString().equals("nipap:request") && + kp.length == 6){ + r.t = Type.Request; + r.op = Operation.DEALLOCATE; + reqs.add(r); + //we dont need to look at children + return DiffIterateResultFlag.ITER_CONTINUE; + } + break; + } + } } catch (Exception e) { LOGGER.error("", e); From dd5402b8c895233e40cc64d02b9177c3e25fb822 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fred=20S=C3=B6derberg?= Date: Mon, 12 Sep 2016 11:14:26 +0200 Subject: [PATCH 03/27] Removed unused variables --- .../src/net/spritelink/nsonipap/ConfigCdbSub.java | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java b/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java index 1b2266b24..46f507418 100644 --- a/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java +++ b/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java @@ -30,7 +30,6 @@ public class ConfigCdbSub implements ApplicationComponent { private CdbSubscription sub = null; private CdbSession wsess; - private CdbSession rsess; public ConfigCdbSub() { } @@ -48,8 +47,6 @@ public ConfigCdbSub() { private Maapi maapi; private int th = -1; - private NavuContainer ncsRoot; - private NavuContainer operRoot; private Connection nipapCon; @@ -63,11 +60,6 @@ public void init() { MaapiUserSessionFlag.PROTO_TCP); th = maapi.startTrans(Conf.DB_RUNNING, Conf.MODE_READ); - NavuContainer root = new NavuContainer(new NavuContext(maapi, th)); - ncsRoot = root.container(Ncs.hash); - - NavuContainer cdbRoot = new NavuContainer(new NavuContext(cdb)); - NavuContainer operRoot = cdbRoot.container(Ncs.hash); sub = cdb.newSubscription(); @@ -160,8 +152,6 @@ protected void writeResponse(Prefix p, String responsePath) throws ConfException ConfBuf orderIdValue = new ConfBuf(p.order_id); wsess.setElem(orderIdValue, responsePath + "/" + nipap._order_id_); } - - } protected void removeResponse(String responsePath) throws ConfException, Exception { @@ -177,9 +167,6 @@ protected void removeResponse(String responsePath) throws ConfException, Excepti wsess.delete(responsePath + "/" + nipap._order_id_); } catch (CdbException e ){ } - - - } protected void removeChildPrefix(String prefixPath) throws ConfException, Exception { From f41b6560ffa09b0a9d6083f8fb21fa7c3d328b43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fred=20S=C3=B6derberg?= Date: Mon, 12 Sep 2016 13:10:15 +0200 Subject: [PATCH 04/27] Added support for from-prefix --- .../net/spritelink/nsonipap/ConfigCdbSub.java | 112 +++++++++++------- 1 file changed, 71 insertions(+), 41 deletions(-) diff --git a/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java b/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java index 46f507418..76e83c187 100644 --- a/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java +++ b/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java @@ -77,7 +77,29 @@ public void init() { LOGGER.error("", e); } } + protected void addPrefixFromPrefix(String path, Prefix parentPrefix, ConfKey prefixKey) throws Exception { + LOGGER.info("Create, From prefix request"); + Prefix child_prefix = new Prefix(); + try { + + AddPrefixOptions child_opts = new AddPrefixOptions(); + child_opts.put("prefix_length", "32"); + + child_prefix = getPrefixAttributes(th, maapi, path + "/" + nipap._from_prefix_request_ + prefixKey ); + child_prefix.type = "host"; + child_prefix.save(nipapCon, parentPrefix, child_opts); + + }catch (Exception e ) { + LOGGER.error("Unable to get prefix from prefix" + e.getMessage(), e); + return; + } + //write response + wsess.create(path + "/" + nipap._response_ + "/" + nipap._from_prefix_ + prefixKey); + writeResponse(child_prefix, path + "/" + nipap._response_ + "/" + nipap._from_prefix_ + prefixKey); + + + } protected Prefix getPrefixAttributes(int th, Maapi maapi_, String attributePath) throws Exception { Prefix p = new Prefix(); @@ -241,7 +263,7 @@ public void run() { // allocate prefix - if (req.op == Operation.ALLOCATE) { + if (req.op == Operation.ALLOCATE && req.t == Type.Request ) { LOGGER.info("Trying to allocate a prefix for: " + req.request_key + " from pool: " + req.pool_key); @@ -251,7 +273,7 @@ public void run() { // Gather prefix data and perform NIPAP request try { // Pool - HashMap poolSpec = new HashMap(); + HashMap poolSpec = new HashMap<>(); poolSpec.put("name", poolName); List poolRes = Pool.list(nipapCon, poolSpec); @@ -283,23 +305,8 @@ public void run() { MaapiCursor pfx_cur = maapi.newCursor(th, req.path + "/" + nipap._from_prefix_request_); ConfKey pfx = null; while((pfx = maapi.getNext(pfx_cur)) != null) { - Prefix child_prefix = new Prefix(); - try { - - AddPrefixOptions child_opts = new AddPrefixOptions(); - child_opts.put("prefix_length", "32"); - - child_prefix = getPrefixAttributes(th, maapi, req.path + "/" + nipap._from_prefix_request_ + pfx ); - child_prefix.type = "host"; - child_prefix.save(nipapCon, (Prefix)p, child_opts); - }catch (Exception e ) { - LOGGER.error("Unable to get prefix from prefix" + e.getMessage(), e); - continue; - } - //write response - wsess.create(req.path + "/" + nipap._response_ + "/" + nipap._from_prefix_ + pfx); - writeResponse(child_prefix, req.path + "/" + nipap._response_ + "/" + nipap._from_prefix_ + pfx); + addPrefixFromPrefix(req.path.toString(), p, pfx); } } @@ -311,30 +318,39 @@ public void run() { redeploy(redeployPath.toString()); } catch (Exception e) { LOGGER.error("Redeploy failed: " + e.getMessage()); - } - } + } + } + else if (req.op == Operation.ALLOCATE && req.t == Type.FromPrefixRequest){ + LOGGER.info("Create, From prefix request"); - else if (req.op == Operation.DEALLOCATE && - (req.t == Type.Request)) { - //Deallocate prefix - LOGGER.info(req.path + "/" + nipap._response_ + "/" + nipap._from_prefix_); - String path = req.path + "/" + nipap._response_ + "/" + nipap._from_prefix_; - int n = wsess.getNumberOfInstances(path); - try { - List objArrList = wsess.getObjects(4, 0, n, path); - for ( int i = 0; i < objArrList.size(); i++){ - ConfObject[] objArr = objArrList.get(i); - LOGGER.info(objArr[0]); - ConfKey t = new ConfKey(objArr[0]); - removeChildPrefix(req.path+ "/" + nipap._response_ + "/" + nipap._from_prefix_ + t); - } - }catch (Exception e){ - } + String path = "/ncs:services/nipap:nipap/from-pool" + req.pool_key + "/request" + req.request_key; - removePrefix(req.path + "/" + nipap._response_ + "/" + nipap._from_pool_); - removeResponse(req.path + "/" + nipap._response_ + "/" + nipap._from_pool_); + ConfUInt64 p_id = (ConfUInt64)wsess.getElem(path +"/" +nipap._response_ + "/" + nipap._from_pool_ + "/" + nipap._prefix_id_); + Prefix parentPrefix= Prefix.get(nipapCon, Integer.parseInt(String.valueOf(p_id))); + + addPrefixFromPrefix(path, parentPrefix, req.prefix_key); + } + else if (req.op == Operation.DEALLOCATE && + (req.t == Type.Request)) { + //Deallocate prefix + LOGGER.info(req.path + "/" + nipap._response_ + "/" + nipap._from_prefix_); + String path = req.path + "/" + nipap._response_ + "/" + nipap._from_prefix_; + int n = wsess.getNumberOfInstances(path); + try { + List objArrList = wsess.getObjects(4, 0, n, path); + for ( int i = 0; i < objArrList.size(); i++){ + ConfObject[] objArr = objArrList.get(i); + LOGGER.info(objArr[0]); + ConfKey t = new ConfKey(objArr[0]); + removeChildPrefix(req.path+ "/" + nipap._response_ + "/" + nipap._from_prefix_ + t); + } + }catch (Exception e){ } + + removePrefix(req.path + "/" + nipap._response_ + "/" + nipap._from_pool_); + removeResponse(req.path + "/" + nipap._response_ + "/" + nipap._from_pool_); } + } // Tell the subscription we are done sub.sync(CdbSubscriptionSyncType.DONE_PRIORITY); } @@ -371,8 +387,9 @@ private class Request { Operation op; Type t; ConfPath path; - ConfKey pool_key; + ConfKey pool_key; ConfKey request_key; + ConfKey prefix_key; } private class Iter implements CdbDiffIterate { @@ -400,8 +417,6 @@ public DiffIterateResultFlag iterate( Request r = new Request(); r.path = p; - r.pool_key = (ConfKey)kp[2]; - r.request_key = (ConfKey)kp[0]; switch(op) { @@ -409,18 +424,33 @@ public DiffIterateResultFlag iterate( // new request if(kp[1].toString().equals("nipap:request") && kp.length == 6){ + r.pool_key = (ConfKey)kp[2]; + r.request_key = (ConfKey)kp[0]; r.t = Type.Request; r.op = Operation.ALLOCATE; reqs.add(r); //the request is new, we dont need to look at children return DiffIterateResultFlag.ITER_CONTINUE; } + else if(kp[1].toString().equals("nipap:from-prefix-request") && + kp.length == 8){ + r.prefix_key = (ConfKey)kp[0]; + r.request_key = (ConfKey)kp[2]; + r.pool_key = (ConfKey)kp[4]; + r.t = Type.FromPrefixRequest; + r.op = Operation.ALLOCATE; + reqs.add(r); + //the request is new, we dont need to look at children + return DiffIterateResultFlag.ITER_CONTINUE; + } break; } case MOP_DELETED: { if(kp[1].toString().equals("nipap:request") && kp.length == 6){ + r.pool_key = (ConfKey)kp[2]; + r.request_key = (ConfKey)kp[0]; r.t = Type.Request; r.op = Operation.DEALLOCATE; reqs.add(r); From 84bc00765d2d8da7c01cccc3e6fa3ac51eb25636 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fred=20S=C3=B6derberg?= Date: Tue, 13 Sep 2016 13:54:26 +0200 Subject: [PATCH 05/27] nso-nipap: Added config false to error code --- nso-nipap/nso-nipap/src/yang/nipap.yang | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/nso-nipap/nso-nipap/src/yang/nipap.yang b/nso-nipap/nso-nipap/src/yang/nipap.yang index a60b3a207..0b7a26c4d 100644 --- a/nso-nipap/nso-nipap/src/yang/nipap.yang +++ b/nso-nipap/nso-nipap/src/yang/nipap.yang @@ -145,6 +145,10 @@ module nipap { choice response-choice { case error { leaf error { + config false; + tailf:cdb-oper { + tailf:persistent true; + } type string; } } From 4e9bff38b0feecee549aa64d9c5b4e4ca2fa2539 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fred=20S=C3=B6derberg?= Date: Tue, 13 Sep 2016 13:56:17 +0200 Subject: [PATCH 06/27] nso-nipap: Added writeError function --- .../net/spritelink/nsonipap/ConfigCdbSub.java | 36 ++++++++++++++----- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java b/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java index 76e83c187..89f52fe39 100644 --- a/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java +++ b/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java @@ -77,9 +77,10 @@ public void init() { LOGGER.error("", e); } } + protected void addPrefixFromPrefix(String path, Prefix parentPrefix, ConfKey prefixKey) throws Exception { - LOGGER.info("Create, From prefix request"); + LOGGER.info("Create, From prefix request, path = " + path); Prefix child_prefix = new Prefix(); try { @@ -92,14 +93,15 @@ protected void addPrefixFromPrefix(String path, Prefix parentPrefix, ConfKey pre }catch (Exception e ) { LOGGER.error("Unable to get prefix from prefix" + e.getMessage(), e); + writeError(path, e.getMessage()); return; } //write response wsess.create(path + "/" + nipap._response_ + "/" + nipap._from_prefix_ + prefixKey); writeResponse(child_prefix, path + "/" + nipap._response_ + "/" + nipap._from_prefix_ + prefixKey); + } - } protected Prefix getPrefixAttributes(int th, Maapi maapi_, String attributePath) throws Exception { Prefix p = new Prefix(); @@ -176,6 +178,14 @@ protected void writeResponse(Prefix p, String responsePath) throws ConfException } } + + protected void writeError(String path, String errorMessage) throws Exception { + + wsess.setElem(new ConfBuf(errorMessage), path + "/" + nipap._response_ + "/" + nipap._error_); + wsess.setCase(nipap._response_choice_, nipap._error_, path + "/" + nipap._response_); + + } + protected void removeResponse(String responsePath) throws ConfException, Exception { //unset case @@ -276,6 +286,10 @@ public void run() { HashMap poolSpec = new HashMap<>(); poolSpec.put("name", poolName); List poolRes = Pool.list(nipapCon, poolSpec); + if(poolRes.size() < 1){ + writeError(req.path.toString(), "Nipap pool not found"); + continue; + } // options, like address-family AddPrefixOptions opts = getPrefixOptions(th, maapi, req.path + "/" + nipap._arguments_); @@ -288,8 +302,7 @@ public void run() { } catch (Exception e) { LOGGER.error("Unable to get prefix from NIPAP: " + e.getMessage(), e); - wsess.setElem(new ConfBuf(e.getMessage()), req.path + "/" + nipap._response_ + "/" + nipap._error_); - wsess.setCase(nipap._response_choice_, nipap._error_, req.path + "/" + nipap._response_); + writeError(req.path.toString(), e.getMessage()); continue; } @@ -320,12 +333,19 @@ public void run() { LOGGER.error("Redeploy failed: " + e.getMessage()); } } + /* + * Allocate from-prefix + * + */ else if (req.op == Operation.ALLOCATE && req.t == Type.FromPrefixRequest){ LOGGER.info("Create, From prefix request"); - String path = "/ncs:services/nipap:nipap/from-pool" + req.pool_key + "/request" + req.request_key; + String path = "/ncs:services/" + nipap.prefix + ":" + nipap.prefix + "/" + + nipap._from_pool_ + req.pool_key + "/request" + req.request_key; + + ConfUInt64 p_id = (ConfUInt64)wsess.getElem( + path +"/" +nipap._response_ + "/" + nipap._from_pool_ + "/" + nipap._prefix_id_); - ConfUInt64 p_id = (ConfUInt64)wsess.getElem(path +"/" +nipap._response_ + "/" + nipap._from_pool_ + "/" + nipap._prefix_id_); Prefix parentPrefix= Prefix.get(nipapCon, Integer.parseInt(String.valueOf(p_id))); addPrefixFromPrefix(path, parentPrefix, req.prefix_key); @@ -333,7 +353,7 @@ else if (req.op == Operation.ALLOCATE && req.t == Type.FromPrefixRequest){ else if (req.op == Operation.DEALLOCATE && (req.t == Type.Request)) { //Deallocate prefix - LOGGER.info(req.path + "/" + nipap._response_ + "/" + nipap._from_prefix_); + String path = req.path + "/" + nipap._response_ + "/" + nipap._from_prefix_; int n = wsess.getNumberOfInstances(path); try { @@ -411,9 +431,7 @@ public DiffIterateResultFlag iterate( try { ConfPath p = new ConfPath(kp); LOGGER.info("ITER " + op + " " + p); - LOGGER.info("kp= " + kp); LOGGER.info("length " + kp.length); - // The kp array contains the keypath to the ConfObject in reverse order, for example: Request r = new Request(); r.path = p; From c97ffbea59a8a9923d6dff39dd59722b7a100b02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fred=20S=C3=B6derberg?= Date: Tue, 13 Sep 2016 20:09:02 +0200 Subject: [PATCH 07/27] nso-nipap: Added updatePrefix function --- .../net/spritelink/nsonipap/ConfigCdbSub.java | 154 ++++++++++++++++-- 1 file changed, 139 insertions(+), 15 deletions(-) diff --git a/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java b/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java index 89f52fe39..86532fae0 100644 --- a/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java +++ b/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java @@ -109,7 +109,7 @@ protected Prefix getPrefixAttributes(int th, Maapi maapi_, String attributePath) if (maapi_.exists(th, attributePath + "/" + nipap._customer_id_)) { ConfValue rCustomer_id = maapi.getElem(th, attributePath + "/" + nipap._customer_id_); p.customer_id = String.valueOf(rCustomer_id); - } + } if (maapi_.exists(th, attributePath + "/" + nipap._description_)) { ConfValue rDescription = maapi.getElem(th, attributePath + "/" + nipap._description_); p.description = String.valueOf(rDescription); @@ -125,6 +125,37 @@ protected Prefix getPrefixAttributes(int th, Maapi maapi_, String attributePath) return p; } + protected Prefix getPrefixAttributes(Prefix oldPrefix, String attributePath) throws Exception + { + Prefix p = oldPrefix; + + if (maapi.exists(th, attributePath + "/" + nipap._customer_id_)) { + ConfValue rCustomer_id = maapi.getElem(th, attributePath + "/" + nipap._customer_id_); + p.customer_id = String.valueOf(rCustomer_id); + }else { + p.customer_id = null; + } + if (maapi.exists(th, attributePath + "/" + nipap._description_)) { + ConfValue rDescription = maapi.getElem(th, attributePath + "/" + nipap._description_); + p.description = String.valueOf(rDescription); + }else { + p.description = null; + } + if (maapi.exists(th, attributePath + "/" + nipap._node_)) { + ConfValue rNode = maapi.getElem(th, attributePath + "/" + nipap._node_); + p.node = String.valueOf(rNode); + }else { + p.node = null; + } + if (maapi.exists(th, attributePath + "/" + nipap._order_id_)) { + ConfValue rOrder_id = maapi.getElem(th, attributePath + "/" + nipap._customer_id_); + p.order_id = String.valueOf(rOrder_id); + }else { + p.order_id = null; + } + + return p; + } protected AddPrefixOptions getPrefixOptions (int th, Maapi maapi_, String argumentPath) throws Exception { AddPrefixOptions opts = new AddPrefixOptions(); @@ -207,6 +238,19 @@ protected void removeChildPrefix(String prefixPath) throws ConfException, Except LOGGER.info("delete path + " + prefixPath); wsess.delete(prefixPath); } + + protected void updatePrefix(String prefixPath, String responsePath) throws Exception { + + LOGGER.info("Update prefix: " + prefixPath); + + int p_id = (int) ((ConfUInt32)wsess.getElem(responsePath + "/" + nipap._prefix_id_)).longValue(); + + Prefix p = Prefix.get(nipapCon, p_id); + + Prefix newPrefix = getPrefixAttributes(p, prefixPath + "/" + nipap._attributes_); + + newPrefix.save(nipapCon); + } protected void removePrefix(String prefixPath) throws ConfException, Exception { try { @@ -255,6 +299,7 @@ public void run() { for (Request req : reqs) { LOGGER.debug("Requested NIPAP action, op=" + req.op + " , type=" + req.t); + sub.sync(CdbSubscriptionSyncType.DONE_PRIORITY); try { // TODO: make backend configurable (now it is 'default') ConfValue bHost = maapi.getElem(th, "/services/nipap/backend{default}/hostname"); @@ -272,7 +317,10 @@ public void run() { } - // allocate prefix + /* + * Allocate new Prefix + * + */ if (req.op == Operation.ALLOCATE && req.t == Type.Request ) { LOGGER.info("Trying to allocate a prefix for: " + req.request_key + " from pool: " + req.pool_key); @@ -286,6 +334,7 @@ public void run() { HashMap poolSpec = new HashMap<>(); poolSpec.put("name", poolName); List poolRes = Pool.list(nipapCon, poolSpec); + if(poolRes.size() < 1){ writeError(req.path.toString(), "Nipap pool not found"); continue; @@ -309,19 +358,17 @@ public void run() { // Write the result String resPath = req.path + "/" + nipap._response_ + "/" + nipap._from_pool_; writeResponse(p, resPath); - //set case - wsess.setCase(nipap._response_choice_, nipap._ok_, req.path + "/" + nipap._response_ ); + wsess.setCase(nipap._response_choice_, nipap._ok_, req.path + "/" + nipap._response_ ); // Request prefix from prefix if(maapi.exists(th, req.path + "/" + nipap._from_prefix_request_)){ MaapiCursor pfx_cur = maapi.newCursor(th, req.path + "/" + nipap._from_prefix_request_); ConfKey pfx = null; - while((pfx = maapi.getNext(pfx_cur)) != null) { + while((pfx = maapi.getNext(pfx_cur)) != null) { addPrefixFromPrefix(req.path.toString(), p, pfx); } - } // Redeploy @@ -341,26 +388,30 @@ else if (req.op == Operation.ALLOCATE && req.t == Type.FromPrefixRequest){ LOGGER.info("Create, From prefix request"); String path = "/ncs:services/" + nipap.prefix + ":" + nipap.prefix + "/" + - nipap._from_pool_ + req.pool_key + "/request" + req.request_key; + nipap._from_pool_ + req.pool_key + "/" + nipap._request_ + req.request_key; - ConfUInt64 p_id = (ConfUInt64)wsess.getElem( - path +"/" +nipap._response_ + "/" + nipap._from_pool_ + "/" + nipap._prefix_id_); + int p_id = (int) ((ConfUInt32)wsess.getElem( + path +"/" +nipap._response_ + "/" + nipap._from_pool_ + "/" + + nipap._prefix_id_)).longValue(); - Prefix parentPrefix= Prefix.get(nipapCon, Integer.parseInt(String.valueOf(p_id))); + Prefix parentPrefix = Prefix.get(nipapCon, p_id); addPrefixFromPrefix(path, parentPrefix, req.prefix_key); } + /* + * Deallocate Prefix + * + */ else if (req.op == Operation.DEALLOCATE && (req.t == Type.Request)) { - //Deallocate prefix String path = req.path + "/" + nipap._response_ + "/" + nipap._from_prefix_; int n = wsess.getNumberOfInstances(path); + try { List objArrList = wsess.getObjects(4, 0, n, path); for ( int i = 0; i < objArrList.size(); i++){ ConfObject[] objArr = objArrList.get(i); - LOGGER.info(objArr[0]); ConfKey t = new ConfKey(objArr[0]); removeChildPrefix(req.path+ "/" + nipap._response_ + "/" + nipap._from_prefix_ + t); } @@ -370,10 +421,30 @@ else if (req.op == Operation.DEALLOCATE && removePrefix(req.path + "/" + nipap._response_ + "/" + nipap._from_pool_); removeResponse(req.path + "/" + nipap._response_ + "/" + nipap._from_pool_); } + /* + * Modify prefix attributes + * + */ + else if (req.op == Operation.SET && req.t == Type.Request){ + + String reqPath = "/services/" + nipap.prefix + ":" + nipap.prefix + "/" + + nipap._from_pool_ + req.pool_key + "/" + nipap._request_ + req.request_key; + + updatePrefix(reqPath, reqPath + "/" + nipap._response_ + "/" + nipap._from_pool_); + + } + /* + * Modify from-prefix attributes + * + */ + else if (req.op == Operation.SET && req.t == Type.FromPrefixRequest){ + + + + } - // Tell the subscription we are done - sub.sync(CdbSubscriptionSyncType.DONE_PRIORITY); } + } } catch (SocketException e) { // silence here, normal close (redeploy/reload package) @@ -400,7 +471,7 @@ private void safeclose(Cdb s) { } - private enum Operation { ALLOCATE, DEALLOCATE } + private enum Operation { ALLOCATE, DEALLOCATE, SET } private enum Type { Request, FromPrefixRequest, Prefix } private class Request { @@ -477,6 +548,59 @@ else if(kp[1].toString().equals("nipap:from-prefix-request") && } break; } + case MOP_VALUE_SET: { + LOGGER.info( "HEJ-->" + kp[2]); + if(kp[1].toString().equals("nipap:attributes") && + kp.length == 8) { + r.pool_key = (ConfKey)kp[4]; + r.request_key = (ConfKey)kp[2]; + r.t = Type.Request; + r.op = Operation.SET; + + boolean found = false; + + for( Request req : reqs){ + + if(req.t.equals(Type.Request) && + req.request_key.equals(r.request_key)) { + found = true; + } + } + if(found == false){ + LOGGER.info("add " + r.request_key.toString()); + reqs.add(r); + } + + } else if(kp[2].toString().equals("nipap:from-prefix-request") && + kp.length == 9){ + + r.pool_key = (ConfKey)kp[5]; + r.request_key = (ConfKey)kp[3]; + r.prefix_key = (ConfKey)kp[1]; + r.t = Type.FromPrefixRequest; + r.op = Operation.SET; + + boolean found = false; + + for( Request req : reqs){ + LOGGER.info(req.t); + LOGGER.info(req.request_key + " " + r.request_key); + LOGGER.info(req.prefix_key + " " + r.prefix_key); + if(req.t.equals(Type.FromPrefixRequest) && + req.request_key.equals(r.request_key) && + req.prefix_key.equals(r.prefix_key)) { + found = true; + } + } + if(found == false){ + LOGGER.info("add " + r.prefix_key); + reqs.add(r); + } + + + } + break; + } } } catch (Exception e) { From aa0535d38a954086cc13707e1a84d0e04e546b1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fred=20S=C3=B6derberg?= Date: Tue, 13 Sep 2016 20:09:34 +0200 Subject: [PATCH 08/27] nso-nipap: Refactoring yang model --- nso-nipap/nso-nipap/src/yang/nipap.yang | 124 ++++++++++-------------- 1 file changed, 50 insertions(+), 74 deletions(-) diff --git a/nso-nipap/nso-nipap/src/yang/nipap.yang b/nso-nipap/nso-nipap/src/yang/nipap.yang index 0b7a26c4d..38d8e9c7a 100644 --- a/nso-nipap/nso-nipap/src/yang/nipap.yang +++ b/nso-nipap/nso-nipap/src/yang/nipap.yang @@ -38,6 +38,47 @@ module nipap { } } + grouping response-grouping { + container response { + description "CDB Subscriber will write the response here"; + config false; + tailf:cdb-oper { + tailf:persistent true; + } + + choice response-choice { + case error { + leaf error { + config false; + tailf:cdb-oper { + tailf:persistent true; + } + type string; + } + } + case ok { + } + } + + leaf prefix { + type inet:ip-prefix; + config false; + tailf:cdb-oper { + tailf:persistent true; + } + } + + leaf prefix_id { + type uint32; + config false; + tailf:cdb-oper { + tailf:persistent true; + } + } + uses prefix-attributes; + } + } + augment /ncs:services { description "NIPAP settings"; @@ -108,6 +149,7 @@ module nipap { leaf family { description "Address family"; + mandatory true; type enumeration { enum 4 { value 4; @@ -127,92 +169,26 @@ module nipap { list from-prefix-request { description "Request prefix from the requested prefix"; key name; + ordered-by user; leaf name { description "Uniqe identifier for the prefix"; type string; } - ordered-by user; - uses prefix-attributes; - } - - container response { - description "CDB Subscriber will write the response here"; - config false; - tailf:cdb-oper { - tailf:persistent true; - } - choice response-choice { - case error { - leaf error { - config false; - tailf:cdb-oper { - tailf:persistent true; - } - type string; - } - } - case ok { - } - } - - container from-pool { - config false; - tailf:cdb-oper { - tailf:persistent true; - } + container attributes { + description "Prefix attributes"; uses prefix-attributes; - - leaf prefix { - type inet:ip-prefix; - config false; - tailf:cdb-oper { - tailf:persistent true; - } } - leaf prefix_id { - type uint64; - config false; - tailf:cdb-oper { - tailf:persistent true; - } - } + uses response-grouping; } - list from-prefix { - config false; - tailf:cdb-oper { - tailf:persistent true; - } - key name; - leaf name { - type string; - } - leaf prefix { - type inet:ip-prefix; - config false; - tailf:cdb-oper { - tailf:persistent true; - } - } + uses response-grouping; - leaf prefix_id { - type uint64; - config false; - tailf:cdb-oper { - tailf:persistent true; - } - } - uses prefix-attributes; + leaf redeploy-service { + description "Redeploy service after succesful allocation"; + type string; } - - - } - leaf redeploy-service { - description "Redeploy service after succesful allocation"; - type string; - } } } } From b71599bcd6436c8c6db2da27c60ccb0b17dad7b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fred=20S=C3=B6derberg?= Date: Tue, 13 Sep 2016 21:00:41 +0200 Subject: [PATCH 09/27] nso-nipap: Added support for the new yang model --- .../net/spritelink/nsonipap/ConfigCdbSub.java | 48 ++++++++++--------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java b/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java index 86532fae0..1b05a4f12 100644 --- a/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java +++ b/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java @@ -78,7 +78,7 @@ public void init() { } } - protected void addPrefixFromPrefix(String path, Prefix parentPrefix, ConfKey prefixKey) throws Exception { + protected void addPrefixFromPrefix(String path, Prefix parentPrefix) throws Exception { LOGGER.info("Create, From prefix request, path = " + path); Prefix child_prefix = new Prefix(); @@ -87,7 +87,7 @@ protected void addPrefixFromPrefix(String path, Prefix parentPrefix, ConfKey pre AddPrefixOptions child_opts = new AddPrefixOptions(); child_opts.put("prefix_length", "32"); - child_prefix = getPrefixAttributes(th, maapi, path + "/" + nipap._from_prefix_request_ + prefixKey ); + child_prefix = getPrefixAttributes(th, maapi, path + "/" + nipap._attributes_ ); child_prefix.type = "host"; child_prefix.save(nipapCon, parentPrefix, child_opts); @@ -97,8 +97,7 @@ protected void addPrefixFromPrefix(String path, Prefix parentPrefix, ConfKey pre return; } //write response - wsess.create(path + "/" + nipap._response_ + "/" + nipap._from_prefix_ + prefixKey); - writeResponse(child_prefix, path + "/" + nipap._response_ + "/" + nipap._from_prefix_ + prefixKey); + writeResponse(child_prefix, path + "/" + nipap._response_); } @@ -185,11 +184,11 @@ protected void writeResponse(Prefix p, String responsePath) throws ConfException wsess.setElem(prefixValue, responsePath + "/" + nipap._prefix_); } - ConfUInt64 prefixIdValue = new ConfUInt64(p.id); + ConfUInt32 prefixIdValue = new ConfUInt32(p.id); wsess.setElem(prefixIdValue, responsePath + "/" + nipap._prefix_id_); - ConfBuf customerIdValue = new ConfBuf(p.customer_id); - if(customerIdValue != null){ + if(p.customer_id != null){ + ConfBuf customerIdValue = new ConfBuf(p.customer_id); wsess.setElem(customerIdValue, responsePath + "/" + nipap._customer_id_); } @@ -239,9 +238,10 @@ protected void removeChildPrefix(String prefixPath) throws ConfException, Except wsess.delete(prefixPath); } - protected void updatePrefix(String prefixPath, String responsePath) throws Exception { + protected void updatePrefix(String prefixPath) throws Exception { LOGGER.info("Update prefix: " + prefixPath); + String responsePath = prefixPath + "/" + nipap._response_; int p_id = (int) ((ConfUInt32)wsess.getElem(responsePath + "/" + nipap._prefix_id_)).longValue(); @@ -250,6 +250,7 @@ protected void updatePrefix(String prefixPath, String responsePath) throws Excep Prefix newPrefix = getPrefixAttributes(p, prefixPath + "/" + nipap._attributes_); newPrefix.save(nipapCon); + writeResponse(newPrefix, responsePath); } protected void removePrefix(String prefixPath) throws ConfException, Exception { @@ -356,18 +357,19 @@ public void run() { } // Write the result - String resPath = req.path + "/" + nipap._response_ + "/" + nipap._from_pool_; + String resPath = req.path + "/" + nipap._response_; writeResponse(p, resPath); - wsess.setCase(nipap._response_choice_, nipap._ok_, req.path + "/" + nipap._response_ ); + wsess.setCase(nipap._response_choice_, nipap._ok_, resPath); // Request prefix from prefix - if(maapi.exists(th, req.path + "/" + nipap._from_prefix_request_)){ - MaapiCursor pfx_cur = maapi.newCursor(th, req.path + "/" + nipap._from_prefix_request_); + String fromPrefixPath = req.path + "/" + nipap._from_prefix_request_; + if(maapi.exists(th, fromPrefixPath)){ + MaapiCursor pfx_cur = maapi.newCursor(th, fromPrefixPath); ConfKey pfx = null; while((pfx = maapi.getNext(pfx_cur)) != null) { - addPrefixFromPrefix(req.path.toString(), p, pfx); + addPrefixFromPrefix(fromPrefixPath + pfx, p); } } @@ -391,12 +393,12 @@ else if (req.op == Operation.ALLOCATE && req.t == Type.FromPrefixRequest){ nipap._from_pool_ + req.pool_key + "/" + nipap._request_ + req.request_key; int p_id = (int) ((ConfUInt32)wsess.getElem( - path +"/" +nipap._response_ + "/" + nipap._from_pool_ + "/" + + path +"/" +nipap._response_ + "/" + nipap._prefix_id_)).longValue(); Prefix parentPrefix = Prefix.get(nipapCon, p_id); - addPrefixFromPrefix(path, parentPrefix, req.prefix_key); + addPrefixFromPrefix(path + "/" + nipap._from_prefix_request_ + req.prefix_key, parentPrefix); } /* * Deallocate Prefix @@ -405,7 +407,7 @@ else if (req.op == Operation.ALLOCATE && req.t == Type.FromPrefixRequest){ else if (req.op == Operation.DEALLOCATE && (req.t == Type.Request)) { - String path = req.path + "/" + nipap._response_ + "/" + nipap._from_prefix_; + String path = req.path + "/" + nipap._from_prefix_request_ ; int n = wsess.getNumberOfInstances(path); try { @@ -413,13 +415,13 @@ else if (req.op == Operation.DEALLOCATE && for ( int i = 0; i < objArrList.size(); i++){ ConfObject[] objArr = objArrList.get(i); ConfKey t = new ConfKey(objArr[0]); - removeChildPrefix(req.path+ "/" + nipap._response_ + "/" + nipap._from_prefix_ + t); + removeChildPrefix(path + "/" + nipap._from_prefix_request_ + t + "/" + nipap._response_); } }catch (Exception e){ } - removePrefix(req.path + "/" + nipap._response_ + "/" + nipap._from_pool_); - removeResponse(req.path + "/" + nipap._response_ + "/" + nipap._from_pool_); + removePrefix(req.path + "/" + nipap._response_); + removeResponse(req.path + "/" + nipap._response_); } /* * Modify prefix attributes @@ -430,7 +432,7 @@ else if (req.op == Operation.SET && req.t == Type.Request){ String reqPath = "/services/" + nipap.prefix + ":" + nipap.prefix + "/" + nipap._from_pool_ + req.pool_key + "/" + nipap._request_ + req.request_key; - updatePrefix(reqPath, reqPath + "/" + nipap._response_ + "/" + nipap._from_pool_); + updatePrefix(reqPath); } /* @@ -439,9 +441,11 @@ else if (req.op == Operation.SET && req.t == Type.Request){ */ else if (req.op == Operation.SET && req.t == Type.FromPrefixRequest){ + String reqPath = "/services/" + nipap.prefix + ":" + nipap.prefix + "/" + + nipap._from_pool_ + req.pool_key + "/" + nipap._request_ + req.request_key + + nipap._from_prefix_request_ + req.prefix_key; - - + updatePrefix(reqPath); } } } From 75a8c60c2211b359ef5e37aa7a1a1fb3423afc41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fred=20S=C3=B6derberg?= Date: Wed, 14 Sep 2016 08:03:30 +0200 Subject: [PATCH 10/27] nso-nipap: Clean up --- .../net/spritelink/nsonipap/ConfigCdbSub.java | 116 +++++++----------- nso-nipap/nso-nipap/src/yang/nipap.yang | 2 +- 2 files changed, 46 insertions(+), 72 deletions(-) diff --git a/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java b/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java index 1b05a4f12..e90b415dc 100644 --- a/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java +++ b/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java @@ -3,7 +3,6 @@ import net.spritelink.nsonipap.namespaces.*; import java.util.*; -import java.math.BigInteger; import org.apache.log4j.Logger; @@ -87,7 +86,7 @@ protected void addPrefixFromPrefix(String path, Prefix parentPrefix) throws Exce AddPrefixOptions child_opts = new AddPrefixOptions(); child_opts.put("prefix_length", "32"); - child_prefix = getPrefixAttributes(th, maapi, path + "/" + nipap._attributes_ ); + child_prefix = getPrefixAttributes(path + "/" + nipap._attributes_ ); child_prefix.type = "host"; child_prefix.save(nipapCon, parentPrefix, child_opts); @@ -99,30 +98,13 @@ protected void addPrefixFromPrefix(String path, Prefix parentPrefix) throws Exce //write response writeResponse(child_prefix, path + "/" + nipap._response_); } - - - protected Prefix getPrefixAttributes(int th, Maapi maapi_, String attributePath) throws Exception - { + + protected Prefix getPrefixAttributes(String attributePath) throws Exception { + Prefix p = new Prefix(); - if (maapi_.exists(th, attributePath + "/" + nipap._customer_id_)) { - ConfValue rCustomer_id = maapi.getElem(th, attributePath + "/" + nipap._customer_id_); - p.customer_id = String.valueOf(rCustomer_id); - } - if (maapi_.exists(th, attributePath + "/" + nipap._description_)) { - ConfValue rDescription = maapi.getElem(th, attributePath + "/" + nipap._description_); - p.description = String.valueOf(rDescription); - } - if (maapi.exists(th, attributePath + "/" + nipap._node_)) { - ConfValue rNode = maapi.getElem(th, attributePath + "/" + nipap._node_); - p.node = String.valueOf(rNode); - } - if (maapi_.exists(th, attributePath + "/" + nipap._order_id_)) { - ConfValue rOrder_id = maapi.getElem(th, attributePath + "/" + nipap._customer_id_); - p.order_id = String.valueOf(rOrder_id); - } - - return p; + return getPrefixAttributes(p, attributePath); + } protected Prefix getPrefixAttributes(Prefix oldPrefix, String attributePath) throws Exception { @@ -156,15 +138,15 @@ protected Prefix getPrefixAttributes(Prefix oldPrefix, String attributePath) thr return p; } - protected AddPrefixOptions getPrefixOptions (int th, Maapi maapi_, String argumentPath) throws Exception { + protected AddPrefixOptions getPrefixOptions (String argumentPath) throws Exception { AddPrefixOptions opts = new AddPrefixOptions(); - ConfEnumeration family = (ConfEnumeration)maapi_.getElem( + ConfEnumeration family = (ConfEnumeration)maapi.getElem( th, argumentPath + "/" + nipap._family_); opts.put("family", family.getOrdinalValue()); - if (maapi_.exists(th, argumentPath + "/" + nipap._prefix_length_)) { - ConfValue pfx_length = maapi_.getElem(th, argumentPath + "/" + nipap._prefix_length_); + if (maapi.exists(th, argumentPath + "/" + nipap._prefix_length_)) { + ConfValue pfx_length = maapi.getElem(th, argumentPath + "/" + nipap._prefix_length_); opts.put("prefix_length", String.valueOf(pfx_length)); } @@ -231,19 +213,12 @@ protected void removeResponse(String responsePath) throws ConfException, Excepti } } - protected void removeChildPrefix(String prefixPath) throws ConfException, Exception { - - removePrefix(prefixPath); - LOGGER.info("delete path + " + prefixPath); - wsess.delete(prefixPath); - } - protected void updatePrefix(String prefixPath) throws Exception { LOGGER.info("Update prefix: " + prefixPath); String responsePath = prefixPath + "/" + nipap._response_; - int p_id = (int) ((ConfUInt32)wsess.getElem(responsePath + "/" + nipap._prefix_id_)).longValue(); + int p_id = getPrefixId(responsePath); Prefix p = Prefix.get(nipapCon, p_id); @@ -255,15 +230,20 @@ protected void updatePrefix(String prefixPath) throws Exception { protected void removePrefix(String prefixPath) throws ConfException, Exception { try { - ConfUInt64 p_id = (ConfUInt64)wsess.getElem(prefixPath + "/" + nipap._prefix_id_); + int p_id = getPrefixId(prefixPath); LOGGER.info("Removing prefix ID: " + p_id); - Prefix p = Prefix.get(nipapCon, Integer.parseInt(String.valueOf(p_id))); + Prefix p = Prefix.get(nipapCon, p_id); p.remove(nipapCon); } catch (Exception e) { LOGGER.error("Unable to remove prefix from NIPAP: " + e.getMessage(),e); } } + protected int getPrefixId(String path) throws Exception { + return (int) ((ConfUInt32)wsess.getElem(path + "/" + + nipap._prefix_id_)).longValue(); + } + public void run() { LOGGER.info("Starting the CDB subscriber..."); try { @@ -300,7 +280,6 @@ public void run() { for (Request req : reqs) { LOGGER.debug("Requested NIPAP action, op=" + req.op + " , type=" + req.t); - sub.sync(CdbSubscriptionSyncType.DONE_PRIORITY); try { // TODO: make backend configurable (now it is 'default') ConfValue bHost = maapi.getElem(th, "/services/nipap/backend{default}/hostname"); @@ -342,11 +321,11 @@ public void run() { } // options, like address-family - AddPrefixOptions opts = getPrefixOptions(th, maapi, req.path + "/" + nipap._arguments_); + AddPrefixOptions opts = getPrefixOptions(req.path + "/" + nipap._arguments_); //set prefix attributes String attrPath = req.path + "/" + nipap._attributes_; - p = getPrefixAttributes(th, maapi, attrPath); + p = getPrefixAttributes(attrPath); p.save(nipapCon, (Pool)poolRes.get(0), opts); @@ -389,12 +368,10 @@ public void run() { else if (req.op == Operation.ALLOCATE && req.t == Type.FromPrefixRequest){ LOGGER.info("Create, From prefix request"); - String path = "/ncs:services/" + nipap.prefix + ":" + nipap.prefix + "/" + + String path = "/" + ncs._services_ + "/" + nipap.prefix + ":" + nipap.prefix + "/" + nipap._from_pool_ + req.pool_key + "/" + nipap._request_ + req.request_key; - int p_id = (int) ((ConfUInt32)wsess.getElem( - path +"/" +nipap._response_ + "/" + - nipap._prefix_id_)).longValue(); + int p_id = getPrefixId(path + "/" + nipap._response_ ); Prefix parentPrefix = Prefix.get(nipapCon, p_id); @@ -408,20 +385,23 @@ else if (req.op == Operation.DEALLOCATE && (req.t == Type.Request)) { String path = req.path + "/" + nipap._from_prefix_request_ ; - int n = wsess.getNumberOfInstances(path); + NavuContext context = new NavuContext(maapi); + int to = context.startPreCommitRunningTrans(); + + NavuNode request = KeyPath2NavuNode.getNode(req.path, context); + + for(NavuContainer prefix_key : request.list("from-prefix-request").elements()){ + removePrefix(path + prefix_key.leaf("name").toKey() + "/" + nipap._response_); + removeResponse(path prefix_key.leaf("name").toKey() + "/" + nipap._response_); + } try { - List objArrList = wsess.getObjects(4, 0, n, path); - for ( int i = 0; i < objArrList.size(); i++){ - ConfObject[] objArr = objArrList.get(i); - ConfKey t = new ConfKey(objArr[0]); - removeChildPrefix(path + "/" + nipap._from_prefix_request_ + t + "/" + nipap._response_); - } - }catch (Exception e){ + removePrefix(req.path + "/" + nipap._response_); + } catch (Exception e ){ + continue; } - - removePrefix(req.path + "/" + nipap._response_); removeResponse(req.path + "/" + nipap._response_); + context.finishClearTrans(); } /* * Modify prefix attributes @@ -429,7 +409,7 @@ else if (req.op == Operation.DEALLOCATE && */ else if (req.op == Operation.SET && req.t == Type.Request){ - String reqPath = "/services/" + nipap.prefix + ":" + nipap.prefix + "/" + + String reqPath = "/" + Ncs._services_ + "/" + nipap.prefix + ":" + nipap.prefix + "/" + nipap._from_pool_ + req.pool_key + "/" + nipap._request_ + req.request_key; updatePrefix(reqPath); @@ -441,13 +421,14 @@ else if (req.op == Operation.SET && req.t == Type.Request){ */ else if (req.op == Operation.SET && req.t == Type.FromPrefixRequest){ - String reqPath = "/services/" + nipap.prefix + ":" + nipap.prefix + "/" + - nipap._from_pool_ + req.pool_key + "/" + nipap._request_ + req.request_key + + String reqPath = "/" + Ncs._services_ + "/" + nipap.prefix + ":" + nipap.prefix + "/" + + nipap._from_pool_ + req.pool_key + "/" + nipap._request_ + req.request_key + "/" + nipap._from_prefix_request_ + req.prefix_key; updatePrefix(reqPath); } } + sub.sync(CdbSubscriptionSyncType.DONE_PRIORITY); } } catch (SocketException e) { @@ -553,7 +534,6 @@ else if(kp[1].toString().equals("nipap:from-prefix-request") && break; } case MOP_VALUE_SET: { - LOGGER.info( "HEJ-->" + kp[2]); if(kp[1].toString().equals("nipap:attributes") && kp.length == 8) { r.pool_key = (ConfKey)kp[4]; @@ -571,25 +551,21 @@ else if(kp[1].toString().equals("nipap:from-prefix-request") && } } if(found == false){ - LOGGER.info("add " + r.request_key.toString()); reqs.add(r); } - } else if(kp[2].toString().equals("nipap:from-prefix-request") && - kp.length == 9){ + } else if(kp[3].toString().equals("nipap:from-prefix-request") && + kp.length == 10){ - r.pool_key = (ConfKey)kp[5]; - r.request_key = (ConfKey)kp[3]; - r.prefix_key = (ConfKey)kp[1]; + r.pool_key = (ConfKey)kp[6]; + r.request_key = (ConfKey)kp[4]; + r.prefix_key = (ConfKey)kp[2]; r.t = Type.FromPrefixRequest; r.op = Operation.SET; boolean found = false; for( Request req : reqs){ - LOGGER.info(req.t); - LOGGER.info(req.request_key + " " + r.request_key); - LOGGER.info(req.prefix_key + " " + r.prefix_key); if(req.t.equals(Type.FromPrefixRequest) && req.request_key.equals(r.request_key) && req.prefix_key.equals(r.prefix_key)) { @@ -600,12 +576,10 @@ else if(kp[1].toString().equals("nipap:from-prefix-request") && LOGGER.info("add " + r.prefix_key); reqs.add(r); } - - } break; } - } + } } catch (Exception e) { LOGGER.error("", e); diff --git a/nso-nipap/nso-nipap/src/yang/nipap.yang b/nso-nipap/nso-nipap/src/yang/nipap.yang index 38d8e9c7a..7e098fc3d 100644 --- a/nso-nipap/nso-nipap/src/yang/nipap.yang +++ b/nso-nipap/nso-nipap/src/yang/nipap.yang @@ -149,7 +149,7 @@ module nipap { leaf family { description "Address family"; - mandatory true; + default '4'; type enumeration { enum 4 { value 4; From 1772018d021e4bdd74f17507e67a31d03b4d92da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fred=20S=C3=B6derberg?= Date: Mon, 19 Sep 2016 20:20:37 +0200 Subject: [PATCH 11/27] nso-nipap: Add javadoc --- .../net/spritelink/nsonipap/ConfigCdbSub.java | 98 +++++++++++++++++-- 1 file changed, 90 insertions(+), 8 deletions(-) diff --git a/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java b/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java index e90b415dc..6a1bc7a9b 100644 --- a/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java +++ b/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java @@ -77,6 +77,13 @@ public void init() { } } + /** + * Add a host prefix from a prefix. + * + * @param path Path to the prefix request + * @param parentPrefix From prefix + * @throws Exception + */ protected void addPrefixFromPrefix(String path, Prefix parentPrefix) throws Exception { LOGGER.info("Create, From prefix request, path = " + path); @@ -99,13 +106,28 @@ protected void addPrefixFromPrefix(String path, Prefix parentPrefix) throws Exce writeResponse(child_prefix, path + "/" + nipap._response_); } + /** + * Fetch attributes and returns the populated prefix object + * + * @param attributePath Path to the prefix attribute container + * @return Prefix + * @throws Exception + */ protected Prefix getPrefixAttributes(String attributePath) throws Exception { Prefix p = new Prefix(); return getPrefixAttributes(p, attributePath); - } + + /** + * Fetch attributes and returns the populated prefix object + * + * @param oldPrefix Used if you already have a prefix object + * @param attributePath Path to the prefix attribute container + * @return Prefix + * @throws Exception + */ protected Prefix getPrefixAttributes(Prefix oldPrefix, String attributePath) throws Exception { Prefix p = oldPrefix; @@ -138,6 +160,14 @@ protected Prefix getPrefixAttributes(Prefix oldPrefix, String attributePath) thr return p; } + /** + * Populate PrefixOptions + * + * @param argumentPath Path to prefix argument container + * @return AddPrefixOptions + * @throws Exception + */ + protected AddPrefixOptions getPrefixOptions (String argumentPath) throws Exception { AddPrefixOptions opts = new AddPrefixOptions(); @@ -153,7 +183,15 @@ protected AddPrefixOptions getPrefixOptions (String argumentPath) throws Excepti return opts; } - protected void writeResponse(Prefix p, String responsePath) throws ConfException, Exception { + /** + * Write response data to cdb oper + * + * @param prefix Prefix + * @param responsePath Path were the response should be written. + * @throws ConfException + * @throws Exception + */ + protected void writeResponse(Prefix prefix, String responsePath) throws ConfException, Exception { if (p.family == 4) { ConfIPv4Prefix prefixValue = new ConfIPv4Prefix(p.prefix); @@ -190,14 +228,27 @@ protected void writeResponse(Prefix p, String responsePath) throws ConfException } } - + /** + * Write error message + * + * @param path Path to request + * @param errorMessage Error message + * @throws Exception + */ protected void writeError(String path, String errorMessage) throws Exception { wsess.setElem(new ConfBuf(errorMessage), path + "/" + nipap._response_ + "/" + nipap._error_); wsess.setCase(nipap._response_choice_, nipap._error_, path + "/" + nipap._response_); - } + /** + * Remove response from cdb oper. + * TODO: Do we need to do this? + * + * @param responsePath path to response + * @throws ConfException + * @throws Exception + */ protected void removeResponse(String responsePath) throws ConfException, Exception { //unset case @@ -213,6 +264,13 @@ protected void removeResponse(String responsePath) throws ConfException, Excepti } } + /** + * Update NIPAP with the new prefix information + * + * @param prefixPath Path to prefix request + * @throws Exception + */ + protected void updatePrefix(String prefixPath) throws Exception { LOGGER.info("Update prefix: " + prefixPath); @@ -228,6 +286,14 @@ protected void updatePrefix(String prefixPath) throws Exception { writeResponse(newPrefix, responsePath); } + /** + * Remove Prefix from NIPAP + * + * @param prefixPath path to Prefix + * @throws ConfException + * @throws Exception + */ + protected void removePrefix(String prefixPath) throws ConfException, Exception { try { int p_id = getPrefixId(prefixPath); @@ -238,7 +304,14 @@ protected void removePrefix(String prefixPath) throws ConfException, Exception { LOGGER.error("Unable to remove prefix from NIPAP: " + e.getMessage(),e); } } - + + /** + * Get prefix id + * + * @param path Path to prefix response + * @return Prefix id + * @throws Exception + */ protected int getPrefixId(String path) throws Exception { return (int) ((ConfUInt32)wsess.getElem(path + "/" + nipap._prefix_id_)).longValue(); @@ -391,9 +464,9 @@ else if (req.op == Operation.DEALLOCATE && NavuNode request = KeyPath2NavuNode.getNode(req.path, context); - for(NavuContainer prefix_key : request.list("from-prefix-request").elements()){ - removePrefix(path + prefix_key.leaf("name").toKey() + "/" + nipap._response_); - removeResponse(path prefix_key.leaf("name").toKey() + "/" + nipap._response_); + for(NavuContainer prefix_key : request.list(nipap._from_prefix_request).elements()){ + removePrefix(path + prefix_key.leaf(nipap._name_).toKey() + "/" + nipap._response_); + removeResponse(path prefix_key.leaf(nipap._name_).toKey() + "/" + nipap._response_); } try { removePrefix(req.path + "/" + nipap._response_); @@ -403,6 +476,15 @@ else if (req.op == Operation.DEALLOCATE && removeResponse(req.path + "/" + nipap._response_); context.finishClearTrans(); } + /* + * Deallocate from-prefix prefix + * + */ + else if (req.op == Operation.DEALLOCATE && + (req.t == Type.FromPrefixRequest) { + + + } /* * Modify prefix attributes * From 06186c6ab8e16655da6b856c075237960b1e6bce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fred=20S=C3=B6derberg?= Date: Mon, 19 Sep 2016 20:24:46 +0200 Subject: [PATCH 12/27] fix --- .../net/spritelink/nsonipap/ConfigCdbSub.java | 38 ++++++++++--------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java b/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java index 6a1bc7a9b..1f0c32466 100644 --- a/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java +++ b/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java @@ -193,37 +193,37 @@ protected AddPrefixOptions getPrefixOptions (String argumentPath) throws Excepti */ protected void writeResponse(Prefix prefix, String responsePath) throws ConfException, Exception { - if (p.family == 4) { - ConfIPv4Prefix prefixValue = new ConfIPv4Prefix(p.prefix); + if (prefix.family == 4) { + ConfIPv4Prefix prefixValue = new ConfIPv4Prefix(prefix.prefix); LOGGER.info("SET: " + responsePath + "/prefix -> " + prefixValue); wsess.setElem(prefixValue, responsePath + "/" + nipap._prefix_); - } else if (p.family == 6) { + } else if (prefix.family == 6) { - ConfIPv6Prefix prefixValue = new ConfIPv6Prefix(p.prefix); + ConfIPv6Prefix prefixValue = new ConfIPv6Prefix(prefix.prefix); LOGGER.info("SET: " + responsePath + "/prefix -> " + prefixValue); wsess.setElem(prefixValue, responsePath + "/" + nipap._prefix_); } - ConfUInt32 prefixIdValue = new ConfUInt32(p.id); + ConfUInt32 prefixIdValue = new ConfUInt32(prefix.id); wsess.setElem(prefixIdValue, responsePath + "/" + nipap._prefix_id_); - if(p.customer_id != null){ - ConfBuf customerIdValue = new ConfBuf(p.customer_id); + if(prefix.customer_id != null){ + ConfBuf customerIdValue = new ConfBuf(prefix.customer_id); wsess.setElem(customerIdValue, responsePath + "/" + nipap._customer_id_); } - if(p.description != null){ - ConfBuf descriptionValue = new ConfBuf(p.description); + if(prefix.description != null){ + ConfBuf descriptionValue = new ConfBuf(prefix.description); wsess.setElem(descriptionValue, responsePath + "/" + nipap._description_); } - if(p.node != null){ - ConfBuf nodeValue = new ConfBuf(p.node); + if(prefix.node != null){ + ConfBuf nodeValue = new ConfBuf(prefix.node); wsess.setElem(nodeValue, responsePath + "/" + nipap._node_); } - if(p.order_id != null){ - ConfBuf orderIdValue = new ConfBuf(p.order_id); + if(prefix.order_id != null){ + ConfBuf orderIdValue = new ConfBuf(prefix.order_id); wsess.setElem(orderIdValue, responsePath + "/" + nipap._order_id_); } } @@ -441,7 +441,7 @@ public void run() { else if (req.op == Operation.ALLOCATE && req.t == Type.FromPrefixRequest){ LOGGER.info("Create, From prefix request"); - String path = "/" + ncs._services_ + "/" + nipap.prefix + ":" + nipap.prefix + "/" + + String path = "/" + Ncs._services_ + "/" + nipap.prefix + ":" + nipap.prefix + "/" + nipap._from_pool_ + req.pool_key + "/" + nipap._request_ + req.request_key; int p_id = getPrefixId(path + "/" + nipap._response_ ); @@ -466,7 +466,7 @@ else if (req.op == Operation.DEALLOCATE && for(NavuContainer prefix_key : request.list(nipap._from_prefix_request).elements()){ removePrefix(path + prefix_key.leaf(nipap._name_).toKey() + "/" + nipap._response_); - removeResponse(path prefix_key.leaf(nipap._name_).toKey() + "/" + nipap._response_); + removeResponse(path + prefix_key.leaf(nipap._name_).toKey() + "/" + nipap._response_); } try { removePrefix(req.path + "/" + nipap._response_); @@ -481,7 +481,7 @@ else if (req.op == Operation.DEALLOCATE && * */ else if (req.op == Operation.DEALLOCATE && - (req.t == Type.FromPrefixRequest) { + (req.t == Type.FromPrefixRequest)) { } @@ -598,8 +598,7 @@ else if(kp[1].toString().equals("nipap:from-prefix-request") && reqs.add(r); //the request is new, we dont need to look at children return DiffIterateResultFlag.ITER_CONTINUE; - } - + } break; } case MOP_DELETED: { @@ -736,4 +735,7 @@ public void run() { } } } + + + } From 44e3d4763cc07c4af6a628637c052f5b20512d1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fred=20S=C3=B6derberg?= Date: Tue, 20 Sep 2016 15:09:20 +0200 Subject: [PATCH 13/27] nso-nipap: Indent fix --- .../net/spritelink/nsonipap/ConfigCdbSub.java | 746 +++++++++--------- 1 file changed, 370 insertions(+), 376 deletions(-) diff --git a/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java b/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java index 1f0c32466..48372ac68 100644 --- a/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java +++ b/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java @@ -35,19 +35,19 @@ public ConfigCdbSub() { @Resource(type=ResourceType.CDB, scope=Scope.CONTEXT, qualifier="reactive-fm-loop-subscriber") - private Cdb cdb; + private Cdb cdb; @Resource(type=ResourceType.CDB, scope=Scope.CONTEXT, qualifier="w-reactive-fm-loop") - private Cdb wcdb; + private Cdb wcdb; @Resource(type=ResourceType.MAAPI, scope=Scope.INSTANCE, qualifier="reactive-fm-m") - private Maapi maapi; + private Maapi maapi; - private int th = -1; + private int th = -1; - private Connection nipapCon; + private Connection nipapCon; public void init() { LOGGER.info("Starting the CDB Connection..."); @@ -58,15 +58,15 @@ public void init() { new String[] {"admin"}, MaapiUserSessionFlag.PROTO_TCP); - th = maapi.startTrans(Conf.DB_RUNNING, Conf.MODE_READ); + th = maapi.startTrans(Conf.DB_RUNNING, Conf.MODE_READ); - sub = cdb.newSubscription(); + sub = cdb.newSubscription(); - sub.subscribe(1, new nipap(), - "/services/" + - nipap.prefix + "/" + - nipap._from_pool_ + "/" + - nipap._request_); + sub.subscribe(1, new nipap(), + "/services/" + + nipap.prefix + "/" + + nipap._from_pool_ + "/" + + nipap._request_); // Tell CDB we are ready for notifications sub.subscribeDone(); @@ -86,26 +86,26 @@ public void init() { */ protected void addPrefixFromPrefix(String path, Prefix parentPrefix) throws Exception { - LOGGER.info("Create, From prefix request, path = " + path); - Prefix child_prefix = new Prefix(); - try { + LOGGER.info("Create, From prefix request, path = " + path); + Prefix child_prefix = new Prefix(); + try { - AddPrefixOptions child_opts = new AddPrefixOptions(); - child_opts.put("prefix_length", "32"); + AddPrefixOptions child_opts = new AddPrefixOptions(); + child_opts.put("prefix_length", "32"); - child_prefix = getPrefixAttributes(path + "/" + nipap._attributes_ ); - child_prefix.type = "host"; - child_prefix.save(nipapCon, parentPrefix, child_opts); + child_prefix = getPrefixAttributes(path + "/" + nipap._attributes_ ); + child_prefix.type = "host"; + child_prefix.save(nipapCon, parentPrefix, child_opts); - }catch (Exception e ) { - LOGGER.error("Unable to get prefix from prefix" + e.getMessage(), e); - writeError(path, e.getMessage()); - return; - } - //write response - writeResponse(child_prefix, path + "/" + nipap._response_); + }catch (Exception e ) { + LOGGER.error("Unable to get prefix from prefix" + e.getMessage(), e); + writeError(path, e.getMessage()); + return; + } + //write response + writeResponse(child_prefix, path + "/" + nipap._response_); } - + /** * Fetch attributes and returns the populated prefix object * @@ -114,10 +114,10 @@ protected void addPrefixFromPrefix(String path, Prefix parentPrefix) throws Exce * @throws Exception */ protected Prefix getPrefixAttributes(String attributePath) throws Exception { - - Prefix p = new Prefix(); - return getPrefixAttributes(p, attributePath); + Prefix p = new Prefix(); + + return getPrefixAttributes(p, attributePath); } /** @@ -130,34 +130,34 @@ protected Prefix getPrefixAttributes(String attributePath) throws Exception { */ protected Prefix getPrefixAttributes(Prefix oldPrefix, String attributePath) throws Exception { - Prefix p = oldPrefix; - - if (maapi.exists(th, attributePath + "/" + nipap._customer_id_)) { - ConfValue rCustomer_id = maapi.getElem(th, attributePath + "/" + nipap._customer_id_); - p.customer_id = String.valueOf(rCustomer_id); - }else { - p.customer_id = null; - } - if (maapi.exists(th, attributePath + "/" + nipap._description_)) { - ConfValue rDescription = maapi.getElem(th, attributePath + "/" + nipap._description_); - p.description = String.valueOf(rDescription); - }else { - p.description = null; - } - if (maapi.exists(th, attributePath + "/" + nipap._node_)) { - ConfValue rNode = maapi.getElem(th, attributePath + "/" + nipap._node_); - p.node = String.valueOf(rNode); - }else { - p.node = null; - } - if (maapi.exists(th, attributePath + "/" + nipap._order_id_)) { - ConfValue rOrder_id = maapi.getElem(th, attributePath + "/" + nipap._customer_id_); - p.order_id = String.valueOf(rOrder_id); - }else { - p.order_id = null; - } - - return p; + Prefix p = oldPrefix; + + if (maapi.exists(th, attributePath + "/" + nipap._customer_id_)) { + ConfValue rCustomer_id = maapi.getElem(th, attributePath + "/" + nipap._customer_id_); + p.customer_id = String.valueOf(rCustomer_id); + }else { + p.customer_id = null; + } + if (maapi.exists(th, attributePath + "/" + nipap._description_)) { + ConfValue rDescription = maapi.getElem(th, attributePath + "/" + nipap._description_); + p.description = String.valueOf(rDescription); + }else { + p.description = null; + } + if (maapi.exists(th, attributePath + "/" + nipap._node_)) { + ConfValue rNode = maapi.getElem(th, attributePath + "/" + nipap._node_); + p.node = String.valueOf(rNode); + }else { + p.node = null; + } + if (maapi.exists(th, attributePath + "/" + nipap._order_id_)) { + ConfValue rOrder_id = maapi.getElem(th, attributePath + "/" + nipap._customer_id_); + p.order_id = String.valueOf(rOrder_id); + }else { + p.order_id = null; + } + + return p; } /** @@ -169,18 +169,18 @@ protected Prefix getPrefixAttributes(Prefix oldPrefix, String attributePath) thr */ protected AddPrefixOptions getPrefixOptions (String argumentPath) throws Exception { - AddPrefixOptions opts = new AddPrefixOptions(); + AddPrefixOptions opts = new AddPrefixOptions(); - ConfEnumeration family = (ConfEnumeration)maapi.getElem( - th, argumentPath + "/" + nipap._family_); - opts.put("family", family.getOrdinalValue()); + ConfEnumeration family = (ConfEnumeration)maapi.getElem( + th, argumentPath + "/" + nipap._family_); + opts.put("family", family.getOrdinalValue()); - if (maapi.exists(th, argumentPath + "/" + nipap._prefix_length_)) { - ConfValue pfx_length = maapi.getElem(th, argumentPath + "/" + nipap._prefix_length_); - opts.put("prefix_length", String.valueOf(pfx_length)); - } + if (maapi.exists(th, argumentPath + "/" + nipap._prefix_length_)) { + ConfValue pfx_length = maapi.getElem(th, argumentPath + "/" + nipap._prefix_length_); + opts.put("prefix_length", String.valueOf(pfx_length)); + } - return opts; + return opts; } /** @@ -193,39 +193,39 @@ protected AddPrefixOptions getPrefixOptions (String argumentPath) throws Excepti */ protected void writeResponse(Prefix prefix, String responsePath) throws ConfException, Exception { - if (prefix.family == 4) { - ConfIPv4Prefix prefixValue = new ConfIPv4Prefix(prefix.prefix); - LOGGER.info("SET: " + responsePath + "/prefix -> " + prefixValue); - wsess.setElem(prefixValue, responsePath + "/" + nipap._prefix_); - } else if (prefix.family == 6) { - - ConfIPv6Prefix prefixValue = new ConfIPv6Prefix(prefix.prefix); - LOGGER.info("SET: " + responsePath + "/prefix -> " + prefixValue); - wsess.setElem(prefixValue, responsePath + "/" + nipap._prefix_); - } - - ConfUInt32 prefixIdValue = new ConfUInt32(prefix.id); - wsess.setElem(prefixIdValue, responsePath + "/" + nipap._prefix_id_); - - if(prefix.customer_id != null){ - ConfBuf customerIdValue = new ConfBuf(prefix.customer_id); - wsess.setElem(customerIdValue, responsePath + "/" + nipap._customer_id_); - } - - if(prefix.description != null){ - ConfBuf descriptionValue = new ConfBuf(prefix.description); - wsess.setElem(descriptionValue, responsePath + "/" + nipap._description_); - } - - if(prefix.node != null){ - ConfBuf nodeValue = new ConfBuf(prefix.node); - wsess.setElem(nodeValue, responsePath + "/" + nipap._node_); - } - - if(prefix.order_id != null){ - ConfBuf orderIdValue = new ConfBuf(prefix.order_id); - wsess.setElem(orderIdValue, responsePath + "/" + nipap._order_id_); - } + if (prefix.family == 4) { + ConfIPv4Prefix prefixValue = new ConfIPv4Prefix(prefix.prefix); + LOGGER.info("SET: " + responsePath + "/prefix -> " + prefixValue); + wsess.setElem(prefixValue, responsePath + "/" + nipap._prefix_); + } else if (prefix.family == 6) { + + ConfIPv6Prefix prefixValue = new ConfIPv6Prefix(prefix.prefix); + LOGGER.info("SET: " + responsePath + "/prefix -> " + prefixValue); + wsess.setElem(prefixValue, responsePath + "/" + nipap._prefix_); + } + + ConfUInt32 prefixIdValue = new ConfUInt32(prefix.id); + wsess.setElem(prefixIdValue, responsePath + "/" + nipap._prefix_id_); + + if(prefix.customer_id != null){ + ConfBuf customerIdValue = new ConfBuf(prefix.customer_id); + wsess.setElem(customerIdValue, responsePath + "/" + nipap._customer_id_); + } + + if(prefix.description != null){ + ConfBuf descriptionValue = new ConfBuf(prefix.description); + wsess.setElem(descriptionValue, responsePath + "/" + nipap._description_); + } + + if(prefix.node != null){ + ConfBuf nodeValue = new ConfBuf(prefix.node); + wsess.setElem(nodeValue, responsePath + "/" + nipap._node_); + } + + if(prefix.order_id != null){ + ConfBuf orderIdValue = new ConfBuf(prefix.order_id); + wsess.setElem(orderIdValue, responsePath + "/" + nipap._order_id_); + } } /** @@ -237,8 +237,8 @@ protected void writeResponse(Prefix prefix, String responsePath) throws ConfExce */ protected void writeError(String path, String errorMessage) throws Exception { - wsess.setElem(new ConfBuf(errorMessage), path + "/" + nipap._response_ + "/" + nipap._error_); - wsess.setCase(nipap._response_choice_, nipap._error_, path + "/" + nipap._response_); + wsess.setElem(new ConfBuf(errorMessage), path + "/" + nipap._response_ + "/" + nipap._error_); + wsess.setCase(nipap._response_choice_, nipap._error_, path + "/" + nipap._response_); } /** @@ -250,18 +250,18 @@ protected void writeError(String path, String errorMessage) throws Exception { * @throws Exception */ protected void removeResponse(String responsePath) throws ConfException, Exception { - //unset case - - LOGGER.info("remove response " + responsePath); - try { - wsess.delete(responsePath + "/" + nipap._prefix_); - wsess.delete(responsePath + "/" + nipap._prefix_id_); - wsess.delete(responsePath + "/" + nipap._customer_id_); - wsess.delete(responsePath + "/" + nipap._description_); - wsess.delete(responsePath + "/" + nipap._node_); - wsess.delete(responsePath + "/" + nipap._order_id_); - } catch (CdbException e ){ - } + //unset case + + LOGGER.info("remove response " + responsePath); + try { + wsess.delete(responsePath + "/" + nipap._prefix_); + wsess.delete(responsePath + "/" + nipap._prefix_id_); + wsess.delete(responsePath + "/" + nipap._customer_id_); + wsess.delete(responsePath + "/" + nipap._description_); + wsess.delete(responsePath + "/" + nipap._node_); + wsess.delete(responsePath + "/" + nipap._order_id_); + } catch (CdbException e ){ + } } /** @@ -273,17 +273,17 @@ protected void removeResponse(String responsePath) throws ConfException, Excepti protected void updatePrefix(String prefixPath) throws Exception { - LOGGER.info("Update prefix: " + prefixPath); - String responsePath = prefixPath + "/" + nipap._response_; + LOGGER.info("Update prefix: " + prefixPath); + String responsePath = prefixPath + "/" + nipap._response_; - int p_id = getPrefixId(responsePath); + int p_id = getPrefixId(responsePath); - Prefix p = Prefix.get(nipapCon, p_id); + Prefix p = Prefix.get(nipapCon, p_id); - Prefix newPrefix = getPrefixAttributes(p, prefixPath + "/" + nipap._attributes_); + Prefix newPrefix = getPrefixAttributes(p, prefixPath + "/" + nipap._attributes_); - newPrefix.save(nipapCon); - writeResponse(newPrefix, responsePath); + newPrefix.save(nipapCon); + writeResponse(newPrefix, responsePath); } /** @@ -295,16 +295,16 @@ protected void updatePrefix(String prefixPath) throws Exception { */ protected void removePrefix(String prefixPath) throws ConfException, Exception { - try { - int p_id = getPrefixId(prefixPath); - LOGGER.info("Removing prefix ID: " + p_id); - Prefix p = Prefix.get(nipapCon, p_id); - p.remove(nipapCon); - } catch (Exception e) { - LOGGER.error("Unable to remove prefix from NIPAP: " + e.getMessage(),e); - } + try { + int p_id = getPrefixId(prefixPath); + LOGGER.info("Removing prefix ID: " + p_id); + Prefix p = Prefix.get(nipapCon, p_id); + p.remove(nipapCon); + } catch (Exception e) { + LOGGER.error("Unable to remove prefix from NIPAP: " + e.getMessage(),e); + } } - + /** * Get prefix id * @@ -313,8 +313,8 @@ protected void removePrefix(String prefixPath) throws ConfException, Exception { * @throws Exception */ protected int getPrefixId(String path) throws Exception { - return (int) ((ConfUInt32)wsess.getElem(path + "/" + - nipap._prefix_id_)).longValue(); + return (int) ((ConfUInt32)wsess.getElem(path + "/" + + nipap._prefix_id_)).longValue(); } public void run() { @@ -333,10 +333,10 @@ public void run() { } // DiffIterateFlags tell our DiffIterator implementation what values we want EnumSet enumSet = - EnumSet.of( - DiffIterateFlags.ITER_WANT_PREV, - DiffIterateFlags.ITER_WANT_ANCESTOR_DELETE, - DiffIterateFlags.ITER_WANT_SCHEMA_ORDER); + EnumSet.of( + DiffIterateFlags.ITER_WANT_PREV, + DiffIterateFlags.ITER_WANT_ANCESTOR_DELETE, + DiffIterateFlags.ITER_WANT_SCHEMA_ORDER); ArrayList reqs = new ArrayList(); try { // Iterate through the diff tree using the Iter class @@ -353,163 +353,162 @@ public void run() { for (Request req : reqs) { LOGGER.debug("Requested NIPAP action, op=" + req.op + " , type=" + req.t); - try { - // TODO: make backend configurable (now it is 'default') - ConfValue bHost = maapi.getElem(th, "/services/nipap/backend{default}/hostname"); - ConfValue bPort = maapi.getElem(th, "/services/nipap/backend{default}/port"); - ConfValue bUser = maapi.getElem(th, "/services/nipap/backend{default}/username"); - ConfValue bPass = maapi.getElem(th, "/services/nipap/backend{default}/password"); + try { + // TODO: make backend configurable (now it is 'default') + ConfValue bHost = maapi.getElem(th, "/services/nipap/backend{default}/hostname"); + ConfValue bPort = maapi.getElem(th, "/services/nipap/backend{default}/port"); + ConfValue bUser = maapi.getElem(th, "/services/nipap/backend{default}/username"); + ConfValue bPass = maapi.getElem(th, "/services/nipap/backend{default}/password"); - URL url = new URL("http://" + String.valueOf(bHost) + ":" + String.valueOf(bPort) + "/RPC2"); - nipapCon = new Connection(url, String.valueOf(bUser), String.valueOf(bPass)); - nipapCon.authoritative_source = "ncs"; + URL url = new URL("http://" + String.valueOf(bHost) + ":" + String.valueOf(bPort) + "/RPC2"); + nipapCon = new Connection(url, String.valueOf(bUser), String.valueOf(bPass)); + nipapCon.authoritative_source = "ncs"; - } catch (Exception e) { - LOGGER.error("Unable to initiate connection to NIPAP: " + e.getMessage()); - continue; - } + } catch (Exception e) { + LOGGER.error("Unable to initiate connection to NIPAP: " + e.getMessage()); + continue; + } - /* - * Allocate new Prefix - * - */ - if (req.op == Operation.ALLOCATE && req.t == Type.Request ) { + /* + * Allocate new Prefix + * + */ + if (req.op == Operation.ALLOCATE && req.t == Type.Request ) { - LOGGER.info("Trying to allocate a prefix for: " + req.request_key + " from pool: " + req.pool_key); + LOGGER.info("Trying to allocate a prefix for: " + req.request_key + " from pool: " + req.pool_key); - String poolName = String.valueOf(req.pool_key).replaceAll("[{}]", ""); + String poolName = String.valueOf(req.pool_key).replaceAll("[{}]", ""); - Prefix p = new Prefix(); - // Gather prefix data and perform NIPAP request - try { - // Pool - HashMap poolSpec = new HashMap<>(); - poolSpec.put("name", poolName); - List poolRes = Pool.list(nipapCon, poolSpec); + Prefix p = new Prefix(); + // Gather prefix data and perform NIPAP request + try { + // Pool + HashMap poolSpec = new HashMap<>(); + poolSpec.put("name", poolName); + List poolRes = Pool.list(nipapCon, poolSpec); - if(poolRes.size() < 1){ - writeError(req.path.toString(), "Nipap pool not found"); - continue; - } + if(poolRes.size() < 1){ + writeError(req.path.toString(), "Nipap pool not found"); + continue; + } - // options, like address-family - AddPrefixOptions opts = getPrefixOptions(req.path + "/" + nipap._arguments_); + // options, like address-family + AddPrefixOptions opts = getPrefixOptions(req.path + "/" + nipap._arguments_); - //set prefix attributes - String attrPath = req.path + "/" + nipap._attributes_; - p = getPrefixAttributes(attrPath); + //set prefix attributes + String attrPath = req.path + "/" + nipap._attributes_; + p = getPrefixAttributes(attrPath); - p.save(nipapCon, (Pool)poolRes.get(0), opts); + p.save(nipapCon, (Pool)poolRes.get(0), opts); - } catch (Exception e) { - LOGGER.error("Unable to get prefix from NIPAP: " + e.getMessage(), e); - writeError(req.path.toString(), e.getMessage()); - continue; - } + } catch (Exception e) { + LOGGER.error("Unable to get prefix from NIPAP: " + e.getMessage(), e); + writeError(req.path.toString(), e.getMessage()); + continue; + } - // Write the result - String resPath = req.path + "/" + nipap._response_; - writeResponse(p, resPath); + // Write the result + String resPath = req.path + "/" + nipap._response_; + writeResponse(p, resPath); - wsess.setCase(nipap._response_choice_, nipap._ok_, resPath); + wsess.setCase(nipap._response_choice_, nipap._ok_, resPath); - // Request prefix from prefix - String fromPrefixPath = req.path + "/" + nipap._from_prefix_request_; - if(maapi.exists(th, fromPrefixPath)){ - MaapiCursor pfx_cur = maapi.newCursor(th, fromPrefixPath); - ConfKey pfx = null; + // Request prefix from prefix + String fromPrefixPath = req.path + "/" + nipap._from_prefix_request_; + if(maapi.exists(th, fromPrefixPath)){ + MaapiCursor pfx_cur = maapi.newCursor(th, fromPrefixPath); + ConfKey pfx = null; - while((pfx = maapi.getNext(pfx_cur)) != null) { - addPrefixFromPrefix(fromPrefixPath + pfx, p); - } - } + while((pfx = maapi.getNext(pfx_cur)) != null) { + addPrefixFromPrefix(fromPrefixPath + pfx, p); + } + } - // Redeploy - try { - ConfValue redeployPath = maapi.getElem(th, req.path + "/redeploy-service"); - LOGGER.info("redeploy-service: " + redeployPath); - redeploy(redeployPath.toString()); - } catch (Exception e) { - LOGGER.error("Redeploy failed: " + e.getMessage()); - } - } - /* - * Allocate from-prefix - * - */ - else if (req.op == Operation.ALLOCATE && req.t == Type.FromPrefixRequest){ - LOGGER.info("Create, From prefix request"); - - String path = "/" + Ncs._services_ + "/" + nipap.prefix + ":" + nipap.prefix + "/" + - nipap._from_pool_ + req.pool_key + "/" + nipap._request_ + req.request_key; - - int p_id = getPrefixId(path + "/" + nipap._response_ ); - - Prefix parentPrefix = Prefix.get(nipapCon, p_id); - - addPrefixFromPrefix(path + "/" + nipap._from_prefix_request_ + req.prefix_key, parentPrefix); - } - /* - * Deallocate Prefix - * - */ - else if (req.op == Operation.DEALLOCATE && - (req.t == Type.Request)) { - - String path = req.path + "/" + nipap._from_prefix_request_ ; - - NavuContext context = new NavuContext(maapi); - int to = context.startPreCommitRunningTrans(); - - NavuNode request = KeyPath2NavuNode.getNode(req.path, context); - - for(NavuContainer prefix_key : request.list(nipap._from_prefix_request).elements()){ - removePrefix(path + prefix_key.leaf(nipap._name_).toKey() + "/" + nipap._response_); - removeResponse(path + prefix_key.leaf(nipap._name_).toKey() + "/" + nipap._response_); - } - try { - removePrefix(req.path + "/" + nipap._response_); - } catch (Exception e ){ - continue; - } - removeResponse(req.path + "/" + nipap._response_); - context.finishClearTrans(); - } - /* - * Deallocate from-prefix prefix - * - */ - else if (req.op == Operation.DEALLOCATE && - (req.t == Type.FromPrefixRequest)) { - - - } - /* - * Modify prefix attributes - * - */ - else if (req.op == Operation.SET && req.t == Type.Request){ - - String reqPath = "/" + Ncs._services_ + "/" + nipap.prefix + ":" + nipap.prefix + "/" + - nipap._from_pool_ + req.pool_key + "/" + nipap._request_ + req.request_key; - - updatePrefix(reqPath); - - } - /* - * Modify from-prefix attributes - * - */ - else if (req.op == Operation.SET && req.t == Type.FromPrefixRequest){ - - String reqPath = "/" + Ncs._services_ + "/" + nipap.prefix + ":" + nipap.prefix + "/" + - nipap._from_pool_ + req.pool_key + "/" + nipap._request_ + req.request_key + "/" + - nipap._from_prefix_request_ + req.prefix_key; - - updatePrefix(reqPath); - } - } + // Redeploy + try { + ConfValue redeployPath = maapi.getElem(th, req.path + "/redeploy-service"); + LOGGER.info("redeploy-service: " + redeployPath); + redeploy(redeployPath.toString()); + } catch (Exception e) { + LOGGER.error("Redeploy failed: " + e.getMessage()); + } + } + /* + * Allocate from-prefix + * + */ + else if (req.op == Operation.ALLOCATE && req.t == Type.FromPrefixRequest){ + LOGGER.info("Create, From prefix request"); + + String path = "/" + Ncs._services_ + "/" + nipap.prefix + ":" + nipap.prefix + "/" + + nipap._from_pool_ + req.pool_key + "/" + nipap._request_ + req.request_key; + + int p_id = getPrefixId(path + "/" + nipap._response_ ); + + Prefix parentPrefix = Prefix.get(nipapCon, p_id); + + addPrefixFromPrefix(path + "/" + nipap._from_prefix_request_ + req.prefix_key, parentPrefix); + } + /* + * Deallocate Prefix + * + */ + else if (req.op == Operation.DEALLOCATE && + (req.t == Type.Request)) { + + String path = req.path + "/" + nipap._from_prefix_request_ ; + + NavuContext context = new NavuContext(maapi); + int to = context.startPreCommitRunningTrans(); + + NavuNode request = KeyPath2NavuNode.getNode(req.path, context); + + for(NavuContainer prefix_key : request.list(nipap._from_prefix_request).elements()){ + removePrefix(path + prefix_key.leaf(nipap._name_).toKey() + "/" + nipap._response_); + removeResponse(path + prefix_key.leaf(nipap._name_).toKey() + "/" + nipap._response_); + } + try { + removePrefix(req.path + "/" + nipap._response_); + } catch (Exception e ){ + continue; + } + removeResponse(req.path + "/" + nipap._response_); + context.finishClearTrans(); + } + /* + * Deallocate from-prefix prefix + * + */ + else if (req.op == Operation.DEALLOCATE && (req.t == Type.FromPrefixRequest)) { + + + } + /* + * Modify prefix attributes + * + */ + else if (req.op == Operation.SET && req.t == Type.Request){ + + String reqPath = "/" + Ncs._services_ + "/" + nipap.prefix + ":" + nipap.prefix + "/" + + nipap._from_pool_ + req.pool_key + "/" + nipap._request_ + req.request_key; + + updatePrefix(reqPath); + + } + /* + * Modify from-prefix attributes + * + */ + else if (req.op == Operation.SET && req.t == Type.FromPrefixRequest){ + + String reqPath = "/" + Ncs._services_ + "/" + nipap.prefix + ":" + nipap.prefix + "/" + + nipap._from_pool_ + req.pool_key + "/" + nipap._request_ + req.request_key + "/" + + nipap._from_prefix_request_ + req.prefix_key; + + updatePrefix(reqPath); + } + } sub.sync(CdbSubscriptionSyncType.DONE_PRIORITY); } } @@ -573,101 +572,99 @@ public DiffIterateResultFlag iterate( Request r = new Request(); r.path = p; - switch(op) { - case MOP_CREATED: { - // new request - if(kp[1].toString().equals("nipap:request") && - kp.length == 6){ - r.pool_key = (ConfKey)kp[2]; - r.request_key = (ConfKey)kp[0]; - r.t = Type.Request; - r.op = Operation.ALLOCATE; - reqs.add(r); - //the request is new, we dont need to look at children - return DiffIterateResultFlag.ITER_CONTINUE; - } - else if(kp[1].toString().equals("nipap:from-prefix-request") && - kp.length == 8){ - r.prefix_key = (ConfKey)kp[0]; - r.request_key = (ConfKey)kp[2]; - r.pool_key = (ConfKey)kp[4]; - r.t = Type.FromPrefixRequest; - r.op = Operation.ALLOCATE; - reqs.add(r); - //the request is new, we dont need to look at children - return DiffIterateResultFlag.ITER_CONTINUE; - } - break; - } - case MOP_DELETED: { - if(kp[1].toString().equals("nipap:request") && - kp.length == 6){ - r.pool_key = (ConfKey)kp[2]; - r.request_key = (ConfKey)kp[0]; - r.t = Type.Request; - r.op = Operation.DEALLOCATE; - reqs.add(r); - //we dont need to look at children - return DiffIterateResultFlag.ITER_CONTINUE; - } - break; - } - case MOP_VALUE_SET: { - if(kp[1].toString().equals("nipap:attributes") && - kp.length == 8) { - r.pool_key = (ConfKey)kp[4]; - r.request_key = (ConfKey)kp[2]; - r.t = Type.Request; - r.op = Operation.SET; - - boolean found = false; - - for( Request req : reqs){ - - if(req.t.equals(Type.Request) && - req.request_key.equals(r.request_key)) { - found = true; - } - } - if(found == false){ + case MOP_CREATED: { + // new request + if(kp[1].toString().equals("nipap:request") && + kp.length == 6){ + r.pool_key = (ConfKey)kp[2]; + r.request_key = (ConfKey)kp[0]; + r.t = Type.Request; + r.op = Operation.ALLOCATE; reqs.add(r); - } - - } else if(kp[3].toString().equals("nipap:from-prefix-request") && - kp.length == 10){ - - r.pool_key = (ConfKey)kp[6]; - r.request_key = (ConfKey)kp[4]; - r.prefix_key = (ConfKey)kp[2]; - r.t = Type.FromPrefixRequest; - r.op = Operation.SET; - - boolean found = false; - - for( Request req : reqs){ - if(req.t.equals(Type.FromPrefixRequest) && - req.request_key.equals(r.request_key) && - req.prefix_key.equals(r.prefix_key)) { - found = true; - } + //the request is new, we dont need to look at children + return DiffIterateResultFlag.ITER_CONTINUE; + } + else if(kp[1].toString().equals("nipap:from-prefix-request") && + kp.length == 8){ + r.prefix_key = (ConfKey)kp[0]; + r.request_key = (ConfKey)kp[2]; + r.pool_key = (ConfKey)kp[4]; + r.t = Type.FromPrefixRequest; + r.op = Operation.ALLOCATE; + reqs.add(r); + //the request is new, we dont need to look at children + return DiffIterateResultFlag.ITER_CONTINUE; + } + break; } - if(found == false){ - LOGGER.info("add " + r.prefix_key); - reqs.add(r); + case MOP_DELETED: { + if(kp[1].toString().equals("nipap:request") && + kp.length == 6){ + r.pool_key = (ConfKey)kp[2]; + r.request_key = (ConfKey)kp[0]; + r.t = Type.Request; + r.op = Operation.DEALLOCATE; + reqs.add(r); + //we dont need to look at children + return DiffIterateResultFlag.ITER_CONTINUE; } + break; + } + case MOP_VALUE_SET: { + if(kp[1].toString().equals("nipap:attributes") && + kp.length == 8) { + r.pool_key = (ConfKey)kp[4]; + r.request_key = (ConfKey)kp[2]; + r.t = Type.Request; + r.op = Operation.SET; + + boolean found = false; + + for( Request req : reqs){ + + if(req.t.equals(Type.Request) && + req.request_key.equals(r.request_key)) { + found = true; + } + } + if(found == false){ + reqs.add(r); + } + + } else if(kp[3].toString().equals("nipap:from-prefix-request") && + kp.length == 10){ + + r.pool_key = (ConfKey)kp[6]; + r.request_key = (ConfKey)kp[4]; + r.prefix_key = (ConfKey)kp[2]; + r.t = Type.FromPrefixRequest; + r.op = Operation.SET; + + boolean found = false; + + for( Request req : reqs){ + if(req.t.equals(Type.FromPrefixRequest) && + req.request_key.equals(r.request_key) && + req.prefix_key.equals(r.prefix_key)) { + found = true; + } + } + if(found == false){ + LOGGER.info("add " + r.prefix_key); + reqs.add(r); + } + } + break; } - break; - } } } catch (Exception e) { LOGGER.error("", e); } return DiffIterateResultFlag.ITER_RECURSE; - - } + } } // redeploy MUST be done in another thread, if not system @@ -689,14 +686,14 @@ public Redeployer(String path) { try { s = new Socket(NcsMain.getInstance().getNcsHost(), - NcsMain.getInstance().getNcsPort()); + NcsMain.getInstance().getNcsPort()); m = new Maapi(s); m.startUserSession("admin", - m.getSocket().getInetAddress(), - "system", - new String[] {"admin"}, - MaapiUserSessionFlag.PROTO_TCP); + m.getSocket().getInetAddress(), + "system", + new String[] {"admin"}, + MaapiUserSessionFlag.PROTO_TCP); } catch (Exception e) { System.err.println("redeployer exception: "+e); } @@ -712,17 +709,17 @@ public void run() { int counter = 0; while (true) { - Thread.sleep(50); - if (m.exists(tid, path)) - break; - if (counter++ == 40) { - break; - } - Thread.sleep(1000); + Thread.sleep(50); + if (m.exists(tid, path)) + break; + if (counter++ == 40) { + break; + } + Thread.sleep(1000); } m.requestAction(new ConfXMLParam[] {}, - path+"/reactive-re-deploy"); + path+"/reactive-re-deploy"); try { m.finishTrans(tid); } @@ -735,7 +732,4 @@ public void run() { } } } - - - } From fff25bd87a02bac1a26544aee7cf48dae9b2dbf0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fred=20S=C3=B6derberg?= Date: Tue, 20 Sep 2016 15:27:59 +0200 Subject: [PATCH 14/27] nso-nipap: Code style fix --- .../net/spritelink/nsonipap/ConfigCdbSub.java | 60 +++++++++---------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java b/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java index 48372ac68..eef8cb1c9 100644 --- a/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java +++ b/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java @@ -135,25 +135,25 @@ protected Prefix getPrefixAttributes(Prefix oldPrefix, String attributePath) thr if (maapi.exists(th, attributePath + "/" + nipap._customer_id_)) { ConfValue rCustomer_id = maapi.getElem(th, attributePath + "/" + nipap._customer_id_); p.customer_id = String.valueOf(rCustomer_id); - }else { + } else { p.customer_id = null; } if (maapi.exists(th, attributePath + "/" + nipap._description_)) { ConfValue rDescription = maapi.getElem(th, attributePath + "/" + nipap._description_); p.description = String.valueOf(rDescription); - }else { + } else { p.description = null; } if (maapi.exists(th, attributePath + "/" + nipap._node_)) { ConfValue rNode = maapi.getElem(th, attributePath + "/" + nipap._node_); p.node = String.valueOf(rNode); - }else { + } else { p.node = null; } if (maapi.exists(th, attributePath + "/" + nipap._order_id_)) { ConfValue rOrder_id = maapi.getElem(th, attributePath + "/" + nipap._customer_id_); p.order_id = String.valueOf(rOrder_id); - }else { + } else { p.order_id = null; } @@ -207,22 +207,22 @@ protected void writeResponse(Prefix prefix, String responsePath) throws ConfExce ConfUInt32 prefixIdValue = new ConfUInt32(prefix.id); wsess.setElem(prefixIdValue, responsePath + "/" + nipap._prefix_id_); - if(prefix.customer_id != null){ + if (prefix.customer_id != null){ ConfBuf customerIdValue = new ConfBuf(prefix.customer_id); wsess.setElem(customerIdValue, responsePath + "/" + nipap._customer_id_); } - if(prefix.description != null){ + if (prefix.description != null){ ConfBuf descriptionValue = new ConfBuf(prefix.description); wsess.setElem(descriptionValue, responsePath + "/" + nipap._description_); } - if(prefix.node != null){ + if (prefix.node != null){ ConfBuf nodeValue = new ConfBuf(prefix.node); wsess.setElem(nodeValue, responsePath + "/" + nipap._node_); } - if(prefix.order_id != null){ + if (prefix.order_id != null){ ConfBuf orderIdValue = new ConfBuf(prefix.order_id); wsess.setElem(orderIdValue, responsePath + "/" + nipap._order_id_); } @@ -388,7 +388,7 @@ public void run() { poolSpec.put("name", poolName); List poolRes = Pool.list(nipapCon, poolSpec); - if(poolRes.size() < 1){ + if (poolRes.size() != 1){ writeError(req.path.toString(), "Nipap pool not found"); continue; } @@ -416,7 +416,7 @@ public void run() { // Request prefix from prefix String fromPrefixPath = req.path + "/" + nipap._from_prefix_request_; - if(maapi.exists(th, fromPrefixPath)){ + if (maapi.exists(th, fromPrefixPath)){ MaapiCursor pfx_cur = maapi.newCursor(th, fromPrefixPath); ConfKey pfx = null; @@ -464,7 +464,7 @@ else if (req.op == Operation.DEALLOCATE && NavuNode request = KeyPath2NavuNode.getNode(req.path, context); - for(NavuContainer prefix_key : request.list(nipap._from_prefix_request).elements()){ + for (NavuContainer prefix_key : request.list(nipap._from_prefix_request).elements()){ removePrefix(path + prefix_key.leaf(nipap._name_).toKey() + "/" + nipap._response_); removeResponse(path + prefix_key.leaf(nipap._name_).toKey() + "/" + nipap._response_); } @@ -576,17 +576,17 @@ public DiffIterateResultFlag iterate( case MOP_CREATED: { // new request - if(kp[1].toString().equals("nipap:request") && - kp.length == 6){ - r.pool_key = (ConfKey)kp[2]; - r.request_key = (ConfKey)kp[0]; - r.t = Type.Request; - r.op = Operation.ALLOCATE; - reqs.add(r); - //the request is new, we dont need to look at children - return DiffIterateResultFlag.ITER_CONTINUE; + if (kp[1].toString().equals("nipap:request") && + kp.length == 6){ + r.pool_key = (ConfKey)kp[2]; + r.request_key = (ConfKey)kp[0]; + r.t = Type.Request; + r.op = Operation.ALLOCATE; + reqs.add(r); + //the request is new, we dont need to look at children + return DiffIterateResultFlag.ITER_CONTINUE; } - else if(kp[1].toString().equals("nipap:from-prefix-request") && + else if (kp[1].toString().equals("nipap:from-prefix-request") && kp.length == 8){ r.prefix_key = (ConfKey)kp[0]; r.request_key = (ConfKey)kp[2]; @@ -600,7 +600,7 @@ else if(kp[1].toString().equals("nipap:from-prefix-request") && break; } case MOP_DELETED: { - if(kp[1].toString().equals("nipap:request") && + if (kp[1].toString().equals("nipap:request") && kp.length == 6){ r.pool_key = (ConfKey)kp[2]; r.request_key = (ConfKey)kp[0]; @@ -613,7 +613,7 @@ else if(kp[1].toString().equals("nipap:from-prefix-request") && break; } case MOP_VALUE_SET: { - if(kp[1].toString().equals("nipap:attributes") && + if (kp[1].toString().equals("nipap:attributes") && kp.length == 8) { r.pool_key = (ConfKey)kp[4]; r.request_key = (ConfKey)kp[2]; @@ -622,18 +622,18 @@ else if(kp[1].toString().equals("nipap:from-prefix-request") && boolean found = false; - for( Request req : reqs){ + for (Request req : reqs){ - if(req.t.equals(Type.Request) && + if (req.t.equals(Type.Request) && req.request_key.equals(r.request_key)) { found = true; } } - if(found == false){ + if (found == false){ reqs.add(r); } - } else if(kp[3].toString().equals("nipap:from-prefix-request") && + } else if (kp[3].toString().equals("nipap:from-prefix-request") && kp.length == 10){ r.pool_key = (ConfKey)kp[6]; @@ -644,14 +644,14 @@ else if(kp[1].toString().equals("nipap:from-prefix-request") && boolean found = false; - for( Request req : reqs){ - if(req.t.equals(Type.FromPrefixRequest) && + for (Request req : reqs){ + if (req.t.equals(Type.FromPrefixRequest) && req.request_key.equals(r.request_key) && req.prefix_key.equals(r.prefix_key)) { found = true; } } - if(found == false){ + if (found == false){ LOGGER.info("add " + r.prefix_key); reqs.add(r); } From d9a71314e3fcb4ffc1f8a3012710227c2b532534 Mon Sep 17 00:00:00 2001 From: Lukas Garberg Date: Mon, 5 Dec 2016 13:35:59 +0100 Subject: [PATCH 15/27] nso-nipap: Various style fixes Various style & indent fixes. --- .../net/spritelink/nsonipap/ConfigCdbSub.java | 75 +++++++++---------- 1 file changed, 37 insertions(+), 38 deletions(-) diff --git a/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java b/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java index eef8cb1c9..6edf426ed 100644 --- a/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java +++ b/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java @@ -34,16 +34,16 @@ public ConfigCdbSub() { } @Resource(type=ResourceType.CDB, scope=Scope.CONTEXT, - qualifier="reactive-fm-loop-subscriber") - private Cdb cdb; + qualifier="reactive-fm-loop-subscriber") + private Cdb cdb; @Resource(type=ResourceType.CDB, scope=Scope.CONTEXT, - qualifier="w-reactive-fm-loop") - private Cdb wcdb; + qualifier="w-reactive-fm-loop") + private Cdb wcdb; @Resource(type=ResourceType.MAAPI, scope=Scope.INSTANCE, - qualifier="reactive-fm-m") - private Maapi maapi; + qualifier="reactive-fm-m") + private Maapi maapi; private int th = -1; @@ -62,7 +62,7 @@ public void init() { sub = cdb.newSubscription(); - sub.subscribe(1, new nipap(), + sub.subscribe(1, new nipap(), "/services/" + nipap.prefix + "/" + nipap._from_pool_ + "/" + @@ -71,8 +71,7 @@ public void init() { // Tell CDB we are ready for notifications sub.subscribeDone(); - } - catch (Exception e) { + } catch (Exception e) { LOGGER.error("", e); } } @@ -97,7 +96,7 @@ protected void addPrefixFromPrefix(String path, Prefix parentPrefix) throws Exce child_prefix.type = "host"; child_prefix.save(nipapCon, parentPrefix, child_opts); - }catch (Exception e ) { + } catch (Exception e ) { LOGGER.error("Unable to get prefix from prefix" + e.getMessage(), e); writeError(path, e.getMessage()); return; @@ -123,7 +122,7 @@ protected Prefix getPrefixAttributes(String attributePath) throws Exception { /** * Fetch attributes and returns the populated prefix object * - * @param oldPrefix Used if you already have a prefix object + * @param oldPrefix Used if you already have a prefix object * @param attributePath Path to the prefix attribute container * @return Prefix * @throws Exception @@ -168,7 +167,7 @@ protected Prefix getPrefixAttributes(Prefix oldPrefix, String attributePath) thr * @throws Exception */ - protected AddPrefixOptions getPrefixOptions (String argumentPath) throws Exception { + protected AddPrefixOptions getPrefixOptions (String argumentPath) throws Exception { AddPrefixOptions opts = new AddPrefixOptions(); ConfEnumeration family = (ConfEnumeration)maapi.getElem( @@ -260,7 +259,7 @@ protected void removeResponse(String responsePath) throws ConfException, Excepti wsess.delete(responsePath + "/" + nipap._description_); wsess.delete(responsePath + "/" + nipap._node_); wsess.delete(responsePath + "/" + nipap._order_id_); - } catch (CdbException e ){ + } catch (CdbException e ) { } } @@ -307,7 +306,7 @@ protected void removePrefix(String prefixPath) throws ConfException, Exception { /** * Get prefix id - * + * * @param path Path to prefix response * @return Prefix id * @throws Exception @@ -318,7 +317,7 @@ protected int getPrefixId(String path) throws Exception { } public void run() { - LOGGER.info("Starting the CDB subscriber..."); + LOGGER.info("Starting the CDB subscriber..."); try { while(true) { @@ -344,8 +343,7 @@ public void run() { sub.diffIterate(points[0], new Iter(sub), enumSet, reqs); - } - catch (Exception e) { + } catch (Exception e) { reqs = null; } @@ -475,7 +473,7 @@ else if (req.op == Operation.DEALLOCATE && } removeResponse(req.path + "/" + nipap._response_); context.finishClearTrans(); - } + } /* * Deallocate from-prefix prefix * @@ -493,7 +491,7 @@ else if (req.op == Operation.SET && req.t == Type.Request){ String reqPath = "/" + Ncs._services_ + "/" + nipap.prefix + ":" + nipap.prefix + "/" + nipap._from_pool_ + req.pool_key + "/" + nipap._request_ + req.request_key; - updatePrefix(reqPath); + updatePrefix(reqPath); } /* @@ -506,7 +504,7 @@ else if (req.op == Operation.SET && req.t == Type.FromPrefixRequest){ nipap._from_pool_ + req.pool_key + "/" + nipap._request_ + req.request_key + "/" + nipap._from_prefix_request_ + req.prefix_key; - updatePrefix(reqPath); + updatePrefix(reqPath); } } sub.sync(CdbSubscriptionSyncType.DONE_PRIORITY); @@ -533,7 +531,8 @@ public void finish() { private void safeclose(Cdb s) { try {s.close();} - catch (Exception ignore) {} + catch (Exception ignore) { + } } @@ -560,7 +559,7 @@ public DiffIterateResultFlag iterate( ConfObject[] kp, DiffIterateOperFlag op, ConfObject oldValue, - ConfObject newValue, Object initstate) { + ConfObject newValue, Object initstate) { @SuppressWarnings("unchecked") ArrayList reqs = (ArrayList) initstate; @@ -576,7 +575,7 @@ public DiffIterateResultFlag iterate( case MOP_CREATED: { // new request - if (kp[1].toString().equals("nipap:request") && + if (kp[1].toString().equals("nipap:request") && kp.length == 6){ r.pool_key = (ConfKey)kp[2]; r.request_key = (ConfKey)kp[0]; @@ -586,7 +585,7 @@ public DiffIterateResultFlag iterate( //the request is new, we dont need to look at children return DiffIterateResultFlag.ITER_CONTINUE; } - else if (kp[1].toString().equals("nipap:from-prefix-request") && + else if (kp[1].toString().equals("nipap:from-prefix-request") && kp.length == 8){ r.prefix_key = (ConfKey)kp[0]; r.request_key = (ConfKey)kp[2]; @@ -596,11 +595,11 @@ else if (kp[1].toString().equals("nipap:from-prefix-request") && reqs.add(r); //the request is new, we dont need to look at children return DiffIterateResultFlag.ITER_CONTINUE; - } - break; } + break; + } case MOP_DELETED: { - if (kp[1].toString().equals("nipap:request") && + if (kp[1].toString().equals("nipap:request") && kp.length == 6){ r.pool_key = (ConfKey)kp[2]; r.request_key = (ConfKey)kp[0]; @@ -622,19 +621,19 @@ else if (kp[1].toString().equals("nipap:from-prefix-request") && boolean found = false; - for (Request req : reqs){ + for (Request req : reqs) { if (req.t.equals(Type.Request) && req.request_key.equals(r.request_key)) { found = true; - } + } } - if (found == false){ + if (found == false) { reqs.add(r); } } else if (kp[3].toString().equals("nipap:from-prefix-request") && - kp.length == 10){ + kp.length == 10) { r.pool_key = (ConfKey)kp[6]; r.request_key = (ConfKey)kp[4]; @@ -644,18 +643,18 @@ else if (kp[1].toString().equals("nipap:from-prefix-request") && boolean found = false; - for (Request req : reqs){ - if (req.t.equals(Type.FromPrefixRequest) && - req.request_key.equals(r.request_key) && + for (Request req : reqs) { + if (req.t.equals(Type.FromPrefixRequest) && + req.request_key.equals(r.request_key) && req.prefix_key.equals(r.prefix_key)) { found = true; - } + } } - if (found == false){ + if (found == false) { LOGGER.info("add " + r.prefix_key); reqs.add(r); } - } + } break; } } @@ -664,7 +663,7 @@ else if (kp[1].toString().equals("nipap:from-prefix-request") && LOGGER.error("", e); } return DiffIterateResultFlag.ITER_RECURSE; - } + } } // redeploy MUST be done in another thread, if not system From c7ce3559bf3c905826435eecb7e7857447f2f591 Mon Sep 17 00:00:00 2001 From: Lukas Garberg Date: Mon, 5 Dec 2016 13:48:09 +0100 Subject: [PATCH 16/27] nso-nipap: Rename functions for clarity Renamed some functions to make it clear whether they operate on CDB or NIPAP. --- .../net/spritelink/nsonipap/ConfigCdbSub.java | 56 +++++++++---------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java b/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java index 6edf426ed..272781f40 100644 --- a/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java +++ b/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java @@ -83,7 +83,7 @@ public void init() { * @param parentPrefix From prefix * @throws Exception */ - protected void addPrefixFromPrefix(String path, Prefix parentPrefix) throws Exception { + protected void addHostPrefixFromPrefix(String path, Prefix parentPrefix) throws Exception { LOGGER.info("Create, From prefix request, path = " + path); Prefix child_prefix = new Prefix(); @@ -92,7 +92,7 @@ protected void addPrefixFromPrefix(String path, Prefix parentPrefix) throws Exce AddPrefixOptions child_opts = new AddPrefixOptions(); child_opts.put("prefix_length", "32"); - child_prefix = getPrefixAttributes(path + "/" + nipap._attributes_ ); + child_prefix = getPrefixAttributesFromCDB(path + "/" + nipap._attributes_ ); child_prefix.type = "host"; child_prefix.save(nipapCon, parentPrefix, child_opts); @@ -102,7 +102,7 @@ protected void addPrefixFromPrefix(String path, Prefix parentPrefix) throws Exce return; } //write response - writeResponse(child_prefix, path + "/" + nipap._response_); + writeResponseToCDB(child_prefix, path + "/" + nipap._response_); } /** @@ -112,11 +112,11 @@ protected void addPrefixFromPrefix(String path, Prefix parentPrefix) throws Exce * @return Prefix * @throws Exception */ - protected Prefix getPrefixAttributes(String attributePath) throws Exception { + protected Prefix getPrefixAttributesFromCDB(String attributePath) throws Exception { Prefix p = new Prefix(); - return getPrefixAttributes(p, attributePath); + return getPrefixAttributesFromCDB(p, attributePath); } /** @@ -127,7 +127,7 @@ protected Prefix getPrefixAttributes(String attributePath) throws Exception { * @return Prefix * @throws Exception */ - protected Prefix getPrefixAttributes(Prefix oldPrefix, String attributePath) throws Exception + protected Prefix getPrefixAttributesFromCDB(Prefix oldPrefix, String attributePath) throws Exception { Prefix p = oldPrefix; @@ -160,14 +160,14 @@ protected Prefix getPrefixAttributes(Prefix oldPrefix, String attributePath) thr } /** - * Populate PrefixOptions + * Fetch PrefixOptions from CDB * * @param argumentPath Path to prefix argument container * @return AddPrefixOptions * @throws Exception */ - protected AddPrefixOptions getPrefixOptions (String argumentPath) throws Exception { + protected AddPrefixOptions getPrefixOptionsFromCDB (String argumentPath) throws Exception { AddPrefixOptions opts = new AddPrefixOptions(); ConfEnumeration family = (ConfEnumeration)maapi.getElem( @@ -183,14 +183,14 @@ protected AddPrefixOptions getPrefixOptions (String argumentPath) throws Excepti } /** - * Write response data to cdb oper + * Write response data to CDB oper * - * @param prefix Prefix + * @param prefix Prefix to write * @param responsePath Path were the response should be written. * @throws ConfException * @throws Exception */ - protected void writeResponse(Prefix prefix, String responsePath) throws ConfException, Exception { + protected void writeResponseToCDB(Prefix prefix, String responsePath) throws ConfException, Exception { if (prefix.family == 4) { ConfIPv4Prefix prefixValue = new ConfIPv4Prefix(prefix.prefix); @@ -241,14 +241,14 @@ protected void writeError(String path, String errorMessage) throws Exception { } /** - * Remove response from cdb oper. + * Remove response from CDB oper. * TODO: Do we need to do this? * * @param responsePath path to response * @throws ConfException * @throws Exception */ - protected void removeResponse(String responsePath) throws ConfException, Exception { + protected void removeResponseFromCDB(String responsePath) throws ConfException, Exception { //unset case LOGGER.info("remove response " + responsePath); @@ -270,7 +270,7 @@ protected void removeResponse(String responsePath) throws ConfException, Excepti * @throws Exception */ - protected void updatePrefix(String prefixPath) throws Exception { + protected void updatePrefixInNIPAP(String prefixPath) throws Exception { LOGGER.info("Update prefix: " + prefixPath); String responsePath = prefixPath + "/" + nipap._response_; @@ -279,10 +279,10 @@ protected void updatePrefix(String prefixPath) throws Exception { Prefix p = Prefix.get(nipapCon, p_id); - Prefix newPrefix = getPrefixAttributes(p, prefixPath + "/" + nipap._attributes_); + Prefix newPrefix = getPrefixAttributesFromCDB(p, prefixPath + "/" + nipap._attributes_); newPrefix.save(nipapCon); - writeResponse(newPrefix, responsePath); + writeResponseToCDB(newPrefix, responsePath); } /** @@ -293,7 +293,7 @@ protected void updatePrefix(String prefixPath) throws Exception { * @throws Exception */ - protected void removePrefix(String prefixPath) throws ConfException, Exception { + protected void removePrefixFromNIPAP(String prefixPath) throws ConfException, Exception { try { int p_id = getPrefixId(prefixPath); LOGGER.info("Removing prefix ID: " + p_id); @@ -392,11 +392,11 @@ public void run() { } // options, like address-family - AddPrefixOptions opts = getPrefixOptions(req.path + "/" + nipap._arguments_); + AddPrefixOptions opts = getPrefixOptionsFromCDB(req.path + "/" + nipap._arguments_); //set prefix attributes String attrPath = req.path + "/" + nipap._attributes_; - p = getPrefixAttributes(attrPath); + p = getPrefixAttributesFromCDB(attrPath); p.save(nipapCon, (Pool)poolRes.get(0), opts); @@ -408,7 +408,7 @@ public void run() { // Write the result String resPath = req.path + "/" + nipap._response_; - writeResponse(p, resPath); + writeResponseToCDB(p, resPath); wsess.setCase(nipap._response_choice_, nipap._ok_, resPath); @@ -419,7 +419,7 @@ public void run() { ConfKey pfx = null; while((pfx = maapi.getNext(pfx_cur)) != null) { - addPrefixFromPrefix(fromPrefixPath + pfx, p); + addHostPrefixFromPrefix(fromPrefixPath + pfx, p); } } @@ -446,7 +446,7 @@ else if (req.op == Operation.ALLOCATE && req.t == Type.FromPrefixRequest){ Prefix parentPrefix = Prefix.get(nipapCon, p_id); - addPrefixFromPrefix(path + "/" + nipap._from_prefix_request_ + req.prefix_key, parentPrefix); + addHostPrefixFromPrefix(path + "/" + nipap._from_prefix_request_ + req.prefix_key, parentPrefix); } /* * Deallocate Prefix @@ -463,15 +463,15 @@ else if (req.op == Operation.DEALLOCATE && NavuNode request = KeyPath2NavuNode.getNode(req.path, context); for (NavuContainer prefix_key : request.list(nipap._from_prefix_request).elements()){ - removePrefix(path + prefix_key.leaf(nipap._name_).toKey() + "/" + nipap._response_); - removeResponse(path + prefix_key.leaf(nipap._name_).toKey() + "/" + nipap._response_); + removePrefixFromNIPAP(path + prefix_key.leaf(nipap._name_).toKey() + "/" + nipap._response_); + removeResponseFromCDB(path + prefix_key.leaf(nipap._name_).toKey() + "/" + nipap._response_); } try { - removePrefix(req.path + "/" + nipap._response_); + removePrefixFromNIPAP(req.path + "/" + nipap._response_); } catch (Exception e ){ continue; } - removeResponse(req.path + "/" + nipap._response_); + removeResponseFromCDB(req.path + "/" + nipap._response_); context.finishClearTrans(); } /* @@ -491,7 +491,7 @@ else if (req.op == Operation.SET && req.t == Type.Request){ String reqPath = "/" + Ncs._services_ + "/" + nipap.prefix + ":" + nipap.prefix + "/" + nipap._from_pool_ + req.pool_key + "/" + nipap._request_ + req.request_key; - updatePrefix(reqPath); + updatePrefixInNIPAP(reqPath); } /* @@ -504,7 +504,7 @@ else if (req.op == Operation.SET && req.t == Type.FromPrefixRequest){ nipap._from_pool_ + req.pool_key + "/" + nipap._request_ + req.request_key + "/" + nipap._from_prefix_request_ + req.prefix_key; - updatePrefix(reqPath); + updatePrefixInNIPAP(reqPath); } } sub.sync(CdbSubscriptionSyncType.DONE_PRIORITY); From c3e48d768f465d450cecebaf38fbd70deec127b0 Mon Sep 17 00:00:00 2001 From: Lukas Garberg Date: Mon, 5 Dec 2016 13:54:36 +0100 Subject: [PATCH 17/27] nso-nipap: Renamed oldPrefix to basePrefix --- .../src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java b/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java index 272781f40..97b705b6a 100644 --- a/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java +++ b/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java @@ -122,14 +122,14 @@ protected Prefix getPrefixAttributesFromCDB(String attributePath) throws Excepti /** * Fetch attributes and returns the populated prefix object * - * @param oldPrefix Used if you already have a prefix object + * @param basePrefix Used if you already have a prefix object * @param attributePath Path to the prefix attribute container * @return Prefix * @throws Exception */ - protected Prefix getPrefixAttributesFromCDB(Prefix oldPrefix, String attributePath) throws Exception + protected Prefix getPrefixAttributesFromCDB(Prefix basePrefix, String attributePath) throws Exception { - Prefix p = oldPrefix; + Prefix p = basePrefix; if (maapi.exists(th, attributePath + "/" + nipap._customer_id_)) { ConfValue rCustomer_id = maapi.getElem(th, attributePath + "/" + nipap._customer_id_); From 3f0d561f6fe90fd55da7778b646d1212df1bb58c Mon Sep 17 00:00:00 2001 From: Lukas Garberg Date: Mon, 5 Dec 2016 14:53:49 +0100 Subject: [PATCH 18/27] nso-nipap: Detail thrown exceptions To make it clear what types of errors which can occurr in the different helper functions, all exceptions thrown are now listed instead of a generic "Exception". Also make some minor style fixes. --- .../net/spritelink/nsonipap/ConfigCdbSub.java | 107 ++++++++++-------- 1 file changed, 60 insertions(+), 47 deletions(-) diff --git a/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java b/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java index 97b705b6a..2d026d5dc 100644 --- a/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java +++ b/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java @@ -3,6 +3,7 @@ import net.spritelink.nsonipap.namespaces.*; import java.util.*; +import java.io.IOException; import org.apache.log4j.Logger; @@ -79,30 +80,33 @@ public void init() { /** * Add a host prefix from a prefix. * - * @param path Path to the prefix request - * @param parentPrefix From prefix - * @throws Exception + * @param path Path to the prefix request + * @param parentPrefix Prefix to get host prefix from + * @throws ConfException + * @throws IOException */ - protected void addHostPrefixFromPrefix(String path, Prefix parentPrefix) throws Exception { + protected void addHostPrefixFromPrefix(String path, Prefix parentPrefix) throws ConfException, IOException { LOGGER.info("Create, From prefix request, path = " + path); - Prefix child_prefix = new Prefix(); - try { - AddPrefixOptions child_opts = new AddPrefixOptions(); - child_opts.put("prefix_length", "32"); + AddPrefixOptions child_opts = new AddPrefixOptions(); + child_opts.put("prefix_length", "32"); - child_prefix = getPrefixAttributesFromCDB(path + "/" + nipap._attributes_ ); - child_prefix.type = "host"; - child_prefix.save(nipapCon, parentPrefix, child_opts); + Prefix child_prefix = new Prefix(); + child_prefix = getPrefixAttributesFromCDB(path + "/" + nipap._attributes_ ); + child_prefix.type = "host"; - } catch (Exception e ) { + try { + child_prefix.save(nipapCon, parentPrefix, child_opts); + } catch (JnipapException e) { LOGGER.error("Unable to get prefix from prefix" + e.getMessage(), e); writeError(path, e.getMessage()); return; } - //write response + + // write response writeResponseToCDB(child_prefix, path + "/" + nipap._response_); + } /** @@ -110,9 +114,10 @@ protected void addHostPrefixFromPrefix(String path, Prefix parentPrefix) throws * * @param attributePath Path to the prefix attribute container * @return Prefix - * @throws Exception + * @throws ConfException + * @throws IOException */ - protected Prefix getPrefixAttributesFromCDB(String attributePath) throws Exception { + protected Prefix getPrefixAttributesFromCDB(String attributePath) throws ConfException, IOException { Prefix p = new Prefix(); @@ -125,10 +130,11 @@ protected Prefix getPrefixAttributesFromCDB(String attributePath) throws Excepti * @param basePrefix Used if you already have a prefix object * @param attributePath Path to the prefix attribute container * @return Prefix - * @throws Exception + * @throws ConfException + * @throws IOException */ - protected Prefix getPrefixAttributesFromCDB(Prefix basePrefix, String attributePath) throws Exception - { + protected Prefix getPrefixAttributesFromCDB(Prefix basePrefix, String attributePath) throws ConfException, IOException { + Prefix p = basePrefix; if (maapi.exists(th, attributePath + "/" + nipap._customer_id_)) { @@ -160,14 +166,14 @@ protected Prefix getPrefixAttributesFromCDB(Prefix basePrefix, String attributeP } /** - * Fetch PrefixOptions from CDB + * Fetch prefix options from CDB * * @param argumentPath Path to prefix argument container * @return AddPrefixOptions - * @throws Exception + * @throws ConfException + * @throws IOException */ - - protected AddPrefixOptions getPrefixOptionsFromCDB (String argumentPath) throws Exception { + protected AddPrefixOptions getPrefixOptionsFromCDB (String argumentPath) throws ConfException, IOException { AddPrefixOptions opts = new AddPrefixOptions(); ConfEnumeration family = (ConfEnumeration)maapi.getElem( @@ -185,12 +191,12 @@ protected AddPrefixOptions getPrefixOptionsFromCDB (String argumentPath) throws /** * Write response data to CDB oper * - * @param prefix Prefix to write - * @param responsePath Path were the response should be written. + * @param prefix Prefix object to write + * @param responsePath Path were the response should be written * @throws ConfException - * @throws Exception + * @throws IOException */ - protected void writeResponseToCDB(Prefix prefix, String responsePath) throws ConfException, Exception { + protected void writeResponseToCDB(Prefix prefix, String responsePath) throws ConfException, IOException { if (prefix.family == 4) { ConfIPv4Prefix prefixValue = new ConfIPv4Prefix(prefix.prefix); @@ -232,12 +238,14 @@ protected void writeResponseToCDB(Prefix prefix, String responsePath) throws Con * * @param path Path to request * @param errorMessage Error message - * @throws Exception + * @throws ConfException + * @throws IOException */ - protected void writeError(String path, String errorMessage) throws Exception { + protected void writeError(String path, String errorMessage) throws ConfException, IOException { wsess.setElem(new ConfBuf(errorMessage), path + "/" + nipap._response_ + "/" + nipap._error_); wsess.setCase(nipap._response_choice_, nipap._error_, path + "/" + nipap._response_); + } /** @@ -246,9 +254,9 @@ protected void writeError(String path, String errorMessage) throws Exception { * * @param responsePath path to response * @throws ConfException - * @throws Exception + * @throws IOException */ - protected void removeResponseFromCDB(String responsePath) throws ConfException, Exception { + protected void removeResponseFromCDB(String responsePath) throws ConfException, IOException { //unset case LOGGER.info("remove response " + responsePath); @@ -267,10 +275,11 @@ protected void removeResponseFromCDB(String responsePath) throws ConfException, * Update NIPAP with the new prefix information * * @param prefixPath Path to prefix request - * @throws Exception + * @throws ConfException + * @throws IOException + * @throws JnipapException for NIPAP related issues */ - - protected void updatePrefixInNIPAP(String prefixPath) throws Exception { + protected void updatePrefixInNIPAP(String prefixPath) throws ConfException, IOException, JnipapException { LOGGER.info("Update prefix: " + prefixPath); String responsePath = prefixPath + "/" + nipap._response_; @@ -288,30 +297,34 @@ protected void updatePrefixInNIPAP(String prefixPath) throws Exception { /** * Remove Prefix from NIPAP * - * @param prefixPath path to Prefix + * @param prefixPath Path to Prefix * @throws ConfException - * @throws Exception + * @throws IOException + * @throws JnipapException for NIPAP related issues */ + protected void removePrefixFromNIPAP(String prefixPath) throws ConfException, IOException, JnipapException { + + int p_id = getPrefixId(prefixPath); + LOGGER.info("Removing prefix ID: " + p_id); - protected void removePrefixFromNIPAP(String prefixPath) throws ConfException, Exception { try { - int p_id = getPrefixId(prefixPath); - LOGGER.info("Removing prefix ID: " + p_id); Prefix p = Prefix.get(nipapCon, p_id); p.remove(nipapCon); - } catch (Exception e) { + } catch (JnipapException e) { LOGGER.error("Unable to remove prefix from NIPAP: " + e.getMessage(),e); + throw e; } } /** - * Get prefix id + * Get prefix ID * * @param path Path to prefix response - * @return Prefix id - * @throws Exception + * @return Prefix ID + * @throws ConfException + * @throws IOException */ - protected int getPrefixId(String path) throws Exception { + protected int getPrefixId(String path) throws ConfException, IOException { return (int) ((ConfUInt32)wsess.getElem(path + "/" + nipap._prefix_id_)).longValue(); } @@ -386,21 +399,21 @@ public void run() { poolSpec.put("name", poolName); List poolRes = Pool.list(nipapCon, poolSpec); - if (poolRes.size() != 1){ - writeError(req.path.toString(), "Nipap pool not found"); + if (poolRes.size() != 1) { + writeError(req.path.toString(), "Pool " + poolName + " not found in NIPAP"); continue; } // options, like address-family AddPrefixOptions opts = getPrefixOptionsFromCDB(req.path + "/" + nipap._arguments_); - //set prefix attributes + // set prefix attributes String attrPath = req.path + "/" + nipap._attributes_; p = getPrefixAttributesFromCDB(attrPath); p.save(nipapCon, (Pool)poolRes.get(0), opts); - } catch (Exception e) { + } catch (JnipapException e) { LOGGER.error("Unable to get prefix from NIPAP: " + e.getMessage(), e); writeError(req.path.toString(), e.getMessage()); continue; From a55701cea39e7d42ba0a35d6d667bbe96ea12938 Mon Sep 17 00:00:00 2001 From: Lukas Garberg Date: Mon, 5 Dec 2016 15:19:41 +0100 Subject: [PATCH 19/27] nso-nipap: Handle IPv6 in addHostPrefixFromPrefix Updated the addHostPrefixFromPrefix function to set the host prefix prefix length based on IP version. --- .../src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java b/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java index 2d026d5dc..175496647 100644 --- a/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java +++ b/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java @@ -90,7 +90,11 @@ protected void addHostPrefixFromPrefix(String path, Prefix parentPrefix) throws LOGGER.info("Create, From prefix request, path = " + path); AddPrefixOptions child_opts = new AddPrefixOptions(); - child_opts.put("prefix_length", "32"); + if (parentPrefix.family.intValue() == 4) { + child_opts.put("prefix_length", "32"); + } else { + child_opts.put("prefix_length", "128"); + } Prefix child_prefix = new Prefix(); child_prefix = getPrefixAttributesFromCDB(path + "/" + nipap._attributes_ ); From 531073ae48b7bbb730d470f16a99ff57d1b197fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fred=20S=C3=B6derberg?= Date: Mon, 2 Jan 2017 11:20:46 +0100 Subject: [PATCH 20/27] nso-nipap: Use recrusive delete when removing a prefix --- .../net/spritelink/nsonipap/ConfigCdbSub.java | 24 +++++-------------- 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java b/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java index 175496647..32120a5e1 100644 --- a/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java +++ b/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java @@ -313,7 +313,7 @@ protected void removePrefixFromNIPAP(String prefixPath) throws ConfException, IO try { Prefix p = Prefix.get(nipapCon, p_id); - p.remove(nipapCon); + p.remove(nipapCon, true); } catch (JnipapException e) { LOGGER.error("Unable to remove prefix from NIPAP: " + e.getMessage(),e); throw e; @@ -469,27 +469,15 @@ else if (req.op == Operation.ALLOCATE && req.t == Type.FromPrefixRequest){ * Deallocate Prefix * */ - else if (req.op == Operation.DEALLOCATE && - (req.t == Type.Request)) { + else if (req.op == Operation.DEALLOCATE && (req.t == Type.Request)) { - String path = req.path + "/" + nipap._from_prefix_request_ ; + String path = req.path + "/" + nipap._response_; - NavuContext context = new NavuContext(maapi); - int to = context.startPreCommitRunningTrans(); + LOGGER.info("Deallocate Prefix (" + path + ")"); - NavuNode request = KeyPath2NavuNode.getNode(req.path, context); + removePrefixFromNIPAP(path); - for (NavuContainer prefix_key : request.list(nipap._from_prefix_request).elements()){ - removePrefixFromNIPAP(path + prefix_key.leaf(nipap._name_).toKey() + "/" + nipap._response_); - removeResponseFromCDB(path + prefix_key.leaf(nipap._name_).toKey() + "/" + nipap._response_); - } - try { - removePrefixFromNIPAP(req.path + "/" + nipap._response_); - } catch (Exception e ){ - continue; - } - removeResponseFromCDB(req.path + "/" + nipap._response_); - context.finishClearTrans(); + removeResponseFromCDB(path); } /* * Deallocate from-prefix prefix From 29d60b5d3b6ccf539f1cbcb650bc0ce6e8adf16f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fred=20S=C3=B6derberg?= Date: Mon, 2 Jan 2017 13:20:18 +0100 Subject: [PATCH 21/27] nso-nipap: Added support for deallocate from-prefix --- .../net/spritelink/nsonipap/ConfigCdbSub.java | 27 ++++++++++++++++--- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java b/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java index 32120a5e1..e46ae6717 100644 --- a/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java +++ b/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java @@ -475,7 +475,9 @@ else if (req.op == Operation.DEALLOCATE && (req.t == Type.Request)) { LOGGER.info("Deallocate Prefix (" + path + ")"); - removePrefixFromNIPAP(path); + if(maapi.exists(th, path + "/" + nipap._prefix_)){ + removePrefixFromNIPAP(path); + } removeResponseFromCDB(path); } @@ -485,6 +487,15 @@ else if (req.op == Operation.DEALLOCATE && (req.t == Type.Request)) { */ else if (req.op == Operation.DEALLOCATE && (req.t == Type.FromPrefixRequest)) { + String path = req.path + "/" + nipap._response_; + + LOGGER.info("Deallocate Prefix (" + path + ")"); + + if(maapi.exists(th, path + "/" + nipap._prefix_)){ + removePrefixFromNIPAP(path); + } + + removeResponseFromCDB(path); } /* @@ -611,10 +622,18 @@ else if (kp[1].toString().equals("nipap:from-prefix-request") && r.t = Type.Request; r.op = Operation.DEALLOCATE; reqs.add(r); - //we dont need to look at children - return DiffIterateResultFlag.ITER_CONTINUE; } - break; + else if (kp[1].toString().equals("nipap:from-prefix-request") && + kp.length == 8){ + r.prefix_key = (ConfKey)kp[0]; + r.request_key = (ConfKey)kp[2]; + r.pool_key = (ConfKey)kp[4]; + r.t = Type.FromPrefixRequest; + r.op = Operation.DEALLOCATE; + reqs.add(r); + } + //we dont need to look at children + return DiffIterateResultFlag.ITER_CONTINUE; } case MOP_VALUE_SET: { if (kp[1].toString().equals("nipap:attributes") && From 296c54d0bb5e2d4408ebc9ae04cdcaffd8e96c0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fred=20S=C3=B6derberg?= Date: Mon, 23 Jan 2017 09:28:10 +0100 Subject: [PATCH 22/27] nso-nipap: Changed response-choice to leaf --- .../net/spritelink/nsonipap/ConfigCdbSub.java | 9 ++++-- nso-nipap/nso-nipap/src/yang/nipap.yang | 29 +++++++++++-------- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java b/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java index e46ae6717..2eb03b6ab 100644 --- a/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java +++ b/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java @@ -246,9 +246,10 @@ protected void writeResponseToCDB(Prefix prefix, String responsePath) throws Con * @throws IOException */ protected void writeError(String path, String errorMessage) throws ConfException, IOException { + ConfEnumeration error = ConfEnumeration.getEnumByLabel(path + "/" + nipap._response_ + "/" + nipap._status_, "error"); - wsess.setElem(new ConfBuf(errorMessage), path + "/" + nipap._response_ + "/" + nipap._error_); - wsess.setCase(nipap._response_choice_, nipap._error_, path + "/" + nipap._response_); + wsess.setElem(new ConfBuf(errorMessage), path + "/" + nipap._response_ + "/" + nipap._status_message_); + wsess.setElem(error , path + "/" + nipap._response_ + "/" + nipap._status_); } @@ -427,7 +428,9 @@ public void run() { String resPath = req.path + "/" + nipap._response_; writeResponseToCDB(p, resPath); - wsess.setCase(nipap._response_choice_, nipap._ok_, resPath); + wsess.setElem(ConfEnumeration.getEnumByLabel( resPath + "/" + nipap._status_, "ok"), + resPath + "/" + nipap._status_); + // Request prefix from prefix String fromPrefixPath = req.path + "/" + nipap._from_prefix_request_; diff --git a/nso-nipap/nso-nipap/src/yang/nipap.yang b/nso-nipap/nso-nipap/src/yang/nipap.yang index 7e098fc3d..bb61339a2 100644 --- a/nso-nipap/nso-nipap/src/yang/nipap.yang +++ b/nso-nipap/nso-nipap/src/yang/nipap.yang @@ -14,7 +14,7 @@ module nipap { import tailf-ncs { prefix ncs; } - + grouping prefix-attributes { leaf customer_id { @@ -46,20 +46,25 @@ module nipap { tailf:persistent true; } - choice response-choice { - case error { - leaf error { - config false; - tailf:cdb-oper { - tailf:persistent true; - } - type string; + leaf status { + config false; + tailf:cdb-oper { + tailf:persistent true; } - } - case ok { + type enumeration { + enum ok; + enum error; } } + leaf status-message { + config false; + tailf:cdb-oper { + tailf:persistent true; + } + type string; + } + leaf prefix { type inet:ip-prefix; config false; @@ -122,7 +127,7 @@ module nipap { // TODO: make sure only one backend is marked as default } } - + list from-pool { key name; leaf name { From 6561b506d7b0268d7d5f8d41c6a6a369e78807cc Mon Sep 17 00:00:00 2001 From: Lukas Garberg Date: Mon, 6 Mar 2017 08:15:50 +0100 Subject: [PATCH 23/27] nso-nipap: Added allocation request templates Added templates for creating NIPAP allocation requests. --- .../nso-nipap-from-prefix-request.xml | 21 +++++++++++++++++ .../templates/nso-nipap-prefix-request.xml | 23 +++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 nso-nipap/nso-nipap/templates/nso-nipap-from-prefix-request.xml create mode 100644 nso-nipap/nso-nipap/templates/nso-nipap-prefix-request.xml diff --git a/nso-nipap/nso-nipap/templates/nso-nipap-from-prefix-request.xml b/nso-nipap/nso-nipap/templates/nso-nipap-from-prefix-request.xml new file mode 100644 index 000000000..70deeb3b2 --- /dev/null +++ b/nso-nipap/nso-nipap/templates/nso-nipap-from-prefix-request.xml @@ -0,0 +1,21 @@ + + + + + { $POOL_NAME } + + { $ALLOCATION_NAME } + + { $FROM_PREFIX_ALLOCATION_NAME } + + { $CUSTOMER_ID } + { $DESCRIPTION } + { $NODE } + { $ORDER_ID } + + + + + + + diff --git a/nso-nipap/nso-nipap/templates/nso-nipap-prefix-request.xml b/nso-nipap/nso-nipap/templates/nso-nipap-prefix-request.xml new file mode 100644 index 000000000..dc542b373 --- /dev/null +++ b/nso-nipap/nso-nipap/templates/nso-nipap-prefix-request.xml @@ -0,0 +1,23 @@ + + + + + { $POOL_NAME } + + { $ALLOCATION_NAME } + + { $CUSTOMER_ID } + { $DESCRIPTION } + { $NODE } + { $ORDER_ID } + + + { $FAMILY } + { $PREFIX_LENGTH } + + { $SERVICE } + + + + + From ca2dc15d3e85752f9ded3c786e37f553ee72ac52 Mon Sep 17 00:00:00 2001 From: Lukas Garberg Date: Thu, 9 Mar 2017 09:55:42 +0100 Subject: [PATCH 24/27] nso-nipap: Added python helper functions Added python helper functions for creating NIPAP requests. --- .../nso-nipap/python/nso_nipap/__init__.py | 0 .../python/nso_nipap/prefix_allocator.py | 195 ++++++++++++++++++ 2 files changed, 195 insertions(+) create mode 100644 nso-nipap/nso-nipap/python/nso_nipap/__init__.py create mode 100644 nso-nipap/nso-nipap/python/nso_nipap/prefix_allocator.py diff --git a/nso-nipap/nso-nipap/python/nso_nipap/__init__.py b/nso-nipap/nso-nipap/python/nso_nipap/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/nso-nipap/nso-nipap/python/nso_nipap/prefix_allocator.py b/nso-nipap/nso-nipap/python/nso_nipap/prefix_allocator.py new file mode 100644 index 000000000..b1faa8bf5 --- /dev/null +++ b/nso-nipap/nso-nipap/python/nso_nipap/prefix_allocator.py @@ -0,0 +1,195 @@ +import logging + +import ncs +import ncs.maapi as maapi +import ncs.maagic as maagic + +log = logging.getLogger() + + +def prefix_request(service, svc_xpath, pool_name, allocation_name, + prefix_length, family=4, prefix_attributes=None): + """ Create a prefix allocation request + + Arguments: + service -- the requesting service node + svc_xpath -- xpath to the requesting service + pool_name -- name of pool to request from + allocation_name -- unique allocation name + prefix_length -- the prefix length of the allocated network + family -- address family of the network, 4 (IPv4) or 6 (IPv6) + prefix_attributes -- dict with prefix attributes + """ + + if prefix_attributes is None: + prefix_attributes = {} + + template = ncs.template.Template(service) + vars = ncs.template.Variables() + + # required variables + vars.add("POOL_NAME", pool_name) + vars.add("ALLOCATION_NAME", allocation_name) + vars.add("SERVICE", svc_xpath) + vars.add("PREFIX_LENGTH", prefix_length) + vars.add("FAMILY", family) + + # optional prefix attributes + _set_prefix_attributes(prefix_attributes, vars) + + log.debug("Placing prefix request with data %s" % vars) + + template.apply('nso-nipap-prefix-request', vars) + + +def from_prefix_request(service, pool_name, main_allocation_name, + from_pref_allocation_name, prefix_attributes=None): + """ Create a from-prefix allocation request + + Arguments: + service -- the requesting service node + pool_name -- name of pool to request from + main_allocation_name -- name of main allocation which the from-prefix is appended to + from_pref_allocation_name -- name of from-prefix allocation + prefix_attributes -- dict with prefix attributes + """ + + if prefix_attributes is None: + prefix_attributes = {} + + template = ncs.template.Template(service) + vars = ncs.template.Variables() + + # required variables + vars.add('POOL_NAME', pool_name) + vars.add('ALLOCATION_NAME', main_allocation_name) + vars.add('FROM_PREFIX_ALLOCATION_NAME', from_pref_allocation_name) + + # optional prefix attributes + _set_prefix_attributes(prefix_attributes, vars) + + log.debug("Placing from-prefix request with data %s" % vars) + + template.apply('nso-nipap-from-prefix-request', vars) + + +def prefix_read(root, pool_name, allocation_name): + """Returns the allocated network or None + + Arguments: + root -- a maagic root for the current transaction + pool_name -- name of pool to request from + allocation_name -- unique allocation name + """ + # Look in the current transaction + _verify_allocation(root, pool_name, allocation_name) + + # Now we switch from the current trans to actually see if + # we have received the alloc + with maapi.single_read_trans("admin", "system", + db=ncs.OPERATIONAL) as th: + + oper_root = maagic.get_root(th) + alloc = _get_allocation(oper_root, pool_name, allocation_name) + if alloc is None: + return None + + if alloc.response.prefix: + return alloc.response.prefix + else: + return None + + +def from_prefix_read(root, pool_name, main_allocation_name, from_prefix_allocation_name): + """Returns the allocated network or None + + Arguments: + root -- a maagic root for the current transaction + pool_name -- name of pool to request from + main_allocation_name -- name of allocation which the from-prefix allocation belongs to + from_prefix_allocation_name -- name of from-prefix allocation + """ + # Look in the current transaction + alloc = _verify_allocation(root, pool_name, main_allocation_name) + if from_prefix_allocation_name not in alloc.from_prefix_request: + raise LookupError("from-prefix allocation %s does not exist in main allocation %s from pool %s" % + (from_prefix_allocation_name, main_allocation_name, pool_name)) + + # Now we switch from the current trans to actually see if + # we have received the alloc + with maapi.single_read_trans("admin", "system", + db=ncs.OPERATIONAL) as th: + oper_root = maagic.get_root(th) + alloc = _get_allocation(oper_root, pool_name, main_allocation_name) + if alloc is None: + return None + + if from_prefix_allocation_name not in alloc.from_prefix_request: + return None + + from_pref_alloc = alloc.from_prefix_request[from_prefix_allocation_name] + + if from_pref_alloc.response.prefix: + return from_pref_alloc.response.prefix + else: + return None + + +def _set_prefix_attributes(attributes, template_vars): + """ Fetch prefix attributes from CDB and write to template vars + """ + + template_vars.add('CUSTOMER_ID', + attributes['customer_id'] if 'customer_id' in attributes else '-1') + + template_vars.add('DESCRIPTION', + attributes['description'] if 'description' in attributes else '-1') + + template_vars.add('NODE', + attributes['node'] if 'node' in attributes else '-1') + + template_vars.add('ORDER_ID', + attributes['order_id'] if 'order_id' in attributes else '-1') + + +def _verify_allocation(root, pool_name, allocation_name): + """ Verify that the allocation exists and return it + + Throws LookupError if allocation is missing. + """ + pool_l = root.ncs__services.nipap__nipap.from_pool + + if pool_name not in pool_l: + raise LookupError("Pool %s does not exist" % (pool_name)) + + pool = pool_l[pool_name] + + if allocation_name not in pool.request: + raise LookupError("allocation %s does not exist in pool %s" % + (allocation_name, pool_name)) + + return pool.request[allocation_name] + + +def _get_allocation(root, pool_name, allocation_name): + """ Return allocation. + + Returns None if allocation does not exist, raises exception if + allocation status == 'error'. + """ + alloc = None + try: + alloc = _verify_allocation(root, pool_name, allocation_name) + except LookupError as e: + return None + + if alloc.response.status == 'ok': + return alloc + elif alloc.response.status == 'error': + raise AllocationError(alloc.response.status_message) + + +class AllocationError(Exception): + """ Exception thrown when allocation has failed. + """ + pass From cdeea83542dfdff9f9c47cb1d471a711ff1b482e Mon Sep 17 00:00:00 2001 From: Lukas Garberg Date: Wed, 5 Apr 2017 08:53:13 +0200 Subject: [PATCH 25/27] nso-nipap: Improve error handling to avoid lockup Improve error handling in nso-nipap to avoid locking CDB on certain failures, for example when unable to communicate with the NIPAP backend. Certain errors caused exceptions to be thrown which were not handled within the request loop which caused sub.sync() to never be run. --- .../net/spritelink/nsonipap/ConfigCdbSub.java | 286 ++++++++++-------- 1 file changed, 163 insertions(+), 123 deletions(-) diff --git a/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java b/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java index 2eb03b6ab..b28153d71 100644 --- a/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java +++ b/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java @@ -370,160 +370,200 @@ public void run() { LOGGER.debug("Requested NIPAP action, op=" + req.op + " , type=" + req.t); try { - // TODO: make backend configurable (now it is 'default') - ConfValue bHost = maapi.getElem(th, "/services/nipap/backend{default}/hostname"); - ConfValue bPort = maapi.getElem(th, "/services/nipap/backend{default}/port"); - ConfValue bUser = maapi.getElem(th, "/services/nipap/backend{default}/username"); - ConfValue bPass = maapi.getElem(th, "/services/nipap/backend{default}/password"); - - URL url = new URL("http://" + String.valueOf(bHost) + ":" + String.valueOf(bPort) + "/RPC2"); - nipapCon = new Connection(url, String.valueOf(bUser), String.valueOf(bPass)); - nipapCon.authoritative_source = "ncs"; - - } catch (Exception e) { - LOGGER.error("Unable to initiate connection to NIPAP: " + e.getMessage()); - continue; - } - - - /* - * Allocate new Prefix - * - */ - if (req.op == Operation.ALLOCATE && req.t == Type.Request ) { - LOGGER.info("Trying to allocate a prefix for: " + req.request_key + " from pool: " + req.pool_key); - - String poolName = String.valueOf(req.pool_key).replaceAll("[{}]", ""); - - Prefix p = new Prefix(); - // Gather prefix data and perform NIPAP request try { - // Pool - HashMap poolSpec = new HashMap<>(); - poolSpec.put("name", poolName); - List poolRes = Pool.list(nipapCon, poolSpec); + // TODO: make backend configurable (now it is 'default') + ConfValue bHost = maapi.getElem(th, "/services/nipap/backend{default}/hostname"); + ConfValue bPort = maapi.getElem(th, "/services/nipap/backend{default}/port"); + ConfValue bUser = maapi.getElem(th, "/services/nipap/backend{default}/username"); + ConfValue bPass = maapi.getElem(th, "/services/nipap/backend{default}/password"); - if (poolRes.size() != 1) { - writeError(req.path.toString(), "Pool " + poolName + " not found in NIPAP"); - continue; - } - - // options, like address-family - AddPrefixOptions opts = getPrefixOptionsFromCDB(req.path + "/" + nipap._arguments_); + URL url = new URL("http://" + String.valueOf(bHost) + ":" + String.valueOf(bPort) + "/RPC2"); + nipapCon = new Connection(url, String.valueOf(bUser), String.valueOf(bPass)); + nipapCon.authoritative_source = "ncs"; - // set prefix attributes - String attrPath = req.path + "/" + nipap._attributes_; - p = getPrefixAttributesFromCDB(attrPath); + } catch (Exception e) { + String estr = "Unable to initiate connection to NIPAP: " + e.getMessage(); + LOGGER.error(estr); + writeError(req.path.toString(), estr); + } - p.save(nipapCon, (Pool)poolRes.get(0), opts); + /* + * Allocate new Prefix + * + */ + if (req.op == Operation.ALLOCATE && req.t == Type.Request ) { - } catch (JnipapException e) { - LOGGER.error("Unable to get prefix from NIPAP: " + e.getMessage(), e); - writeError(req.path.toString(), e.getMessage()); - continue; - } + LOGGER.info("Trying to allocate a prefix for: " + req.request_key + " from pool: " + req.pool_key); - // Write the result - String resPath = req.path + "/" + nipap._response_; - writeResponseToCDB(p, resPath); + String poolName = String.valueOf(req.pool_key).replaceAll("[{}]", ""); - wsess.setElem(ConfEnumeration.getEnumByLabel( resPath + "/" + nipap._status_, "ok"), - resPath + "/" + nipap._status_); + Prefix p = new Prefix(); + // Gather prefix data and perform NIPAP request + try { + // Pool + HashMap poolSpec = new HashMap<>(); + poolSpec.put("name", poolName); + List poolRes = Pool.list(nipapCon, poolSpec); + if (poolRes.size() != 1) { + writeError(req.path.toString(), "Pool " + poolName + " not found in NIPAP"); + continue; + } - // Request prefix from prefix - String fromPrefixPath = req.path + "/" + nipap._from_prefix_request_; - if (maapi.exists(th, fromPrefixPath)){ - MaapiCursor pfx_cur = maapi.newCursor(th, fromPrefixPath); - ConfKey pfx = null; + // options, like address-family + AddPrefixOptions opts = getPrefixOptionsFromCDB(req.path + "/" + nipap._arguments_); - while((pfx = maapi.getNext(pfx_cur)) != null) { - addHostPrefixFromPrefix(fromPrefixPath + pfx, p); - } - } + // set prefix attributes + String attrPath = req.path + "/" + nipap._attributes_; + p = getPrefixAttributesFromCDB(attrPath); - // Redeploy - try { - ConfValue redeployPath = maapi.getElem(th, req.path + "/redeploy-service"); - LOGGER.info("redeploy-service: " + redeployPath); - redeploy(redeployPath.toString()); - } catch (Exception e) { - LOGGER.error("Redeploy failed: " + e.getMessage()); - } - } - /* - * Allocate from-prefix - * - */ - else if (req.op == Operation.ALLOCATE && req.t == Type.FromPrefixRequest){ - LOGGER.info("Create, From prefix request"); + p.save(nipapCon, (Pool)poolRes.get(0), opts); - String path = "/" + Ncs._services_ + "/" + nipap.prefix + ":" + nipap.prefix + "/" + - nipap._from_pool_ + req.pool_key + "/" + nipap._request_ + req.request_key; + } catch (JnipapException e) { + LOGGER.error("Unable to get prefix from NIPAP: " + e.getMessage(), e); + writeError(req.path.toString(), e.getMessage()); + continue; + } - int p_id = getPrefixId(path + "/" + nipap._response_ ); + // Write the result + String resPath = req.path + "/" + nipap._response_; + writeResponseToCDB(p, resPath); - Prefix parentPrefix = Prefix.get(nipapCon, p_id); + wsess.setElem(ConfEnumeration.getEnumByLabel( resPath + "/" + nipap._status_, "ok"), + resPath + "/" + nipap._status_); - addHostPrefixFromPrefix(path + "/" + nipap._from_prefix_request_ + req.prefix_key, parentPrefix); - } - /* - * Deallocate Prefix - * - */ - else if (req.op == Operation.DEALLOCATE && (req.t == Type.Request)) { - String path = req.path + "/" + nipap._response_; + // Request prefix from prefix + String fromPrefixPath = req.path + "/" + nipap._from_prefix_request_; + if (maapi.exists(th, fromPrefixPath)){ + MaapiCursor pfx_cur = maapi.newCursor(th, fromPrefixPath); + ConfKey pfx = null; - LOGGER.info("Deallocate Prefix (" + path + ")"); + while((pfx = maapi.getNext(pfx_cur)) != null) { + addHostPrefixFromPrefix(fromPrefixPath + pfx, p); + } + } - if(maapi.exists(th, path + "/" + nipap._prefix_)){ - removePrefixFromNIPAP(path); + // Redeploy + try { + ConfValue redeployPath = maapi.getElem(th, req.path + "/redeploy-service"); + LOGGER.info("redeploy-service: " + redeployPath); + redeploy(redeployPath.toString()); + } catch (Exception e) { + LOGGER.error("Redeploy failed: " + e.getMessage()); + } + } + /* + * Allocate from-prefix + * + */ + else if (req.op == Operation.ALLOCATE && req.t == Type.FromPrefixRequest){ + LOGGER.info("Create, From prefix request"); + + String path = "/" + Ncs._services_ + "/" + nipap.prefix + ":" + nipap.prefix + "/" + + nipap._from_pool_ + req.pool_key + "/" + nipap._request_ + req.request_key; + + try { + int p_id = getPrefixId(path + "/" + nipap._response_ ); + + Prefix parentPrefix = Prefix.get(nipapCon, p_id); + + addHostPrefixFromPrefix(path + "/" + nipap._from_prefix_request_ + req.prefix_key, parentPrefix); + } catch (JnipapException e) { + String estr = "Allocating from-prefix failed: " + e.getMessage(); + LOGGER.error(estr); + writeError(req.path.toString(), estr); + continue; + } } + /* + * Deallocate Prefix + * + */ + else if (req.op == Operation.DEALLOCATE && (req.t == Type.Request)) { - removeResponseFromCDB(path); - } - /* - * Deallocate from-prefix prefix - * - */ - else if (req.op == Operation.DEALLOCATE && (req.t == Type.FromPrefixRequest)) { + String path = req.path + "/" + nipap._response_; - String path = req.path + "/" + nipap._response_; + LOGGER.info("Deallocate Prefix (" + path + ")"); - LOGGER.info("Deallocate Prefix (" + path + ")"); + try { + if(maapi.exists(th, path + "/" + nipap._prefix_)){ + removePrefixFromNIPAP(path); + } - if(maapi.exists(th, path + "/" + nipap._prefix_)){ - removePrefixFromNIPAP(path); + removeResponseFromCDB(path); + } catch (JnipapException e) { + String estr = "Deallocation of prefix failed: " + e.getMessage(); + LOGGER.error(estr); + writeError(req.path.toString(), estr); + continue; + } } + /* + * Deallocate from-prefix prefix + * + */ + else if (req.op == Operation.DEALLOCATE && (req.t == Type.FromPrefixRequest)) { - removeResponseFromCDB(path); - - } - /* - * Modify prefix attributes - * - */ - else if (req.op == Operation.SET && req.t == Type.Request){ + String path = req.path + "/" + nipap._response_; - String reqPath = "/" + Ncs._services_ + "/" + nipap.prefix + ":" + nipap.prefix + "/" + - nipap._from_pool_ + req.pool_key + "/" + nipap._request_ + req.request_key; + LOGGER.info("Deallocate Prefix (" + path + ")"); - updatePrefixInNIPAP(reqPath); + try { + if(maapi.exists(th, path + "/" + nipap._prefix_)){ + removePrefixFromNIPAP(path); + } - } - /* - * Modify from-prefix attributes - * - */ - else if (req.op == Operation.SET && req.t == Type.FromPrefixRequest){ + removeResponseFromCDB(path); + } catch (JnipapException e) { + String estr = "Deallocation of from-prefix failed: " + e.getMessage(); + LOGGER.error(estr); + writeError(req.path.toString(), estr); + continue; + } - String reqPath = "/" + Ncs._services_ + "/" + nipap.prefix + ":" + nipap.prefix + "/" + - nipap._from_pool_ + req.pool_key + "/" + nipap._request_ + req.request_key + "/" + - nipap._from_prefix_request_ + req.prefix_key; + } + /* + * Modify prefix attributes + * + */ + else if (req.op == Operation.SET && req.t == Type.Request){ + + String reqPath = "/" + Ncs._services_ + "/" + nipap.prefix + ":" + nipap.prefix + "/" + + nipap._from_pool_ + req.pool_key + "/" + nipap._request_ + req.request_key; + + try { + updatePrefixInNIPAP(reqPath); + } catch (JnipapException e) { + String estr = "Update of prefix failed: " + e.getMessage(); + LOGGER.error(estr); + writeError(req.path.toString(), estr); + continue; + } - updatePrefixInNIPAP(reqPath); + } + /* + * Modify from-prefix attributes + * + */ + else if (req.op == Operation.SET && req.t == Type.FromPrefixRequest){ + + String reqPath = "/" + Ncs._services_ + "/" + nipap.prefix + ":" + nipap.prefix + "/" + + nipap._from_pool_ + req.pool_key + "/" + nipap._request_ + req.request_key + "/" + + nipap._from_prefix_request_ + req.prefix_key; + + try { + updatePrefixInNIPAP(reqPath); + } catch (Exception e) { + String estr = "Update of from-prefix failed: " + e.getMessage(); + LOGGER.error(estr); + writeError(req.path.toString(), estr); + continue; + } + } + } catch (ConfException|IOException e) { + LOGGER.error("Request failed: " + e.getMessage()); } } sub.sync(CdbSubscriptionSyncType.DONE_PRIORITY); From 4c5197f0907e22835a22f4013445ebabc6cd9c1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fred=20S=C3=B6derberg?= Date: Tue, 11 Apr 2017 11:02:35 +0200 Subject: [PATCH 26/27] nso-nipap: Write status on from-prefix-requests --- .../src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java b/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java index b28153d71..a3165863e 100644 --- a/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java +++ b/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java @@ -235,6 +235,8 @@ protected void writeResponseToCDB(Prefix prefix, String responsePath) throws Con ConfBuf orderIdValue = new ConfBuf(prefix.order_id); wsess.setElem(orderIdValue, responsePath + "/" + nipap._order_id_); } + wsess.setElem(ConfEnumeration.getEnumByLabel( responsePath + "/" + nipap._status_, "ok"), + responsePath + "/" + nipap._status_); } /** @@ -430,9 +432,6 @@ public void run() { String resPath = req.path + "/" + nipap._response_; writeResponseToCDB(p, resPath); - wsess.setElem(ConfEnumeration.getEnumByLabel( resPath + "/" + nipap._status_, "ok"), - resPath + "/" + nipap._status_); - // Request prefix from prefix String fromPrefixPath = req.path + "/" + nipap._from_prefix_request_; From 8b8a0daf160675cdfbee7842638bd7ccf5b9ba97 Mon Sep 17 00:00:00 2001 From: Lukas Garberg Date: Thu, 18 May 2017 10:32:11 +0200 Subject: [PATCH 27/27] nso-nipap: Pass re-deploy path as ConfPath Pass the path to re-deploy as a ConfPath-object rather than as a String. From some reason the string, which is extracted directly from CDB, can't be parsed by the ConfPath-constructor. --- .../src/net/spritelink/nsonipap/ConfigCdbSub.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java b/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java index a3165863e..935e3cd80 100644 --- a/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java +++ b/nso-nipap/nso-nipap/src/java/src/net/spritelink/nsonipap/ConfigCdbSub.java @@ -446,9 +446,10 @@ public void run() { // Redeploy try { - ConfValue redeployPath = maapi.getElem(th, req.path + "/redeploy-service"); + ConfObjectRef v = (ConfObjectRef)maapi.getElem(th, req.path + "/redeploy-service"); + ConfPath redeployPath = new ConfPath(v.getElems()); LOGGER.info("redeploy-service: " + redeployPath); - redeploy(redeployPath.toString()); + redeploy(redeployPath); } catch (Exception e) { LOGGER.error("Redeploy failed: " + e.getMessage()); } @@ -734,19 +735,19 @@ else if (kp[1].toString().equals("nipap:from-prefix-request") && // redeploy MUST be done in another thread, if not system // hangs, since the CDB subscriber cannot do its work - private void redeploy(String path) { + private void redeploy(ConfPath path) { Redeployer r = new Redeployer(path); Thread t = new Thread(r); t.start(); } private class Redeployer implements Runnable { - private String path; + private ConfPath path; private ConfKey k; private Maapi m; private Socket s; - public Redeployer(String path) { + public Redeployer(ConfPath path) { this.path = path; this.k = k; try { @@ -784,7 +785,7 @@ public void run() { } m.requestAction(new ConfXMLParam[] {}, - path+"/reactive-re-deploy"); + path.toString()+"/reactive-re-deploy"); try { m.finishTrans(tid); }