public CreatePhysicalPortCommand(NbiPort nbiPort, String defaultTopologyName) {
   setNmsPortId(nbiPort.getNmsPortId());
   setNocLabel(nbiPort.getSuggestedNocLabel());
   setBodPortId(nbiPort.getNmsNeId());
   this.allowedVlan = nbiPort.getVlan().map(Vlan::toRangeString).orElse("");
   this.topologyName = defaultTopologyName;
 }
  @RequestMapping(value = "create", params = ID_KEY, method = RequestMethod.GET)
  public String createForm(@RequestParam(ID_KEY) String nmsPortId, Model model) {
    Optional<NbiPort> maybeNbiPort = physicalPortService.findNbiPort(nmsPortId);
    if (!maybeNbiPort.isPresent()) {
      return "redirect:";
    }

    NbiPort nbiPort = maybeNbiPort.get();

    model.addAttribute("interfaceTypes", availableInterfaceTypes(nbiPort));
    model.addAttribute("availableTopologies", physicalPortService.findTopologyNames());
    model.addAttribute("vlanRequired", nbiPort.isVlanRequired());
    model.addAttribute(
        "createPhysicalPortCommand",
        new CreatePhysicalPortCommand(nbiPort, bodEnvironment.getBodTopologyName()));

    return PAGE_URL + CREATE;
  }
    private void validateEnniPort(NbiPort nbiPort, String bodTopologyName, BindingResult result) {
      nbiPort
          .getVlan()
          .ifPresent(
              nbiVlan -> {
                if (getSVlan() != null) {
                  if (!nbiVlan.contains(getSVlan())) {
                    result.rejectValue(
                        "sVlan",
                        "validation.physicalport.invalid.svlan",
                        new Object[] {nbiVlan.toRangeString()},
                        "S-VLAN must be enclosed in the range {0}");
                  }
                }

                if (StringUtils.isBlank(getAllowedVlan())) {
                  result.rejectValue("allowedVlan", "validation.not.empty");
                } else {
                  Vlan allowedVlan = Vlan.parse(getAllowedVlan());

                  Vlan downstreamAllowedVlan = getSVlan() == null ? nbiVlan : Vlan.any();
                  if (!downstreamAllowedVlan.enclosesAll(allowedVlan)) {
                    result.rejectValue(
                        "allowedVlan",
                        "validation.physicalport.invalid.vlan",
                        new Object[] {downstreamAllowedVlan.toRangeString()},
                        "VLAN must be enclosed in the range {0}");
                  }
                }
              });

      if (StringUtils.isBlank(getTopologyName())) {
        result.rejectValue("topologyName", "validation.not.empty");
      } else if (Objects.equals(bodTopologyName, getTopologyName())) {
        if (getInboundPeer() == null || !matches(NsiHelper.NURN_PATTERN_REGEXP, getInboundPeer())) {
          result.rejectValue(
              "inboundPeer",
              "nl.surfnet.bod.domain.urn.message",
              new String[] {NsiHelper.NURN_PATTERN_REGEXP},
              "Did not match pattern");
        }
        if (getInboundPeer() == null
            || !matches(NsiHelper.NURN_PATTERN_REGEXP, getOutboundPeer())) {
          result.rejectValue(
              "outboundPeer",
              "nl.surfnet.bod.domain.urn.message",
              new String[] {NsiHelper.NURN_PATTERN_REGEXP},
              "Did not match pattern");
        }
      }
    }
  private List<InterfaceType> availableInterfaceTypes(NbiPort nbiPort) {
    List<PhysicalPort> allocatedPorts =
        physicalPortService.findAllocatedByNmsPortId(nbiPort.getNmsPortId());
    Set<InterfaceType> allocatedInterfaceTypes =
        allocatedPorts.stream().map(PhysicalPort::getInterfaceType).collect(Collectors.toSet());

    if (allocatedInterfaceTypes.contains(InterfaceType.E_NNI)) {
      return Arrays.asList(InterfaceType.E_NNI);
    } else if (allocatedInterfaceTypes.contains(InterfaceType.UNI)) {
      return Arrays.asList(InterfaceType.UNI);
    } else {
      return Arrays.asList(InterfaceType.values());
    }
  }
  @RequestMapping(method = RequestMethod.POST)
  public String create(
      @Valid CreatePhysicalPortCommand command,
      BindingResult result,
      RedirectAttributes redirectAttributes,
      Model model) {
    PhysicalPort physicalPort = physicalPortService.findByBodPortId(command.getBodPortId());
    if (physicalPort != null) {
      result.rejectValue("bodPortId", "validation.not.unique");
    }

    Optional<NbiPort> maybeNbiPort = physicalPortService.findNbiPort(command.getNmsPortId());
    if (!maybeNbiPort.isPresent()) {
      return "redirect:";
    }

    NbiPort nbiPort = maybeNbiPort.get();

    command.validate(nbiPort, bodEnvironment.getBodTopologyName(), result);

    if (result.hasErrors()) {
      model.addAttribute("interfaceTypes", availableInterfaceTypes(nbiPort));
      model.addAttribute("availableTopologies", physicalPortService.findTopologyNames());
      model.addAttribute("vlanRequired", nbiPort.isVlanRequired());
      model.addAttribute("createPhysicalPortCommand", command);

      return PAGE_URL + CREATE;
    }

    PhysicalPort port;
    switch (command.getInterfaceType()) {
      case E_NNI:
        port = createEnniPort(command, nbiPort);

        messageManager.addInfoFlashMessage(
            redirectAttributes, "info_physicalport_enni_created", port.getNocLabel());

        break;
      case UNI:
        UniPort uniPort = createUniPort(command, nbiPort);

        messageManager.addInfoFlashMessage(
            redirectAttributes,
            "info_physicalport_uni_created",
            uniPort.getNocLabel(),
            uniPort.getPhysicalResourceGroup().getName());

        port = uniPort;
        break;
      default:
        throw new IllegalArgumentException("unknown interface type " + command.getInterfaceType());
    }

    try {
      physicalPortService.save(port);
    } catch (OverlappingSubportAllocationException e) {
      result.reject(e.getMessage());
      model.addAttribute("interfaceTypes", availableInterfaceTypes(nbiPort));
      model.addAttribute("availableTopologies", physicalPortService.findTopologyNames());
      model.addAttribute("vlanRequired", nbiPort.isVlanRequired());
      model.addAttribute("createPhysicalPortCommand", command);

      return PAGE_URL + CREATE;
    }

    return redirectPortListPage(port);
  }